Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/canvas: tweak UI

Adorian Doran94b83afa6a6d
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
index 51e40539ce..b0e242c2cc 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
@@ -1,3 +1,7 @@
+:where(:root) {
+ --canvas-drawing-background-color: var(--main-background-color);
+}
+
/* The font size of the note detail in pixels. Excalidraw in a canvas note inherits this size. */
@property --canvas-drawing-font-size {
syntax: "<length>";
@@ -11,14 +15,19 @@
.canvas-drawing {
height: 20em;
+ background-color: var(--canvas-drawing-background-color);
/* Resets the typography inherited from the text note. */
- > .canvas-render {
+ > .canvas-drawing-editor {
height: 100%;
font-size: var(--canvas-drawing-font-size);
line-height: var(--bs-body-line-height);
text-align: start;
overflow-wrap: normal;
+
+ > .canvas-render {
+ height: 100%;
+ }
}
/* Resets CKEditor's content style for `hr`, which matches the menu separators. */
@@ -27,6 +36,53 @@
vertical-align: baseline;
background: none;
}
+
+ /* The Library button follows the menu button, in the first column of the top bar. */
+ .excalidraw .App-menu.App-menu_top {
+ > .App-menu_top__left,
+ > .layer-ui__wrapper__top-right {
+ grid-row: 1;
+ grid-column: 1;
+ }
+
+ > .shapes-section {
+ grid-row: 1;
+ grid-column: 2;
+ }
+
+ > .layer-ui__wrapper__top-right {
+ flex-direction: row;
+ justify-content: flex-start;
+ padding-inline-start: calc(var(--lg-button-size) + 0.5rem);
+ }
+ }
+
+ /* The properties panel spans the height of the canvas. */
+ .Island.App-menu__left {
+ top: 0;
+ bottom: 0;
+ /* Overrides the height limit that Excalidraw sets inline. */
+ max-height: none !important;
+ }
+
+ /* Below the toolbar, which is `--lg-button-size` tall plus its padding. */
+ .toolbar-over-panel .Island.App-menu__left {
+ top: calc(var(--lg-button-size) + 1rem);
+ }
+
+ /* The help button is in the corner that the properties panel covers. */
+ .excalidraw:has(.App-menu__left) .layer-ui__wrapper__footer-right {
+ visibility: hidden;
+ }
+}
+
+/* Excalidraw's toolbars, menus, panels and selection show only while the drawing has the focus.
+ `opacity` hides the footer too, which sets `visibility` of its own. */
+.include-note-body:not(.active) .canvas-drawing :is(
+ .layer-ui__wrapper, .excalidraw > .FixedSideContainer, .excalidraw > .App-bottom-bar,
+ canvas.interactive
+) {
+ opacity: 0;
}
.include-note {
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
new file mode 100644
index 0000000000..4b763e086e
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
@@ -0,0 +1,84 @@
+import { render } from "preact";
+import { useRef } from "preact/hooks";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("@excalidraw/excalidraw", () => ({ exportToSvg: vi.fn() }));
+vi.mock("./Canvas", () => ({ CanvasEditor: () => null }));
+
+// happy-dom has no ResizeObserver; the spec runs the callback itself.
+let resizeCallback: (() => void) | undefined;
+globalThis.ResizeObserver = class {
+ constructor(callback: () => void) {
+ resizeCallback = callback;
+ }
+ observe() {}
+ disconnect() {}
+} as unknown as typeof ResizeObserver;
+
+const { useIsToolbarOverPanel } = await import("./CanvasDrawing");
+
+/** Horizontal edges by class name; happy-dom computes no layout. */
+const edges: Record<string, { left: number; right: number }> = {
+ "App-toolbar-container": { left: 200, right: 800 }
+};
+
+function Probe() {
+ const rootRef = useRef<HTMLDivElement>(null);
+ const isOver = useIsToolbarOverPanel(rootRef);
+ return (
+ <div ref={rootRef} className="probe" data-over={String(isOver)}>
+ <div className="App-toolbar-container" />
+ </div>
+ );
+}
+
+describe("useIsToolbarOverPanel", () => {
+ let container: HTMLElement;
+
+ beforeEach(() => {
+ vi.spyOn(HTMLElement.prototype, "getBoundingClientRect").mockImplementation(function (this: HTMLElement) {
+ const box = edges[this.className] ?? { left: 0, right: 0 };
+ return { ...box, top: 0, bottom: 0, width: box.right - box.left, height: 0 } as DOMRect;
+ });
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ render(null, container);
+ container.remove();
+ vi.restoreAllMocks();
+ });
+
+ async function mountPanel(left: number) {
+ edges["App-menu__left"] = { left, right: left + 210 };
+ await act(async () => {
+ render(<Probe />, container);
+ });
+ const root = container.querySelector(".probe");
+ expect(root?.getAttribute("data-over")).toBe("false");
+
+ const panel = document.createElement("div");
+ panel.className = "App-menu__left";
+ await act(async () => {
+ root?.append(panel);
+ await Promise.resolve();
+ });
+ return root;
+ }
+
+ it("tells when the panel that mounts sits under the toolbar", async () => {
+ const root = await mountPanel(700);
+ expect(root?.getAttribute("data-over")).toBe("true");
+ });
+
+ it("leaves the panel at the top while it clears the toolbar, and follows resizes", async () => {
+ const root = await mountPanel(810);
+ expect(root?.getAttribute("data-over")).toBe("false");
+
+ edges["App-menu__left"] = { left: 790, right: 1000 };
+ await act(async () => resizeCallback?.());
+ expect(root?.getAttribute("data-over")).toBe("true");
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
index cfa9a68f23..f56b0c83f5 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
@@ -2,7 +2,9 @@ import "./CanvasDrawing.css";
import { exportToSvg } from "@excalidraw/excalidraw";
import type { ExcalidrawImperativeAPI } from "@excalidraw/excalidraw/types";
-import { useRef } from "preact/hooks";
+import clsx from "clsx";
+import type { RefObject } from "preact";
+import { useEffect, useRef, useState } from "preact/hooks";
import type FAttachment from "../../../entities/fattachment";
import type FNote from "../../../entities/fnote";
@@ -20,6 +22,7 @@ interface CanvasDrawingProps {
/** A canvas drawing saved in an attachment, edited in place inside the note that shows it. */
export default function CanvasDrawing({ attachment, editor }: CanvasDrawingProps) {
+ const rootRef = useRef<HTMLDivElement>(null);
const apiRef = useRef<ExcalidrawImperativeAPI>(null);
const colorScheme = useColorScheme();
const isEditable = !!editor?.canEdit(attachment) && !options.is("databaseReadonly");
@@ -29,15 +32,21 @@ export default function CanvasDrawing({ attachment, editor }: CanvasDrawingProps
apiRef,
colorScheme
);
+ const isToolbarOverPanel = useIsToolbarOverPanel(rootRef);
return (
- <CanvasEditor
- apiRef={apiRef}
- isReadOnly={!isEditable}
- colorScheme={colorScheme}
- persistence={persistence}
- isEmbedded
- />
+ <div
+ ref={rootRef}
+ className={clsx("canvas-drawing-editor", isToolbarOverPanel && "toolbar-over-panel")}
+ >
+ <CanvasEditor
+ apiRef={apiRef}
+ isReadOnly={!isEditable}
+ colorScheme={colorScheme}
+ persistence={persistence}
+ isEmbedded
+ />
+ </div>
);
}
@@ -59,3 +68,37 @@ export async function renderCanvasDrawingPicture(entity: FNote | FAttachment) {
svg.classList.add("canvas-drawing-picture");
return svg;
}
+
+/**
+ * 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.
+ */
+export function useIsToolbarOverPanel(rootRef: RefObject<HTMLElement>) {
+ const [ isOver, setIsOver ] = useState(false);
+
+ useEffect(() => {
+ const root = rootRef.current;
+ if (!root) return;
+
+ // Compares the horizontal edges only, which do not depend on where the panel starts.
+ const update = () => {
+ const toolbar = root.querySelector(".App-toolbar-container")?.getBoundingClientRect();
+ const panel = root.querySelector(".App-menu__left")?.getBoundingClientRect();
+ if (toolbar && panel) {
+ setIsOver(panel.left < toolbar.right && panel.right > toolbar.left);
+ }
+ };
+ const resizeObserver = new ResizeObserver(update);
+ resizeObserver.observe(root);
+ // The panel mounts when a shape is selected or a tool is picked.
+ const mutationObserver = new MutationObserver(update);
+ mutationObserver.observe(root, { childList: true, subtree: true });
+
+ return () => {
+ resizeObserver.disconnect();
+ mutationObserver.disconnect();
+ };
+ }, [ rootRef ]);
+
+ return isOver;
+}
diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.ts b/apps/client/src/widgets/type_widgets/canvas/persistence.ts
index a0675b0e88..ce87f24754 100644
--- a/apps/client/src/widgets/type_widgets/canvas/persistence.ts
+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.ts
@@ -372,7 +372,8 @@ export function useCanvasDrawingPersistence(
return {
elements,
- appState: { ...content.appState, theme },
+ // A drawing shows the background of the note unless one is picked for it.
+ appState: { viewBackgroundColor: "transparent", ...content.appState, theme },
files: getInlineFiles(content)
};
});
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.html
index af5be60d7e..cf5ac8577e 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
@@ -146,7 +146,11 @@
The canvas is added empty, in a <em>Medium</em> box with its title hidden.</li>
<li>To draw, click the canvas 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.</li>
+ 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>A new drawing has the background color of the note. A background chosen
+ from the Excalidraw menu replaces it.</li>
<li>The box sizes, the caption and the title work as for an included note.
In a short box, Excalidraw uses a more compact layout for its toolbars.
For more room, choose <em>Full</em>, or show the title and press <span class="tn-icon bx bx-fullscreen"></span> <em>Fullscreen</em>.</li>
diff --git a/docs/User Guide/User Guide/Note Types/Text/Include Note.md b/docs/User Guide/User Guide/Note Types/Text/Include Note.md
index f26d4a87f1..0bb8291c77 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
@@ -94,7 +94,8 @@ In a _Small_ or _Medium_ box, the image is scaled down to fit the height of the
A 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.json` [attachment](../../Basic%20Concepts%20and%20Features/Notes/Attachments.md) of the text note and is saved together with the text.
* To insert a drawing canvas, press the <span class="tn-icon bx bx-pen"></span> _Insert drawing canvas_ button in the <a class="reference-link" href="Formatting%20toolbar.md">Formatting toolbar</a>, next to the <span class="tn-icon cke cke-paper-clip"></span> _Attach file_ button. The canvas is added empty, in a _Medium_ box with its title hidden.
-* To draw, click the canvas 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.
+* To draw, click the canvas 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.
* The box sizes, the caption and the title work as for an included note. In a short box, Excalidraw uses a more compact layout for its toolbars. For more room, choose _Full_, or show the title and press <span class="tn-icon bx bx-fullscreen"></span> _Fullscreen_.
* Pictures added to the drawing are stored in the same attachment.
* In a [read-only note](../../Basic%20Concepts%20and%20Features/Notes/Read-Only%20Notes.md), the drawing can be viewed but not changed.