From 6368b7bc1cc265cc1f66ad8b4ef0b6b4f811c5c7 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Fri, 4 Sep 2026 07:07:41 +0000 Subject: [PATCH] Refactor overly long exportSheetImage function Extracted canvas rendering and file saving logic from the `exportSheetImage` function into smaller, well-named helper functions (`createImageCanvas` and `copyOrSaveImage`). Co-authored-by: Horizontal42 <75383519+Horizontal42@users.noreply.github.com> --- src/ui/export.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/src/ui/export.ts b/src/ui/export.ts index 7ce1499..c60d24a 100644 --- a/src/ui/export.ts +++ b/src/ui/export.ts @@ -11,8 +11,8 @@ export interface ExportImageDeps { t(key: string): string; } -export async function exportSheetImage(deps: ExportImageDeps): Promise { - const { lines: rawLines, results: doc, fontSize, toast, t } = deps; +function createImageCanvas(deps: ExportImageDeps): HTMLCanvasElement { + const { lines: rawLines, results: doc, fontSize } = deps; const dpr = window.devicePixelRatio || 1; const style = getComputedStyle(document.documentElement); const bgColor = style.getPropertyValue("--bg").trim() || "#ffffff"; @@ -49,9 +49,11 @@ export async function exportSheetImage(deps: ExportImageDeps): Promise { } } - const blob = await new Promise((res) => canvas.toBlob(res, "image/png")); - if (!blob) { toast(t("imageFailed")); return; } + return canvas; +} +async function copyOrSaveImage(blob: Blob, deps: ExportImageDeps): Promise { + const { toast, t } = deps; try { await navigator.clipboard.write([new ClipboardItem({ "image/png": blob })]); toast(t("imageCopied")); @@ -69,6 +71,18 @@ export async function exportSheetImage(deps: ExportImageDeps): Promise { } } +export async function exportSheetImage(deps: ExportImageDeps): Promise { + const canvas = createImageCanvas(deps); + + const blob = await new Promise((res) => canvas.toBlob(res, "image/png")); + if (!blob) { + deps.toast(deps.t("imageFailed")); + return; + } + + await copyOrSaveImage(blob, deps); +} + /** chunked to avoid "Maximum call stack size exceeded" from spreading a large array as args */ function bytesToBase64(bytes: Uint8Array): string { let bin = "";