Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/canvas: relocate the sidebar outside of the canvas area

Adorian Doran25c575a863fd
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
index 1ba4961b86..77586ff485 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
@@ -85,6 +85,24 @@
opacity: 0;
}
+/* The properties panel beside the drawing, in the top layer, placed by `useSidePanel()`. */
+.canvas-drawing-editor[data-side-panel] {
+ /* Resets the user agent style of a popover as well as Excalidraw's own placement. */
+ .Island.App-menu__left[popover] {
+ position: fixed;
+ inset: var(--side-panel-top) auto auto var(--side-panel-left);
+ margin: 0;
+ border: none;
+ /* Overrides the height limit that Excalidraw sets inline. */
+ max-height: var(--side-panel-max-height) !important;
+ color: inherit;
+ }
+
+ .layer-ui__wrapper__footer-right {
+ visibility: inherit;
+ }
+}
+
/* The tools are buttons of the embed's toolbar, which fullscreen hides. */
.include-note-content:not(:fullscreen) .canvas-drawing .excalidraw {
.App-toolbar-container > .App-toolbar,
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
index 1bae2391fd..9de8e23037 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
@@ -21,10 +21,13 @@ globalThis.ResizeObserver = class {
resizeCallback = callback;
}
observe() {}
+ unobserve() {}
disconnect() {}
} as unknown as typeof ResizeObserver;
-const { default: CanvasDrawing, useIsToolbarOverPanel } = await import("./CanvasDrawing");
+const {
+ default: CanvasDrawing, useIsToolbarOverPanel, useSidePanel
+} = await import("./CanvasDrawing");
/** Horizontal edges by class name; happy-dom computes no layout. */
const edges: Record<string, { left: number; right: number }> = {
@@ -91,6 +94,162 @@ describe("useIsToolbarOverPanel", () => {
});
});
+describe("useSidePanel", () => {
+ /** Boxes by class name, in pixels; happy-dom computes no layout. */
+ const rects: Record<string, { left: number; top: number; right: number; bottom: number }> = {};
+ let container: HTMLElement;
+ let showPopover: ReturnType<typeof vi.fn>;
+
+ function SidePanelProbe({ isEnabled }: { isEnabled: boolean }) {
+ const rootRef = useRef<HTMLDivElement>(null);
+ useSidePanel(rootRef, isEnabled);
+ return (
+ <div className="scrolling-container">
+ <div className="include-note-body active">
+ <div ref={rootRef} className="drawing">
+ <div className="App-menu__left" />
+ </div>
+ </div>
+ </div>
+ );
+ }
+
+ beforeEach(() => {
+ // A window of 1280 × 900 and a 1rem of 16px. The note area of the split is 750 × 700,
+ // from 240 to 990 and from 100 to 800.
+ Object.defineProperties(document.documentElement, {
+ clientWidth: { value: 1280, configurable: true },
+ clientHeight: { value: 900, configurable: true }
+ });
+ rects["scrolling-container"] = { left: 240, top: 100, right: 1000, bottom: 800 };
+ rects.drawing = { left: 340, top: 200, right: 840, bottom: 520 };
+ vi.spyOn(HTMLElement.prototype, "getBoundingClientRect")
+ .mockImplementation(function (this: HTMLElement) {
+ const { left, top, right, bottom } = rects[this.className] ?? rects.drawing;
+ const size = { width: right - left, height: bottom - top };
+ return { left, top, right, bottom, ...size } as DOMRect;
+ });
+ vi.spyOn(window, "getComputedStyle")
+ .mockReturnValue({ fontSize: "16px" } as CSSStyleDeclaration);
+ // happy-dom has no Popover API.
+ showPopover = vi.fn();
+ Object.defineProperty(HTMLElement.prototype, "showPopover", {
+ value: showPopover,
+ configurable: true
+ });
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ render(null, container);
+ container.remove();
+ vi.restoreAllMocks();
+ Reflect.deleteProperty(HTMLElement.prototype, "showPopover");
+ Reflect.deleteProperty(document.documentElement, "clientWidth");
+ Reflect.deleteProperty(document.documentElement, "clientHeight");
+ Reflect.deleteProperty(document, "fullscreenElement");
+ });
+
+ async function mount(panelHeight: number) {
+ await act(async () => render(<SidePanelProbe isEnabled={false} />, container));
+ const viewport = container.querySelector(".scrolling-container");
+ const body = container.querySelector(".include-note-body");
+ const root = container.querySelector<HTMLElement>(".drawing");
+ const panel = container.querySelector<HTMLElement>(".App-menu__left");
+ if (!viewport || !body || !root || !panel) {
+ throw new Error("Expected the probe.");
+ }
+ Object.defineProperties(viewport, {
+ clientWidth: { value: 750 },
+ clientHeight: { value: 700 }
+ });
+ Object.defineProperties(panel, {
+ offsetWidth: { value: 200 },
+ offsetHeight: { value: panelHeight, configurable: true }
+ });
+
+ await act(async () => render(<SidePanelProbe isEnabled />, container));
+ return { viewport, body, root, panel };
+ }
+
+ /** The position of the panel beside the drawing, or `false` while it is in the drawing. */
+ function getPlacement(root: HTMLElement, panel: HTMLElement) {
+ const isPlaced = root.hasAttribute("data-side-panel");
+ expect(panel.getAttribute("popover")).toBe(isPlaced ? "manual" : null);
+ return isPlaced && [ "left", "top", "max-height" ]
+ .map((name) => root.style.getPropertyValue(`--side-panel-${name}`));
+ }
+
+ it("places the panel level with the drawing's top, on its right or else its left", async () => {
+ const { viewport, root, panel } = await mount(300);
+ expect(getPlacement(root, panel)).toEqual([ "848px", "200px", "884px" ]);
+
+ rects.drawing = { left: 340, top: 200, right: 1100, bottom: 520 };
+ viewport.dispatchEvent(new Event("scroll"));
+ expect(getPlacement(root, panel)).toEqual([ "132px", "200px", "884px" ]);
+ expect(showPopover).toHaveBeenCalledTimes(1);
+ });
+
+ it("keeps the panel inside the window while the note scrolls", async () => {
+ const { viewport, root, panel } = await mount(300);
+
+ rects.drawing = { left: 340, top: -50, right: 840, bottom: 270 };
+ viewport.dispatchEvent(new Event("scroll"));
+ expect(getPlacement(root, panel)).toEqual([ "848px", "8px", "884px" ]);
+
+ rects.drawing = { left: 340, top: 700, right: 840, bottom: 1020 };
+ viewport.dispatchEvent(new Event("scroll"));
+ expect(getPlacement(root, panel)).toEqual([ "848px", "592px", "884px" ]);
+
+ Object.defineProperty(panel, "offsetHeight", { value: 1000 });
+ viewport.dispatchEvent(new Event("scroll"));
+ expect(getPlacement(root, panel)).toEqual([ "848px", "8px", "884px" ]);
+ });
+
+ it("keeps the panel in the drawing without room beside it or out of view", async () => {
+ const { viewport, root, panel } = await mount(300);
+
+ rects.drawing = { left: 100, top: 200, right: 1100, bottom: 520 };
+ viewport.dispatchEvent(new Event("scroll"));
+ expect(getPlacement(root, panel)).toBe(false);
+
+ // Inside the window, but scrolled out of the note area.
+ rects.drawing = { left: 340, top: 810, right: 840, bottom: 1130 };
+ viewport.dispatchEvent(new Event("scroll"));
+ expect(getPlacement(root, panel)).toBe(false);
+
+ rects.drawing = { left: 340, top: -300, right: 840, bottom: 90 };
+ viewport.dispatchEvent(new Event("scroll"));
+ expect(getPlacement(root, panel)).toBe(false);
+
+ rects.drawing = { left: 340, top: 200, right: 840, bottom: 520 };
+ viewport.dispatchEvent(new Event("scroll"));
+ expect(getPlacement(root, panel)).not.toBe(false);
+ expect(showPopover).toHaveBeenCalledTimes(2);
+
+ await act(async () => render(<SidePanelProbe isEnabled={false} />, container));
+ expect(getPlacement(root, panel)).toBe(false);
+ });
+
+ it("keeps the panel in the drawing without the focus or in fullscreen", async () => {
+ const { body, root, panel } = await mount(300);
+
+ await act(async () => body.classList.remove("active"));
+ expect(getPlacement(root, panel)).toBe(false);
+
+ await act(async () => body.classList.add("active"));
+ expect(getPlacement(root, panel)).not.toBe(false);
+
+ Object.defineProperty(document, "fullscreenElement", {
+ value: container,
+ configurable: true
+ });
+ document.dispatchEvent(new Event("fullscreenchange"));
+ expect(getPlacement(root, panel)).toBe(false);
+ });
+});
+
describe("CanvasDrawing", () => {
let box: HTMLElement;
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
index 043f10f00d..9773f743e9 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
@@ -35,6 +35,7 @@ export default function CanvasDrawing({ attachment, editor }: CanvasDrawingProps
);
const isToolbarOverPanel = useIsToolbarOverPanel(rootRef);
useFocusFromEmbedBox(rootRef);
+ useSidePanel(rootRef, isEditable);
return (
<div
@@ -135,3 +136,152 @@ export function useIsToolbarOverPanel(rootRef: RefObject<HTMLElement>) {
return isOver;
}
+
+/** The space between the drawing, the properties panel and the edges of the window, in rem. */
+const SIDE_PANEL_GAP_REM = 0.5;
+
+/**
+ * Shows Excalidraw's properties panel beside the drawing while the drawing has the focus, in the
+ * top layer so that no clip or stacking of the note covers it. Sets `data-side-panel` on the root
+ * while the panel is there, and its position in `--side-panel-*`.
+ */
+export function useSidePanel(rootRef: RefObject<HTMLElement>, isEnabled: boolean) {
+ useEffect(() => {
+ const root = rootRef.current;
+ const viewport = root?.closest<HTMLElement>(".scrolling-container");
+ const embedBody = root?.closest<HTMLElement>(".include-note-body");
+ if (!root || !viewport || !embedBody || !isEnabled) return;
+
+ let panel: HTMLElement | null = null;
+
+ const update = () => {
+ // `ContentEmbed` marks the body active while the drawing has the focus.
+ const isShown = embedBody.classList.contains("active")
+ && !document.fullscreenElement?.contains(root);
+ const position = panel && isShown ? getSidePanelPosition(root, viewport, panel) : null;
+
+ root.toggleAttribute("data-side-panel", !!position);
+ if (position) {
+ root.style.setProperty("--side-panel-left", `${position.left}px`);
+ root.style.setProperty("--side-panel-top", `${position.top}px`);
+ root.style.setProperty("--side-panel-max-height", `${position.maxHeight}px`);
+ }
+ setTopLayer(panel, !!position);
+ };
+
+ const resizeObserver = new ResizeObserver(update);
+ resizeObserver.observe(root);
+ resizeObserver.observe(viewport);
+ const activeObserver = new MutationObserver(update);
+ activeObserver.observe(embedBody, { attributes: true, attributeFilter: [ "class" ] });
+
+ // The panel mounts when a shape is selected or a tool is picked, and its height follows
+ // the properties of the selection.
+ const watchPanel = () => {
+ const current = root.querySelector<HTMLElement>(".App-menu__left");
+ if (current === panel) return;
+
+ if (panel) resizeObserver.unobserve(panel);
+ panel = current;
+ if (panel) resizeObserver.observe(panel);
+ update();
+ };
+ const mutationObserver = new MutationObserver(watchPanel);
+ mutationObserver.observe(root, { childList: true, subtree: true });
+ viewport.addEventListener("scroll", update, { passive: true });
+ document.addEventListener("fullscreenchange", update);
+ watchPanel();
+ update();
+
+ return () => {
+ resizeObserver.disconnect();
+ activeObserver.disconnect();
+ mutationObserver.disconnect();
+ viewport.removeEventListener("scroll", update);
+ document.removeEventListener("fullscreenchange", update);
+ root.removeAttribute("data-side-panel");
+ setTopLayer(panel, false);
+ };
+ }, [ rootRef, isEnabled ]);
+}
+
+/**
+ * The position of `panel` beside the drawing `root`: on its right when the window has room there,
+ * otherwise on its left, level with the top of the drawing and inside the window. `null` when
+ * neither side has room or the drawing is scrolled out of `viewport`.
+ */
+function getSidePanelPosition(root: HTMLElement, viewport: HTMLElement, panel: HTMLElement) {
+ const rem = parseFloat(getComputedStyle(document.documentElement).fontSize);
+ const gap = SIDE_PANEL_GAP_REM * rem;
+ const drawing = root.getBoundingClientRect();
+ const noteArea = getVisibleArea(viewport);
+ if (drawing.bottom <= noteArea.top || drawing.top >= noteArea.bottom) {
+ return null;
+ }
+
+ const area = getWindowArea();
+ const left = getSidePanelLeft(drawing, area, panel.offsetWidth, gap);
+ if (left === null) {
+ return null;
+ }
+
+ const maxHeight = area.bottom - area.top - 2 * gap;
+ const height = Math.min(panel.offsetHeight, maxHeight);
+ const top = Math.max(area.top + gap, Math.min(drawing.top, area.bottom - gap - height));
+ return { left, top, maxHeight };
+}
+
+/**
+ * Moves `panel` into the top layer, or back into the drawing. The `popover` attribute is present
+ * exactly while the panel is in the top layer; removing it hides the popover.
+ */
+function setTopLayer(panel: HTMLElement | null, isMoved: boolean) {
+ if (!panel || isMoved === panel.hasAttribute("popover")) return;
+
+ if (isMoved) {
+ panel.setAttribute("popover", "manual");
+ panel.showPopover();
+ } else {
+ panel.removeAttribute("popover");
+ }
+}
+
+interface VisibleArea {
+ left: number;
+ top: number;
+ right: number;
+ bottom: number;
+}
+
+/** The part of `viewport` that shows the note, without its scrollbars. */
+function getVisibleArea(viewport: HTMLElement): VisibleArea {
+ const rect = viewport.getBoundingClientRect();
+ const left = rect.left + viewport.clientLeft;
+ const top = rect.top + viewport.clientTop;
+ return {
+ left,
+ top,
+ right: left + viewport.clientWidth,
+ bottom: top + viewport.clientHeight
+ };
+}
+
+/** The window, without its scrollbars. */
+function getWindowArea(): VisibleArea {
+ const { clientWidth, clientHeight } = document.documentElement;
+ return { left: 0, top: 0, right: clientWidth, bottom: clientHeight };
+}
+
+/**
+ * The left edge of a panel `width` wide in the margin right of `drawing`, else in the margin left
+ * of it, or `null` when neither margin fits the panel with a `gap` on both sides.
+ */
+function getSidePanelLeft(drawing: DOMRect, area: VisibleArea, width: number, gap: number) {
+ const rightOfDrawing = drawing.right + gap;
+ if (rightOfDrawing + width + gap <= area.right) {
+ return rightOfDrawing;
+ }
+
+ const leftOfDrawing = drawing.left - gap - width;
+ return leftOfDrawing >= area.left + gap ? leftOfDrawing : null;
+}