Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/text editor/drawing canvas: prevent Excalidraw from capturing scroll gestures while in image canvas mode
Adorian Doran81ddd9671fd2
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsxindex c491581f3b..abb6358060 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx@@ -64,7 +64,7 @@ globalThis.ResizeObserver = class {const {default: CanvasDrawing, CanvasDrawingDetail, renderCanvasDrawingPicture, useIsToolbarOverPanel,- useSidePanels, useTopLayerContextMenu+ useNoteScrollingWheel, useSidePanels, useTopLayerContextMenu} = await import("./CanvasDrawing");const { default: CanvasDrawingMenu } = await import("./CanvasDrawingMenu");const { default: CanvasEmbedTools } = await import("./CanvasEmbedTools");@@ -486,6 +486,79 @@ describe("useTopLayerContextMenu", () => {});});+describe("useNoteScrollingWheel", () => {+ const IMAGE = { type: "image", locked: true, isDeleted: false };+ let container: HTMLElement;+ let elements: object[];+ const api = { getSceneElements: () => elements };++ function WheelProbe() {+ const rootRef = useRef<HTMLDivElement>(null);+ const apiRef = useRef(api as unknown as ExcalidrawImperativeAPI);+ useNoteScrollingWheel(rootRef, apiRef);+ return <div ref={rootRef}><div className="excalidraw-container"><canvas /></div></div>;+ }++ beforeEach(async () => {+ elements = [ IMAGE ];+ container = document.createElement("div");+ document.body.append(container);+ await act(async () => render(<WheelProbe />, container));+ });++ afterEach(() => {+ render(null, container);+ container.remove();+ Reflect.deleteProperty(document, "fullscreenElement");+ });++ /** Whether Excalidraw's container gets a wheel over the drawing that the page can scroll. */+ function reachesDrawing(init: WheelEventInit = {}) {+ const excalidraw = container.querySelector(".excalidraw-container");+ const canvas = container.querySelector("canvas");+ if (!excalidraw || !canvas) {+ throw new Error("Expected the drawing.");+ }+ const listener = vi.fn();+ excalidraw.addEventListener("wheel", listener);+ const event = new WheelEvent("wheel", {+ bubbles: true, cancelable: true, deltaY: 100, ...init+ });+ // happy-dom's `WheelEvent` extends `UIEvent`, which has no modifier keys.+ Object.defineProperties(event, {+ ctrlKey: { value: !!init.ctrlKey },+ metaKey: { value: !!init.metaKey },+ shiftKey: { value: !!init.shiftKey }+ });+ canvas.dispatchEvent(event);+ excalidraw.removeEventListener("wheel", listener);+ expect(event.defaultPrevented).toBe(false);+ return listener.mock.calls.length > 0;+ }++ it("scrolls the note with the wheel over a locked image, which zooms with Ctrl or Cmd", () => {+ expect(reachesDrawing()).toBe(false);+ expect(reachesDrawing({ shiftKey: true, deltaY: 0, deltaX: 100 })).toBe(false);+ expect(reachesDrawing({ ctrlKey: true })).toBe(true);+ expect(reachesDrawing({ metaKey: true })).toBe(true);++ elements = [ { ...IMAGE, isDeleted: true }, { type: "rectangle", locked: false } ];+ expect(reachesDrawing()).toBe(true);+ });++ it("leaves the wheel to an unlocked image, and to a drawing in fullscreen", () => {+ elements = [ { ...IMAGE, locked: false } ];+ expect(reachesDrawing()).toBe(true);++ elements = [ IMAGE ];+ Object.defineProperty(document, "fullscreenElement", {+ value: container.firstElementChild,+ configurable: true+ });+ expect(reachesDrawing()).toBe(true);+ });+});+describe("CanvasDrawing", () => {const ATTACHMENT = { attachmentId: "a1" } as never;let box: HTMLElement;@@ -571,6 +644,23 @@ describe("CanvasDrawing", () => {expect(getEditingState()).toEqual({ isReadOnly: false, menu: true, tools: true });});+ it("scrolls the note with the wheel over an image canvas", async () => {+ await mount();+ const { apiRef } = canvasEditorProps.mock.lastCall?.[0] as MockCanvasEditorProps;+ const excalidraw = box.querySelector(".excalidraw");+ if (!apiRef || !excalidraw) {+ throw new Error("Expected the editor.");+ }+ apiRef.current = { getSceneElements: () => [ { type: "image", locked: true } ] };+ const listener = vi.fn();+ excalidraw.addEventListener("wheel", listener);++ excalidraw.dispatchEvent(new WheelEvent("wheel", { bubbles: true, deltaY: 100 }));++ expect(listener).not.toHaveBeenCalled();+ apiRef.current = null;+ });+it("centers the drawing after a change of fullscreen, hidden until then", async () => {await mount();const root = box.querySelector(".canvas-drawing-editor");diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsxindex 3aaab61747..d6edf42fab 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx@@ -20,7 +20,7 @@ import { useEditableEmbed } from "../text/editable_embed";import { CanvasEditor } from "./Canvas";import CanvasDrawingMenu from "./CanvasDrawingMenu";import CanvasEmbedTools from "./CanvasEmbedTools";-import { getBoundedView } from "./drawing_view";+import { getBoundedView, getBoundingImage } from "./drawing_view";import { loadDrawingFiles, parseContent, useCanvasDrawingPersistence } from "./persistence";interface CanvasDrawingProps {@@ -55,6 +55,7 @@ export default function CanvasDrawing({ attachment, editor }: CanvasDrawingPropsconst isRecentering = useRecenteringOnFullscreen(rootRef, apiRef);useSidePanels(rootRef, isEditable);useTopLayerContextMenu(rootRef, apiRef);+ useNoteScrollingWheel(rootRef, apiRef);return (<div@@ -202,6 +203,31 @@ function useRecenteringOnFullscreen(return isRecentering;}+/**+ * Scrolls the note with the wheel over a drawing bounded by a locked image, which the Hand tool+ * pans instead. With Ctrl or Cmd, and in fullscreen, the wheel stays the drawing's.+ */+export function useNoteScrollingWheel(+ rootRef: RefObject<HTMLElement | null>,+ apiRef: RefObject<ExcalidrawImperativeAPI | null>+) {+ useEffect(() => {+ const root = rootRef.current;+ if (!root) return;++ // Runs before Excalidraw's own listener on its container, which prevents the scroll.+ const onWheel = (event: WheelEvent) => {+ const elements = apiRef.current?.getSceneElements();+ if (event.ctrlKey || event.metaKey || document.fullscreenElement?.contains(root)+ || !elements || !getBoundingImage(elements)) return;++ event.stopPropagation();+ };+ root.addEventListener("wheel", onWheel, { capture: true });+ return () => root.removeEventListener("wheel", onWheel, { capture: true });+ }, [ rootRef, apiRef ]);+}+/*** Whether Excalidraw's toolbar reaches over the column of the properties panel, which then starts* below the toolbar instead of at the top of the canvas.diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_view.ts b/apps/client/src/widgets/type_widgets/canvas/drawing_view.tsindex 0138209566..7790d74dfa 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_view.ts+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_view.ts@@ -32,9 +32,9 @@ export function getBoundedView(elements: readonly ExcalidrawElement[],appState: AppState): DrawingView | null {- const area = elements.find((element) => !element.isDeleted);+ const area = getBoundingImage(elements);const { width, height, zoom, scrollX, scrollY } = appState;- if (area?.type !== "image" || !area.locked || !width || !height) return null;+ if (!area || !width || !height) return null;const [ minX, minY, maxX, maxY ] = getCommonBounds([ area ]);const fitted = fitView([ minX, minY, maxX, maxY ], width, height);@@ -50,6 +50,12 @@ export function getBoundedView(return isInside ? null : view;}+/** The locked image at the bottom of a drawing, which bounds its view, or `null`. */+export function getBoundingImage(elements: readonly ExcalidrawElement[]) {+ const area = elements.find((element) => !element.isDeleted);+ return area?.type === "image" && area.locked ? area : null;+}+const EPSILON = 1e-6;/** The view of a drawing `width` by `height` that shows the box from `minX` to `maxY` whole. */diff --git a/apps/client/src/widgets/type_widgets/canvas/excalidraw_contract.spec.ts b/apps/client/src/widgets/type_widgets/canvas/excalidraw_contract.spec.tsindex 56ab0fdb7c..dbbe9d5c97 100644--- a/apps/client/src/widgets/type_widgets/canvas/excalidraw_contract.spec.ts+++ b/apps/client/src/widgets/type_widgets/canvas/excalidraw_contract.spec.ts@@ -128,6 +128,14 @@ describe("Excalidraw contract of the canvas drawing", () => {expect(handleWheel.match(/this\.translateCanvas\(/g)).toHaveLength(3);expect(handleWheel).not.toContain("this.setState(");});++ it("takes the wheel on its container, after `useNoteScrollingWheel()` on the drawing", () => {+ const source = readBundle(developmentDir, [ ".js" ]).replace(/\s+/g, "");++ expect(source).toContain("addEventListener(this.excalidrawContainerRef.current,"+ + "\"wheel\"/*WHEEL*/,this.handleWheel,{passive:false})");+ expect(source).not.toMatch(/addEventListener\((window|document),"wheel"/);+ });});/** The text of the scripts or stylesheets in `dir`, without its locales and fonts. */diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.htmlindex 4eae4c06e2..acd061268d 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Include Note.html@@ -210,10 +210,12 @@sits at its original size in the middle of the canvas, zoomed so that allof it shows, and is locked so that it cannot be selected or moved by accident.While it is locked, the canvas can be zoomed in on the image but not zoomed- out past it or scrolled beyond its edges. To move or resize the image anyway,- right-click it and choose <em>Unlock</em>. The caption of the image is kept,- but not its size, alignment or link. Only an image on its own line can- be converted, not one inside a paragraph. Undo brings the image back.</li>+ out past it or scrolled beyond its edges. The mouse wheel scrolls the note;+ to move around a zoomed-in image, use the <span class="tn-icon bx bxs-hand"></span> <em>Hand</em> tool.+ To move or resize the image anyway, right-click it and choose <em>Unlock</em>.+ The caption of the image is kept, but not its size, alignment or link.+ Only an image on its own line can be converted, not one inside a paragraph.+ Undo brings the image back.</li><li>An Excalidraw file (<code>.excalidraw</code>) attached with the <span class="tn-icon cke cke-paper-clip"></span><em>Attach file</em> buttonbecomes a drawing canvas too, in a <em>Medium</em> box titled with the filename. It starts with <em>Editable</em> off, so the drawing shows but cannotdiff --git a/docs/User Guide/User Guide/Note Types/Text/Include Note.md b/docs/User Guide/User Guide/Note Types/Text/Include Note.mdindex b813cbed0a..2abba53fa7 100644--- a/docs/User Guide/User Guide/Note Types/Text/Include Note.md+++ b/docs/User Guide/User Guide/Note Types/Text/Include Note.md@@ -120,7 +120,7 @@ An included text note, an included code note and an embedded code file such as aA drawing canvas is an Excalidraw canvas, like the one of a <a class="reference-link" href="../Canvas.md">Canvas</a> note, drawn directly in the text note. It is not a note of its own: the drawing is kept in a `Canvas.excalidraw` [attachment](../../Basic%20Concepts%20and%20Features/Notes/Attachments.md) of the text note and is saved together with the text.* To insert a drawing canvas, open the <span class="tn-icon cke cke-plus"></span> _Insert_ menu of the <a class="reference-link" href="Formatting%20toolbar.md">Formatting toolbar</a> and choose <span class="tn-icon bx bx-pen"></span> _Drawing canvas_, above _Mermaid diagram_. It is also available as _Drawing canvas_ in the <a class="reference-link" href="Slash%20Commands.md">Slash Commands</a>. The canvas is added empty and editable, in a _Medium_ box with its title hidden, and takes the focus, ready for drawing.-* To draw or write over an image, select the image and press the <span class="tn-icon bx bxs-pen"></span> _Convert image to a drawing canvas_ button of its toolbar. The image is replaced by an editable drawing canvas in a _Medium_ box, with its title hidden and the focus in it. The image sits at its original size in the middle of the canvas, zoomed so that all of it shows, and is locked so that it cannot be selected or moved by accident. While it is locked, the canvas can be zoomed in on the image but not zoomed out past it or scrolled beyond its edges. To move or resize the image anyway, right-click it and choose _Unlock_. The caption of the image is kept, but not its size, alignment or link. Only an image on its own line can be converted, not one inside a paragraph. Undo brings the image back.+* To draw or write over an image, select the image and press the <span class="tn-icon bx bxs-pen"></span> _Convert image to a drawing canvas_ button of its toolbar. The image is replaced by an editable drawing canvas in a _Medium_ box, with its title hidden and the focus in it. The image sits at its original size in the middle of the canvas, zoomed so that all of it shows, and is locked so that it cannot be selected or moved by accident. While it is locked, the canvas can be zoomed in on the image but not zoomed out past it or scrolled beyond its edges. The mouse wheel scrolls the note; to move around a zoomed-in image, use the <span class="tn-icon bx bxs-hand"></span> _Hand_ tool. To move or resize the image anyway, right-click it and choose _Unlock_. The caption of the image is kept, but not its size, alignment or link. Only an image on its own line can be converted, not one inside a paragraph. Undo brings the image back.* An Excalidraw file (`.excalidraw`) attached with the <span class="tn-icon cke cke-paper-clip"></span>_Attach file_ button becomes a drawing canvas too, in a _Medium_ box titled with the file name. It starts with _Editable_ off, so the drawing shows but cannot be changed until _Editable_ is turned on (see below).* To draw on a canvas that does not have the focus, click it first. Until then, the mouse wheel and touch gestures scroll the note. The canvas then has the tools, menus and keyboard shortcuts of a Canvas note. The toolbars, the menus and the properties of the selected shapes show only while the canvas has the focus, so elsewhere the drawing reads like a picture in the text.* A new drawing has the background color of the note. A background chosen from the Excalidraw menu replaces it.