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 for every canvas widget
Adorian Doran44d3b735e29a
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsxindex abb6358060..20a1ece583 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx@@ -487,20 +487,15 @@ 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);+ useNoteScrollingWheel(rootRef);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));@@ -536,26 +531,20 @@ describe("useNoteScrollingWheel", () => {return listener.mock.calls.length > 0;}- it("scrolls the note with the wheel over a locked image, which zooms with Ctrl or Cmd", () => {+ it("gives the wheel over the drawing to the note, with any modifier key", () => {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);+ expect(reachesDrawing({ ctrlKey: true })).toBe(false);+ expect(reachesDrawing({ metaKey: true })).toBe(false);});- it("leaves the wheel to an unlocked image, and to a drawing in fullscreen", () => {- elements = [ { ...IMAGE, locked: false } ];- expect(reachesDrawing()).toBe(true);-- elements = [ IMAGE ];+ it("leaves the wheel to a drawing in fullscreen", () => {Object.defineProperty(document, "fullscreenElement", {value: container.firstElementChild,configurable: true});expect(reachesDrawing()).toBe(true);+ expect(reachesDrawing({ ctrlKey: true })).toBe(true);});});@@ -644,21 +633,18 @@ describe("CanvasDrawing", () => {expect(getEditingState()).toEqual({ isReadOnly: false, menu: true, tools: true });});- it("scrolls the note with the wheel over an image canvas", async () => {+ it("gives the wheel over the drawing to the note", async () => {await mount();- const { apiRef } = canvasEditorProps.mock.lastCall?.[0] as MockCanvasEditorProps;const excalidraw = box.querySelector(".excalidraw");- if (!apiRef || !excalidraw) {+ if (!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 () => {diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsxindex d6edf42fab..996aa2e7fe 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, getBoundingImage } from "./drawing_view";+import { getBoundedView } from "./drawing_view";import { loadDrawingFiles, parseContent, useCanvasDrawingPersistence } from "./persistence";interface CanvasDrawingProps {@@ -55,7 +55,7 @@ export default function CanvasDrawing({ attachment, editor }: CanvasDrawingPropsconst isRecentering = useRecenteringOnFullscreen(rootRef, apiRef);useSidePanels(rootRef, isEditable);useTopLayerContextMenu(rootRef, apiRef);- useNoteScrollingWheel(rootRef, apiRef);+ useNoteScrollingWheel(rootRef);return (<div@@ -204,28 +204,24 @@ function useRecenteringOnFullscreen(}/**- * 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.+ * Gives the wheel over the drawing to the note, as over the text around it, so that the drawing+ * zooms and pans with the buttons and the Hand tool of its toolbar only. In fullscreen, where+ * Excalidraw shows its own controls, the wheel stays the drawing's.*/-export function useNoteScrollingWheel(- rootRef: RefObject<HTMLElement | null>,- apiRef: RefObject<ExcalidrawImperativeAPI | null>-) {+export function useNoteScrollingWheel(rootRef: RefObject<HTMLElement | 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();+ if (!document.fullscreenElement?.contains(root)) {+ event.stopPropagation();+ }};root.addEventListener("wheel", onWheel, { capture: true });return () => root.removeEventListener("wheel", onWheel, { capture: true });- }, [ rootRef, apiRef ]);+ }, [ rootRef ]);}/**diff --git a/apps/client/src/widgets/type_widgets/canvas/drawing_view.ts b/apps/client/src/widgets/type_widgets/canvas/drawing_view.tsindex 7790d74dfa..24d4b701bd 100644--- a/apps/client/src/widgets/type_widgets/canvas/drawing_view.ts+++ b/apps/client/src/widgets/type_widgets/canvas/drawing_view.ts@@ -51,7 +51,7 @@ export function getBoundedView(}/** The locked image at the bottom of a drawing, which bounds its view, or `null`. */-export function getBoundingImage(elements: readonly ExcalidrawElement[]) {+function getBoundingImage(elements: readonly ExcalidrawElement[]) {const area = elements.find((element) => !element.isDeleted);return area?.type === "image" && area.locked ? area : null;}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 acd061268d..8cba6e6d91 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,22 +210,24 @@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. 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>+ 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><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 cannotbe changed until <em>Editable</em> is turned on (see below).</li><li>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.</li>+ then, 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.</li>+ <li>The mouse wheel and the touchpad scroll the note over a canvas too, as+ over the text. To zoom, use the zoom buttons of the canvas toolbar; to+ move around the drawing, use the <span class="tn-icon bx bxs-hand"></span> <em>Hand</em> tool,+ or drag it while it is not editable. In fullscreen, the mouse wheel moves+ and zooms the drawing instead.</li><li>A new drawing has the background color of the note. A background chosenfrom the Excalidraw menu replaces it.</li><li>To protect a finished drawing from accidental changes, select it and turndiff --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 2abba53fa7..32764a2b0d 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,9 +120,10 @@ 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. 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.+* 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.* 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.+* To draw on a canvas that does not have the focus, click it first. Until then, 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.+* The mouse wheel and the touchpad scroll the note over a canvas too, as over the text. To zoom, use the zoom buttons of the canvas toolbar; to move around the drawing, use the <span class="tn-icon bx bxs-hand"></span> _Hand_ tool, or drag it while it is not editable. In fullscreen, the mouse wheel moves and zooms the drawing instead.* A new drawing has the background color of the note. A background chosen from the Excalidraw menu replaces it.* To protect a finished drawing from accidental changes, select it and turn off the <span class="tn-icon bx bx-edit-alt"></span> _Editable_ button at the start of its toolbar, or uncheck _Editable_ in its <span class="tn-icon cke cke-three-vertical-dots"></span> _More actions_ menu. The drawing can then be panned and zoomed but not changed, and its toolbar holds only the zoom buttons. To draw again, turn _Editable_ back on.* The box sizes, resizing, the caption and the title work as for an included note, but the toolbar of a drawing leaves the box size, _Show title_ and _Convert to link_ to its _More actions_ menu.