From 0a6c0b5950d3bbd638854a3cd3784e5633a99a68 Mon Sep 17 00:00:00 2001 From: Gabriel Gomes Date: Sat, 29 Mar 2025 21:18:20 +0000 Subject: [PATCH] some minor improvements --- packages/excalidraw/data/image.ts | 17 +++++++++-------- packages/excalidraw/global.d.ts | 15 ++++++++------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/packages/excalidraw/data/image.ts b/packages/excalidraw/data/image.ts index d1425b804..399ba87d3 100644 --- a/packages/excalidraw/data/image.ts +++ b/packages/excalidraw/data/image.ts @@ -17,7 +17,7 @@ export const getMetadataChunk = async ( ): Promise<{ keyword: string; text: string } | null> => { const chunks = decodePng(new Uint8Array(await blobToArrayBuffer(blob))); - const iTXtChunk = chunks.find((chunk) => chunk.name === "iTXt"); + const iTXtChunk = chunks.find((chunk) => chunk.name === PNGChunkType.iTXt); if (iTXtChunk) { try { const decoded = decodeITXt(iTXtChunk.data); @@ -27,7 +27,7 @@ export const getMetadataChunk = async ( } } - const tEXtChunk = chunks.find((chunk) => chunk.name === "tEXt"); + const tEXtChunk = chunks.find((chunk) => chunk.name === PNGChunkType.tEXt); if (tEXtChunk) { return tEXt.decode(tEXtChunk.data); } @@ -48,9 +48,9 @@ export const encodePngMetadata = async ({ const filteredChunks = chunks.filter( (chunk) => - !(chunk.name === "tEXt" && + !(chunk.name === PNGChunkType.tEXt && tEXt.decode(chunk.data).keyword === MIME_TYPES.excalidraw) && - !(chunk.name === "iTXt" && + !(chunk.name === PNGChunkType.iTXt && decodeITXt(chunk.data).keyword === MIME_TYPES.excalidraw) ); @@ -68,7 +68,8 @@ export const encodePngMetadata = async ({ MIME_TYPES.excalidraw, encodedData, { - compressed: false, //Already compressed in encode + compressed: true, + compressedMethod: 0, language: "en", translated: "" } @@ -101,13 +102,13 @@ export const decodePngMetadata = async (blob: Blob) => { ) { return metadata.text; } - throw new Error("FAILED"); + throw new Error("Malformed or unexpected metadata format"); } return decode(encodedData); } catch (error: any) { console.error(error); - throw new Error("FAILED"); + throw new Error("Malformed or unexpected metadata format"); } } - throw new Error("INVALID"); + throw new Error("Invalid or unsupported PNG metadata format"); }; diff --git a/packages/excalidraw/global.d.ts b/packages/excalidraw/global.d.ts index f3c2e2f26..401039b13 100644 --- a/packages/excalidraw/global.d.ts +++ b/packages/excalidraw/global.d.ts @@ -32,23 +32,26 @@ interface Clipboard extends EventTarget { // PNG encoding/decoding // ----------------------------------------------------------------------------- +enum PNGChunkType { + tEXt = "tEXt", + iTXt = "iTXt", +} + type TEXtChunk = { name: "tEXt"; data: Uint8Array }; type ITXtChunk = { name: "iTXt"; data: Uint8Array }; - declare module "png-chunk-text" { function encode( name: string, value: string, - ): { name: "tEXt"; data: Uint8Array }; + ): { name: PNGChunkType.tEXt; data: Uint8Array }; function decode(data: Uint8Array): { keyword: string; text: string }; } - declare module "png-chunk-itxt" { function encode( keyword: string, text: string, - options?: { compressed?: boolean; language?: string; translated?: string }, - ): { name: "iTXt"; data: Uint8Array }; + options?: { compressed?: boolean; compressedMethod: number; language?: string; translated?: string }, + ): { name: PNGChunkType.iTXt; data: Uint8Array }; function decode(data: Uint8Array): { keyword: string; text: string; @@ -57,12 +60,10 @@ declare module "png-chunk-itxt" { }; export { encode, decode }; } - declare module "png-chunks-encode" { function encode(chunks: (TEXtChunk | ITXtChunk)[]): Uint8Array; export = encode; } - declare module "png-chunks-extract" { function extract(buffer: Uint8Array): (TEXtChunk | ITXtChunk)[]; export = extract;