From d92a84903887078449bf4e4bab9fa307169b28b6 Mon Sep 17 00:00:00 2001 From: Marcel Mraz Date: Wed, 14 May 2025 16:01:43 +0200 Subject: [PATCH] fix: issues when importing package outside of browser (#9525) --- packages/common/package.json | 2 +- packages/common/src/constants.ts | 3 ++- packages/element/package.json | 2 +- packages/element/src/renderElement.ts | 14 ++++++++++++-- packages/excalidraw/components/PublishLibrary.tsx | 4 ++-- packages/excalidraw/data/json.ts | 6 +++--- packages/math/package.json | 2 +- packages/utils/package.json | 2 +- 8 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/common/package.json b/packages/common/package.json index 32cffc717..8fedd6742 100644 --- a/packages/common/package.json +++ b/packages/common/package.json @@ -13,7 +13,7 @@ "default": "./dist/prod/index.js" }, "./*": { - "types": "./../common/dist/types/common/src/*.d.ts" + "types": "./dist/types/common/src/*.d.ts" } }, "files": [ diff --git a/packages/common/src/constants.ts b/packages/common/src/constants.ts index 04a473cd7..3850ed1c7 100644 --- a/packages/common/src/constants.ts +++ b/packages/common/src/constants.ts @@ -10,6 +10,7 @@ export const isDarwin = /Mac|iPod|iPhone|iPad/.test(navigator.platform); export const isWindows = /^Win/.test(navigator.platform); export const isAndroid = /\b(android)\b/i.test(navigator.userAgent); export const isFirefox = + typeof window !== "undefined" && "netscape" in window && navigator.userAgent.indexOf("rv:") > 1 && navigator.userAgent.indexOf("Gecko") > 1; @@ -255,7 +256,7 @@ export const EXPORT_DATA_TYPES = { excalidrawClipboardWithAPI: "excalidraw-api/clipboard", } as const; -export const EXPORT_SOURCE = +export const getExportSource = () => window.EXCALIDRAW_EXPORT_SOURCE || window.location.origin; // time in milliseconds diff --git a/packages/element/package.json b/packages/element/package.json index 1eec60742..16b9a49e7 100644 --- a/packages/element/package.json +++ b/packages/element/package.json @@ -13,7 +13,7 @@ "default": "./dist/prod/index.js" }, "./*": { - "types": "./../element/dist/types/element/src/*.d.ts" + "types": "./dist/types/element/src/*.d.ts" } }, "files": [ diff --git a/packages/element/src/renderElement.ts b/packages/element/src/renderElement.ts index e749bd90c..2786f3f84 100644 --- a/packages/element/src/renderElement.ts +++ b/packages/element/src/renderElement.ts @@ -349,12 +349,22 @@ const generateElementCanvas = ( }; }; -const IMAGE_PLACEHOLDER_IMG = document.createElement("img"); +export const DEFAULT_LINK_SIZE = 14; + +const IMAGE_PLACEHOLDER_IMG = + typeof document !== "undefined" + ? document.createElement("img") + : ({ src: "" } as HTMLImageElement); // mock image element outside of browser + IMAGE_PLACEHOLDER_IMG.src = `data:${MIME_TYPES.svg},${encodeURIComponent( ``, )}`; -const IMAGE_ERROR_PLACEHOLDER_IMG = document.createElement("img"); +const IMAGE_ERROR_PLACEHOLDER_IMG = + typeof document !== "undefined" + ? document.createElement("img") + : ({ src: "" } as HTMLImageElement); // mock image element outside of browser + IMAGE_ERROR_PLACEHOLDER_IMG.src = `data:${MIME_TYPES.svg},${encodeURIComponent( ``, )}`; diff --git a/packages/excalidraw/components/PublishLibrary.tsx b/packages/excalidraw/components/PublishLibrary.tsx index 580b909d4..076b303d7 100644 --- a/packages/excalidraw/components/PublishLibrary.tsx +++ b/packages/excalidraw/components/PublishLibrary.tsx @@ -5,10 +5,10 @@ import { useCallback, useEffect, useRef, useState } from "react"; import { EDITOR_LS_KEYS, EXPORT_DATA_TYPES, - EXPORT_SOURCE, MIME_TYPES, VERSIONS, chunk, + getExportSource, } from "@excalidraw/common"; import { EditorLocalStorage } from "../data/EditorLocalStorage"; @@ -281,7 +281,7 @@ const PublishLibrary = ({ const libContent: ExportedLibraryData = { type: EXPORT_DATA_TYPES.excalidrawLibrary, version: VERSIONS.excalidrawLibrary, - source: EXPORT_SOURCE, + source: getExportSource(), libraryItems: clonedLibItems, }; const content = JSON.stringify(libContent, null, 2); diff --git a/packages/excalidraw/data/json.ts b/packages/excalidraw/data/json.ts index 527c9e56e..52cbf9958 100644 --- a/packages/excalidraw/data/json.ts +++ b/packages/excalidraw/data/json.ts @@ -1,7 +1,7 @@ import { DEFAULT_FILENAME, EXPORT_DATA_TYPES, - EXPORT_SOURCE, + getExportSource, MIME_TYPES, VERSIONS, } from "@excalidraw/common"; @@ -56,7 +56,7 @@ export const serializeAsJSON = ( const data: ExportedDataState = { type: EXPORT_DATA_TYPES.excalidraw, version: VERSIONS.excalidraw, - source: EXPORT_SOURCE, + source: getExportSource(), elements: type === "local" ? clearElementsForExport(elements) @@ -142,7 +142,7 @@ export const serializeLibraryAsJSON = (libraryItems: LibraryItems) => { const data: ExportedLibraryData = { type: EXPORT_DATA_TYPES.excalidrawLibrary, version: VERSIONS.excalidrawLibrary, - source: EXPORT_SOURCE, + source: getExportSource(), libraryItems, }; return JSON.stringify(data, null, 2); diff --git a/packages/math/package.json b/packages/math/package.json index e9f5fd8da..5fac47bef 100644 --- a/packages/math/package.json +++ b/packages/math/package.json @@ -13,7 +13,7 @@ "default": "./dist/prod/index.js" }, "./*": { - "types": "./../math/dist/types/math/src/*.d.ts" + "types": "./dist/types/math/src/*.d.ts" } }, "files": [ diff --git a/packages/utils/package.json b/packages/utils/package.json index 2dc54c59c..6dc400c65 100644 --- a/packages/utils/package.json +++ b/packages/utils/package.json @@ -13,7 +13,7 @@ "default": "./dist/prod/index.js" }, "./*": { - "types": "./../utils/dist/types/utils/src/*.d.ts" + "types": "./dist/types/utils/src/*.d.ts" } }, "files": [