From 062ef67a7ade34be8d2d7713d9ea2257c4b0418f Mon Sep 17 00:00:00 2001 From: Dominik Jain Date: Tue, 13 Jan 2026 20:39:26 +0100 Subject: [PATCH] Remove workaround for atob being unavailable Resolves #17 --- penpot-plugin/src/PenpotUtils.ts | 35 ++++++-------------------------- 1 file changed, 6 insertions(+), 29 deletions(-) diff --git a/penpot-plugin/src/PenpotUtils.ts b/penpot-plugin/src/PenpotUtils.ts index f5627ee..008a474 100644 --- a/penpot-plugin/src/PenpotUtils.ts +++ b/penpot-plugin/src/PenpotUtils.ts @@ -142,39 +142,16 @@ export class PenpotUtils { /** * Decodes a base64 string to a Uint8Array. - * This is required because the Penpot plugin environment does not provide the atob function. * * @param base64 - The base64-encoded string to decode * @returns The decoded data as a Uint8Array */ - public static atob(base64: string): Uint8Array { - const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; - const lookup = new Uint8Array(256); - for (let i = 0; i < chars.length; i++) { - lookup[chars.charCodeAt(i)] = i; + public static base64ToByteArray(base64: string): Uint8Array { + const binary = atob(base64); + const bytes = new Uint8Array(binary.length); + for (let i = 0; i < binary.length; i++) { + bytes[i] = binary.charCodeAt(i); } - - let bufferLength = base64.length * 0.75; - if (base64[base64.length - 1] === "=") { - bufferLength--; - if (base64[base64.length - 2] === "=") { - bufferLength--; - } - } - - const bytes = new Uint8Array(bufferLength); - let p = 0; - for (let i = 0; i < base64.length; i += 4) { - const encoded1 = lookup[base64.charCodeAt(i)]; - const encoded2 = lookup[base64.charCodeAt(i + 1)]; - const encoded3 = lookup[base64.charCodeAt(i + 2)]; - const encoded4 = lookup[base64.charCodeAt(i + 3)]; - - bytes[p++] = (encoded1 << 2) | (encoded2 >> 4); - bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2); - bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63); - } - return bytes; } @@ -204,7 +181,7 @@ export class PenpotUtils { height: number | undefined ): Promise { // convert base64 to Uint8Array - const bytes = PenpotUtils.atob(base64); + const bytes = PenpotUtils.base64ToByteArray(base64); // upload the image data to Penpot const imageData = await penpot.uploadMediaData(name, bytes, mimeType);