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
+42 -12
View File
@@ -11,6 +11,30 @@ import {
parseImage,
saveImages,
} from "../storage/images.ts";
import { workspaceDownloadImage } from "../../../workspace/api.ts";
import { copiedImage } from "../../../workspace/copiedImage.ts";
import { mayAnimate, pastedImageSource } from "../../../workspace/media.ts";
async function fileDataUrl(file: File) {
const bytes = new Uint8Array(await file.arrayBuffer());
let binary = "";
for (let offset = 0; offset < bytes.length; offset += 8192)
binary += String.fromCharCode(...bytes.subarray(offset, offset + 8192));
return `data:${file.type};base64,${btoa(binary)}`;
}
/**
* The original of a copied web image. The RuneStone browser keeps the bytes of the image that it copied.
* For a copy from another browser, download the original of a format with frames.
* Undefined keeps the still copy. "refused" means that the website did not give the original.
*/
async function webImage(source: string): Promise<{ data: string; type: string } | "refused" | undefined> {
const data = await copiedImage(source) ?? (mayAnimate(source) ? await workspaceDownloadImage(source).catch(() => "") : undefined);
if (data === undefined) return undefined;
if (!data) return "refused";
const type = /^data:([^;]+);/.exec(data)?.[1] ?? "";
return imageTypes.includes(type) ? { data, type } : undefined;
}
/** The image data stays outside the card Markdown and loads only in the open editor. */
export function createCardImages(
@@ -226,7 +250,8 @@ export function createCardImages(
}
}
}
async function addFiles(files: File[], attachOnly = false) {
/** `source` is the web address of the original of a copied image. `files` holds its still copy. */
async function addFiles(files: File[], attachOnly = false, source?: string) {
if (disposed || loading || description.disabled || !files.length) return;
loading = true;
updateControls();
@@ -237,18 +262,20 @@ export function createCardImages(
end = description.selectionEnd;
try {
const staged: { id: string; name: string; data: string }[] = [];
let refused = false;
for (const file of files) {
if (!imageTypes.includes(file.type))
// A browser copies only the first frame of a web image. The original keeps all of its frames.
const web = source ? await webImage(source) : undefined;
refused ||= web === "refused";
const original = typeof web === "object" ? web : undefined;
if (!original && !imageTypes.includes(file.type))
throw new Error(message("Use a PNG, JPEG, GIF, or WebP image."));
if (!file.size || file.size > maxImageBytes)
if (!original && !file.size)
throw new Error(message("The image file is empty. A browser can give an empty file when the website refuses a download. To add the image, copy it in the RuneStone browser. You can also save the image and add the saved file."));
if (!original && file.size > maxImageBytes)
throw new Error(message("Use an image between 1 byte and 50 MiB."));
const bytes = new Uint8Array(await file.arrayBuffer());
let binary = "";
for (let offset = 0; offset < bytes.length; offset += 8192)
binary += String.fromCharCode(
...bytes.subarray(offset, offset + 8192),
);
const data = `data:${file.type};base64,${btoa(binary)}`;
const data = original?.data ?? await fileDataUrl(file);
const name = original ? `${(file.name || "image").replace(/\.\w+$/, "")}.${original.type.slice(6)}` : file.name;
// Decode the image before the reference goes in, so that an invalid clipboard file keeps the draft.
const image = new Image();
image.src = data;
@@ -257,7 +284,7 @@ export function createCardImages(
});
staged.push({
id: crypto.randomUUID(),
name: (file.name || tr("Pasted image")).slice(0, 255),
name: (name || tr("Pasted image")).slice(0, 255),
data,
});
}
@@ -269,6 +296,7 @@ export function createCardImages(
description.setRangeText(`\n${markdown}\n`, start, end, "end");
}
description.dispatchEvent(new Event("input", { bubbles: true }));
if (refused) error.textContent = tr("The website refused the original image, so RuneStone added only its first frame. To keep the animation, copy the image in the RuneStone browser. You can also save the image and add the saved file.");
} catch (reason) {
if (!disposed)
(attachOnly ? attachmentError : error).textContent = errorText(reason);
@@ -288,7 +316,9 @@ export function createCardImages(
if (!files.length) return;
event.preventDefault();
event.stopPropagation();
void addFiles(files);
// Read the HTML now. The clipboard data is empty after the paste event.
const source = files.length === 1 ? pastedImageSource(event.clipboardData?.getData("text/html") ?? "") : undefined;
void addFiles(files, false, source);
};
const dragOver = (event: DragEvent) => {
if (event.dataTransfer?.types.includes("Files")) {