Offer to edit images

Any SVG image in assets/ can now be edited with js-draw. It won't reload automatically (yet)
This commit is contained in:
MassiveBox 2025-04-01 23:03:22 +02:00
parent 5c261b35f2
commit 8d1438de33
Signed by: massivebox
GPG key ID: 9B74D3A59181947D
4 changed files with 240 additions and 18 deletions

View file

@ -54,4 +54,37 @@ export function getPreviewHTML(id: string): string {
return `
<iframe src="${EMBED_PATH + id}&antiCache=0"></iframe>
`
}
}
// given a tag (such as a div) containing an image as a child at any level, return the src of the image
export function findImgSrc(element: HTMLElement): string | null {
// Base case: if current element is an image
if (element.tagName === 'IMG') {
const fullSrc = (element as HTMLImageElement).src;
// Extract the path after host:port using URL API
const url = new URL(fullSrc);
return url.pathname.startsWith('/assets/')
? url.pathname.substring(1) // Remove leading slash
: null;
}
// Recursively check children
if (element.children) {
for (const child of Array.from(element.children)) {
const src = findImgSrc(child as HTMLElement);
if (src) return src;
}
}
return null;
}
export function extractFileID(imgSrc: string | null): string | null {
if (!imgSrc) return null;
const [pathPart] = imgSrc.split('?');
// Match pattern: assets/{fileID}.svg
const match = pathPart.match(/^assets\/([^\/]+)\.svg$/i);
return match?.[1] || null;
}