From d3a9c2a195182bf65b526ccb7a19de96f4bdfdb3 Mon Sep 17 00:00:00 2001 From: zsviczian Date: Tue, 20 Oct 2026 05:53:27 +0200 Subject: [PATCH] fix(packages/excalidraw): complete cross-document runtime ownership (#11997) * fix: complete cross-document runtime ownership * fix: schedule cross-document animations in owner window * fix: Prewarm the right font even if it's not Excalifont Signed-off-by: Mark Tolmacs * fix: Hyperlink and tooltip host staleness issues Signed-off-by: Mark Tolmacs * fix: Various hyperlink fixes * feat: Host abstraction * fix: exportToSvg support for document override Signed-off-by: Mark Tolmacs * fix: Remove caches Signed-off-by: Mark Tolmacs * fix: Refactor tests (cut non-critical) Signed-off-by: Mark Tolmacs * chore: Break out host from App Signed-off-by: Mark Tolmacs * fix: hold after release fixes Signed-off-by: Mark Tolmacs * fix: repaint when link image decode is complete per app Signed-off-by: Mark Tolmacs * chore: Font loading per-doc test Signed-off-by: Mark Tolmacs * fix: Lint Signed-off-by: Mark Tolmacs * fix: draw SVG frame labels without text measurement Frame labels were baked into exports as measured, ellipsis-truncated text elements, which made `exportToSvg` depend on a canvas (through the render environment) for the only text it ever measures. - add `getFrameLabel` (element package): a measurement-free label spec -- position, font, color, arithmetic height, the frame's width as max width - SVG: draw labels from the spec in the frame branch, full title clipped to the frame's width; extract `appendSvgTextLines` so text elements and labels produce identical markup - canvas: unchanged output (measured, truncated text elements), now built from the same spec - fit label boxes into the SVG export bounds explicitly (`extraBounds`) - test: SVG export of a named frame succeeds with a render environment whose `createCanvas` throws and creates no canvas through `document` Co-Authored-By: Claude Fable 5.1 * fix: measurement-free embed placeholders; no render environment in SVG export The placeholder drawn for an embed that isn't rendered live was the last text `exportToSvg` measured, which kept a canvas (via the render environment) in the SVG path and made headless SVG export throw on any scene with an embed unless the process had been polyfilled. - show the placeholder as a single line capped at 30 characters (`getPlaceholderLinkText`: scheme and `www.` dropped, ellipsis) instead of wrapping the whole link: at or below that length the existing `width / length` font size fits the box by construction, so nothing is measured, wrapped or clipped, and output is identical on every host. The full link stays on the element's `` in SVG - `newTextElement` accepts pre-computed `metrics` to skip measuring - drop `renderEnvironment` from `SVGRenderConfig`, `exportToSvg` and the `@excalidraw/utils` signature: SVG export's only host dependency is its owner document - throw a guided error from the default canvas/image factories when there is no `document`/`Image`, instead of a bare ReferenceError - tests: placeholder text capping and layout; SVG export of an iframe and a long-link embed with a render environment whose `createCanvas` throws Co-Authored-By: Claude Fable 5.1 * fix: scope top picks drag to source window * feat: StickyNotes support Signed-off-by: Mark Tolmacs * Revert "fix: measurement-free embed placeholders; no render environment in SVG export" This reverts commit 886bea142. Rich text makes SVG export measure text anyway (explicit x per line, run boundaries, baselines), so placeholder labels go back to being measured and wrapped like on canvas, through the render environment. Kept: the guided errors thrown by the default canvas/image factories when there is no `document`/`Image`. Claude Opus 5.5 (xhigh) * Revert "fix: draw SVG frame labels without text measurement" This reverts commit 295e7d88b. SVG export measures text again (rich text needs it regardless), so both formats go back to master's single path: frame labels baked in as measured, truncated text elements, measured through the export's render environment. Drops `getFrameLabel` and the SVG label renderer. Claude Opus 5.5 (xhigh) * export: one `config.environment` for every format, `document` for SVG SVG exports take the same environment as canvas ones: they measure text (frame labels, embed placeholders) through its `createCanvas`, and are built in its `document` (was the SVG-only `ownerDocument`). Claude Opus 5.5 (xhigh) * RenderEnvironment: carry the `document` The environment is the one host parameter: its `document` is what SVG exports are built in (the editor's owner document by default), so export call sites pass a single environment instead of an environment plus an `ownerDocument`. `exportCanvas` drops its `ownerDocument` option, and `config.environment.document` defaults to the environment's. Rich text's DOM layout will read the same member, rather than threading `ownerDocument` through the measuring functions next to the environment. Claude Opus 5.5 (xhigh) * drop the process-wide `setRenderEnvironment` / `resetRenderEnvironment` Every render and export now takes its environment explicitly: exports through `config.environment`, the editor through its `renderEnvironment` prop. Text measured outside both (e.g. creating elements on a server) has `setCustomTextMetricsProvider`. A second, global way to configure the same thing would be state shared by every job in the process, which is what per-call export environments avoid. The default is now a constant (the browser's). Claude Opus 5.5 (xhigh) * render: log per-element failures on export too, rather than throwing As in the editor (and on master): one element that fails to render is logged and skipped, so the rest of the exported scene survives it rather than the whole export failing. Claude Opus 5.5 (xhigh) * RenderEnvironment: carry the text metrics provider `textMetrics` replaces the per-environment registry of `setCustomTextMetricsProvider(provider, env)`, which looked providers up by the environment's identity, so any rebuilt environment (as exports build one from `config.environment`) silently lost them. It now travels with the environment, and `config.environment.textMetrics` measures an export's text. The global `setCustomTextMetricsProvider(provider)` stays for text measured outside both (e.g. elements created on a server). Exports use a complete `config.environment` as-is (the editor's keeps its caches warm), and document that it needn't be the same object across exports. Frame label truncation measures through the environment too, rather than on a canvas of its own. Claude Opus 5.5 (xhigh) * render: restore the canvas state when an element fails to draw The scene renderer logs a failing element and carries on, but the `save()`s it had made were never restored: its transforms and frame clip stayed on the context for every element after it (a failing freedraw at (80, 30) moved the next rectangle from (10, 10) to (90, 40)). Every `save()` around drawing now restores in a `finally` (`withSavedContext`). Pre-existing on master, in the editor and on export. Claude Opus 5.5 (xhigh) * export: truncate frame names down to "...", or none, to fit the frame When not even one character plus the ellipsis fit, the frame name kept its full title (overflowing narrow frames into their neighbors) while reporting the frame's width. It now shrinks to just "...", and is dropped where even that doesn't fit. Pre-existing on master. Claude Opus 5.5 (xhigh) --------- Signed-off-by: Mark Tolmacs Co-authored-by: Mark Tolmacs Co-authored-by: dwelle <5153846+dwelle@users.noreply.github.com> Co-authored-by: Claude Fable 5.1 --- packages/common/src/editorInterface.ts | 32 +- packages/common/src/utils.ts | 41 +- .../element/src/__tests__/stickyNote.test.ts | 37 + packages/element/src/dragElements.ts | 7 + packages/element/src/embeddable.ts | 5 +- packages/element/src/image.ts | 18 +- packages/element/src/index.ts | 1 + packages/element/src/newElement.ts | 25 +- packages/element/src/renderElement.ts | 1000 +++++++++-------- packages/element/src/renderEnvironment.ts | 120 ++ packages/element/src/resizeElements.ts | 20 + packages/element/src/stickyNote.ts | 26 +- packages/element/src/textElement.ts | 19 +- packages/element/src/textMeasurements.ts | 126 ++- packages/element/src/textWrapping.ts | 31 +- packages/element/src/transform.ts | 16 +- .../tests/linearElementEditor.test.tsx | 1 + .../tests/renderEnvironmentCaches.test.ts | 83 ++ packages/excalidraw/CHANGELOG.md | 2 +- .../excalidraw/actions/actionBoundText.tsx | 18 +- .../excalidraw/actions/actionClipboard.tsx | 2 + packages/excalidraw/actions/actionFlip.ts | 1 + .../excalidraw/actions/actionProperties.tsx | 17 +- packages/excalidraw/actions/actionStyles.ts | 12 +- packages/excalidraw/animatedTrail.ts | 20 +- packages/excalidraw/appState.ts | 14 +- packages/excalidraw/charts/charts.bar.ts | 14 +- packages/excalidraw/charts/charts.helpers.ts | 124 +- packages/excalidraw/charts/charts.line.ts | 14 +- packages/excalidraw/charts/charts.radar.ts | 14 +- packages/excalidraw/charts/index.ts | 9 +- .../excalidraw/components/App.arrowText.ts | 1 + .../excalidraw/components/App.bucketFill.ts | 4 +- .../excalidraw/components/App.clipboard.ts | 24 +- packages/excalidraw/components/App.cursor.ts | 2 +- packages/excalidraw/components/App.host.ts | 234 ++++ packages/excalidraw/components/App.pan.ts | 4 +- .../components/App.selectionTool.ts | 1 + packages/excalidraw/components/App.text.ts | 4 + packages/excalidraw/components/App.tsx | 101 +- .../excalidraw/components/App.viewport.ts | 59 +- .../ColorPicker/colorTopPicksDnD.ts | 8 +- .../components/ConvertElementTypePopup.tsx | 9 +- .../components/FontPicker/fontTopPicksDnD.ts | 10 +- .../components/PasteChartDialog.tsx | 18 +- .../excalidraw/components/PublishLibrary.tsx | 15 +- packages/excalidraw/components/SearchMenu.tsx | 23 +- .../excalidraw/components/Stats/FontSize.tsx | 2 + .../components/Stats/MultiDimension.tsx | 11 +- .../components/Stats/MultiFontSize.tsx | 3 + .../excalidraw/components/TTDDialog/common.ts | 2 +- packages/excalidraw/components/Tooltip.tsx | 97 +- .../components/TopPicksDnD/topPicksDnD.tsx | 85 +- .../components/canvases/InteractiveCanvas.tsx | 22 +- .../components/hyperlink/Hyperlink.tsx | 138 ++- .../components/hyperlink/helpers.ts | 108 +- packages/excalidraw/data/blob.ts | 4 +- packages/excalidraw/data/index.ts | 6 +- packages/excalidraw/fonts/Fonts.ts | 53 +- packages/excalidraw/index.tsx | 3 + packages/excalidraw/package.json | 2 +- packages/excalidraw/reactUtils.ts | 12 +- packages/excalidraw/renderer/animation.ts | 137 ++- packages/excalidraw/renderer/staticScene.ts | 317 ++++-- .../excalidraw/renderer/staticSvgScene.ts | 7 +- packages/excalidraw/scene/Renderer.ts | 6 +- packages/excalidraw/scene/export.ts | 188 +++- packages/excalidraw/scene/types.ts | 20 + .../excalidraw/tests/crossDocument.test.tsx | 188 ++++ .../excalidraw/tests/fitToContent.test.tsx | 11 +- .../excalidraw/tests/hostAbstraction.test.tsx | 508 +++++++++ .../excalidraw/tests/renderCaches.test.ts | 291 +++++ .../excalidraw/tests/renderOverrides.test.tsx | 14 + .../excalidraw/tests/scene/export.test.ts | 37 + .../tests/scrollConstraints.test.tsx | 102 +- packages/excalidraw/tests/tooltip.test.tsx | 152 ++- packages/excalidraw/types.ts | 26 + packages/excalidraw/wysiwyg/textWysiwyg.tsx | 26 +- packages/utils/src/index.ts | 1 + 79 files changed, 3943 insertions(+), 1022 deletions(-) create mode 100644 packages/element/src/renderEnvironment.ts create mode 100644 packages/element/tests/renderEnvironmentCaches.test.ts create mode 100644 packages/excalidraw/components/App.host.ts create mode 100644 packages/excalidraw/tests/hostAbstraction.test.tsx create mode 100644 packages/excalidraw/tests/renderCaches.test.ts diff --git a/packages/common/src/editorInterface.ts b/packages/common/src/editorInterface.ts index d18b7a7d08..40e26ae10c 100644 --- a/packages/common/src/editorInterface.ts +++ b/packages/common/src/editorInterface.ts @@ -34,24 +34,32 @@ export const MQ_RIGHT_SIDEBAR_MIN_WIDTH = 1229; // ----------------------------------------------------------------------------- // user agent detections -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); +// +// evaluated at module scope, so they must tolerate a non-browser realm -- +// these modules are imported by the export pipeline, which is expected to be +// loadable under Node +const ua = typeof navigator === "undefined" ? undefined : navigator; +const uaPlatform = ua?.platform ?? ""; +const uaString = ua?.userAgent ?? ""; + +export const isDarwin = /Mac|iPod|iPhone|iPad/.test(uaPlatform); +export const isWindows = /^Win/.test(uaPlatform); +export const isAndroid = /\b(android)\b/i.test(uaString); export const isFirefox = typeof window !== "undefined" && "netscape" in window && - navigator.userAgent.indexOf("rv:") > 1 && - navigator.userAgent.indexOf("Gecko") > 1; -export const isChrome = navigator.userAgent.indexOf("Chrome") !== -1; -export const isSafari = - !isChrome && navigator.userAgent.indexOf("Safari") !== -1; + uaString.indexOf("rv:") > 1 && + uaString.indexOf("Gecko") > 1; +export const isChrome = uaString.indexOf("Chrome") !== -1; +export const isSafari = !isChrome && uaString.indexOf("Safari") !== -1; export const isIOS = - /iPad|iPhone/i.test(navigator.platform) || + /iPad|iPhone/i.test(uaPlatform) || // iPadOS 13+ - (navigator.userAgent.includes("Mac") && "ontouchend" in document); + (uaString.includes("Mac") && + typeof document !== "undefined" && + "ontouchend" in document); // keeping function so it can be mocked in test -export const isBrave = () => - (navigator as any).brave?.isBrave?.name === "isBrave"; +export const isBrave = () => (ua as any)?.brave?.isBrave?.name === "isBrave"; // export const isMobile = // isIOS || diff --git a/packages/common/src/utils.ts b/packages/common/src/utils.ts index dcab0fcd56..be2e4b1384 100644 --- a/packages/common/src/utils.ts +++ b/packages/common/src/utils.ts @@ -48,12 +48,26 @@ export const getDateTime = () => { export const capitalizeString = (str: string) => str.charAt(0).toUpperCase() + str.slice(1); -const getTargetWindow = ( - target: Element | EventTarget | null, -): (Window & typeof globalThis) | null => - (target as (EventTarget & { ownerDocument?: Document | null }) | null) - ?.ownerDocument?.defaultView ?? - (typeof window === "undefined" ? null : window); +/** + * Resolves the window owning `target`, so that we keep working when the editor + * is rendered into another document. Accepts a `Document` as well, in which + * case its `defaultView` is used. Falls back to the module realm's window. + */ +export const getTargetWindow = ( + target: Element | EventTarget | Document | null, +): (Window & typeof globalThis) | null => { + const owner = target as + | (EventTarget & { + ownerDocument?: Document | null; + defaultView?: (Window & typeof globalThis) | null; + }) + | null; + return ( + owner?.ownerDocument?.defaultView ?? + owner?.defaultView ?? + (typeof window === "undefined" ? null : window) + ); +}; export const isToolIcon = ( target: Element | EventTarget | null, @@ -181,12 +195,19 @@ export const debounce = ( }; // throttle callback to execute once per animation frame using the latest args -export const throttleRAF = (fn: (...args: T) => void) => { +export const throttleRAF = ( + fn: (...args: T) => void, + ownerWindow?: Pick, +) => { let timerId: number | null = null; let lastArgs: T | null = null; + // resolved lazily so module-scope callers work in environments without + // a global window (headless import) + const getOwnerWindow = () => ownerWindow ?? window; + const scheduleFunc = () => { - timerId = window.requestAnimationFrame(() => { + timerId = getOwnerWindow().requestAnimationFrame(() => { timerId = null; const args = lastArgs; lastArgs = null; @@ -205,7 +226,7 @@ export const throttleRAF = (fn: (...args: T) => void) => { }; ret.flush = () => { if (timerId !== null) { - cancelAnimationFrame(timerId); + getOwnerWindow().cancelAnimationFrame(timerId); timerId = null; } if (lastArgs) { @@ -216,7 +237,7 @@ export const throttleRAF = (fn: (...args: T) => void) => { ret.cancel = () => { lastArgs = null; if (timerId !== null) { - cancelAnimationFrame(timerId); + getOwnerWindow().cancelAnimationFrame(timerId); timerId = null; } }; diff --git a/packages/element/src/__tests__/stickyNote.test.ts b/packages/element/src/__tests__/stickyNote.test.ts index a8d8e58e81..aec3283724 100644 --- a/packages/element/src/__tests__/stickyNote.test.ts +++ b/packages/element/src/__tests__/stickyNote.test.ts @@ -48,6 +48,7 @@ import { redrawTextBoundingBox, } from "../textElement"; +import type { RenderEnvironment } from "../renderEnvironment"; import type { ExcalidrawElement, ExcalidrawStickyNoteElement, @@ -712,6 +713,42 @@ describe("sticky note text layout", () => { measure.mockRestore(); }); + it("measures the fit through the given render environment", () => { + // the test-env canvas provider reports 10px per character; a host whose + // canvas measures three times as wide is observable in the fitted label + const perChar = 3; + const env: RenderEnvironment = { + createCanvas: () => + ({ + getContext: () => ({ + font: "", + measureText: (text: string) => ({ width: text.length * perChar }), + }), + } as unknown as HTMLCanvasElement), + createImage: () => ({} as HTMLImageElement), + }; + const { scene, stickyId, textId } = createStickyWithText("short"); + const sticky = getSticky(scene, stickyId); + const text = getBoundText(scene, textId); + const defaultWidth = getStickyNoteLayout(sticky, text).text!.width; + expect(defaultWidth).toBe("short".length * 10); + + // the pure layout + expect( + getStickyNoteLayout(sticky, text, { renderEnvironment: env }).text!.width, + ).toBe(defaultWidth * perChar); + + // ...and both ways of applying it to a scene + updateStickyNoteLayout(sticky, scene, { renderEnvironment: env }); + expect(getBoundText(scene, textId).width).toBe(defaultWidth * perChar); + + redrawTextBoundingBox(getBoundText(scene, textId), sticky, scene); + expect(getBoundText(scene, textId).width).toBe(defaultWidth); + + redrawTextBoundingBox(getBoundText(scene, textId), sticky, scene, env); + expect(getBoundText(scene, textId).width).toBe(defaultWidth * perChar); + }); + it("honors a lowered ceiling even when the old fitted size still fits", () => { const { scene, stickyId, textId } = createStickyWithText("short"); const sticky = getSticky(scene, stickyId); diff --git a/packages/element/src/dragElements.ts b/packages/element/src/dragElements.ts index 3f8c427b34..7678c0a18b 100644 --- a/packages/element/src/dragElements.ts +++ b/packages/element/src/dragElements.ts @@ -32,6 +32,7 @@ import { isTextElement, } from "./typeChecks"; +import type { RenderEnvironment } from "./renderEnvironment"; import type { Scene } from "./Scene"; import type { ExcalidrawElement, ExcalidrawTextElement } from "./types"; @@ -247,6 +248,7 @@ export const dragNewTextElement = ({ nextY, zoom, scene, + renderEnvironment, informMutation = true, }: { newElement: ExcalidrawTextElement; @@ -258,6 +260,7 @@ export const dragNewTextElement = ({ nextY?: number; zoom: NormalizedZoomValue; scene: Scene; + renderEnvironment?: RenderEnvironment; informMutation?: boolean; }) => { const offset = pointerX - anchorX; @@ -276,6 +279,7 @@ export const dragNewTextElement = ({ fontFamily: newElement.fontFamily, }), newElement.lineHeight, + renderEnvironment, ), ); @@ -304,6 +308,7 @@ export const dragNewElement = ({ shouldResizeFromCenter, zoom, scene, + renderEnvironment, widthAspectRatio = null, originOffset = null, informMutation = true, @@ -320,6 +325,7 @@ export const dragNewElement = ({ shouldResizeFromCenter: boolean; zoom: NormalizedZoomValue; scene: Scene; + renderEnvironment?: RenderEnvironment; /** whether to keep given aspect ratio when `isResizeWithSidesSameLength` is true */ widthAspectRatio?: number | null; @@ -367,6 +373,7 @@ export const dragNewElement = ({ nextY: originY + (originOffset?.y ?? 0), zoom, scene, + renderEnvironment, informMutation, }); return; diff --git a/packages/element/src/embeddable.ts b/packages/element/src/embeddable.ts index 8d938b5ad0..dceda3281e 100644 --- a/packages/element/src/embeddable.ts +++ b/packages/element/src/embeddable.ts @@ -12,6 +12,7 @@ import { newTextElement } from "./newElement"; import { wrapText } from "./textWrapping"; import { isIframeElement } from "./typeChecks"; +import type { RenderEnvironment } from "./renderEnvironment"; import type { ExcalidrawIframeLikeElement, IframeData, @@ -401,6 +402,7 @@ export const getEmbedLink = ( export const createPlaceholderEmbeddableLabel = ( element: ExcalidrawIframeLikeElement, + renderEnvironment?: RenderEnvironment, ): NonDeletedExcalidrawElement => { let text: string; if (isIframeElement(element)) { @@ -429,10 +431,11 @@ export const createPlaceholderEmbeddableLabel = ( backgroundColor: "transparent", fontFamily, fontSize, - text: wrapText(text, fontString, element.width - 20), + text: wrapText(text, fontString, element.width - 20, renderEnvironment), textAlign: "center", verticalAlign: VERTICAL_ALIGN.MIDDLE, angle: element.angle ?? 0, + renderEnvironment, }); }; diff --git a/packages/element/src/image.ts b/packages/element/src/image.ts index 562b489048..c28a270e7a 100644 --- a/packages/element/src/image.ts +++ b/packages/element/src/image.ts @@ -10,6 +10,7 @@ import type { BinaryFiles, } from "@excalidraw/excalidraw/types"; +import { getRenderEnvironment } from "./renderEnvironment"; import { isInitializedImageElement } from "./typeChecks"; import type { @@ -18,9 +19,13 @@ import type { InitializedExcalidrawImageElement, } from "./types"; -export const loadHTMLImageElement = (dataURL: DataURL) => { +export const loadHTMLImageElement = ( + dataURL: DataURL, + createImage: () => HTMLImageElement = () => + getRenderEnvironment().createImage(), +) => { return new Promise((resolve, reject) => { - const image = new Image(); + const image = createImage(); image.onload = () => { resolve(image); }; @@ -37,10 +42,14 @@ export const updateImageCache = async ({ fileIds, files, imageCache, + createImage, }: { fileIds: FileId[]; files: BinaryFiles; imageCache: AppClassProperties["imageCache"]; + /** creates images in the owner window so cross-document scenes keep + * runtime ownership of their HTMLImageElements */ + createImage?: () => HTMLImageElement; }) => { const updatedFiles = new Map(); const erroredFiles = new Map(); @@ -57,7 +66,10 @@ export const updateImageCache = async ({ throw new Error("Only images can be added to ImageCache"); } - const imagePromise = loadHTMLImageElement(fileData.dataURL); + const imagePromise = loadHTMLImageElement( + fileData.dataURL, + createImage, + ); const data = { image: imagePromise, mimeType: fileData.mimeType, diff --git a/packages/element/src/index.ts b/packages/element/src/index.ts index 8fa93b2de6..8732bd9ecf 100644 --- a/packages/element/src/index.ts +++ b/packages/element/src/index.ts @@ -86,6 +86,7 @@ export * from "./mutateElement"; export * from "./newElement"; export * from "./positionElementsOnGrid"; export * from "./renderElement"; +export * from "./renderEnvironment"; export * from "./resizeElements"; export * from "./resizeTest"; export * from "./Scene"; diff --git a/packages/element/src/newElement.ts b/packages/element/src/newElement.ts index 02026067e1..75b98c9ced 100644 --- a/packages/element/src/newElement.ts +++ b/packages/element/src/newElement.ts @@ -34,6 +34,8 @@ import { wrapText } from "./textWrapping"; import { isLineElement } from "./typeChecks"; +import type { RenderEnvironment } from "./renderEnvironment"; + import type { ExcalidrawElement, ExcalidrawImageElement, @@ -345,6 +347,7 @@ export const newTextElement = ( autoResize?: ExcalidrawTextElement["autoResize"]; labelPosition?: ExcalidrawTextElement["labelPosition"]; baseFontSize?: ExcalidrawTextElement["baseFontSize"]; + renderEnvironment?: RenderEnvironment; } & ElementConstructorOpts, ): NonDeleted => { const fontFamily = opts.fontFamily || DEFAULT_FONT_FAMILY; @@ -355,6 +358,7 @@ export const newTextElement = ( text, getFontString({ fontFamily, fontSize }), lineHeight, + opts.renderEnvironment, ); const textAlign = opts.textAlign || DEFAULT_TEXT_ALIGN; const verticalAlign = opts.verticalAlign || DEFAULT_VERTICAL_ALIGN; @@ -396,6 +400,7 @@ const getAdjustedDimensions = ( nextText: string, /** the width the text is to take regardless of its content */ nextFixedWidth?: number, + renderEnvironment?: RenderEnvironment, ): { x: number; y: number; @@ -406,6 +411,7 @@ const getAdjustedDimensions = ( nextText, getFontString(element), element.lineHeight, + renderEnvironment, ); // wrapped text @@ -428,6 +434,7 @@ const getAdjustedDimensions = ( element.text, getFontString(element), element.lineHeight, + renderEnvironment, ); const offsets = getTextElementPositionOffsets(element, { width: nextWidth - prevMetrics.width, @@ -542,6 +549,7 @@ export const refreshTextDimensions = ( * text that is already wider keeps growing. */ maxWidth?: number, + renderEnvironment?: RenderEnvironment, ) => { if (textElement.isDeleted) { return; @@ -553,8 +561,11 @@ export const refreshTextDimensions = ( textElement.width <= maxWidth ) { const font = getFontString(textElement); - if (measureText(text, font, textElement.lineHeight).width > maxWidth) { - const wrappedText = wrapText(text, font, maxWidth); + if ( + measureText(text, font, textElement.lineHeight, renderEnvironment).width > + maxWidth + ) { + const wrappedText = wrapText(text, font, maxWidth, renderEnvironment); return { text: wrappedText, autoResize: false, @@ -563,6 +574,7 @@ export const refreshTextDimensions = ( elementsMap, wrappedText, maxWidth, + renderEnvironment, ), }; } @@ -574,9 +586,16 @@ export const refreshTextDimensions = ( container ? getBoundTextMaxWidth(container, textElement) : textElement.width, + renderEnvironment, ); } - const dimensions = getAdjustedDimensions(textElement, elementsMap, text); + const dimensions = getAdjustedDimensions( + textElement, + elementsMap, + text, + undefined, + renderEnvironment, + ); return { text, ...dimensions }; }; diff --git a/packages/element/src/renderElement.ts b/packages/element/src/renderElement.ts index 833743a217..8cea865371 100644 --- a/packages/element/src/renderElement.ts +++ b/packages/element/src/renderElement.ts @@ -46,6 +46,8 @@ import type { InteractiveCanvasRenderConfig, } from "@excalidraw/excalidraw/scene/types"; +import { getCreatePath, getRenderEnvironment } from "./renderEnvironment"; + import { getElementAbsoluteCoords, getElementBounds } from "./bounds"; import { getUncroppedImageElement } from "./cropElement"; import { LinearElementEditor } from "./linearElementEditor"; @@ -78,6 +80,8 @@ import { type StickyNotePathCommand, } from "./stickyNote"; +import type { RenderEnvironment } from "./renderEnvironment"; + import type { ExcalidrawElement, ExcalidrawTextElement, @@ -206,6 +210,9 @@ export interface ExcalidrawElementWithCanvas { canvas: HTMLCanvasElement; theme: AppState["theme"]; scale: number; + /** backing-store scale the bitmap was rasterized at, see StaticCanvasRenderConfig["scale"] */ + deviceScale: number; + environment: RenderEnvironment; zoomValue: AppState["zoom"]["value"]; canvasOffsetX: number; canvasOffsetY: number; @@ -217,6 +224,8 @@ const cappedElementCanvasSize = ( element: NonDeletedExcalidrawElement, elementsMap: ElementsMap, zoom: Zoom, + /** backing-store scale, see StaticCanvasRenderConfig["scale"] */ + deviceScale: number, ): { width: number; height: number; @@ -244,8 +253,8 @@ const cappedElementCanvasSize = ( ? distance(y1, y2) : element.height; - let width = elementWidth * window.devicePixelRatio + padding * 2; - let height = elementHeight * window.devicePixelRatio + padding * 2; + let width = elementWidth * deviceScale + padding * 2; + let height = elementHeight * deviceScale + padding * 2; let scale: number = zoom.value; @@ -275,7 +284,8 @@ const generateElementCanvas = ( renderConfig: StaticCanvasRenderConfig, appState: StaticCanvasAppState | InteractiveCanvasAppState, ): ExcalidrawElementWithCanvas | null => { - const canvas = document.createElement("canvas"); + const environment = getRenderEnvironment(renderConfig.renderEnvironment); + const canvas = environment.createCanvas(); const context = canvas.getContext("2d")!; const padding = getCanvasPadding(element); @@ -283,6 +293,7 @@ const generateElementCanvas = ( element, elementsMap, zoom, + renderConfig.scale, ); if (!width || !height) { @@ -299,24 +310,17 @@ const generateElementCanvas = ( const [x1, y1] = getElementAbsoluteCoords(element, elementsMap); canvasOffsetX = - element.x > x1 - ? distance(element.x, x1) * window.devicePixelRatio * scale - : 0; + element.x > x1 ? distance(element.x, x1) * renderConfig.scale * scale : 0; canvasOffsetY = - element.y > y1 - ? distance(element.y, y1) * window.devicePixelRatio * scale - : 0; + element.y > y1 ? distance(element.y, y1) * renderConfig.scale * scale : 0; context.translate(canvasOffsetX, canvasOffsetY); } context.save(); context.translate(padding * scale, padding * scale); - context.scale( - window.devicePixelRatio * scale, - window.devicePixelRatio * scale, - ); + context.scale(renderConfig.scale * scale, renderConfig.scale * scale); const rc = rough.canvas(canvas); @@ -329,6 +333,8 @@ const generateElementCanvas = ( canvas, theme: appState.theme, scale, + deviceScale: renderConfig.scale, + environment, zoomValue: zoom.value, canvasOffsetX, canvasOffsetY, @@ -340,28 +346,31 @@ const generateElementCanvas = ( 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_PLACEHOLDER_SRC = `data:${MIME_TYPES.svg},${encodeURIComponent( ``, )}`; -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( +const IMAGE_ERROR_PLACEHOLDER_SRC = `data:${ + MIME_TYPES.svg +},${encodeURIComponent( ``, )}`; +/** + * Built lazily so that importing this module never touches `document`, and so + * that a custom render environment supplies the element. + */ +const getPlaceholderImg = (src: string, env: RenderEnvironment) => { + const img = env.createImage(); + img.src = src; + return img; +}; + const drawImagePlaceholder = ( element: ExcalidrawImageElement, context: CanvasRenderingContext2D, theme: StaticCanvasRenderConfig["theme"], + env: RenderEnvironment, ) => { context.fillStyle = theme === THEME.DARK ? "#2E2E2E" : "#E7E7E7"; context.fillRect(0, 0, element.width, element.height); @@ -374,9 +383,12 @@ const drawImagePlaceholder = ( ); context.drawImage( - element.status === "error" - ? IMAGE_ERROR_PLACEHOLDER_IMG - : IMAGE_PLACEHOLDER_IMG, + getPlaceholderImg( + element.status === "error" + ? IMAGE_ERROR_PLACEHOLDER_SRC + : IMAGE_PLACEHOLDER_SRC, + env, + ), element.width / 2 - size / 2, element.height / 2 - size / 2, size, @@ -428,6 +440,23 @@ const strokeStickyNoteEdge = ( context.restore(); }; +/** + * `save()`s the context around `draw`, restoring it even when `draw` throws: + * the scene renderer logs a failing element and carries on, and the element + * mustn't leave its transforms and clips behind for the next one. + */ +export const withSavedContext = ( + context: CanvasRenderingContext2D, + draw: () => void, +) => { + context.save(); + try { + draw(); + } finally { + context.restore(); + } +}; + const drawElementOnCanvas = ( element: NonDeletedExcalidrawElement, rc: RoughCanvas, @@ -436,38 +465,37 @@ const drawElementOnCanvas = ( ) => { switch (element.type) { case "stickynote": { - context.save(); - context.fillStyle = `rgba(0, 0, 0, ${STICKY_NOTE_SHADOW_OPACITY})`; - fillStickyNoteShape( - context, - getStickyNotePathCommands(element, { shadow: true }), - ); + withSavedContext(context, () => { + context.fillStyle = `rgba(0, 0, 0, ${STICKY_NOTE_SHADOW_OPACITY})`; + fillStickyNoteShape( + context, + getStickyNotePathCommands(element, { shadow: true }), + ); - const commands = getStickyNotePathCommands(element); - context.fillStyle = applyDarkModeFilter( - element.backgroundColor, - renderConfig.theme === THEME.DARK, - ); - fillStickyNoteShape(context, commands); - strokeStickyNoteEdge(context, commands); - - // the label is absolute, so this cached canvas only goes stale at a - // year boundary — and is regenerated on the next zoom, theme or - // element change anyway - const footer = getStickyNoteFooter(element); - if (footer) { - context.font = `${STICKY_NOTE_FOOTER.fontSize}px ${STICKY_NOTE_FOOTER.fontFamily}`; - context.textAlign = "right"; - context.textBaseline = "alphabetic"; + const commands = getStickyNotePathCommands(element); context.fillStyle = applyDarkModeFilter( - element.strokeColor, + element.backgroundColor, renderConfig.theme === THEME.DARK, ); - context.globalAlpha *= STICKY_NOTE_FOOTER.opacity; - context.fillText(footer.text, footer.x, footer.y); - } + fillStickyNoteShape(context, commands); + strokeStickyNoteEdge(context, commands); - context.restore(); + // the label is absolute, so this cached canvas only goes stale at a + // year boundary — and is regenerated on the next zoom, theme or + // element change anyway + const footer = getStickyNoteFooter(element); + if (footer) { + context.font = `${STICKY_NOTE_FOOTER.fontSize}px ${STICKY_NOTE_FOOTER.fontFamily}`; + context.textAlign = "right"; + context.textBaseline = "alphabetic"; + context.fillStyle = applyDarkModeFilter( + element.strokeColor, + renderConfig.theme === THEME.DARK, + ); + context.globalAlpha *= STICKY_NOTE_FOOTER.opacity; + context.fillText(footer.text, footer.x, footer.y); + } + }); break; } case "rectangle": @@ -495,180 +523,194 @@ const drawElementOnCanvas = ( } case "freedraw": { // Draw directly to canvas - context.save(); + withSavedContext(context, () => { + const shapes = ShapeCache.generateElementShape(element, renderConfig); + const createPath = getCreatePath(renderConfig.renderEnvironment); - const shapes = ShapeCache.generateElementShape(element, renderConfig); - - for (const shape of shapes) { - if (typeof shape === "string") { - context.fillStyle = applyDarkModeFilter( - element.strokeColor, - renderConfig.theme === THEME.DARK, - ); - context.fill(new Path2D(shape)); - } else { - rc.draw(shape); + for (const shape of shapes) { + if (typeof shape === "string") { + context.fillStyle = applyDarkModeFilter( + element.strokeColor, + renderConfig.theme === THEME.DARK, + ); + context.fill(createPath(shape)); + } else { + rc.draw(shape); + } } - } - - context.restore(); + }); break; } case "image": { - context.save(); - const cacheEntry = - element.fileId !== null - ? renderConfig.imageCache.get(element.fileId) - : null; - const img = isInitializedImageElement(element) - ? cacheEntry?.image - : undefined; + withSavedContext(context, () => { + const cacheEntry = + element.fileId !== null + ? renderConfig.imageCache.get(element.fileId) + : null; + const img = isInitializedImageElement(element) + ? cacheEntry?.image + : undefined; - if (img != null && !(img instanceof Promise)) { - if (element.roundness && context.roundRect) { - context.beginPath(); - context.roundRect( - 0, - 0, - element.width, - element.height, - getCornerRadius(Math.min(element.width, element.height), element), - ); - context.clip(); - } + if (img != null && !(img instanceof Promise)) { + if (element.roundness && context.roundRect) { + context.beginPath(); + context.roundRect( + 0, + 0, + element.width, + element.height, + getCornerRadius(Math.min(element.width, element.height), element), + ); + context.clip(); + } - const { x, y, width, height } = element.crop - ? element.crop - : { - x: 0, - y: 0, - width: img.naturalWidth, - height: img.naturalHeight, - }; + const { x, y, width, height } = element.crop + ? element.crop + : { + x: 0, + y: 0, + width: img.naturalWidth, + height: img.naturalHeight, + }; - const shouldInvertImage = - renderConfig.theme === THEME.DARK && - cacheEntry?.mimeType === MIME_TYPES.svg; + const shouldInvertImage = + renderConfig.theme === THEME.DARK && + cacheEntry?.mimeType === MIME_TYPES.svg; - if (shouldInvertImage && isSafari) { - const devicePixelRatio = window.devicePixelRatio || 1; - const tempCanvas = document.createElement("canvas"); - tempCanvas.width = element.width * devicePixelRatio; - tempCanvas.height = element.height * devicePixelRatio; - const tempContext = tempCanvas.getContext("2d"); + if (shouldInvertImage && isSafari) { + const devicePixelRatio = renderConfig.scale || 1; + const tempCanvas = getRenderEnvironment( + renderConfig.renderEnvironment, + ).createCanvas(); + tempCanvas.width = element.width * devicePixelRatio; + tempCanvas.height = element.height * devicePixelRatio; + const tempContext = tempCanvas.getContext("2d"); - if (tempContext) { - tempContext.scale(devicePixelRatio, devicePixelRatio); - tempContext.drawImage( + if (tempContext) { + tempContext.scale(devicePixelRatio, devicePixelRatio); + tempContext.drawImage( + img, + x, + y, + width, + height, + 0, + 0, + element.width, + element.height, + ); + + const imageData = tempContext.getImageData( + 0, + 0, + tempCanvas.width, + tempCanvas.height, + ); + + const data = imageData.data; + + for (let i = 0; i < data.length; i += 4) { + data[i] = 255 - data[i]; + data[i + 1] = 255 - data[i + 1]; + data[i + 2] = 255 - data[i + 2]; + } + + tempContext.putImageData(imageData, 0, 0); + context.drawImage( + tempCanvas, + 0, + 0, + tempCanvas.width, + tempCanvas.height, + 0, + 0, + element.width, + element.height, + ); + } + } else { + if (shouldInvertImage) { + context.filter = DARK_THEME_FILTER; + } + + context.drawImage( img, x, y, width, height, - 0, - 0, - element.width, - element.height, - ); - - const imageData = tempContext.getImageData( - 0, - 0, - tempCanvas.width, - tempCanvas.height, - ); - - const data = imageData.data; - - for (let i = 0; i < data.length; i += 4) { - data[i] = 255 - data[i]; - data[i + 1] = 255 - data[i + 1]; - data[i + 2] = 255 - data[i + 2]; - } - - tempContext.putImageData(imageData, 0, 0); - context.drawImage( - tempCanvas, - 0, - 0, - tempCanvas.width, - tempCanvas.height, - 0, + 0 /* hardcoded for the selection box*/, 0, element.width, element.height, ); } } else { - if (shouldInvertImage) { - context.filter = DARK_THEME_FILTER; - } - - context.drawImage( - img, - x, - y, - width, - height, - 0 /* hardcoded for the selection box*/, - 0, - element.width, - element.height, + drawImagePlaceholder( + element, + context, + renderConfig.theme, + getRenderEnvironment(renderConfig.renderEnvironment), ); } - } else { - drawImagePlaceholder(element, context, renderConfig.theme); - } - context.restore(); + }); break; } default: { if (isTextElement(element)) { const rtl = isRTL(element.text); - const shouldTemporarilyAttach = rtl && !context.canvas.isConnected; + // NOTE: `isConnected === false` rather than `!isConnected` -- a + // non-DOM canvas (node-canvas et al) has no such property, and must + // not be handed to `document.body` + const shouldTemporarilyAttach = + rtl && + context.canvas.isConnected === false && + typeof document !== "undefined"; if (shouldTemporarilyAttach) { // to correctly render RTL text mixed with LTR, we have to append it // to the DOM document.body.appendChild(context.canvas); } - context.canvas.setAttribute("dir", rtl ? "rtl" : "ltr"); - context.save(); - context.font = getFontString(element); - context.fillStyle = applyDarkModeFilter( - element.strokeColor, - renderConfig.theme === THEME.DARK, - ); - context.textAlign = element.textAlign as CanvasTextAlign; - - // Canvas does not support multiline text by default - const lines = element.text.replace(/\r\n?/g, "\n").split("\n"); - - const horizontalOffset = - element.textAlign === "center" - ? element.width / 2 - : element.textAlign === "right" - ? element.width - : 0; - - const lineHeightPx = getLineHeightInPx( - element.fontSize, - element.lineHeight, - ); - - const verticalOffset = getVerticalOffset( - element.fontFamily, - element.fontSize, - lineHeightPx, - ); - - for (let index = 0; index < lines.length; index++) { - context.fillText( - lines[index], - horizontalOffset, - index * lineHeightPx + verticalOffset, - ); + if (typeof context.canvas.setAttribute === "function") { + context.canvas.setAttribute("dir", rtl ? "rtl" : "ltr"); } - context.restore(); + withSavedContext(context, () => { + context.font = getFontString(element); + context.fillStyle = applyDarkModeFilter( + element.strokeColor, + renderConfig.theme === THEME.DARK, + ); + context.textAlign = element.textAlign as CanvasTextAlign; + + // Canvas does not support multiline text by default + const lines = element.text.replace(/\r\n?/g, "\n").split("\n"); + + const horizontalOffset = + element.textAlign === "center" + ? element.width / 2 + : element.textAlign === "right" + ? element.width + : 0; + + const lineHeightPx = getLineHeightInPx( + element.fontSize, + element.lineHeight, + ); + + const verticalOffset = getVerticalOffset( + element.fontFamily, + element.fontSize, + lineHeightPx, + ); + + for (let index = 0; index < lines.length; index++) { + context.fillText( + lines[index], + horizontalOffset, + index * lineHeightPx + verticalOffset, + ); + } + }); if (shouldTemporarilyAttach) { context.canvas.remove(); } @@ -691,6 +733,9 @@ const generateElementWithCanvas = ( appState: StaticCanvasAppState | InteractiveCanvasAppState, ) => { const zoom: Zoom = renderConfig ? appState.zoom : DEFAULT_ZOOM; + const renderEnvironment = getRenderEnvironment( + renderConfig.renderEnvironment, + ); const prevElementWithCanvas = elementWithCanvasCache.get(element); const shouldRegenerateBecauseZoom = prevElementWithCanvas && @@ -705,6 +750,8 @@ const generateElementWithCanvas = ( !prevElementWithCanvas || shouldRegenerateBecauseZoom || prevElementWithCanvas.theme !== appState.theme || + prevElementWithCanvas.deviceScale !== renderConfig.scale || + prevElementWithCanvas.environment !== renderEnvironment || prevElementWithCanvas.imageCrop !== imageCrop || prevElementWithCanvas.containingFrameOpacity !== containingFrameOpacity ) { @@ -768,9 +815,10 @@ const drawElementFromCanvas = ( positionOffset: RenderPositionOffset, ) => { const element = elementWithCanvas.element; - // the ratio the cached bitmap was generated with (`generateElementCanvas`); - // the blit's size math needs the same one, so it is not the owner window's - const devicePixelRatio = window.devicePixelRatio; + // the backing-store scale the cached bitmap was generated with + // (`generateElementCanvas`); the blit's size math needs the same one, so it + // is the render config's, not the owner window's + const devicePixelRatio = renderConfig.scale; const padding = getCanvasPadding(element); const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, allElementsMap); const cx = @@ -778,157 +826,166 @@ const drawElementFromCanvas = ( const cy = ((y1 + y2) / 2 + positionOffset.y + appState.scrollY) * devicePixelRatio; - context.save(); - context.scale(1 / devicePixelRatio, 1 / devicePixelRatio); + withSavedContext(context, () => { + context.scale(1 / devicePixelRatio, 1 / devicePixelRatio); - const boundTextElement = getBoundTextElement(element, allElementsMap); + const boundTextElement = getBoundTextElement(element, allElementsMap); - if (isArrowElement(element) && boundTextElement) { - // punch the label "hole" by clipping the arrow's blit out of the label - // rect (even-odd) instead of baking a rotated arrow copy with a cleared - // hole into a separate (`maxDim`-squared!) canvas. The hole stays - // axis-aligned in scene space while the blit below rotates. - const [, , , , boundTextCx, boundTextCy] = getElementAbsoluteCoords( - boundTextElement, - allElementsMap, + if (isArrowElement(element) && boundTextElement) { + // punch the label "hole" by clipping the arrow's blit out of the label + // rect (even-odd) instead of baking a rotated arrow copy with a cleared + // hole into a separate (`maxDim`-squared!) canvas. The hole stays + // axis-aligned in scene space while the blit below rotates. + const [, , , , boundTextCx, boundTextCy] = getElementAbsoluteCoords( + boundTextElement, + allElementsMap, + ); + // generously covers the arrow's blit at any rotation + const outerHalf = + Math.max(distance(x1, x2), distance(y1, y2)) * devicePixelRatio + + padding * 10; + context.beginPath(); + context.rect( + cx - outerHalf, + cy - outerHalf, + outerHalf * 2, + outerHalf * 2, + ); + context.rect( + (boundTextCx - + boundTextElement.width / 2 - + BOUND_TEXT_PADDING + + positionOffset.x + + appState.scrollX) * + devicePixelRatio, + (boundTextCy - + boundTextElement.height / 2 - + BOUND_TEXT_PADDING + + positionOffset.y + + appState.scrollY) * + devicePixelRatio, + (boundTextElement.width + BOUND_TEXT_PADDING * 2) * devicePixelRatio, + (boundTextElement.height + BOUND_TEXT_PADDING * 2) * devicePixelRatio, + ); + context.clip("evenodd"); + } + + // we translate context to element center so that rotation and scale + // originates from the element center + context.translate(cx, cy); + + context.rotate(element.angle); + + if ( + "scale" in elementWithCanvas.element && + !isPendingImageElement(element, renderConfig) + ) { + context.scale( + elementWithCanvas.element.scale[0], + elementWithCanvas.element.scale[1], + ); + } + + // revert afterwards we don't have account for it during drawing + context.translate(-cx, -cy); + + // the blit origin, in the space the context is in here (scaled by + // canvas scale × zoom ÷ devicePixelRatio) + let drawX = + (x1 + positionOffset.x + appState.scrollX) * devicePixelRatio - padding; + let drawY = + (y1 + positionOffset.y + appState.scrollY) * devicePixelRatio - padding; + + const transform = context.getTransform(); + + if (canSnapElement(element, appState)) { + // blit the cached bitmap on whole device pixels. Nearest-neighbor at a + // fractional offset is a pixel-exact copy shifted to the nearest pixel + // — except at an exact half pixel, where a GPU-accelerated canvas + // decides the rounding per scanline by float precision and a few rows + // sample the neighboring row: doubled or broken strokes, varying with + // scroll and position. A centered label lands on a half pixel + // routinely. + // + // Done by moving the transform's origin onto the rounded device + // position of the blit and drawing at (0, 0): the rotation, mirroring + // and scale stay in `a`–`d` (a right angle keeps its scale in `b`/`c`), + // and the origin is an exact integer even in the canvas's float32 + // matrix. Rounding `drawX` itself only lands on device pixels at 100% + // zoom. + const { a, b, c, d, e, f } = transform; + const container = isTextElement(element) + ? getContainerElement(element, allElementsMap) + : null; + // A bound label shares its unrotated container's offset and anchor. + // Other bitmaps anchor to themselves. + const anchor = container && !container.angle ? container : element; + const anchorCoords = + anchor === element + ? null + : getElementAbsoluteCoords(anchor, allElementsMap); + const anchorSceneX = anchorCoords?.[0] ?? x1; + const anchorSceneY = anchorCoords?.[1] ?? y1; + const anchorPadding = + anchor === element ? padding : getCanvasPadding(anchor); + const anchorX = + (anchorSceneX + positionOffset.x + appState.scrollX) * + devicePixelRatio - + anchorPadding; + const anchorY = + (anchorSceneY + positionOffset.y + appState.scrollY) * + devicePixelRatio - + anchorPadding; + + // Form the relative vector before introducing the scroll translation. + const dx = + (x1 - anchorSceneX) * devicePixelRatio + anchorPadding - padding; + const dy = + (y1 - anchorSceneY) * devicePixelRatio + anchorPadding - padding; + context.setTransform( + a, + b, + c, + d, + Math.round(a * anchorX + c * anchorY + e) + + Math.round(a * dx + c * dy + SNAP_TIE_BIAS), + Math.round(b * anchorX + d * anchorY + f) + + Math.round(b * dx + d * dy + SNAP_TIE_BIAS), + ); + drawX = 0; + drawY = 0; + } + + context.drawImage( + elementWithCanvas.canvas!, + drawX, + drawY, + elementWithCanvas.canvas!.width / elementWithCanvas.scale, + elementWithCanvas.canvas!.height / elementWithCanvas.scale, ); - // generously covers the arrow's blit at any rotation - const outerHalf = - Math.max(distance(x1, x2), distance(y1, y2)) * devicePixelRatio + - padding * 10; - context.beginPath(); - context.rect(cx - outerHalf, cy - outerHalf, outerHalf * 2, outerHalf * 2); - context.rect( - (boundTextCx - - boundTextElement.width / 2 - - BOUND_TEXT_PADDING + - positionOffset.x + - appState.scrollX) * - devicePixelRatio, - (boundTextCy - - boundTextElement.height / 2 - - BOUND_TEXT_PADDING + - positionOffset.y + - appState.scrollY) * - devicePixelRatio, - (boundTextElement.width + BOUND_TEXT_PADDING * 2) * devicePixelRatio, - (boundTextElement.height + BOUND_TEXT_PADDING * 2) * devicePixelRatio, - ); - context.clip("evenodd"); - } - // we translate context to element center so that rotation and scale - // originates from the element center - context.translate(cx, cy); + context.setTransform(transform); - context.rotate(element.angle); - - if ( - "scale" in elementWithCanvas.element && - !isPendingImageElement(element, renderConfig) - ) { - context.scale( - elementWithCanvas.element.scale[0], - elementWithCanvas.element.scale[1], - ); - } - - // revert afterwards we don't have account for it during drawing - context.translate(-cx, -cy); - - // the blit origin, in the space the context is in here (scaled by - // canvas scale × zoom ÷ devicePixelRatio) - let drawX = - (x1 + positionOffset.x + appState.scrollX) * devicePixelRatio - padding; - let drawY = - (y1 + positionOffset.y + appState.scrollY) * devicePixelRatio - padding; - - const transform = context.getTransform(); - - if (canSnapElement(element, appState)) { - // blit the cached bitmap on whole device pixels. Nearest-neighbor at a - // fractional offset is a pixel-exact copy shifted to the nearest pixel - // — except at an exact half pixel, where a GPU-accelerated canvas - // decides the rounding per scanline by float precision and a few rows - // sample the neighboring row: doubled or broken strokes, varying with - // scroll and position. A centered label lands on a half pixel - // routinely. - // - // Done by moving the transform's origin onto the rounded device - // position of the blit and drawing at (0, 0): the rotation, mirroring - // and scale stay in `a`–`d` (a right angle keeps its scale in `b`/`c`), - // and the origin is an exact integer even in the canvas's float32 - // matrix. Rounding `drawX` itself only lands on device pixels at 100% - // zoom. - const { a, b, c, d, e, f } = transform; - const container = isTextElement(element) - ? getContainerElement(element, allElementsMap) - : null; - // A bound label shares its unrotated container's offset and anchor. - // Other bitmaps anchor to themselves. - const anchor = container && !container.angle ? container : element; - const anchorCoords = - anchor === element - ? null - : getElementAbsoluteCoords(anchor, allElementsMap); - const anchorSceneX = anchorCoords?.[0] ?? x1; - const anchorSceneY = anchorCoords?.[1] ?? y1; - const anchorPadding = - anchor === element ? padding : getCanvasPadding(anchor); - const anchorX = - (anchorSceneX + positionOffset.x + appState.scrollX) * devicePixelRatio - - anchorPadding; - const anchorY = - (anchorSceneY + positionOffset.y + appState.scrollY) * devicePixelRatio - - anchorPadding; - - // Form the relative vector before introducing the scroll translation. - const dx = (x1 - anchorSceneX) * devicePixelRatio + anchorPadding - padding; - const dy = (y1 - anchorSceneY) * devicePixelRatio + anchorPadding - padding; - context.setTransform( - a, - b, - c, - d, - Math.round(a * anchorX + c * anchorY + e) + - Math.round(a * dx + c * dy + SNAP_TIE_BIAS), - Math.round(b * anchorX + d * anchorY + f) + - Math.round(b * dx + d * dy + SNAP_TIE_BIAS), - ); - drawX = 0; - drawY = 0; - } - - context.drawImage( - elementWithCanvas.canvas!, - drawX, - drawY, - elementWithCanvas.canvas!.width / elementWithCanvas.scale, - elementWithCanvas.canvas!.height / elementWithCanvas.scale, - ); - - context.setTransform(transform); - - if ( - import.meta.env.VITE_APP_DEBUG_ENABLE_TEXT_CONTAINER_BOUNDING_BOX === - "true" && - hasBoundTextElement(element) - ) { - const textElement = getBoundTextElement( - element, - allElementsMap, - ) as ExcalidrawTextElementWithContainer; - const coords = getContainerCoords(element); - context.strokeStyle = "#c92a2a"; - context.lineWidth = 3; - context.strokeRect( - (coords.x + positionOffset.x + appState.scrollX) * devicePixelRatio, - (coords.y + positionOffset.y + appState.scrollY) * devicePixelRatio, - getBoundTextMaxWidth(element, textElement) * devicePixelRatio, - getBoundTextMaxHeight(element, textElement) * devicePixelRatio, - ); - } - context.restore(); + if ( + import.meta.env.VITE_APP_DEBUG_ENABLE_TEXT_CONTAINER_BOUNDING_BOX === + "true" && + hasBoundTextElement(element) + ) { + const textElement = getBoundTextElement( + element, + allElementsMap, + ) as ExcalidrawTextElementWithContainer; + const coords = getContainerCoords(element); + context.strokeStyle = "#c92a2a"; + context.lineWidth = 3; + context.strokeRect( + (coords.x + positionOffset.x + appState.scrollX) * devicePixelRatio, + (coords.y + positionOffset.y + appState.scrollY) * devicePixelRatio, + getBoundTextMaxWidth(element, textElement) * devicePixelRatio, + getBoundTextMaxHeight(element, textElement) * devicePixelRatio, + ); + } + }); // Clear the nested element we appended to the DOM }; @@ -1022,43 +1079,42 @@ const drawElement = ( case "magicframe": case "frame": { if (appState.frameRendering.enabled && appState.frameRendering.outline) { - context.save(); - context.translate( - element.x + appState.scrollX, - element.y + appState.scrollY, - ); - context.fillStyle = "rgba(0, 0, 200, 0.04)"; - - context.lineWidth = FRAME_STYLE.strokeWidth / appState.zoom.value; - context.strokeStyle = applyDarkModeFilter( - FRAME_STYLE.strokeColor, - appState.theme === THEME.DARK, - ); - - // TODO change later to only affect AI frames - if (isMagicFrameElement(element)) { - context.strokeStyle = - appState.theme === THEME.LIGHT - ? "#7affd7" - : applyDarkModeFilter("#1d8264"); - } - - if (FRAME_STYLE.radius && context.roundRect) { - context.beginPath(); - context.roundRect( - 0, - 0, - element.width, - element.height, - FRAME_STYLE.radius / appState.zoom.value, + withSavedContext(context, () => { + context.translate( + element.x + appState.scrollX, + element.y + appState.scrollY, ); - context.stroke(); - context.closePath(); - } else { - context.strokeRect(0, 0, element.width, element.height); - } + context.fillStyle = "rgba(0, 0, 200, 0.04)"; - context.restore(); + context.lineWidth = FRAME_STYLE.strokeWidth / appState.zoom.value; + context.strokeStyle = applyDarkModeFilter( + FRAME_STYLE.strokeColor, + appState.theme === THEME.DARK, + ); + + // TODO change later to only affect AI frames + if (isMagicFrameElement(element)) { + context.strokeStyle = + appState.theme === THEME.LIGHT + ? "#7affd7" + : applyDarkModeFilter("#1d8264"); + } + + if (FRAME_STYLE.radius && context.roundRect) { + context.beginPath(); + context.roundRect( + 0, + 0, + element.width, + element.height, + FRAME_STYLE.radius / appState.zoom.value, + ); + context.stroke(); + context.closePath(); + } else { + context.strokeRect(0, 0, element.width, element.height); + } + }); } break; } @@ -1069,12 +1125,12 @@ const drawElement = ( const cy = (y1 + y2) / 2 + appState.scrollY; const shiftX = (x2 - x1) / 2 - (element.x - x1); const shiftY = (y2 - y1) / 2 - (element.y - y1); - context.save(); - context.translate(cx, cy); - context.rotate(element.angle); - context.translate(-shiftX, -shiftY); - drawElementOnCanvas(element, rc, context, renderConfig); - context.restore(); + withSavedContext(context, () => { + context.translate(cx, cy); + context.rotate(element.angle); + context.translate(-shiftX, -shiftY); + drawElementOnCanvas(element, rc, context, renderConfig); + }); } else { const elementWithCanvas = generateElementWithCanvas( element, @@ -1129,65 +1185,69 @@ const drawElement = ( shiftY = (y2 - y1) / 2 - (boundTextCoords.y - y1); } } - context.save(); - context.translate(cx, cy); + withSavedContext(context, () => { + context.translate(cx, cy); - const boundTextElement = getBoundTextElement(element, elementsMap); + const boundTextElement = getBoundTextElement(element, elementsMap); - if (isArrowElement(element) && boundTextElement) { - // Draw arrow directly as vector (no temp-canvas bitmap blit which - // introduces resampling blur). The label "hole" is cut by clipping - // the arrow's own strokes out of the label rect (even-odd clip) - // so that elements rendered beneath the arrow keep showing through - // the gap. - shiftX = element.width / 2 - (element.x - x1); - shiftY = element.height / 2 - (element.y - y1); + if (isArrowElement(element) && boundTextElement) { + // Draw arrow directly as vector (no temp-canvas bitmap blit which + // introduces resampling blur). The label "hole" is cut by clipping + // the arrow's own strokes out of the label rect (even-odd clip) + // so that elements rendered beneath the arrow keep showing through + // the gap. + shiftX = element.width / 2 - (element.x - x1); + shiftY = element.height / 2 - (element.y - y1); - const [, , , , boundTextCx, boundTextCy] = getElementAbsoluteCoords( - boundTextElement, - elementsMap, - ); - const holeX = - boundTextCx - - centerX - - boundTextElement.width / 2 - - BOUND_TEXT_PADDING; - const holeY = - boundTextCy - - centerY - - boundTextElement.height / 2 - - BOUND_TEXT_PADDING; - const holeWidth = boundTextElement.width + BOUND_TEXT_PADDING * 2; - const holeHeight = boundTextElement.height + BOUND_TEXT_PADDING * 2; + const [, , , , boundTextCx, boundTextCy] = getElementAbsoluteCoords( + boundTextElement, + elementsMap, + ); + const holeX = + boundTextCx - + centerX - + boundTextElement.width / 2 - + BOUND_TEXT_PADDING; + const holeY = + boundTextCy - + centerY - + boundTextElement.height / 2 - + BOUND_TEXT_PADDING; + const holeWidth = boundTextElement.width + BOUND_TEXT_PADDING * 2; + const holeHeight = boundTextElement.height + BOUND_TEXT_PADDING * 2; - // generously covers the arrow's painted extent at any rotation - // (the hole rect stays axis-aligned in scene space) - const outerHalf = - Math.max(distance(x1, x2), distance(y1, y2)) + - getCanvasPadding(element) * 10; + // generously covers the arrow's painted extent at any rotation + // (the hole rect stays axis-aligned in scene space) + const outerHalf = + Math.max(distance(x1, x2), distance(y1, y2)) + + getCanvasPadding(element) * 10; - context.save(); - context.beginPath(); - context.rect(-outerHalf, -outerHalf, outerHalf * 2, outerHalf * 2); - context.rect(holeX, holeY, holeWidth, holeHeight); - context.clip("evenodd"); - context.rotate(element.angle); - context.translate(-shiftX, -shiftY); - drawElementOnCanvas(element, rc, context, renderConfig); - context.restore(); - } else { - context.rotate(element.angle); + withSavedContext(context, () => { + context.beginPath(); + context.rect( + -outerHalf, + -outerHalf, + outerHalf * 2, + outerHalf * 2, + ); + context.rect(holeX, holeY, holeWidth, holeHeight); + context.clip("evenodd"); + context.rotate(element.angle); + context.translate(-shiftX, -shiftY); + drawElementOnCanvas(element, rc, context, renderConfig); + }); + } else { + context.rotate(element.angle); - if (element.type === "image") { - // note: scale must be applied *after* rotating - context.scale(element.scale[0], element.scale[1]); + if (element.type === "image") { + // note: scale must be applied *after* rotating + context.scale(element.scale[0], element.scale[1]); + } + + context.translate(-shiftX, -shiftY); + drawElementOnCanvas(element, rc, context, renderConfig); } - - context.translate(-shiftX, -shiftY); - drawElementOnCanvas(element, rc, context, renderConfig); - } - - context.restore(); + }); // not exporting → optimized rendering (cache & render from element // canvases) } else { @@ -1238,44 +1298,44 @@ const drawElement = ( // crop: one bitmap, so cropping moves only the crop's edges over it // (the cropped image's own bitmap snaps to whole pixels by its own // corner, shifting the image by a pixel as that corner moves) - context.save(); - context.globalAlpha = 0.1; - drawElementFromCanvas( - uncroppedElementCanvas, - context, - renderConfig, - appState, - allElementsMap, - // The crop editor's uncropped preview stays at document coordinates. - ZERO_RENDER_OFFSET, - ); - context.restore(); + withSavedContext(context, () => { + context.globalAlpha = 0.1; + drawElementFromCanvas( + uncroppedElementCanvas, + context, + renderConfig, + appState, + allElementsMap, + // The crop editor's uncropped preview stays at document coordinates. + ZERO_RENDER_OFFSET, + ); + }); const [x1, y1, x2, y2] = getElementAbsoluteCoords( elementWithCanvas.element, allElementsMap, ); - context.save(); - // (built in the element's rotated box: save/restore leaves the path) - context.save(); - context.translate( - (x1 + x2) / 2 + renderState.offset.x + appState.scrollX, - (y1 + y2) / 2 + renderState.offset.y + appState.scrollY, - ); - context.rotate(elementWithCanvas.element.angle); - context.beginPath(); - context.rect((x1 - x2) / 2, (y1 - y2) / 2, x2 - x1, y2 - y1); - context.restore(); - context.clip(); - drawElementFromCanvas( - uncroppedElementCanvas, - context, - renderConfig, - appState, - allElementsMap, - renderState.offset, - ); - context.restore(); + withSavedContext(context, () => { + // (built in the element's rotated box: save/restore leaves the path) + context.save(); + context.translate( + (x1 + x2) / 2 + renderState.offset.x + appState.scrollX, + (y1 + y2) / 2 + renderState.offset.y + appState.scrollY, + ); + context.rotate(elementWithCanvas.element.angle); + context.beginPath(); + context.rect((x1 - x2) / 2, (y1 - y2) / 2, x2 - x1, y2 - y1); + context.restore(); + context.clip(); + drawElementFromCanvas( + uncroppedElementCanvas, + context, + renderConfig, + appState, + allElementsMap, + renderState.offset, + ); + }); } else { drawElementFromCanvas( elementWithCanvas, diff --git a/packages/element/src/renderEnvironment.ts b/packages/element/src/renderEnvironment.ts new file mode 100644 index 0000000000..56cb7af50e --- /dev/null +++ b/packages/element/src/renderEnvironment.ts @@ -0,0 +1,120 @@ +/** + * Host environment used by the renderer and export pipeline. + * + * The default is the browser's. An explicit environment (see + * `StaticCanvasRenderConfig["renderEnvironment"]`) scopes canvas and image + * creation to one render target -- e.g. the editor's owner window, so that + * one process can host editors in multiple realms (popouts, multi-tenant + * embeds), or an export running where `document`/`window` don't exist (Node + * with `node-canvas` / `@napi-rs/canvas`, passed in `config.environment`). + * There is deliberately no process-wide override: every render and export + * takes its environment explicitly. + * + * `document` is the DOM side of the same host: what SVG exports are built in + * (a DOM implementation such as `linkedom` under Node). + */ +import type { TextMetricsProvider } from "./textMeasurements"; + +export type RenderEnvironment = { + /** Creates a blank canvas. Callers set `width`/`height` themselves. */ + createCanvas: () => HTMLCanvasElement; + /** + * Creates an image to be decoded from a data URL and later used as a + * `drawImage` source. + * + * NOTE: typed as `HTMLImageElement` because the renderer is typed against + * the DOM lib throughout. Non-DOM implementations (node-canvas et al) need + * to cast; they must support `src`, `onload`, `onerror`, `naturalWidth`, + * `naturalHeight` and be accepted by their canvas' `drawImage`. + */ + createImage: () => HTMLImageElement; + /** + * Creates a fillable path from an SVG path string -- freedraw elements are + * filled through one. + * + * Optional, because browsers always have the global `Path2D` this falls back + * to. A Node host MUST supply it (e.g. `@napi-rs/canvas`'s `Path2D`) or + * install `globalThis.Path2D` itself: Node has no such global, and without + * one every freedraw element fails to render. + */ + createPath?: (svgPath: string) => Path2D; + /** + * The document SVG exports are built in: the editor's owner document, or + * outside the browser a DOM implementation's (e.g. `linkedom`). + * + * Optional; falls back to the global `document`. + */ + document?: Document; + /** + * Measures text (line widths, for wrapping and layout). + * + * Optional; falls back to the global provider set with + * `setCustomTextMetricsProvider`, else to measuring on a canvas from + * `createCanvas`. + */ + textMetrics?: TextMetricsProvider; +}; + +const defaultCreateCanvas = () => { + if (typeof document === "undefined") { + throw new Error( + "Excalidraw: creating a canvas needs a `document`, and this " + + "environment has none. Pass `createCanvas` in the export's " + + "`config.environment` (e.g. backed by `@napi-rs/canvas`), or, if the " + + "canvas was only needed to measure text, set a provider via " + + "`setCustomTextMetricsProvider`.", + ); + } + return document.createElement("canvas"); +}; +const defaultCreateImage = () => { + if (typeof Image === "undefined") { + throw new Error( + "Excalidraw: decoding an image needs a global `Image`, and this " + + "environment has none. Pass `createImage` in the export's " + + "`config.environment` (e.g. `@napi-rs/canvas`'s `Image`).", + ); + } + return new Image(); +}; +const defaultCreatePath = (svgPath: string) => { + if (typeof Path2D === "undefined") { + throw new Error( + "Excalidraw: rendering a freedraw element needs a `Path2D` " + + "implementation, and this environment has no global one. Pass " + + "`createPath` in the render environment (e.g. `@napi-rs/canvas`'s " + + "`Path2D`) or assign `globalThis.Path2D`.", + ); + } + return new Path2D(svgPath); +}; + +/** + * NOTE: deliberately does NOT carry a devicePixelRatio. The backing-store scale + * is per-canvas, not per-process -- it is threaded through + * `StaticCanvasRenderConfig["scale"]`, which already carries the owner window's + * ratio (editor) or exportScale (export). + */ +const defaultEnvironment: RenderEnvironment = { + createCanvas: defaultCreateCanvas, + createImage: defaultCreateImage, + createPath: defaultCreatePath, +}; + +/** + * Resolves the host environment for a render: the explicitly scoped one (e.g. + * the editor's owner window, or an export's), else the browser default. + */ +export const getRenderEnvironment = ( + env?: RenderEnvironment, +): RenderEnvironment => env ?? defaultEnvironment; + +/** + * Resolves the path factory for a render. `createPath` is the one optional + * member of the contract, so this fills in the global-`Path2D` default for + * environments that omit it. + */ +export const getCreatePath = ( + env?: RenderEnvironment, +): ((svgPath: string) => Path2D) => + getRenderEnvironment(env).createPath ?? defaultCreatePath; diff --git a/packages/element/src/resizeElements.ts b/packages/element/src/resizeElements.ts index 5e0e56b2cb..78aad8e163 100644 --- a/packages/element/src/resizeElements.ts +++ b/packages/element/src/resizeElements.ts @@ -49,6 +49,7 @@ import { getApproxMinLineHeight, } from "./textMeasurements"; import { wrapText } from "./textWrapping"; + import { isArrowElement, isBindingElement, @@ -69,6 +70,8 @@ import { updateStickyNoteLayout, } from "./stickyNote"; +import type { RenderEnvironment } from "./renderEnvironment"; + import type { Scene } from "./Scene"; import type { BoundingBox } from "./bounds"; @@ -103,6 +106,7 @@ export const transformElements = ( pointerY: number, centerX: number, centerY: number, + renderEnvironment?: RenderEnvironment, ): boolean => { const elementsMap = scene.getNonDeletedElementsMap(); if (selectedElements.length === 1) { @@ -148,6 +152,7 @@ export const transformElements = ( { shouldMaintainAspectRatio, shouldResizeFromCenter, + renderEnvironment, }, ); } @@ -198,6 +203,7 @@ export const transformElements = ( nextWidth, nextHeight, originalBoundingBox, + renderEnvironment, }, ); @@ -322,6 +328,7 @@ export const resizeSingleTextElement = ( shouldResizeFromCenter: boolean, nextWidth: number, nextHeight: number, + renderEnvironment?: RenderEnvironment, ) => { const elementsMap = scene.getNonDeletedElementsMap(); @@ -373,6 +380,7 @@ export const resizeSingleTextElement = ( fontFamily: element.fontFamily, }), element.lineHeight, + renderEnvironment, ); const newWidth = Math.max(minWidth, nextWidth); @@ -381,11 +389,13 @@ export const resizeSingleTextElement = ( element.originalText, getFontString(element), Math.abs(newWidth), + renderEnvironment, ); const metrics = measureText( text, getFontString(element), element.lineHeight, + renderEnvironment, ); const newHeight = metrics.height; @@ -747,10 +757,12 @@ export const resizeSingleElement = ( shouldInformMutation = true, shouldMaintainAspectRatio = false, shouldResizeFromCenter = false, + renderEnvironment, }: { shouldMaintainAspectRatio?: boolean; shouldResizeFromCenter?: boolean; shouldInformMutation?: boolean; + renderEnvironment?: RenderEnvironment; } = {}, ) => { if (isTextElement(latestElement) && isTextElement(origElement)) { @@ -762,6 +774,7 @@ export const resizeSingleElement = ( shouldResizeFromCenter, nextWidth, nextHeight, + renderEnvironment, ); } @@ -789,6 +802,7 @@ export const resizeSingleElement = ( width: getApproxMinLineWidth( getFontString(boundTextElement), boundTextElement.lineHeight, + renderEnvironment, ), height: getApproxMinLineHeight( boundTextElement.fontSize, @@ -973,6 +987,7 @@ export const resizeSingleElement = ( ), // the arrow pass below is this function's — keep it single bindings: false, + renderEnvironment, }); } else { if (boundTextElement && boundTextFont != null) { @@ -987,6 +1002,7 @@ export const resizeSingleElement = ( shouldMaintainAspectRatio, shouldResizeFromCenter, flipFactorY < 0, + renderEnvironment, ); } @@ -1229,6 +1245,7 @@ export const resizeMultipleElements = ( nextHeight, nextWidth, originalBoundingBox, + renderEnvironment, }: { nextWidth?: number; nextHeight?: number; @@ -1238,6 +1255,7 @@ export const resizeMultipleElements = ( flipByY?: boolean; // added to improve performance originalBoundingBox?: BoundingBox; + renderEnvironment?: RenderEnvironment; } = {}, ) => { // in the case of just flipping, there is no need to specify the next width and height @@ -1557,6 +1575,7 @@ export const resizeMultipleElements = ( }, ), bindings: { simultaneouslyUpdated: elementsToUpdate }, + renderEnvironment, }); } else { updateBoundElements(element, scene, { @@ -1594,6 +1613,7 @@ export const resizeMultipleElements = ( true, shouldResizeFromCenter, flipByY, + renderEnvironment, ); } } diff --git a/packages/element/src/stickyNote.ts b/packages/element/src/stickyNote.ts index 9036136da0..f7ab36c5da 100644 --- a/packages/element/src/stickyNote.ts +++ b/packages/element/src/stickyNote.ts @@ -28,6 +28,7 @@ import { measureText } from "./textMeasurements"; import { wrapText } from "./textWrapping"; import { isStickyNoteElement, isTextElement } from "./typeChecks"; +import type { RenderEnvironment } from "./renderEnvironment"; import type { Scene } from "./Scene"; import type { VerticalResizeAnchor } from "./sizeHelpers"; import type { TransformHandleDirection } from "./transformHandles"; @@ -543,6 +544,12 @@ export type StickyNoteLayoutOpts = { baseFontSize?: number; /** the edge that stays put when the content correction changes the height */ anchor?: StickyNoteLayoutAnchor; + /** + * the host whose text metrics the fit measures with; omitted = the global + * environment. An editor passes its own so a note laid out in one window + * (or headlessly) is measured with that realm's fonts and canvas. + */ + renderEnvironment?: RenderEnvironment; }; export type StickyNoteLayout = { @@ -677,6 +684,7 @@ export const getStickyNoteLayout = ( STICKY_NOTE_MIN_SIZE, ); const anchor = opts.anchor ?? "top"; + const { renderEnvironment } = opts; if (!textElement) { // an empty note sits at its base height @@ -702,8 +710,12 @@ export const getStickyNoteLayout = ( const fit = (fontSize: number): FontFit => { const font = getFontString({ fontFamily, fontSize }); - const text = wrapText(originalText, font, maxWidth); - return { text, fontSize, ...measureText(text, font, lineHeight) }; + const text = wrapText(originalText, font, maxWidth, renderEnvironment); + return { + text, + fontSize, + ...measureText(text, font, lineHeight, renderEnvironment), + }; }; const isBlank = !originalText.trim(); @@ -715,6 +727,7 @@ export const getStickyNoteLayout = ( "", getFontString({ fontFamily, fontSize: baseFontSize }), lineHeight, + renderEnvironment, ), } : fitStickyNoteFont(fit, { @@ -913,7 +926,10 @@ const hasStickyNoteLayoutInputChanged = ( export const relayoutStickyNotes = ( elements: readonly T[], affectedIds: ReadonlySet, - opts?: { prevElementsMap?: ElementsMap }, + opts?: { + prevElementsMap?: ElementsMap; + renderEnvironment?: RenderEnvironment; + }, ): readonly T[] => { if (!affectedIds.size) { return elements; @@ -952,7 +968,9 @@ export const relayoutStickyNotes = ( ) { continue; } - const layout = getStickyNoteLayout(container, textElement); + const layout = getStickyNoteLayout(container, textElement, { + renderEnvironment: opts?.renderEnvironment, + }); const nextContainer = newElementWith(container, layout.container); if (nextContainer !== container) { replacements.set(id, nextContainer as unknown as T); diff --git a/packages/element/src/textElement.ts b/packages/element/src/textElement.ts index 64c05c1313..843b5d0f3a 100644 --- a/packages/element/src/textElement.ts +++ b/packages/element/src/textElement.ts @@ -26,6 +26,7 @@ import { getPositionAfterHeightChange } from "./sizeHelpers"; import { updateStickyNoteLayout } from "./stickyNote"; import { measureText } from "./textMeasurements"; import { wrapText } from "./textWrapping"; + import { isBoundToContainer, isArrowElement, @@ -35,6 +36,8 @@ import { import { isNonDeletedElement } from "."; +import type { RenderEnvironment } from "./renderEnvironment"; + import type { Scene } from "./Scene"; import type { MaybeTransformHandleType } from "./transformHandles"; @@ -52,6 +55,7 @@ export const redrawTextBoundingBox = ( textElement: ExcalidrawTextElement, container: ExcalidrawElement | null, scene: Scene, + renderEnvironment?: RenderEnvironment, ) => { const elementsMap = scene.getNonDeletedElementsMap(); @@ -59,7 +63,10 @@ export const redrawTextBoundingBox = ( // the sticky fit owns both halves (label + note geometry). `textElement` // may be an uncommitted clone (font actions clone before install), so it // is passed explicitly instead of looked up in the scene - updateStickyNoteLayout(container, scene, { text: textElement }); + updateStickyNoteLayout(container, scene, { + text: textElement, + renderEnvironment, + }); return; } @@ -95,6 +102,7 @@ export const redrawTextBoundingBox = ( textElement.originalText, getFontString(textElement), maxWidth, + renderEnvironment, ); } @@ -102,6 +110,7 @@ export const redrawTextBoundingBox = ( boundTextUpdates.text, getFontString(textElement), textElement.lineHeight, + renderEnvironment, ); // Note: only update width for unwrapped text and bound texts (which always have autoResize set to true) @@ -159,11 +168,15 @@ export const handleBindTextResize = ( shouldMaintainAspectRatio = false, shouldResizeFromCenter = false, flipByY = false, + renderEnvironment?: RenderEnvironment, ) => { if (isStickyNoteElement(container)) { // resize callers pass their intents to `updateStickyNoteLayout` directly // and own the bound-arrow pass; this is the fallback for generic callers - updateStickyNoteLayout(container, scene, { bindings: false }); + updateStickyNoteLayout(container, scene, { + bindings: false, + renderEnvironment, + }); return; } const elementsMap = scene.getNonDeletedElementsMap(); @@ -193,12 +206,14 @@ export const handleBindTextResize = ( textElement.originalText, getFontString(textElement), maxWidth, + renderEnvironment, ); } const metrics = measureText( text, getFontString(textElement), textElement.lineHeight, + renderEnvironment, ); nextHeight = metrics.height; nextWidth = metrics.width; diff --git a/packages/element/src/textMeasurements.ts b/packages/element/src/textMeasurements.ts index 5e790261d1..f5081f3eab 100644 --- a/packages/element/src/textMeasurements.ts +++ b/packages/element/src/textMeasurements.ts @@ -7,12 +7,17 @@ import { normalizeEOL, } from "@excalidraw/common"; +import { getRenderEnvironment } from "./renderEnvironment"; + +import type { RenderEnvironment } from "./renderEnvironment"; + import type { FontString, ExcalidrawTextElement } from "./types"; export const measureText = ( text: string, font: FontString, lineHeight: ExcalidrawTextElement["lineHeight"], + env?: RenderEnvironment, ) => { const _text = text .split("\n") @@ -22,7 +27,7 @@ export const measureText = ( .join("\n"); const fontSize = parseFloat(font); const height = getTextHeight(_text, fontSize, lineHeight); - const width = getTextWidth(_text, font); + const width = getTextWidth(_text, font, env); return { width, height }; }; @@ -32,11 +37,13 @@ const DUMMY_TEXT = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toLocaleUpperCase(); export const getApproxMinLineWidth = ( font: FontString, lineHeight: ExcalidrawTextElement["lineHeight"], + env?: RenderEnvironment, ) => { - const maxCharWidth = getMaxCharWidth(font); + const maxCharWidth = getMaxCharWidth(font, env); if (maxCharWidth === 0) { return ( - measureText(DUMMY_TEXT.split("").join("\n"), font, lineHeight).width + + measureText(DUMMY_TEXT.split("").join("\n"), font, lineHeight, env) + .width + BOUND_TEXT_PADDING * 2 ); } @@ -46,8 +53,9 @@ export const getApproxMinLineWidth = ( export const getMinTextElementWidth = ( font: FontString, lineHeight: ExcalidrawTextElement["lineHeight"], + env?: RenderEnvironment, ) => { - return measureText("", font, lineHeight).width + BOUND_TEXT_PADDING * 2; + return measureText("", font, lineHeight, env).width + BOUND_TEXT_PADDING * 2; }; export const isMeasureTextSupported = () => { @@ -103,15 +111,30 @@ export const getApproxMinLineHeight = ( return getLineHeightInPx(fontSize, lineHeight) + BOUND_TEXT_PADDING * 2; }; -let textMetricsProvider: TextMetricsProvider | undefined; +let customTextMetricsProvider: TextMetricsProvider | undefined; +/** + * The lazily-built canvas-backed default provider per environment -- each + * environment (e.g. each editor's owner window) measures on a canvas of its + * own. A custom provider (the environment's `textMetrics`, or the global one + * set with `setCustomTextMetricsProvider`) replaces it. + */ +const defaultTextMetricsProviders = new WeakMap< + RenderEnvironment, + TextMetricsProvider +>(); /** - * Set a custom text metrics provider. + * Set a custom text metrics provider, for all text measured in an environment + * without its own (`RenderEnvironment["textMetrics"]`) -- e.g. elements + * created outside the editor, on a server. Pass `undefined` to go back to + * measuring on a canvas. * * Useful for overriding the width calculation algorithm where canvas API is not available / desired. */ -export const setCustomTextMetricsProvider = (provider: TextMetricsProvider) => { - textMetricsProvider = provider; +export const setCustomTextMetricsProvider = ( + provider: TextMetricsProvider | undefined, +) => { + customTextMetricsProvider = provider; }; export interface TextMetricsProvider { @@ -121,8 +144,8 @@ export interface TextMetricsProvider { class CanvasTextMetricsProvider implements TextMetricsProvider { private canvas: HTMLCanvasElement; - constructor() { - this.canvas = document.createElement("canvas"); + constructor(env: RenderEnvironment) { + this.canvas = getRenderEnvironment(env).createCanvas(); } /** @@ -149,19 +172,40 @@ class CanvasTextMetricsProvider implements TextMetricsProvider { } } -export const getLineWidth = (text: string, font: FontString) => { - if (!textMetricsProvider) { - textMetricsProvider = new CanvasTextMetricsProvider(); +const getDefaultTextMetricsProvider = ( + env: RenderEnvironment | undefined, +): TextMetricsProvider => { + const resolvedEnv = getRenderEnvironment(env); + let provider = defaultTextMetricsProviders.get(resolvedEnv); + if (!provider) { + provider = new CanvasTextMetricsProvider(resolvedEnv); + defaultTextMetricsProviders.set(resolvedEnv, provider); } - - return textMetricsProvider.getLineWidth(text, font); + return provider; }; -export const getTextWidth = (text: string, font: FontString) => { +export const getLineWidth = ( + text: string, + font: FontString, + env?: RenderEnvironment, +) => { + const resolvedEnv = getRenderEnvironment(env); + const provider = + resolvedEnv.textMetrics ?? + customTextMetricsProvider ?? + getDefaultTextMetricsProvider(resolvedEnv); + return provider.getLineWidth(text, font); +}; + +export const getTextWidth = ( + text: string, + font: FontString, + env?: RenderEnvironment, +) => { const lines = splitIntoLines(text); let width = 0; lines.forEach((line) => { - width = Math.max(width, getLineWidth(line, font)); + width = Math.max(width, getLineWidth(line, font, env)); }); return width; @@ -177,27 +221,47 @@ export const getTextHeight = ( }; export const charWidth = (() => { - const cachedCharWidth: { [key: FontString]: Array } = {}; + // one env's metrics at a time: two realms measure the same font string + // differently, so a different env drops the cache rather than mixing them. + // Held weakly -- an editor's env closes over its owner window, and a strong + // ref here would pin a closed popout's realm for the life of the process. + let cachedEnv: WeakRef | undefined; + let cachedCharWidth: { [key: FontString]: Array } = {}; - const calculate = (char: string, font: FontString) => { + const isCachedEnv = (resolvedEnv: RenderEnvironment) => + cachedEnv?.deref() === resolvedEnv; + + const selectEnv = (env: RenderEnvironment | undefined) => { + const resolvedEnv = getRenderEnvironment(env); + if (!isCachedEnv(resolvedEnv)) { + cachedEnv = new WeakRef(resolvedEnv); + cachedCharWidth = {}; + } + }; + + const calculate = ( + char: string, + font: FontString, + env?: RenderEnvironment, + ) => { + selectEnv(env); const unicode = char.charCodeAt(0); if (!cachedCharWidth[font]) { cachedCharWidth[font] = []; } - if (!cachedCharWidth[font][unicode]) { - const width = getLineWidth(char, font); - cachedCharWidth[font][unicode] = width; + if (cachedCharWidth[font][unicode] === undefined) { + cachedCharWidth[font][unicode] = getLineWidth(char, font, env); } - return cachedCharWidth[font][unicode]; }; - const getCache = (font: FontString) => { - return cachedCharWidth[font]; - }; + const getCache = (font: FontString, env?: RenderEnvironment) => + isCachedEnv(getRenderEnvironment(env)) ? cachedCharWidth[font] : undefined; + // dropped rather than emptied, so that the approximation helpers fall back + // to measuring instead of reducing over an empty array const clearCache = (font: FontString) => { - cachedCharWidth[font] = []; + delete cachedCharWidth[font]; }; return { @@ -207,8 +271,8 @@ export const charWidth = (() => { }; })(); -export const getMinCharWidth = (font: FontString) => { - const cache = charWidth.getCache(font); +export const getMinCharWidth = (font: FontString, env?: RenderEnvironment) => { + const cache = charWidth.getCache(font, env); if (!cache) { return 0; } @@ -217,8 +281,8 @@ export const getMinCharWidth = (font: FontString) => { return Math.min(...cacheWithOutEmpty); }; -export const getMaxCharWidth = (font: FontString) => { - const cache = charWidth.getCache(font); +export const getMaxCharWidth = (font: FontString, env?: RenderEnvironment) => { + const cache = charWidth.getCache(font, env); if (!cache) { return 0; } diff --git a/packages/element/src/textWrapping.ts b/packages/element/src/textWrapping.ts index b580f52ed1..7f5064456c 100644 --- a/packages/element/src/textWrapping.ts +++ b/packages/element/src/textWrapping.ts @@ -2,6 +2,7 @@ import { isDevEnv, isTestEnv } from "@excalidraw/common"; import { charWidth, getLineWidth } from "./textMeasurements"; +import type { RenderEnvironment } from "./renderEnvironment"; import type { FontString } from "./types"; /** @@ -106,7 +107,7 @@ const CJK = { * ↑ BREAK AFTER "」" */ // eslint-disable-next-line prettier/prettier - OPENING:/([{〈《⦅「「『【〖〔〘〚<〝/u, + OPENING: /([{〈《⦅「「『【〖〔〘〚<〝/u, CLOSING: /)]}〉》⦆」」』】〗〕〙〛>。.,、〟‥?!:;・〜〞/u, /** * Currency symbols break before, not after @@ -398,8 +399,9 @@ export const wrapText = ( text: string, font: FontString, maxWidth: number, + env?: RenderEnvironment, ): string => { - return getWrappedTextLines(text, font, maxWidth) + return getWrappedTextLines(text, font, maxWidth, env) .map((line) => line.text) .join("\n"); }; @@ -447,6 +449,7 @@ export const getWrappedTextLines = ( text: string, font: FontString, maxWidth: number, + env?: RenderEnvironment, ): WrappedTextLine[] => { // if maxWidth is not finite or NaN which can happen in case of bugs in // computation, we need to make sure we don't continue as we'll end up @@ -459,7 +462,7 @@ export const getWrappedTextLines = ( let offset = 0; for (const originalLine of text.split("\n")) { - const originalLineWidth = getLineWidth(originalLine, font); + const originalLineWidth = getLineWidth(originalLine, font, env); if (originalLineWidth <= maxWidth) { lines.push({ @@ -468,7 +471,7 @@ export const getWrappedTextLines = ( end: offset + originalLine.length, }); } else { - lines.push(...wrapLine(originalLine, font, maxWidth, offset)); + lines.push(...wrapLine(originalLine, font, maxWidth, offset, env)); } offset += originalLine.length + 1; @@ -488,6 +491,7 @@ const wrapLine = ( font: FontString, maxWidth: number, lineStart: number, + env?: RenderEnvironment, ): WrappedTextLine[] => { const lines: WrappedTextLine[] = []; const tokens = parseTokens(line); @@ -508,8 +512,8 @@ const wrapLine = ( // cache single codepoint whitespace, CJK or emoji width calc. as kerning should not apply here const testLineWidth = isSingleCharacter(token) - ? currentLineWidth + charWidth.calculate(token, font) - : getLineWidth(testLine, font); + ? currentLineWidth + charWidth.calculate(token, font, env) + : getLineWidth(testLine, font, env); // build up the current line, skipping length check for possibly trailing whitespaces if (/\s/.test(token) || testLineWidth <= maxWidth) { @@ -526,7 +530,7 @@ const wrapLine = ( // current line is empty => just the token (word) is longer than `maxWidth` and needs to be wrapped if (!currentLine) { - const wrappedWord = wrapWord(token, font, maxWidth, tokenStart); + const wrappedWord = wrapWord(token, font, maxWidth, tokenStart, env); const trailingLine = wrappedWord[wrappedWord.length - 1] ?? { text: "", start: tokenStart, @@ -540,7 +544,7 @@ const wrapLine = ( currentLine = trailingLine.text; currentLineStart = trailingLine.start; currentLineEnd = trailingLine.end; - currentLineWidth = getLineWidth(trailingLine.text, font); + currentLineWidth = getLineWidth(trailingLine.text, font, env); tokenOffset = tokenEnd; tokenIndex++; } else { @@ -565,6 +569,7 @@ const wrapLine = ( currentLineEnd, font, maxWidth, + env, ); lines.push(trailingLine); } @@ -580,6 +585,7 @@ const wrapWord = ( font: FontString, maxWidth: number, wordStart: number, + env?: RenderEnvironment, ): WrappedTextLine[] => { // multi-codepoint emojis are already broken apart and shouldn't be broken further if (getEmojiRegex().test(word)) { @@ -606,7 +612,7 @@ const wrapWord = ( for (const char of chars) { const charStart = offset; const charEnd = charStart + char.length; - const _charWidth = charWidth.calculate(char, font); + const _charWidth = charWidth.calculate(char, font, env); const testLineWidth = currentLineWidth + _charWidth; if (testLineWidth <= maxWidth) { @@ -658,8 +664,9 @@ const trimLine = ( end: number, font: FontString, maxWidth: number, + env?: RenderEnvironment, ): WrappedTextLine => { - const shouldTrimWhitespaces = getLineWidth(line, font) > maxWidth; + const shouldTrimWhitespaces = getLineWidth(line, font, env) > maxWidth; if (!shouldTrimWhitespaces) { return { @@ -676,10 +683,10 @@ const trimLine = ( "", ]; - let trimmedLineWidth = getLineWidth(trimmedLine, font); + let trimmedLineWidth = getLineWidth(trimmedLine, font, env); for (const whitespace of Array.from(whitespaces)) { - const _charWidth = charWidth.calculate(whitespace, font); + const _charWidth = charWidth.calculate(whitespace, font, env); const testLineWidth = trimmedLineWidth + _charWidth; if (testLineWidth > maxWidth) { diff --git a/packages/element/src/transform.ts b/packages/element/src/transform.ts index 2e5f401963..d14ba3214b 100644 --- a/packages/element/src/transform.ts +++ b/packages/element/src/transform.ts @@ -56,6 +56,8 @@ import { getCommonBounds } from "./bounds"; import { Scene } from "./Scene"; +import type { RenderEnvironment } from "./renderEnvironment"; + import type { ExcalidrawArrowElement, ExcalidrawBindableElement, @@ -260,6 +262,7 @@ const bindTextToContainer = ( container: ExcalidrawElement, textProps: { text: string } & FragmentConstructorOpts, scene: Scene, + renderEnvironment?: RenderEnvironment, ) => { // a note and its label share one ink: a label that sets its own color // gives it to the note (the footer paints with it); transparent falls @@ -282,6 +285,7 @@ const bindTextToContainer = ( labelPosition: isArrowElement(container) ? DEFAULT_BOUND_TEXT_LABEL_POSITION : null, + renderEnvironment, }); Object.assign(container, { @@ -294,7 +298,7 @@ const bindTextToContainer = ( : null), }); - redrawTextBoundingBox(textElement, container, scene); + redrawTextBoundingBox(textElement, container, scene, renderEnvironment); return [container, textElement] as const; }; @@ -306,6 +310,7 @@ const bindLinearElementToElement = ( elementStore: ElementStore, scene: Scene, zoom: Zoom, + renderEnvironment?: RenderEnvironment, ): { linearElement: ExcalidrawLinearElement; startBoundElement?: ExcalidrawElement; @@ -355,6 +360,7 @@ const bindLinearElementToElement = ( ...existingElement, ...start, text, + renderEnvironment, }); // to position the text correctly when coordinates not provided Object.assign(startBoundElement, { @@ -433,6 +439,7 @@ const bindLinearElementToElement = ( ...existingElement, ...end, text, + renderEnvironment, }); // to position the text correctly when coordinates not provided Object.assign(endBoundElement, { @@ -571,7 +578,7 @@ class ElementStore { export const convertToExcalidrawElements = ( elementsSkeleton: ExcalidrawElementSkeleton[] | null, - opts?: { regenerateIds: boolean }, + opts?: { regenerateIds?: boolean; renderEnvironment?: RenderEnvironment }, ) => { if (!elementsSkeleton) { return []; @@ -652,6 +659,7 @@ export const convertToExcalidrawElements = ( normalizedText, getFontString({ fontFamily, fontSize }), lineHeight, + opts?.renderEnvironment, ); excalidrawElement = newTextElement({ @@ -660,6 +668,7 @@ export const convertToExcalidrawElements = ( fontFamily, fontSize, ...element, + renderEnvironment: opts?.renderEnvironment, }); break; } @@ -759,6 +768,7 @@ export const convertToExcalidrawElements = ( excalidrawElement, element?.label, scene, + opts?.renderEnvironment, ); elementStore.add(container); elementStore.add(text); @@ -788,6 +798,7 @@ export const convertToExcalidrawElements = ( elementStore, scene, DEFAULT_ZOOM, + opts?.renderEnvironment, ); container = linearElement; elementStore.add(linearElement); @@ -814,6 +825,7 @@ export const convertToExcalidrawElements = ( elementStore, scene, DEFAULT_ZOOM, + opts?.renderEnvironment, ); elementStore.add(linearElement); diff --git a/packages/element/tests/linearElementEditor.test.tsx b/packages/element/tests/linearElementEditor.test.tsx index b0a23cf55b..acbf649d42 100644 --- a/packages/element/tests/linearElementEditor.test.tsx +++ b/packages/element/tests/linearElementEditor.test.tsx @@ -1595,6 +1595,7 @@ describe("Test Linear Elements", () => { false, false, false, + h.app.renderEnvironment, ); expect( wrapText( diff --git a/packages/element/tests/renderEnvironmentCaches.test.ts b/packages/element/tests/renderEnvironmentCaches.test.ts new file mode 100644 index 0000000000..37f2416f16 --- /dev/null +++ b/packages/element/tests/renderEnvironmentCaches.test.ts @@ -0,0 +1,83 @@ +import { describe, it, expect } from "vitest"; + +import { + charWidth, + getMaxCharWidth, + getTextWidth, +} from "../src/textMeasurements"; + +import type { RenderEnvironment } from "../src/renderEnvironment"; +import type { FontString } from "../src/types"; + +const FONT = "20px Excalifont" as FontString; + +// in the test env the canvas provider multiplies the mocked advance width +// (character count) by 10, so an env measuring different widths is observable +const makeEnv = (perChar: number): RenderEnvironment => ({ + createCanvas: () => + ({ + getContext: () => ({ + font: "", + measureText: (text: string) => ({ width: text.length * perChar }), + }), + } as unknown as HTMLCanvasElement), + createImage: () => ({} as HTMLImageElement), +}); + +/** + * Everything the renderer memoizes per host realm (text metrics providers, + * element bitmaps, link icons) is keyed on the render environment's identity, + * so a swap must invalidate rather than serve one realm's pixels to another. + */ +describe("render environment cache invalidation", () => { + it("builds a text metrics provider per environment", () => { + const makeCountingEnv = () => { + const canvases: HTMLCanvasElement[] = []; + const env: RenderEnvironment = { + createCanvas: () => { + const canvas = document.createElement("canvas"); + canvases.push(canvas); + return canvas; + }, + createImage: () => ({} as HTMLImageElement), + }; + return { env, canvases }; + }; + const first = makeCountingEnv(); + const second = makeCountingEnv(); + + // builds + caches the provider's canvas + getTextWidth("measure me", FONT, first.env); + getTextWidth("measure me twice", FONT, first.env); + expect(first.canvases).toHaveLength(1); + + getTextWidth("measure me again", FONT, second.env); + + // measured on a canvas of its own rather than the first env's + expect(second.canvases).toHaveLength(1); + expect(first.canvases).toHaveLength(1); + }); + + it("never serves one environment's char widths to another", () => { + const env = makeEnv(3); + + expect(charWidth.calculate("A", FONT, env)).toBe(30); + // switching env drops the cache rather than reusing the other env's width + expect(charWidth.calculate("A", FONT)).toBe(10); + expect(charWidth.getCache(FONT, env)).toBeUndefined(); + // cached while the env stays put + expect(charWidth.calculate("A", FONT, env)).toBe(30); + expect(charWidth.getCache(FONT, env)?.["A".charCodeAt(0)]).toBe(30); + + // clearing a font drops the entry rather than emptying it, so the + // approximation helpers fall back to measuring + charWidth.clearCache(FONT); + expect(charWidth.getCache(FONT, env)).toBeUndefined(); + expect(getMaxCharWidth(FONT, env)).toBe(0); + + charWidth.calculate("A", FONT, env); + expect(getMaxCharWidth(FONT, env)).toBe(30); + // ...and asking about a different env reports "unmeasured", not 30 + expect(getMaxCharWidth(FONT)).toBe(0); + }); +}); diff --git a/packages/excalidraw/CHANGELOG.md b/packages/excalidraw/CHANGELOG.md index ab3b75908a..690dfbc91f 100644 --- a/packages/excalidraw/CHANGELOG.md +++ b/packages/excalidraw/CHANGELOG.md @@ -36,7 +36,7 @@ Breaking. `exportToCanvas`, `exportToBlob` and `exportToSvg` (in both `@excalidr | `maxWidthOrHeight`, `exportingFrame`, `renderEmbeddables`, `skipInliningFonts`, `reuseImages` | same names, in `config` | - New sizing options: `width`/`height` (only one given: the other follows the content's aspect ratio), `widthOrHeight` (scales the longest side to exactly that size), `fit: "contain" | "none"` and `position: "center" | "topLeft"` within a `width`/`height` frame, and `x`/`y`/`origin` to export a region (cropped, with padding on all sides). The size options are mutually exclusive in the types, and `fit`, `position` and `x`/`y` only combine with `width`/`height`. -- `config.environment` for non-browser hosts: `createCanvas` (e.g. `node-canvas`) and `loadFonts`. +- `config.environment` for non-browser hosts and other windows, the same for every format: `createCanvas` (e.g. `node-canvas`), `createImage`, `createPath`, `document` (the one SVG is built in, e.g. `linkedom`), `textMetrics` and `loadFonts`. - Canvas dimensions are rounded instead of truncated, so float noise no longer costs a pixel (a 20.5px frame-label band now rounds up), and are at least 1px (flat content with no padding used to produce an empty canvas that couldn't be encoded). - In dark exports, frame names now use the editor's dark frame-name color (they were dark-filtered twice and came out lighter). diff --git a/packages/excalidraw/actions/actionBoundText.tsx b/packages/excalidraw/actions/actionBoundText.tsx index 85f4757738..49737f6e80 100644 --- a/packages/excalidraw/actions/actionBoundText.tsx +++ b/packages/excalidraw/actions/actionBoundText.tsx @@ -117,7 +117,9 @@ export const actionUnbindText = register({ unbindTextFromContainer(element, boundTextElement, app.scene); if (isStickyNoteElement(element)) { // an empty note sits at its base height; bound arrows follow - updateStickyNoteLayout(element, app.scene); + updateStickyNoteLayout(element, app.scene, { + renderEnvironment: app.renderEnvironment, + }); } else { app.scene.mutateElement(element, { height: originalContainerHeight @@ -245,7 +247,12 @@ export const bindTextToContainer = ( ...(stickyInk ? { strokeColor: stickyInk } : null), }); const originalContainerHeight = container.height; - redrawTextBoundingBox(textElement, container, app.scene); + redrawTextBoundingBox( + textElement, + container, + app.scene, + app.renderEnvironment, + ); // overwritting the cache with original container height so // it can be restored when unbind updateOriginalContainerCache(container.id, originalContainerHeight); @@ -415,7 +422,12 @@ export const wrapTextsInContainers = ( autoResize: true, }); - redrawTextBoundingBox(textElement, container, app.scene); + redrawTextBoundingBox( + textElement, + container, + app.scene, + app.renderEnvironment, + ); updatedElements = pushContainerBelowText( [...updatedElements, container], diff --git a/packages/excalidraw/actions/actionClipboard.tsx b/packages/excalidraw/actions/actionClipboard.tsx index a1ed98d2b6..3c0a145c47 100644 --- a/packages/excalidraw/actions/actionClipboard.tsx +++ b/packages/excalidraw/actions/actionClipboard.tsx @@ -154,6 +154,7 @@ export const actionCopyAsSvg = register({ { exportingFrame, name: app.getName(), + renderEnvironment: app.renderEnvironment, }, ); @@ -220,6 +221,7 @@ export const actionCopyAsPng = register({ await exportCanvas("clipboard", exportedElements, appState, app.files, { exportingFrame, name: app.getName(), + renderEnvironment: app.renderEnvironment, }); return { appState: { diff --git a/packages/excalidraw/actions/actionFlip.ts b/packages/excalidraw/actions/actionFlip.ts index b7e15275d8..02690e7888 100644 --- a/packages/excalidraw/actions/actionFlip.ts +++ b/packages/excalidraw/actions/actionFlip.ts @@ -146,6 +146,7 @@ const flipElements = ( flipByY: flipDirection === "vertical", shouldResizeFromCenter: true, shouldMaintainAspectRatio: true, + renderEnvironment: app.renderEnvironment, }, ); diff --git a/packages/excalidraw/actions/actionProperties.tsx b/packages/excalidraw/actions/actionProperties.tsx index 86d69482f5..17297434fb 100644 --- a/packages/excalidraw/actions/actionProperties.tsx +++ b/packages/excalidraw/actions/actionProperties.tsx @@ -314,7 +314,12 @@ const changeFontSize = ( oldElement, getBaseFontSizeUpdate(oldElement, newFontSize, elementsMap), ); - redrawTextBoundingBox(newElement, container, app.scene); + redrawTextBoundingBox( + newElement, + container, + app.scene, + app.renderEnvironment, + ); newElement = offsetElementAfterFontResize( oldElement, @@ -1328,7 +1333,12 @@ export const actionChangeFontFamily = register<{ // we either skip the check (have at least one font face loaded) or do the check and find out all the font faces have loaded for (const [element, container] of elementContainerMapping) { // trigger synchronous redraw - redrawTextBoundingBox(element, container, app.scene); + redrawTextBoundingBox( + element, + container, + app.scene, + app.renderEnvironment, + ); } } else { // otherwise try to load all font faces for the given chars and redraw elements once our font faces loaded @@ -1346,6 +1356,7 @@ export const actionChangeFontFamily = register<{ latestElement as ExcalidrawTextElement, latestContainer, app.scene, + app.renderEnvironment, ); } } @@ -1563,6 +1574,7 @@ export const actionChangeTextAlign = register({ newElement, app.scene.getContainerElement(oldElement), app.scene, + app.renderEnvironment, ); return newElement; } @@ -1665,6 +1677,7 @@ export const actionChangeVerticalAlign = register({ newElement, app.scene.getContainerElement(oldElement), app.scene, + app.renderEnvironment, ); return newElement; } diff --git a/packages/excalidraw/actions/actionStyles.ts b/packages/excalidraw/actions/actionStyles.ts index bb1b96906a..2ec81bcbf3 100644 --- a/packages/excalidraw/actions/actionStyles.ts +++ b/packages/excalidraw/actions/actionStyles.ts @@ -171,7 +171,12 @@ export const actionPasteStyles = register({ } else { // sticky labels are laid out together with their (possibly // also restyled) note in the post-pass below - redrawTextBoundingBox(newTextElement, container, app.scene); + redrawTextBoundingBox( + newTextElement, + container, + app.scene, + app.renderEnvironment, + ); } } @@ -203,7 +208,10 @@ export const actionPasteStyles = register({ elementsMap, ), new Set(selectedElementIds), - { prevElementsMap: elementsMap }, + { + prevElementsMap: elementsMap, + renderEnvironment: app.renderEnvironment, + }, ); // a restyled note may have grown or shrunk — arrows bound to it follow diff --git a/packages/excalidraw/animatedTrail.ts b/packages/excalidraw/animatedTrail.ts index e98e6e50ee..dca3c529eb 100644 --- a/packages/excalidraw/animatedTrail.ts +++ b/packages/excalidraw/animatedTrail.ts @@ -93,16 +93,20 @@ export class AnimatedTrail implements Trail { } if (!AnimationController.running(this.key)) { - AnimationController.start(this.key, () => { - const needsNext = this.onFrame(); - if (needsNext) { - return { keep: true }; - } + AnimationController.start( + this.key, + () => { + const needsNext = this.onFrame(); + if (needsNext) { + return { keep: true }; + } - this.cleanup(); + this.cleanup(); - return null; - }); + return null; + }, + this.app.ownerWindow, + ); } } diff --git a/packages/excalidraw/appState.ts b/packages/excalidraw/appState.ts index e4017c73eb..2bd1db37e6 100644 --- a/packages/excalidraw/appState.ts +++ b/packages/excalidraw/appState.ts @@ -17,9 +17,15 @@ import { import type { AppState, InputDevice, NormalizedZoomValue } from "./types"; -const defaultExportScale = EXPORT_SCALES.includes(devicePixelRatio) - ? devicePixelRatio - : 1; +/** + * Resolved lazily rather than at module scope: this module is pulled in by the + * export pipeline, which must be importable outside a browser realm. + */ +const getDefaultExportScale = () => { + const devicePixelRatio = + typeof window === "undefined" ? 1 : window.devicePixelRatio || 1; + return EXPORT_SCALES.includes(devicePixelRatio) ? devicePixelRatio : 1; +}; export const getDefaultAppState = (): Omit< AppState, @@ -67,7 +73,7 @@ export const getDefaultAppState = (): Omit< penDetected: false, errorMessage: null, exportBackground: true, - exportScale: defaultExportScale, + exportScale: getDefaultExportScale(), exportEmbedScene: false, exportWithDarkMode: false, fileHandle: null, diff --git a/packages/excalidraw/charts/charts.bar.ts b/packages/excalidraw/charts/charts.bar.ts index b1a7759606..09cc7f7cb5 100644 --- a/packages/excalidraw/charts/charts.bar.ts +++ b/packages/excalidraw/charts/charts.bar.ts @@ -2,6 +2,8 @@ import { isDevEnv } from "@excalidraw/common"; import { newElement } from "@excalidraw/element"; +import type { RenderEnvironment } from "@excalidraw/element"; + import { commonProps } from "./charts.constants"; import { chartBaseElements, @@ -22,6 +24,7 @@ export const renderBarChart = ( x: number, y: number, colorSeed?: number, + renderEnvironment?: RenderEnvironment, ): ChartElements => { const series = spreadsheet.series; const layout = getCartesianChartLayout("bar", series.length); @@ -83,8 +86,16 @@ export const renderBarChart = ( layout, max, isDevEnv(), + renderEnvironment, + ); + const xLabels = chartXLabels( + spreadsheet, + x, + y, + backgroundColor, + layout, + renderEnvironment, ); - const xLabels = chartXLabels(spreadsheet, x, y, backgroundColor, layout); const xLabelsBottomY = Math.max( y + layout.gap / 2, ...xLabels.map((label) => getRotatedTextElementBottom(label)), @@ -97,6 +108,7 @@ export const renderBarChart = ( xLabelsBottomY, y + layout.gap * 5, backgroundColor, + renderEnvironment, ); return [...baseElements, ...bars, ...seriesLegend]; diff --git a/packages/excalidraw/charts/charts.helpers.ts b/packages/excalidraw/charts/charts.helpers.ts index 18097b1df9..e7b7867a16 100644 --- a/packages/excalidraw/charts/charts.helpers.ts +++ b/packages/excalidraw/charts/charts.helpers.ts @@ -22,6 +22,8 @@ import { wrapText, } from "@excalidraw/element"; +import type { RenderEnvironment } from "@excalidraw/element"; + import type { ChartType, ExcalidrawTextElement, @@ -268,9 +270,10 @@ export const getRadarDisplayText = ( text: string, fontString: ReturnType, maxWidth: number, + renderEnvironment?: RenderEnvironment, ) => { return shouldWrapRadarText(text) - ? wrapText(text, fontString, maxWidth) + ? wrapText(text, fontString, maxWidth, renderEnvironment) : text; }; @@ -281,6 +284,7 @@ export const createRadarAxisLabels = ( centerY: number, radius: number, backgroundColor: string, + renderEnvironment?: RenderEnvironment, ): { axisLabels: ChartElements; axisLabelTopY: number; @@ -294,7 +298,11 @@ export const createRadarAxisLabels = ( RADAR_AXIS_LABEL_MAX_WIDTH, radius * (labels.length > 8 ? 0.56 : 0.72), ); - const minLabelWidth = getApproxMinLineWidth(fontString, lineHeight); + const minLabelWidth = getApproxMinLineWidth( + fontString, + lineHeight, + renderEnvironment, + ); const axisLabels = labels.map((label, index) => { const angle = angles[index]; @@ -304,15 +312,28 @@ export const createRadarAxisLabels = ( .trim() .split(/\s+/) .filter(Boolean) - .map((word) => measureText(word, fontString, lineHeight).width), + .map( + (word) => + measureText(word, fontString, lineHeight, renderEnvironment).width, + ), ); const maxLabelWidth = Math.max( minLabelWidth, baseLabelWidth, longestWordWidth, ); - const displayLabel = getRadarDisplayText(label, fontString, maxLabelWidth); - const metrics = measureText(displayLabel, fontString, lineHeight); + const displayLabel = getRadarDisplayText( + label, + fontString, + maxLabelWidth, + renderEnvironment, + ); + const metrics = measureText( + displayLabel, + fontString, + lineHeight, + renderEnvironment, + ); const cos = Math.cos(angle); const sin = Math.sin(angle); @@ -353,6 +374,7 @@ export const createRadarAxisLabels = ( lineHeight, textAlign, verticalAlign: "middle", + renderEnvironment, }); }); @@ -370,6 +392,7 @@ export const createSeriesLegend = ( minLegendTopY: number, fallbackLegendY: number, backgroundColor: string, + renderEnvironment?: RenderEnvironment, ): ChartElements => { if (series.length <= 1) { return []; @@ -381,8 +404,18 @@ export const createSeriesLegend = ( const fontString = getFontString({ fontFamily, fontSize }); const legendItems = series.map((seriesItem, index) => { const label = seriesItem.title?.trim() || `Series ${index + 1}`; - const displayLabel = getRadarDisplayText(label, fontString, BAR_HEIGHT); - const metrics = measureText(displayLabel, fontString, lineHeight); + const displayLabel = getRadarDisplayText( + label, + fontString, + BAR_HEIGHT, + renderEnvironment, + ); + const metrics = measureText( + displayLabel, + fontString, + lineHeight, + renderEnvironment, + ); const itemWidth = RADAR_LEGEND_SWATCH_SIZE + RADAR_LEGEND_TEXT_GAP + metrics.width; return { @@ -463,6 +496,7 @@ export const createSeriesLegend = ( lineHeight, textAlign: "left", verticalAlign: "middle", + renderEnvironment, }), ); @@ -477,15 +511,22 @@ const ellipsifyTextToWidth = ( maxWidth: number, fontString: ReturnType, lineHeight: ExcalidrawTextElement["lineHeight"], + renderEnvironment?: RenderEnvironment, ) => { - if (measureText(text, fontString, lineHeight).width <= maxWidth) { + if ( + measureText(text, fontString, lineHeight, renderEnvironment).width <= + maxWidth + ) { return text; } let end = text.length; while (end > 1) { const candidate = `${text.slice(0, end)}...`; - if (measureText(candidate, fontString, lineHeight).width <= maxWidth) { + if ( + measureText(candidate, fontString, lineHeight, renderEnvironment).width <= + maxWidth + ) { return candidate; } end--; @@ -499,27 +540,44 @@ const wrapOrEllipsifyTextToWidth = ( maxWidth: number, fontString: ReturnType, lineHeight: ExcalidrawTextElement["lineHeight"], + renderEnvironment?: RenderEnvironment, ) => { - if (measureText(text, fontString, lineHeight).width <= maxWidth) { + if ( + measureText(text, fontString, lineHeight, renderEnvironment).width <= + maxWidth + ) { return { wrapped: false, text }; } const words = text.trim().split(/\s+/).filter(Boolean); if (words.length > 1) { const hasLongWord = words.some((word) => { - return measureText(word, fontString, lineHeight).width > maxWidth; + return ( + measureText(word, fontString, lineHeight, renderEnvironment).width > + maxWidth + ); }); if ( !hasLongWord && - maxWidth >= getApproxMinLineWidth(fontString, lineHeight) + maxWidth >= + getApproxMinLineWidth(fontString, lineHeight, renderEnvironment) ) { - return { wrapped: true, text: wrapText(text, fontString, maxWidth) }; + return { + wrapped: true, + text: wrapText(text, fontString, maxWidth, renderEnvironment), + }; } } return { wrapped: false, - text: ellipsifyTextToWidth(text, maxWidth, fontString, lineHeight), + text: ellipsifyTextToWidth( + text, + maxWidth, + fontString, + lineHeight, + renderEnvironment, + ), }; }; @@ -553,10 +611,11 @@ const getCartesianAxisLabelSpec = ( maxRotatedWidth: number, fontString: ReturnType, lineHeight: ExcalidrawTextElement["lineHeight"], + renderEnvironment?: RenderEnvironment, ): CartesianAxisLabelSpec => { const minWidth = Math.max( CARTESIAN_LABEL_MIN_WIDTH, - Math.ceil(getApproxMinLineWidth(fontString, lineHeight)), + Math.ceil(getApproxMinLineWidth(fontString, lineHeight, renderEnvironment)), ); const maxWidth = Math.max(minWidth, Math.floor(maxLabelWidth)); const candidateWidths: number[] = []; @@ -614,8 +673,14 @@ const getCartesianAxisLabelSpec = ( width, fontString, lineHeight, + renderEnvironment, + ); + const metrics = measureText( + text, + fontString, + lineHeight, + renderEnvironment, ); - const metrics = measureText(text, fontString, lineHeight); const rotated = getRotatedBoundingBox( metrics.width, metrics.height, @@ -691,6 +756,7 @@ export const chartXLabels = ( y: number, backgroundColor: string, layout: CartesianChartLayout, + renderEnvironment?: RenderEnvironment, ): ChartElements => { const fontFamily = commonProps.fontFamily; const fontSize = FONT_SIZES.sm; @@ -713,6 +779,7 @@ export const chartXLabels = ( maxRotatedWidth, fontString, lineHeight, + renderEnvironment, ); const centerX = x + @@ -737,6 +804,7 @@ export const chartXLabels = ( lineHeight, textAlign: "center", verticalAlign: "top", + renderEnvironment, }); }) || [] ); @@ -749,6 +817,7 @@ const chartYLabels = ( backgroundColor: string, layout: CartesianChartLayout, maxValue = Math.max(...spreadsheet.series[0].values), + renderEnvironment?: RenderEnvironment, ): ChartElements => { const minYLabel = newTextElement({ backgroundColor, @@ -757,6 +826,7 @@ const chartYLabels = ( y: y - layout.gap, text: "0", textAlign: "right", + renderEnvironment, }); const maxYLabel = newTextElement({ @@ -766,6 +836,7 @@ const chartYLabels = ( y: y - layout.chartHeight - minYLabel.height / 2, text: maxValue.toLocaleString(), textAlign: "right", + renderEnvironment, }); return [minYLabel, maxYLabel]; @@ -823,6 +894,7 @@ export const chartBaseElements = ( layout: CartesianChartLayout, maxValue = Math.max(...spreadsheet.series[0].values), debug?: boolean, + renderEnvironment?: RenderEnvironment, ): ChartElements => { const { chartWidth, chartHeight } = getChartDimensions(spreadsheet, layout); @@ -837,6 +909,7 @@ export const chartBaseElements = ( textAlign: "center", fontSize: FONT_SIZES.xl, fontFamily: FONT_FAMILY["Lilita One"], + renderEnvironment, }) : null; @@ -858,8 +931,23 @@ export const chartBaseElements = ( return [ ...(debugRect ? [debugRect] : []), ...(title ? [title] : []), - ...chartXLabels(spreadsheet, x, y, backgroundColor, layout), - ...chartYLabels(spreadsheet, x, y, backgroundColor, layout, maxValue), + ...chartXLabels( + spreadsheet, + x, + y, + backgroundColor, + layout, + renderEnvironment, + ), + ...chartYLabels( + spreadsheet, + x, + y, + backgroundColor, + layout, + maxValue, + renderEnvironment, + ), ...chartLines(spreadsheet, x, y, backgroundColor, layout), ]; }; diff --git a/packages/excalidraw/charts/charts.line.ts b/packages/excalidraw/charts/charts.line.ts index b08774d8b3..b8709047e3 100644 --- a/packages/excalidraw/charts/charts.line.ts +++ b/packages/excalidraw/charts/charts.line.ts @@ -4,6 +4,8 @@ import { isDevEnv } from "@excalidraw/common"; import { newElement, newLinearElement } from "@excalidraw/element"; +import type { RenderEnvironment } from "@excalidraw/element"; + import type { LocalPoint } from "@excalidraw/math"; import { GRID_OPACITY, commonProps } from "./charts.constants"; @@ -26,6 +28,7 @@ export const renderLineChart = ( x: number, y: number, colorSeed?: number, + renderEnvironment?: RenderEnvironment, ): ChartElements => { const series = spreadsheet.series; const layout = getCartesianChartLayout("line", series.length); @@ -110,8 +113,16 @@ export const renderLineChart = ( layout, max, isDevEnv(), + renderEnvironment, + ); + const xLabels = chartXLabels( + spreadsheet, + x, + y, + backgroundColor, + layout, + renderEnvironment, ); - const xLabels = chartXLabels(spreadsheet, x, y, backgroundColor, layout); const xLabelsBottomY = Math.max( y + layout.gap / 2, ...xLabels.map((label) => getRotatedTextElementBottom(label)), @@ -124,6 +135,7 @@ export const renderLineChart = ( xLabelsBottomY, y + layout.gap * 5, backgroundColor, + renderEnvironment, ); return [...baseElements, ...lines, ...guides, ...dots, ...seriesLegend]; diff --git a/packages/excalidraw/charts/charts.radar.ts b/packages/excalidraw/charts/charts.radar.ts index 6606a4af60..8f366779ff 100644 --- a/packages/excalidraw/charts/charts.radar.ts +++ b/packages/excalidraw/charts/charts.radar.ts @@ -14,6 +14,8 @@ import { newTextElement, } from "@excalidraw/element"; +import type { RenderEnvironment } from "@excalidraw/element"; + import type { LocalPoint } from "@excalidraw/math"; import { @@ -43,6 +45,7 @@ export const renderRadarChart = ( x: number, y: number, colorSeed?: number, + renderEnvironment?: RenderEnvironment, ): ChartElements | null => { if (!isSpreadsheetValidForChartType(spreadsheet, "radar")) { return null; @@ -72,6 +75,7 @@ export const renderRadarChart = ( centerY, radius, backgroundColor, + renderEnvironment, ); const titleFontFamily = FONT_FAMILY["Lilita One"]; @@ -86,10 +90,16 @@ export const renderRadarChart = ( spreadsheet.title, titleFontString, chartWidth + RADAR_LABEL_OFFSET * 2, + renderEnvironment, ) : null; const titleTextMetrics = titleText - ? measureText(titleText, titleFontString, titleLineHeight) + ? measureText( + titleText, + titleFontString, + titleLineHeight, + renderEnvironment, + ) : null; const title = titleText ? newTextElement({ @@ -103,6 +113,7 @@ export const renderRadarChart = ( fontSize: titleFontSize, lineHeight: titleLineHeight, textAlign: "center", + renderEnvironment, }) : null; @@ -186,6 +197,7 @@ export const renderRadarChart = ( axisLabelBottomY, y + BAR_GAP * 5, backgroundColor, + renderEnvironment, ); return [ diff --git a/packages/excalidraw/charts/index.ts b/packages/excalidraw/charts/index.ts index d806546a49..279bb2bf32 100644 --- a/packages/excalidraw/charts/index.ts +++ b/packages/excalidraw/charts/index.ts @@ -1,5 +1,7 @@ import type { ChartType } from "@excalidraw/element/types"; +import type { RenderEnvironment } from "@excalidraw/element"; + import { renderBarChart } from "./charts.bar"; import { renderLineChart } from "./charts.line"; import { @@ -27,12 +29,13 @@ export const renderSpreadsheet = ( x: number, y: number, colorSeed?: number, + renderEnvironment?: RenderEnvironment, ): ChartElements | null => { if (chartType === "line") { - return renderLineChart(spreadsheet, x, y, colorSeed); + return renderLineChart(spreadsheet, x, y, colorSeed, renderEnvironment); } if (chartType === "radar") { - return renderRadarChart(spreadsheet, x, y, colorSeed); + return renderRadarChart(spreadsheet, x, y, colorSeed, renderEnvironment); } - return renderBarChart(spreadsheet, x, y, colorSeed); + return renderBarChart(spreadsheet, x, y, colorSeed, renderEnvironment); }; diff --git a/packages/excalidraw/components/App.arrowText.ts b/packages/excalidraw/components/App.arrowText.ts index b5f18bb86e..aed8704c5d 100644 --- a/packages/excalidraw/components/App.arrowText.ts +++ b/packages/excalidraw/components/App.arrowText.ts @@ -154,6 +154,7 @@ export class AppArrowText { pointerX: pointerCoords.x, zoom: this.app.state.zoom.value, scene: this.app.scene, + renderEnvironment: this.app.renderEnvironment, }); return true; diff --git a/packages/excalidraw/components/App.bucketFill.ts b/packages/excalidraw/components/App.bucketFill.ts index 03bc3087d5..bc63b9c9d9 100644 --- a/packages/excalidraw/components/App.bucketFill.ts +++ b/packages/excalidraw/components/App.bucketFill.ts @@ -111,7 +111,7 @@ export class AppBucketFill { }, }; this.temporaryEyeDropper = eyeDropper; - this.app.updateEditorAtom(activeEyeDropperAtom, eyeDropper); + this.app.host.setActiveEyeDropper(eyeDropper); }; closeTemporaryEyeDropper = () => { @@ -121,7 +121,7 @@ export class AppBucketFill { eyeDropper && editorJotaiStore.get(activeEyeDropperAtom) === eyeDropper ) { - this.app.updateEditorAtom(activeEyeDropperAtom, null); + this.app.host.setActiveEyeDropper(null); } }; diff --git a/packages/excalidraw/components/App.clipboard.ts b/packages/excalidraw/components/App.clipboard.ts index 068d0a6613..c494044c4e 100644 --- a/packages/excalidraw/components/App.clipboard.ts +++ b/packages/excalidraw/components/App.clipboard.ts @@ -181,6 +181,7 @@ export class AppClipboard { data.programmaticAPI ? convertToExcalidrawElements( data.elements as ExcalidrawElementSkeleton[], + { renderEnvironment: this.app.renderEnvironment }, ) : data.elements ) as readonly ExcalidrawElement[]; @@ -212,6 +213,7 @@ export class AppClipboard { const elements = convertToExcalidrawElements(skeletonElements, { regenerateIds: true, + renderEnvironment: this.app.renderEnvironment, }); this.app.addElementsFromPasteOrLibrary({ @@ -459,15 +461,30 @@ export class AppClipboard { y: currentY, }); - let metrics = measureText(originalText, fontString, lineHeight); + let metrics = measureText( + originalText, + fontString, + lineHeight, + this.app.renderEnvironment, + ); const isTextUnwrapped = metrics.width > maxTextWidth; const text = isTextUnwrapped - ? wrapText(originalText, fontString, maxTextWidth) + ? wrapText( + originalText, + fontString, + maxTextWidth, + this.app.renderEnvironment, + ) : originalText; metrics = isTextUnwrapped - ? measureText(text, fontString, lineHeight) + ? measureText( + text, + fontString, + lineHeight, + this.app.renderEnvironment, + ) : metrics; const startX = x - metrics.width / 2; @@ -482,6 +499,7 @@ export class AppClipboard { lineHeight, autoResize: !isTextUnwrapped, frameId: topLayerFrame ? topLayerFrame.id : null, + renderEnvironment: this.app.renderEnvironment, }); acc.push(element); currentY += element.height + LINE_GAP; diff --git a/packages/excalidraw/components/App.cursor.ts b/packages/excalidraw/components/App.cursor.ts index cf5419dcbc..9689ed4260 100644 --- a/packages/excalidraw/components/App.cursor.ts +++ b/packages/excalidraw/components/App.cursor.ts @@ -211,7 +211,7 @@ export class AppCursor { if (!this.eraserCanvasCache || this.eraserCanvasCache.theme !== theme) { const isDarkTheme = theme === THEME.DARK; - this.eraserCanvasCache = document.createElement("canvas"); + this.eraserCanvasCache = this.app.ownerDocument.createElement("canvas"); this.eraserCanvasCache.theme = theme; this.eraserCanvasCache.height = cursorImageSizePx; this.eraserCanvasCache.width = cursorImageSizePx; diff --git a/packages/excalidraw/components/App.host.ts b/packages/excalidraw/components/App.host.ts new file mode 100644 index 0000000000..e34c411b4b --- /dev/null +++ b/packages/excalidraw/components/App.host.ts @@ -0,0 +1,234 @@ +import { isDevEnv, isTestEnv } from "@excalidraw/common"; + +import type { RenderEnvironment } from "@excalidraw/element"; + +import { editorJotaiStore } from "../editor-jotai"; + +import { activeEyeDropperAtom, type EyeDropperProperties } from "./EyeDropper"; +import { + hideHyperlinkToolip, + type HyperlinkTooltipOwner, +} from "./hyperlink/Hyperlink"; +import { startLinkImgDecoding } from "./hyperlink/helpers"; + +import type { AppProps } from "../types"; +import type App from "./App"; + +/** + * The pointerup handler of whichever editor is mid-interaction. Shared by + * every editor in the process; `AppHost.ownedPointerUp` says whose it is. + */ +let activePointerUp: (() => void) | null = null; + +/** + * Owns everything that ties this editor to the document/window it renders + * into (cross-document runtime ownership): the render environment the + * canvases, images and paths are minted from, and the state that has to be + * re-primed whenever that environment is swapped. + */ +export class AppHost { + constructor(private app: App) {} + + /** + * Identity token for the hyperlink tooltip, which is a module-level + * singleton shared by every editor in the process — the owner tells it + * which editor the currently shown tooltip belongs to, so one editor never + * hides (or leaks a timer into) another's tooltip. + */ + readonly hyperlinkTooltipOwner: HyperlinkTooltipOwner = {}; + + /** + * The process-wide interaction slots this editor currently owns: the + * pointerup handler of the interaction in flight, and the eye dropper (one + * atom in the editor-wide store). Both hold closures over this editor, so a + * teardown that leaves one set retains the editor -- and with it its owner + * window and document, e.g. a closed popout. + * + * @see releaseOwnedInteractionState + */ + private ownedPointerUp: (() => void) | null = null; + private ownedEyeDropper: EyeDropperProperties | null = null; + + /** Takes over the pointerup slot (or clears it, on `null`). */ + public setPointerUp(handler: (() => void) | null) { + activePointerUp = handler; + this.ownedPointerUp = handler; + } + + /** + * Runs the pending pointerup, whoever owns it -- pointerup does not always + * fire (the user tabs away), so interactions are settled manually. + */ + public runPendingPointerUp() { + activePointerUp?.(); + } + + /** Opens the eye dropper (or closes it, on `null`), taking ownership. */ + public setActiveEyeDropper(eyeDropper: EyeDropperProperties | null) { + this.ownedEyeDropper = eyeDropper; + this.app.updateEditorAtom(activeEyeDropperAtom, eyeDropper); + } + + /** + * Drops the ownership token without touching the atom -- for callers that + * have just closed the eye dropper themselves. + */ + public releaseEyeDropperOwnership() { + this.ownedEyeDropper = null; + } + + /** + * Releases the process-wide slots this editor owns, so nothing outside it + * keeps it alive once it is torn down. Ownership-checked: a sibling + * editor's in-flight interaction must survive our teardown. + */ + private releaseOwnedInteractionState() { + // an unmount mid-drag never reaches the pointerup that would clear this + if (this.ownedPointerUp && activePointerUp === this.ownedPointerUp) { + activePointerUp = null; + } + this.ownedPointerUp = null; + + if ( + this.ownedEyeDropper && + editorJotaiStore.get(activeEyeDropperAtom) === this.ownedEyeDropper + ) { + editorJotaiStore.set(activeEyeDropperAtom, null); + } + this.ownedEyeDropper = null; + } + + /** + * Render environment scoped to this editor's owner window, so that the + * canvases, images and paths created during rendering live in the owner + * document. Memoized keyed on the resolved document because render caches + * are keyed by environment identity: the identity must not survive a + * document switch, or caches would mix canvases and images from two realms + * under one bucket. + */ + private _renderEnvironment: RenderEnvironment | null = null; + private _renderEnvironmentDocument: Document | null = null; + public get renderEnvironment(): RenderEnvironment { + if (this.app.props.renderEnvironment) { + return this.app.props.renderEnvironment; + } + const ownerDocument = this.app.ownerDocument; + if ( + !this._renderEnvironment || + this._renderEnvironmentDocument !== ownerDocument + ) { + this._renderEnvironmentDocument = ownerDocument; + this._renderEnvironment = { + createCanvas: () => this.app.ownerDocument.createElement("canvas"), + createImage: () => new this.app.ownerWindow.Image(), + // Browsers accept a `Path2D` minted in another realm, but taking it + // from the owner window keeps runtime ownership complete. Falls back + // to the global for realms without one (e.g. jsdom iframes). + createPath: (svgPath: string) => + new (this.app.ownerWindow.Path2D ?? Path2D)(svgPath), + document: ownerDocument, + }; + } + return this._renderEnvironment; + } + + /** + * Re-primes whatever the render environment owns. Called on mount and on + * every update, so an environment swap is picked up wherever it happens. + */ + public sync(prevProps?: AppProps) { + this.ensureLinkImgDecoding(); + if (prevProps) { + this.warnOnUnstableRenderEnvironment(prevProps); + } + } + + /** + * Releases what this editor owns outside itself, so we don't retain the + * (soon detached) document/window, or leave a pending tooltip timer around. + */ + public destroy() { + this._destroyed = true; + hideHyperlinkToolip(this.hyperlinkTooltipOwner); + this.releaseOwnedInteractionState(); + } + private _destroyed = false; + + /** + * Link icons decode asynchronously and the static scene skips an icon that + * is not decoded yet, so start the decode on mount rather than on the + * first render that happens to have a link, so the decode is settled long + * before there is a link element to draw an icon for. + * + * Re-run on an environment swap: the images are keyed by env identity, so + * a new environment starts with none. + * + * The decode landing only flips a status, which invalidates nothing, so the + * scene is nudged once when an icon becomes drawable -- otherwise a scene + * that paints before the decode settles and is then left alone (an initial + * scene with links, never scrolled or edited) keeps its icons missing until + * something unrelated repaints. Same shape as a font or an image file + * landing: repaint only when there is something new to draw. + */ + private _linkImgDecodingEnvironment: RenderEnvironment | null = null; + private ensureLinkImgDecoding() { + const renderEnvironment = this.renderEnvironment; + if (this._linkImgDecodingEnvironment === renderEnvironment) { + return; + } + this._linkImgDecodingEnvironment = renderEnvironment; + startLinkImgDecoding(renderEnvironment).then((didDecode) => { + if ( + !didDecode || + this._destroyed || + // the environment was swapped while we waited; that swap started its + // own decode, and this one's images are no longer the ones drawn + this._linkImgDecodingEnvironment !== renderEnvironment || + // nothing in the scene draws a link icon, so there is nothing to + // repaint for; an element gaining a link repaints on its own + !this.app.scene + .getNonDeletedElements() + .some((element) => !!element.link) + ) { + return; + } + this.app.scene.triggerUpdate(); + }); + } + + /** + * Every render cache is keyed by environment identity, so a host passing an + * inline `renderEnvironment` literal re-mints the identity on each render + * and re-rasterizes everything, with no visible symptom other than being + * slow. Detected by the factories' source being unchanged across the swap: + * a genuine environment switch (e.g. a document change) reads differently. + */ + private _warnedUnstableRenderEnvironment = false; + private warnOnUnstableRenderEnvironment(prevProps: AppProps) { + if ( + (!isDevEnv() && !isTestEnv()) || + this._warnedUnstableRenderEnvironment + ) { + return; + } + const prev = prevProps.renderEnvironment; + const next = this.app.props.renderEnvironment; + if ( + !prev || + !next || + prev === next || + String(prev.createCanvas) !== String(next.createCanvas) || + String(prev.createImage) !== String(next.createImage) + ) { + return; + } + this._warnedUnstableRenderEnvironment = true; + console.warn( + "Excalidraw: the `renderEnvironment` prop changed identity while its " + + "implementation stayed the same. Render caches are keyed by this " + + "object's identity, so a new identity per render defeats all of them " + + "(elements are re-rasterized every frame). Hoist the object to a " + + "module constant or memoize it (e.g. `useMemo`).", + ); + } +} diff --git a/packages/excalidraw/components/App.pan.ts b/packages/excalidraw/components/App.pan.ts index 81504a3492..a1b0d37ee6 100644 --- a/packages/excalidraw/components/App.pan.ts +++ b/packages/excalidraw/components/App.pan.ts @@ -194,7 +194,7 @@ export class AppPan { * - empty clipboard */ const enableNextPaste = () => { - setTimeout(() => { + app.ownerWindow.setTimeout(() => { app.ownerDocument.body.removeEventListener( EVENT.PASTE, preventNextPaste, @@ -217,7 +217,7 @@ export class AppPan { scrollX: state.scrollX - deltaX / state.zoom.value, scrollY: state.scrollY - deltaY / state.zoom.value, })); - }); + }, app.ownerWindow); this.pendingMoveFlush = onPointerMove.flush; const teardown = withBatchedUpdates( (upEvent?: PointerEvent | FocusEvent) => { diff --git a/packages/excalidraw/components/App.selectionTool.ts b/packages/excalidraw/components/App.selectionTool.ts index dbc67f301c..b1ec916e84 100644 --- a/packages/excalidraw/components/App.selectionTool.ts +++ b/packages/excalidraw/components/App.selectionTool.ts @@ -701,6 +701,7 @@ export class AppSelectionTool { resizeY, pointerDownState.resize.center.x, pointerDownState.resize.center.y, + this.app.renderEnvironment, ) ) { const elementsToHighlight = new Set(); diff --git a/packages/excalidraw/components/App.text.ts b/packages/excalidraw/components/App.text.ts index 496e2867d4..bce77ccb2c 100644 --- a/packages/excalidraw/components/App.text.ts +++ b/packages/excalidraw/components/App.text.ts @@ -199,6 +199,7 @@ export class AppText { const stickyLayout = stickyContainer ? getStickyNoteLayout(stickyContainer, latestTextElement, { originalText: nextOriginalText, + renderEnvironment: this.app.renderEnvironment, }) : null; // a free text stops growing at the view's width (between the side @@ -230,6 +231,7 @@ export class AppText { elementsMap, nextOriginalText, maxWidth, + this.app.renderEnvironment, )), }); } @@ -719,6 +721,7 @@ export class AppText { const minWidth = getApproxMinLineWidth( getFontString(fontString), lineHeight, + this.app.renderEnvironment, ); const minHeight = getApproxMinLineHeight(fontSize, lineHeight); const newHeight = Math.max(container.height, minHeight); @@ -823,6 +826,7 @@ export class AppText { ? container.angle : (0 as Radians), frameId, + renderEnvironment: this.app.renderEnvironment, }); if (!existingTextElement && shouldBindToContainer && container) { diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index 18c06ac8c6..7e8164eea0 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -237,7 +237,10 @@ import type { ExcalidrawBindableElement, } from "@excalidraw/element/types"; -import type { TransformHandleDirection } from "@excalidraw/element"; +import type { + RenderEnvironment, + TransformHandleDirection, +} from "@excalidraw/element"; import type { Mutable, ValueOf } from "@excalidraw/common/utility-types"; @@ -410,6 +413,7 @@ import { hideTooltip } from "./Tooltip"; import { StaticCanvas, InteractiveCanvas } from "./canvases"; import NewElementCanvas from "./canvases/NewElementCanvas"; import { isPointHittingLink } from "./hyperlink/helpers"; +import { AppHost } from "./App.host"; import { CursorHint, CursorHints } from "./CursorHint"; import { MagicIcon, copyIcon, fullscreenIcon } from "./icons"; import { AppStateObserver, type OnStateChange } from "./AppStateObserver"; @@ -569,7 +573,6 @@ const GOOGLE_DRIVE_AUTOPLAYED = new WeakSet(); const MAX_EMBEDDABLE_VIEWPORT_SCALE = 4; -let lastPointerUp: (() => void) | null = null; const gesture: Gesture = { pointers: new Map(), lastCenter: null, @@ -605,6 +608,18 @@ class App extends React.Component { typeof globalThis; } + /** + * Everything binding this editor to the document/window it renders into + * (render environment, hyperlink tooltip ownership). Delegated to so the + * cross-document ownership rules live in one place. + */ + public host: AppHost = new AppHost(this); + + /** @see AppHost.renderEnvironment */ + public get renderEnvironment(): RenderEnvironment { + return this.host.renderEnvironment; + } + public scene: Scene; public fonts: Fonts; public renderer: Renderer; @@ -1091,7 +1106,7 @@ class App extends React.Component { let data = null; try { data = JSON.parse(event.data); - } catch (e) {} + } catch {} if (!data) { return; } @@ -2624,6 +2639,8 @@ class App extends React.Component { renderConfig={{ imageCache: this.imageCache, isExporting: false, + scale: this.ownerWindow.devicePixelRatio, + renderEnvironment: this.renderEnvironment, renderGrid: isGridModeEnabled(this), renderLinks: this.isLinksEnabled(), canvasBackgroundColor: @@ -2649,6 +2666,8 @@ class App extends React.Component { renderConfig={{ imageCache: this.imageCache, isExporting: false, + scale: this.ownerWindow.devicePixelRatio, + renderEnvironment: this.renderEnvironment, renderGrid: false, canvasBackgroundColor: this.state.viewBackgroundColor, @@ -2773,6 +2792,7 @@ class App extends React.Component { { name: this.getName(), exportingFrame: opts.exportingFrame, + renderEnvironment: this.renderEnvironment, }, ) .catch(muteFSAbortError) @@ -3032,7 +3052,7 @@ class App extends React.Component { }; private openEyeDropper = ({ type }: { type: "stroke" | "background" }) => { - this.updateEditorAtom(activeEyeDropperAtom, { + this.host.setActiveEyeDropper({ swapPreviewOnAlt: true, colorPickerType: type === "stroke" ? "elementStroke" : "elementBackground", @@ -3289,7 +3309,7 @@ class App extends React.Component { this.pan.setSpaceHeld(false); this.modifiers.reset(); isDraggingScrollBar = false; - lastPointerUp = null; + this.host.setPointerUp(null); gesture.pointers.clear(); gesture.lastCenter = null; @@ -3313,6 +3333,7 @@ class App extends React.Component { // These components install their own DOM listeners rather than going // through App's input handlers, so they must be explicitly unmounted. editorJotaiStore.set(activeEyeDropperAtom, null); + this.host.releaseEyeDropperOwnership(); editorJotaiStore.set(convertElementTypePopupAtom, null); if (this.state.editingFrame) { @@ -3405,7 +3426,7 @@ class App extends React.Component { if (this.isLinksEnabled(prevProps) !== this.isLinksEnabled()) { if (!this.isLinksEnabled()) { this.hitLinkElement = undefined; - hideHyperlinkToolip(); + hideHyperlinkToolip(this.host.hyperlinkTooltipOwner); this.cursor.reset(); } } @@ -3682,10 +3703,16 @@ class App extends React.Component { // can be loaded fresh this.clearImageShapeCache(); - // manually loading the font faces seems faster even in browsers that do fire the loadingdone event - this.fonts.loadSceneFonts().then((fontFaces) => { - this.fonts.onLoaded(fontFaces); - }); + // manually loading the font faces seems faster even in browsers that do + // fire the loadingdone event. Prewarming the default font family, so that + // the first text element is not rendered with a fallback font. + // NOTE: reading from `restoredAppState` instead of `this.state`, as the + // `syncActionResult` above sets the state asynchronously + this.fonts + .loadSceneFonts(restoredAppState.currentItemFontFamily) + .then((fontFaces) => { + this.fonts.onLoaded(fontFaces); + }); if (isElementLink(this.ownerWindow.location.href)) { this.viewport.setViewport({ @@ -3747,7 +3774,7 @@ class App extends React.Component { this.viewport.invalidateUIOffset("stylesPanel"); if (prevStylesPanelMode !== "full" && nextStylesPanelMode === "full") { - this.setState((prevState) => ({ + this.setState(() => ({ preferredSelectionTool: { type: "selection", initialized: true, @@ -3778,6 +3805,7 @@ class App extends React.Component { public async componentDidMount() { this.unmounted = false; this.api = this.createExcalidrawAPI(); + this.host.sync(); this.excalidrawContainerValue.container = this.excalidrawContainerRef.current; @@ -3903,7 +3931,9 @@ class App extends React.Component { (this.ownerWindow as any).launchQueue?.setConsumer(() => {}); - this.renderer.destroy(); + this.host.destroy(); + + this.renderer.destroy(this.canvas); this.scene.destroy(); this.scene = new Scene(); this.fonts = new Fonts(this.scene, this.ownerDocument); @@ -4282,6 +4312,7 @@ class App extends React.Component { this.handleInteractionStateChange(prevProps, prevState); this.handleForcedToolChange(prevProps, prevState); + this.host.sync(prevProps); this.appStateObserver.flush(prevState); @@ -4403,8 +4434,9 @@ class App extends React.Component { // execute only if the condition still holds when the deferred callback // executes (it can be scheduled multiple times depending on how // many times the component renders) - this.state.selectedLinearElement?.isEditing && + if (this.state.selectedLinearElement?.isEditing) { this.actionManager.executeAction(actionFinalize); + } }); } @@ -4629,7 +4661,12 @@ class App extends React.Component { newElement, this.scene.getElementsMapIncludingDeleted(), ); - redrawTextBoundingBox(newElement, container, this.scene); + redrawTextBoundingBox( + newElement, + container, + this.scene, + this.renderEnvironment, + ); } }); @@ -6502,7 +6539,7 @@ class App extends React.Component { this.editorInterface.formFactor === "phone", ); if (lastPointerDownHittingLinkIcon && lastPointerUpHittingLinkIcon) { - hideHyperlinkToolip(); + hideHyperlinkToolip(this.host.hyperlinkTooltipOwner); let url = this.hitLinkElement.link; if (url) { url = normalizeLink(url); @@ -6546,10 +6583,12 @@ class App extends React.Component { this.hitLinkElement, this.state, this.scene.getNonDeletedElementsMap(), + this.ownerDocument, + this.host.hyperlinkTooltipOwner, ); return true; } - hideHyperlinkToolip(); + hideHyperlinkToolip(this.host.hyperlinkTooltipOwner); return false; }; @@ -7344,7 +7383,7 @@ class App extends React.Component { }; // set touch moving for mobile context menu - private handleTouchMove = (event: React.TouchEvent) => { + private handleTouchMove = (_event: React.TouchEvent) => { if (!this.isInteractionEnabled()) { return; } @@ -7632,7 +7671,7 @@ class App extends React.Component { //fires only once, if pen is detected, penMode is enabled //the user can disable this by toggling the penMode button if (!this.state.penDetected && event.pointerType === "pen") { - this.setState((prevState) => { + this.setState(() => { return { penMode: true, penDetected: true, @@ -8014,7 +8053,7 @@ class App extends React.Component { */ private maybeCleanupAfterMissingPointerUp = (event: PointerEvent | null) => { this.pan.end(); - lastPointerUp?.(); + this.host.runPendingPointerUp(); this.missingPointerEventCleanupEmitter.trigger(event).clear(); }; @@ -8205,9 +8244,9 @@ class App extends React.Component { } this.handlePointerMoveOverScrollbars(event, pointerDownState); - }); + }, this.ownerWindow); const onPointerUp = withBatchedUpdates(() => { - lastPointerUp = null; + this.host.setPointerUp(null); isDraggingScrollBar = false; this.cursor.applyForTool(); this.setState({ @@ -8219,7 +8258,7 @@ class App extends React.Component { onPointerMove.flush(); }); - lastPointerUp = onPointerUp; + this.host.setPointerUp(onPointerUp); this.ownerWindow.addEventListener(EVENT.POINTER_MOVE, onPointerMove); this.ownerWindow.addEventListener(EVENT.POINTER_UP, onPointerUp); @@ -8938,7 +8977,9 @@ class App extends React.Component { ): (event: KeyboardEvent) => void { return withBatchedUpdates((event: KeyboardEvent) => { // Prevents focus from escaping excalidraw tab - event.key === KEYS.ALT && event.preventDefault(); + if (event.key === KEYS.ALT) { + event.preventDefault(); + } if (this.selectionTool.maybeHandleResize(pointerDownState, event)) { return; } @@ -9368,7 +9409,7 @@ class App extends React.Component { event, pointerDownState, ); - }); + }, this.ownerWindow); } // Returns whether the pointer move happened over either scrollbar @@ -9541,7 +9582,9 @@ class App extends React.Component { .map((e) => elementsMap.get(e.id)) .filter((e) => isElbowArrow(e)) .forEach((e) => { - !!e && this.scene.mutateElement(e, {}); + if (e) { + this.scene.mutateElement(e, {}); + } }); } } @@ -9803,6 +9846,7 @@ class App extends React.Component { fontFamily: newElement.fontFamily, }), newElement.lineHeight, + this.renderEnvironment, ); if (newElement.width < minWidth) { @@ -10564,6 +10608,7 @@ class App extends React.Component { imageCache: this.imageCache, fileIds: elements.map((element) => element.fileId), files, + createImage: () => this.renderEnvironment.createImage(), }); if (erroredFiles.size) { @@ -11073,6 +11118,7 @@ class App extends React.Component { shouldResizeFromCenter: false, scene: this.scene, zoom: this.state.zoom.value, + renderEnvironment: this.renderEnvironment, informMutation: false, }); return; @@ -11146,6 +11192,7 @@ class App extends React.Component { shouldResizeFromCenter: shouldResizeFromCenter(event), zoom: this.state.zoom.value, scene: this.scene, + renderEnvironment: this.renderEnvironment, widthAspectRatio: aspectRatio, originOffset: this.state.originSnapOffset, informMutation, @@ -11358,7 +11405,9 @@ class App extends React.Component { offsetTop, }, () => { - cb && cb(); + if (cb) { + cb(); + } }, ); // a smaller viewport may push the min zoom up / shrink the pan range diff --git a/packages/excalidraw/components/App.viewport.ts b/packages/excalidraw/components/App.viewport.ts index 5437d82f93..288c24b52e 100644 --- a/packages/excalidraw/components/App.viewport.ts +++ b/packages/excalidraw/components/App.viewport.ts @@ -22,7 +22,11 @@ import type { } from "@excalidraw/element/types"; import { getLanguage, t } from "../i18n"; -import { AnimationController } from "../renderer/animation"; +import { + AnimationController, + type AnimationKey, + type AnimationScheduler, +} from "../renderer/animation"; import { constrainScrollState, DEFAULT_OVERSCROLL, @@ -44,9 +48,13 @@ import type { } from "../types"; import type App from "./App"; -export const SCROLL_TO_CONTENT_ANIMATION_KEY = "animateScrollToContent"; -export const SCROLL_CONSTRAINTS_SNAP_BACK_ANIMATION_KEY = - "animateScrollConstraintsSnapBack"; +/** The AnimationController keys a viewport uses, unique per AppViewport so + * concurrent editors in the same window never share (or cancel) each + * other's animations. */ +export type ViewportAnimationKeys = { + scrollToContent: AnimationKey; + snapBack: AnimationKey; +}; const DEFAULT_SCROLL_ANIMATION_DURATION = 500; @@ -325,6 +333,8 @@ const animateToViewport = ( from: Viewport, target: Viewport, duration: number, + key: AnimationKey, + scheduler: AnimationScheduler, onFrame: ( state: Pick< AppState, @@ -334,7 +344,7 @@ const animateToViewport = ( onComplete: () => void, ) => { AnimationController.start<{ elapsed: number }>( - SCROLL_TO_CONTENT_ANIMATION_KEY, + key, ({ deltaTime, state }) => { const elapsed = (state?.elapsed ?? 0) + deltaTime; const progress = Math.min(elapsed / duration, 1); @@ -352,6 +362,7 @@ const animateToViewport = ( return null; }, + scheduler, ); }; @@ -379,6 +390,8 @@ export const snapBackToConstraints = ( >, ) => Viewport | null, ) => void, + keys: ViewportAnimationKeys, + scheduler: AnimationScheduler = window, duration = SNAP_BACK_ANIMATION_DURATION, ) => { const target = constrainScrollState(state); @@ -393,7 +406,7 @@ export const snapBackToConstraints = ( // A programmatic navigation owns the viewport until it settles. A stale // rubberband debounce must not supersede it. - if (AnimationController.running(SCROLL_TO_CONTENT_ANIMATION_KEY)) { + if (AnimationController.running(keys.scrollToContent)) { return; } @@ -404,7 +417,7 @@ export const snapBackToConstraints = ( const overscrollY = (state.scrollY - target.scrollY) * state.zoom.value; AnimationController.start<{ elapsed: number }>( - SCROLL_CONSTRAINTS_SNAP_BACK_ANIMATION_KEY, + keys.snapBack, ({ deltaTime, state: animationState }) => { const elapsed = (animationState?.elapsed ?? 0) + deltaTime; const progress = Math.min(elapsed / duration, 1); @@ -426,6 +439,7 @@ export const snapBackToConstraints = ( return progress < 1 ? { elapsed } : null; }, + scheduler, ); }; @@ -446,6 +460,11 @@ export class AppViewport { { side: "left" | "right"; offset: number } >(); + /** Unique per instance (see `ViewportAnimationKeys`); exposed for tests + * observing the animations. */ + readonly scrollToContentAnimationKey = Symbol("scrollToContentAnimation"); + readonly snapBackAnimationKey = Symbol("snapBackAnimation"); + constructor( private app: App, private dependencies: AppViewportDependencies, @@ -453,8 +472,8 @@ export class AppViewport { get isAnimating() { return ( - AnimationController.running(SCROLL_TO_CONTENT_ANIMATION_KEY) || - AnimationController.running(SCROLL_CONSTRAINTS_SNAP_BACK_ANIMATION_KEY) + AnimationController.running(this.scrollToContentAnimationKey) || + AnimationController.running(this.snapBackAnimationKey) ); } @@ -725,7 +744,7 @@ export class AppViewport { setViewport = (opts: SetViewportOptions | null) => { if (opts === null) { this.cancelTransition(); - AnimationController.cancel(SCROLL_CONSTRAINTS_SNAP_BACK_ANIMATION_KEY); + AnimationController.cancel(this.snapBackAnimationKey); this.snapBackDebounced.cancel(); this.app.setState({ scrollConstraints: null, @@ -774,7 +793,7 @@ export class AppViewport { // A new programmatic navigation supersedes the previous one and starts // from whatever viewport its last rendered frame reached. this.cancelTransition(); - AnimationController.cancel(SCROLL_CONSTRAINTS_SNAP_BACK_ANIMATION_KEY); + AnimationController.cancel(this.snapBackAnimationKey); this.snapBackDebounced.cancel(); if (duration === null) { @@ -801,6 +820,8 @@ export class AppViewport { from, viewportUpdate, duration, + this.scrollToContentAnimationKey, + this.app.ownerWindow, (state) => { if (this.activeTransition === transition) { this.app.setState(state); @@ -851,7 +872,7 @@ export class AppViewport { } this.cancelTransition(); if (!opts?.preserveScrollConstraintsSnapBack) { - AnimationController.cancel(SCROLL_CONSTRAINTS_SNAP_BACK_ANIMATION_KEY); + AnimationController.cancel(this.snapBackAnimationKey); } this.app.requestUnfollow(); @@ -900,8 +921,14 @@ export class AppViewport { if (this.app.unmounted || this.dependencies.isGestureActive()) { return; } - snapBackToConstraints(this.app.state, (updater) => - this.app.setState((state) => updater(state)), + snapBackToConstraints( + this.app.state, + (updater) => this.app.setState((state) => updater(state)), + { + scrollToContent: this.scrollToContentAnimationKey, + snapBack: this.snapBackAnimationKey, + }, + this.app.ownerWindow, ); }; @@ -912,12 +939,12 @@ export class AppViewport { private cancelTransition = () => { this.activeTransition = null; - AnimationController.cancel(SCROLL_TO_CONTENT_ANIMATION_KEY); + AnimationController.cancel(this.scrollToContentAnimationKey); }; destroy = () => { this.cancelTransition(); - AnimationController.cancel(SCROLL_CONSTRAINTS_SNAP_BACK_ANIMATION_KEY); + AnimationController.cancel(this.snapBackAnimationKey); this.snapBackDebounced.cancel(); }; } diff --git a/packages/excalidraw/components/ColorPicker/colorTopPicksDnD.ts b/packages/excalidraw/components/ColorPicker/colorTopPicksDnD.ts index f4055d0bd5..ba0ed64c50 100644 --- a/packages/excalidraw/components/ColorPicker/colorTopPicksDnD.ts +++ b/packages/excalidraw/components/ColorPicker/colorTopPicksDnD.ts @@ -23,7 +23,9 @@ const createColorGhost: CreateTopPicksGhost = ({ value: color, sourceEl, }) => { - const swatch = document.createElement("div"); + // the source may live in another window's document (e.g. a popout) + const ownerDocument = sourceEl.ownerDocument; + const swatch = ownerDocument.createElement("div"); swatch.className = "excalidraw-color-dnd-ghost-swatch"; if (isTransparent(color)) { swatch.classList.add("is-transparent"); @@ -31,7 +33,9 @@ const createColorGhost: CreateTopPicksGhost = ({ // swatches render the theme-adjusted color (dark mode remaps colors // rather than CSS-filtering them) — sample the rendered color so the // ghost matches what the user picked up - const rendered = getComputedStyle(sourceEl).backgroundColor; + const rendered = (ownerDocument.defaultView ?? window).getComputedStyle( + sourceEl, + ).backgroundColor; swatch.style.backgroundColor = rendered && rendered !== "rgba(0, 0, 0, 0)" ? rendered : color; } diff --git a/packages/excalidraw/components/ConvertElementTypePopup.tsx b/packages/excalidraw/components/ConvertElementTypePopup.tsx index f0445ee2d0..6c135ea4bc 100644 --- a/packages/excalidraw/components/ConvertElementTypePopup.tsx +++ b/packages/excalidraw/components/ConvertElementTypePopup.tsx @@ -81,6 +81,8 @@ import type { import type { Scene } from "@excalidraw/element"; +import type { RenderEnvironment } from "@excalidraw/element/renderEnvironment"; + import { trackEvent } from "../analytics"; import { atom } from "../editor-jotai"; @@ -371,6 +373,7 @@ export const adjustBoundTextSize = ( container: ExcalidrawTextContainer, boundText: ExcalidrawTextElementWithContainer, scene: Scene, + renderEnvironment?: RenderEnvironment, ) => { const maxWidth = getBoundTextMaxWidth(container, boundText); const maxHeight = getBoundTextMaxHeight(container, boundText); @@ -379,12 +382,14 @@ export const adjustBoundTextSize = ( boundText.text, getFontString(boundText), maxWidth, + renderEnvironment, ); let metrics = measureText( wrappedText, getFontString(boundText), boundText.lineHeight, + renderEnvironment, ); let nextFontSize = boundText.fontSize; @@ -401,6 +406,7 @@ export const adjustBoundTextSize = ( boundText.text, getFontString(_updatedTextElement), boundText.lineHeight, + renderEnvironment, ); } @@ -410,7 +416,7 @@ export const adjustBoundTextSize = ( height: metrics.height, }); - redrawTextBoundingBox(boundText, container, scene); + redrawTextBoundingBox(boundText, container, scene, renderEnvironment); }; type ConversionType = "generic" | "linear" | null; @@ -501,6 +507,7 @@ export const convertElementTypes = ( element as ExcalidrawTextContainer, boundText, app.scene, + app.renderEnvironment, ); } } diff --git a/packages/excalidraw/components/FontPicker/fontTopPicksDnD.ts b/packages/excalidraw/components/FontPicker/fontTopPicksDnD.ts index 61b9659807..4273ec5466 100644 --- a/packages/excalidraw/components/FontPicker/fontTopPicksDnD.ts +++ b/packages/excalidraw/components/FontPicker/fontTopPicksDnD.ts @@ -20,6 +20,9 @@ const createFontGhost: CreateTopPicksGhost = ({ sourceEl, stripEl, }) => { + // the source may live in another window's document (e.g. a popout) + const ownerDocument = sourceEl.ownerDocument; + const ownerWindow = ownerDocument.defaultView ?? window; const isPick = sourceEl.dataset.topPickIndex != null; const template = (isPick @@ -27,10 +30,10 @@ const createFontGhost: CreateTopPicksGhost = ({ : stripEl.querySelector( "[data-top-pick-index]:not(.active)", )) ?? stripEl.querySelector("[data-top-pick-index]")!; - const templateStyle = getComputedStyle(template); + const templateStyle = ownerWindow.getComputedStyle(template); const templateRect = template.getBoundingClientRect(); - const tile = document.createElement("div"); + const tile = ownerDocument.createElement("div"); tile.className = "excalidraw-font-dnd-ghost-tile"; tile.style.backgroundColor = templateStyle.backgroundColor; tile.style.color = templateStyle.color; @@ -58,7 +61,8 @@ const createFontGhost: CreateTopPicksGhost = ({ if (sample) { const sampleClone = sample.cloneNode(true) as HTMLElement; // the ghost is outside the editor's CSS scope - sampleClone.style.fontSize = getComputedStyle(sample).fontSize; + sampleClone.style.fontSize = + ownerWindow.getComputedStyle(sample).fontSize; sampleClone.style.lineHeight = "1"; tile.appendChild(sampleClone); } diff --git a/packages/excalidraw/components/PasteChartDialog.tsx b/packages/excalidraw/components/PasteChartDialog.tsx index 596afc59df..e54c845ca7 100644 --- a/packages/excalidraw/components/PasteChartDialog.tsx +++ b/packages/excalidraw/components/PasteChartDialog.tsx @@ -48,6 +48,7 @@ const ChartPreviewBtn = (props: { null, ); const { theme } = useUIAppState(); + const { renderEnvironment } = useApp(); useLayoutEffect(() => { if (!props.spreadsheet) { @@ -61,6 +62,7 @@ const ChartPreviewBtn = (props: { 0, 0, props.colorSeed, + renderEnvironment, ); if (!elements) { setChartElements(null); @@ -82,6 +84,7 @@ const ChartPreviewBtn = (props: { skipInliningFonts: true, theme, canvasBackgroundColor: false, + environment: renderEnvironment, }, }); svg.querySelector(".style-fonts")?.remove(); @@ -92,7 +95,13 @@ const ChartPreviewBtn = (props: { return () => { previewNode.replaceChildren(); }; - }, [props.spreadsheet, props.chartType, props.colorSeed, theme]); + }, [ + props.spreadsheet, + props.chartType, + props.colorSeed, + theme, + renderEnvironment, + ]); const chartTypeLabel = getChartTypeLabel(props.chartType); @@ -119,6 +128,7 @@ const PlainTextPreviewBtn = (props: { }) => { const previewRef = useRef(null); const { theme } = useUIAppState(); + const { renderEnvironment } = useApp(); useLayoutEffect(() => { if (!props.rawText) { @@ -144,6 +154,7 @@ const PlainTextPreviewBtn = (props: { skipInliningFonts: true, theme, canvasBackgroundColor: false, + environment: renderEnvironment, }, }); svg.querySelector(".style-fonts")?.remove(); @@ -154,7 +165,7 @@ const PlainTextPreviewBtn = (props: { return () => { previewNode.replaceChildren(); }; - }, [props.rawText, theme]); + }, [props.rawText, theme, renderEnvironment]); return (