Unify file opening, importing, and pasting into a file ingest handler (#4548)

* Unify file and data ingest

* Code review

---------

Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
Timon
2026-09-18 22:47:02 -07:00
committed by GitHub
co-authored by Keavon Chambers
parent f90bef159c
commit 7d1303f695
47 changed files with 855 additions and 980 deletions
+1 -21
View File
@@ -1,4 +1,3 @@
import { extractPixelData } from "/src/utility-functions/rasterization";
import { stripIndents } from "/src/utility-functions/strip-indents";
import type { EditorWrapper } from "/wrapper/pkg/graphite_wasm_wrapper";
@@ -114,28 +113,9 @@ export async function triggerClipboardRead(editor: EditorWrapper) {
// Read an image from the clipboard and pass it to the editor to be loaded
const imageType = item.types.find((type) => type.startsWith("image/"));
// Import the actual SVG content if it's an SVG
if (imageType?.includes("svg")) {
const blob = await item.getType("text/plain");
const reader = new FileReader();
reader.onload = () => {
if (typeof reader.result === "string") editor.pasteSvg(undefined, reader.result);
};
reader.readAsText(blob);
return true;
}
// Import the bitmap image if it's an image
if (imageType) {
const blob = await item.getType(imageType);
const reader = new FileReader();
reader.onload = async () => {
if (reader.result instanceof ArrayBuffer) {
const imageData = await extractPixelData(new Blob([reader.result], { type: imageType }));
editor.pasteImage(undefined, new Uint8Array(imageData.data), imageData.width, imageData.height);
}
};
reader.readAsArrayBuffer(blob);
editor.ingestFile(undefined, imageType, new Uint8Array(await blob.arrayBuffer()));
return true;
}