mirror of
https://github.com/Horizontal42/summarum.git
synced 2026-11-10 21:31:59 +03:00
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>
This commit is contained in:
parent
37cfd2144b
commit
6368b7bc1c
1 changed files with 18 additions and 4 deletions
|
|
@ -11,8 +11,8 @@ export interface ExportImageDeps {
|
||||||
t(key: string): string;
|
t(key: string): string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function exportSheetImage(deps: ExportImageDeps): Promise<void> {
|
function createImageCanvas(deps: ExportImageDeps): HTMLCanvasElement {
|
||||||
const { lines: rawLines, results: doc, fontSize, toast, t } = deps;
|
const { lines: rawLines, results: doc, fontSize } = deps;
|
||||||
const dpr = window.devicePixelRatio || 1;
|
const dpr = window.devicePixelRatio || 1;
|
||||||
const style = getComputedStyle(document.documentElement);
|
const style = getComputedStyle(document.documentElement);
|
||||||
const bgColor = style.getPropertyValue("--bg").trim() || "#ffffff";
|
const bgColor = style.getPropertyValue("--bg").trim() || "#ffffff";
|
||||||
|
|
@ -49,9 +49,11 @@ export async function exportSheetImage(deps: ExportImageDeps): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const blob = await new Promise<Blob | null>((res) => canvas.toBlob(res, "image/png"));
|
return canvas;
|
||||||
if (!blob) { toast(t("imageFailed")); return; }
|
}
|
||||||
|
|
||||||
|
async function copyOrSaveImage(blob: Blob, deps: ExportImageDeps): Promise<void> {
|
||||||
|
const { toast, t } = deps;
|
||||||
try {
|
try {
|
||||||
await navigator.clipboard.write([new ClipboardItem({ "image/png": blob })]);
|
await navigator.clipboard.write([new ClipboardItem({ "image/png": blob })]);
|
||||||
toast(t("imageCopied"));
|
toast(t("imageCopied"));
|
||||||
|
|
@ -69,6 +71,18 @@ export async function exportSheetImage(deps: ExportImageDeps): Promise<void> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function exportSheetImage(deps: ExportImageDeps): Promise<void> {
|
||||||
|
const canvas = createImageCanvas(deps);
|
||||||
|
|
||||||
|
const blob = await new Promise<Blob | null>((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 */
|
/** chunked to avoid "Maximum call stack size exceeded" from spreading a large array as args */
|
||||||
function bytesToBase64(bytes: Uint8Array): string {
|
function bytesToBase64(bytes: Uint8Array): string {
|
||||||
let bin = "";
|
let bin = "";
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue