Add blog post image editing
This commit is contained in:
parent
806aedb984
commit
770ab0e5df
156
blog-post.html
156
blog-post.html
@ -233,13 +233,44 @@
|
||||
gap: var(--size-3);
|
||||
margin: var(--size-3) 0;
|
||||
}
|
||||
.chapter-image-item {
|
||||
position: relative;
|
||||
border-radius: var(--radius-2);
|
||||
overflow: hidden;
|
||||
box-shadow: var(--shadow-2);
|
||||
background: var(--surface-2);
|
||||
}
|
||||
.chapter-images img {
|
||||
width: 100%;
|
||||
aspect-ratio: 4 / 3;
|
||||
object-fit: cover;
|
||||
border-radius: var(--radius-2);
|
||||
display: block;
|
||||
}
|
||||
.remove-chapter-image {
|
||||
position: absolute;
|
||||
top: var(--size-1);
|
||||
right: var(--size-1);
|
||||
width: 28px;
|
||||
height: 28px;
|
||||
border: none;
|
||||
border-radius: var(--radius-round);
|
||||
background: rgba(0, 0, 0, 0.72);
|
||||
color: white;
|
||||
cursor: pointer;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
box-shadow: var(--shadow-2);
|
||||
background: var(--surface-2);
|
||||
}
|
||||
.chapter-image-tools {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--size-2);
|
||||
flex-wrap: wrap;
|
||||
margin: var(--size-3) 0;
|
||||
}
|
||||
.chapter-image-input {
|
||||
max-width: 360px;
|
||||
}
|
||||
.chapter-content {
|
||||
line-height: 1.6;
|
||||
@ -294,6 +325,7 @@
|
||||
function renderJourney() {
|
||||
const container = document.getElementById('post-content');
|
||||
const isOwner = currentUser && currentUser.id === currentJourney.owner_id;
|
||||
const canEdit = canEditJourney();
|
||||
const canComment = currentJourney.visibility === 'public' || isOwner;
|
||||
|
||||
// Build chapters from markers
|
||||
@ -304,7 +336,8 @@
|
||||
const title = marker.title || `Chapter ${idx + 1}`;
|
||||
const date = marker.date ? `<span class="marker-date">${new Date(marker.date).toLocaleDateString()}</span>` : '';
|
||||
const content = marker.description ? escapeHtml(marker.description).replace(/\n/g, '<br>') : '';
|
||||
const images = getMarkerImagesHtml(marker.images || (marker.image ? [marker.image] : []));
|
||||
marker.images = getMarkerImages(marker);
|
||||
const images = getMarkerImagesHtml(marker.images, idx, canEdit);
|
||||
const video = marker.videoUrl ? `<iframe src="${marker.videoUrl}" frameborder="0" allowfullscreen></iframe>` : '';
|
||||
chaptersHtml += `
|
||||
<div class="chapter" data-marker-id="${marker.id || idx}">
|
||||
@ -313,6 +346,7 @@
|
||||
${date}
|
||||
</div>
|
||||
${images}
|
||||
${canEdit ? getImageToolsHtml(idx) : ''}
|
||||
${video}
|
||||
<div class="chapter-content">${content}</div>
|
||||
</div>
|
||||
@ -345,6 +379,9 @@
|
||||
if (isOwner) {
|
||||
document.getElementById('delete-journey-btn')?.addEventListener('click', deleteJourney);
|
||||
}
|
||||
if (canEdit) {
|
||||
attachChapterImageControls();
|
||||
}
|
||||
}
|
||||
|
||||
function getUploadUrl(path) {
|
||||
@ -353,20 +390,127 @@
|
||||
return API_BASE.replace('/api', path);
|
||||
}
|
||||
|
||||
function getMarkerImagesHtml(images) {
|
||||
function canEditJourney() {
|
||||
if (!currentUser || !currentJourney) return false;
|
||||
if (currentUser.id === currentJourney.owner_id) return true;
|
||||
return currentJourney.visibility === 'shared'
|
||||
&& Array.isArray(currentJourney.shared_edit)
|
||||
&& currentJourney.shared_edit.includes(currentUser.id);
|
||||
}
|
||||
|
||||
function getMarkerImages(marker) {
|
||||
return marker.images || (marker.image ? [marker.image] : []);
|
||||
}
|
||||
|
||||
function getMarkerImagesHtml(images, markerIndex, canEdit) {
|
||||
if (!images || !images.length) return '';
|
||||
|
||||
const imageHtml = images.map((image) => {
|
||||
const imageHtml = images.map((image, imageIndex) => {
|
||||
const url = typeof image === 'string' ? image : image.url;
|
||||
const alt = typeof image === 'string' ? 'Journey image' : image.originalName || 'Journey image';
|
||||
if (!url) return '';
|
||||
return `<img src="${escapeAttribute(getUploadUrl(url))}" alt="${escapeAttribute(alt)}">`;
|
||||
return `
|
||||
<div class="chapter-image-item">
|
||||
<img src="${escapeAttribute(getUploadUrl(url))}" alt="${escapeAttribute(alt)}">
|
||||
${canEdit ? `
|
||||
<button type="button" class="remove-chapter-image" data-marker-index="${markerIndex}" data-image-index="${imageIndex}" aria-label="Remove image">
|
||||
<i class="fas fa-times"></i>
|
||||
</button>
|
||||
` : ''}
|
||||
</div>
|
||||
`;
|
||||
}).join('');
|
||||
|
||||
if (!imageHtml) return '';
|
||||
return `<div class="chapter-images">${imageHtml}</div>`;
|
||||
}
|
||||
|
||||
function getImageToolsHtml(markerIndex) {
|
||||
return `
|
||||
<div class="chapter-image-tools">
|
||||
<input type="file" class="chapter-image-input" data-marker-index="${markerIndex}" accept="image/*" multiple>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function attachChapterImageControls() {
|
||||
document.querySelectorAll('.chapter-image-input').forEach(input => {
|
||||
input.addEventListener('change', () => uploadChapterImages(input));
|
||||
});
|
||||
document.querySelectorAll('.remove-chapter-image').forEach(button => {
|
||||
button.addEventListener('click', () => removeChapterImage(
|
||||
parseInt(button.dataset.markerIndex),
|
||||
parseInt(button.dataset.imageIndex)
|
||||
));
|
||||
});
|
||||
}
|
||||
|
||||
async function uploadImages(files) {
|
||||
if (!files.length) return [];
|
||||
|
||||
const formData = new FormData();
|
||||
files.forEach(file => formData.append('images', file));
|
||||
|
||||
const res = await fetch(`${API_BASE}/uploads/images`, {
|
||||
method: 'POST',
|
||||
body: formData,
|
||||
credentials: 'include'
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || 'Image upload failed');
|
||||
return data.images || [];
|
||||
}
|
||||
|
||||
async function uploadChapterImages(input) {
|
||||
const markerIndex = parseInt(input.dataset.markerIndex);
|
||||
const files = Array.from(input.files || []);
|
||||
if (!files.length || !currentJourney.markers[markerIndex]) return;
|
||||
|
||||
const originalImages = [...getMarkerImages(currentJourney.markers[markerIndex])];
|
||||
try {
|
||||
showToast('Uploading images...');
|
||||
const uploadedImages = await uploadImages(files);
|
||||
const marker = currentJourney.markers[markerIndex];
|
||||
marker.images = [...originalImages, ...uploadedImages];
|
||||
await saveJourneyMarkers();
|
||||
showToast('Images uploaded');
|
||||
renderJourney();
|
||||
} catch (err) {
|
||||
currentJourney.markers[markerIndex].images = originalImages;
|
||||
showToast(err.message || 'Error uploading images', true);
|
||||
} finally {
|
||||
input.value = '';
|
||||
}
|
||||
}
|
||||
|
||||
async function removeChapterImage(markerIndex, imageIndex) {
|
||||
const marker = currentJourney.markers[markerIndex];
|
||||
if (!marker) return;
|
||||
|
||||
const originalImages = [...getMarkerImages(marker)];
|
||||
marker.images = getMarkerImages(marker).filter((_, idx) => idx !== imageIndex);
|
||||
try {
|
||||
await saveJourneyMarkers();
|
||||
showToast('Image removed');
|
||||
renderJourney();
|
||||
} catch (err) {
|
||||
marker.images = originalImages;
|
||||
showToast('Error removing image', true);
|
||||
}
|
||||
}
|
||||
|
||||
async function saveJourneyMarkers() {
|
||||
const res = await fetch(`${API_BASE}/journeys/${currentJourney.id}`, {
|
||||
method: 'PUT',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ markers: currentJourney.markers }),
|
||||
credentials: 'include'
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error || 'Could not save images');
|
||||
currentJourney = data;
|
||||
}
|
||||
|
||||
function escapeAttribute(value) {
|
||||
return String(value || '').replace(/[&<>"']/g, (char) => ({
|
||||
'&': '&',
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user