Keep the frames of pasted GIF, WebP, and AVIF images

A browser puts only the first frame of an image on the clipboard, with
HTML that names the image address.

- Documents and cards download the original of a GIF, WebP, or AVIF
  address. The host commands workspace_save_image_url and
  workspace_download_image read the format from the file contents and
  refuse other data. A failed download keeps the first frame and shows
  a message.
- Copy image in the RuneStone browser keeps the bytes that the page
  loaded. The engine reads them through the DevTools Protocol, so the
  website gets no second request. The new copiedImage command gives
  them in parts below the line limit of the host. A paste uses them
  first.
- An empty dropped image file shows a message. Firefox gives such a
  file when the website refuses its download.

Copy image needs a new browser engine build. Card 0d5c18f7.
This commit is contained in:
Knotty Wyvern
2026-10-31 17:33:08 -07:00
parent 31ce7e73ce
commit 6e1b24e40f
29 changed files with 654 additions and 41 deletions
+33
View File
@@ -96,6 +96,39 @@ export function attachmentRel(fileName: string): string {
return `attachments/${stamp}-${crypto.randomUUID()}-${safe}`;
}
const ENTITIES: Record<string, string> = { amp: "&", quot: '"', apos: "'", "#39": "'", lt: "<", gt: ">" };
/**
* The web address of a copied image. A browser puts only the first frame on the clipboard,
* with HTML that names the original file.
*/
export function pastedImageSource(html: string): string | undefined {
const images = [...html.matchAll(/<img\b[^>]*?\ssrc\s*=\s*(?:"([^"]*)"|'([^']*)'|([^\s"'>]+))/gi)];
if (images.length !== 1) return undefined;
const [, double, single, bare] = images[0];
const source = (double ?? single ?? bare).trim().replace(/&(amp|quot|apos|#39|lt|gt);/g, (_, name: string) => ENTITIES[name]);
try {
const url = new URL(source);
return /^https?:$/.test(url.protocol) ? url.href : undefined;
} catch {
return undefined;
}
}
/** A GIF, WebP, or AVIF address. These formats can have more frames, so the original is worth a download. */
export function mayAnimate(source: string): boolean {
return /\.(?:gif|webp|avif)$/i.test(new URL(source).pathname);
}
const EXTENSIONS: Record<string, string> = {
"image/gif": "gif", "image/png": "png", "image/jpeg": "jpg", "image/webp": "webp", "image/avif": "avif", "image/bmp": "bmp",
};
/** The file extension of an image data URL, or undefined for another type. */
export function dataUrlExtension(data: string): string | undefined {
return EXTENSIONS[/^data:([^;,]+)[;,]/.exec(data)?.[1] ?? ""];
}
export function imageFiles(data: DataTransfer | null): File[] {
return [...(data?.files ?? [])].filter(file => isImagePath(file.name));
}