Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor/canvas: reorganize the canvas menu

Adorian Dorandd228b8dc155
diff --git a/apps/client/src/stylesheets/theme-next/notes/canvas.css b/apps/client/src/stylesheets/theme-next/notes/canvas.css
index 28603d7db0..4ef2e65bef 100644
--- a/apps/client/src/stylesheets/theme-next/notes/canvas.css
+++ b/apps/client/src/stylesheets/theme-next/notes/canvas.css
@@ -130,8 +130,11 @@
/* Main menu */
-/* Hide separators - no longer needed as the menu group headers feature a delimiter line */
-.excalidraw .Island.dropdown-menu-container>div:not(:has(>*)) {
+/* Hides a separator before a group header or the canvas background label, which have a delimiter
+ line of their own. */
+.excalidraw .Island.dropdown-menu-container > div:not(:has(> *)):has(
+ + .dropdown-menu-group, + div > [data-testid="canvas-background-label"]
+) {
display: none;
}
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
index 1fd861f651..250adba24a 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css
@@ -74,6 +74,12 @@
.excalidraw:has(.App-menu__left) .layer-ui__wrapper__footer-right {
visibility: hidden;
}
+
+ /* Matches the 1rem of the icons of Excalidraw's own menu items. */
+ .dropdown-menu-item__icon > .tn-icon {
+ display: block;
+ font-size: 1rem;
+ }
}
/* Excalidraw's toolbars, menus, panels and selection show only while the drawing has the focus.
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
index 9773f743e9..9c6c7ff62d 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
@@ -12,6 +12,7 @@ import type { AttachmentEditor } from "../../../services/content_renderer";
import options from "../../../services/options";
import { useColorScheme } from "../../react/hooks";
import { CanvasEditor } from "./Canvas";
+import CanvasDrawingMenu from "./CanvasDrawingMenu";
import CanvasEmbedTools from "./CanvasEmbedTools";
import { getInlineFiles, parseContent, useCanvasDrawingPersistence } from "./persistence";
@@ -49,6 +50,7 @@ export default function CanvasDrawing({ attachment, editor }: CanvasDrawingProps
persistence={persistence}
isEmbedded
>
+ <CanvasDrawingMenu apiRef={apiRef} isEditable={isEditable} />
{isEditable && <CanvasEmbedTools rootRef={rootRef} apiRef={apiRef} />}
</CanvasEditor>
</div>
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawingMenu.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawingMenu.spec.tsx
new file mode 100644
index 0000000000..46dab31397
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawingMenu.spec.tsx
@@ -0,0 +1,63 @@
+import type { ExcalidrawImperativeAPI } from "@excalidraw/excalidraw/types";
+import type { ComponentChildren } from "preact";
+import { render } from "preact";
+import { act } from "preact/test-utils";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+/** Renders Excalidraw's own entries by name, and ours as buttons. */
+const MainMenu = vi.hoisted(() => Object.assign(
+ ({ children }: { children: ComponentChildren }) => <div className="menu">{children}</div>,
+ {
+ Item: ({ onSelect, children }: { onSelect: () => void; children: ComponentChildren }) => (
+ <button type="button" onClick={onSelect}>{children}</button>
+ ),
+ Separator: () => <hr />,
+ DefaultItems: Object.fromEntries([ "LoadScene", "SaveAsImage", "SearchMenu", "Help" ]
+ .map((name) => [ name, () => <span>{name}</span> ]))
+ }
+));
+vi.mock("@excalidraw/excalidraw", () => ({
+ MainMenu,
+ useI18n: () => ({ t: (key: string) => `t:${key}`, langCode: "en" })
+}));
+
+const { default: CanvasDrawingMenu } = await import("./CanvasDrawingMenu");
+
+describe("CanvasDrawingMenu", () => {
+ let container: HTMLElement;
+ let toggleSidebar: ReturnType<typeof vi.fn>;
+
+ beforeEach(() => {
+ toggleSidebar = vi.fn();
+ container = document.createElement("div");
+ document.body.appendChild(container);
+ });
+
+ afterEach(() => {
+ render(null, container);
+ container.remove();
+ });
+
+ async function mount(isEditable: boolean) {
+ const apiRef = { current: { toggleSidebar } as unknown as ExcalidrawImperativeAPI };
+ await act(async () => {
+ render(<CanvasDrawingMenu apiRef={apiRef} isEditable={isEditable} />, container);
+ });
+ return [ ...container.querySelector(".menu")?.children ?? [] ]
+ .map((item) => item.tagName === "HR" ? "|" : item.textContent);
+ }
+
+ it("offers opening, exporting, finding, the library and help", async () => {
+ expect(await mount(true)).toEqual([
+ "LoadScene", "SaveAsImage", "|", "SearchMenu", "t:toolBar.library", "Help"
+ ]);
+
+ container.querySelector("button")?.click();
+ expect(toggleSidebar).toHaveBeenCalledWith({ name: "default" });
+ });
+
+ it("leaves the library out of a drawing that cannot change", async () => {
+ expect(await mount(false))
+ .toEqual([ "LoadScene", "SaveAsImage", "|", "SearchMenu", "Help" ]);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawingMenu.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawingMenu.tsx
new file mode 100644
index 0000000000..c57121a4bc
--- /dev/null
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawingMenu.tsx
@@ -0,0 +1,37 @@
+import { MainMenu } from "@excalidraw/excalidraw";
+import type { ExcalidrawImperativeAPI } from "@excalidraw/excalidraw/types";
+import type { RefObject } from "preact";
+
+import Icon from "../../react/Icon";
+import { useExcalidrawTranslation } from "./CanvasEmbedTools";
+
+/** The name of Excalidraw's default sidebar, whose first tab is the library. */
+export const LIBRARY_SIDEBAR = "default";
+
+interface CanvasDrawingMenuProps {
+ apiRef: RefObject<ExcalidrawImperativeAPI>;
+ isEditable: boolean;
+}
+
+/** The main menu of Excalidraw in a canvas drawing. Renders inside `<Excalidraw>`. */
+export default function CanvasDrawingMenu({ apiRef, isEditable }: CanvasDrawingMenuProps) {
+ const { t } = useExcalidrawTranslation();
+
+ return (
+ <MainMenu>
+ <MainMenu.DefaultItems.LoadScene />
+ <MainMenu.DefaultItems.SaveAsImage />
+ <MainMenu.Separator />
+ <MainMenu.DefaultItems.SearchMenu />
+ {isEditable && (
+ <MainMenu.Item
+ icon={<Icon icon="bx bx-library" />}
+ onSelect={() => apiRef.current?.toggleSidebar({ name: LIBRARY_SIDEBAR })}
+ >
+ {t("toolBar.library")}
+ </MainMenu.Item>
+ )}
+ <MainMenu.DefaultItems.Help />
+ </MainMenu>
+ );
+}
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx
index 85feb304a1..73c4294139 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx
@@ -52,8 +52,7 @@ interface CanvasEmbedToolsProps {
* drawing. Renders inside `<Excalidraw>`, for its translations.
*/
export default function CanvasEmbedTools({ rootRef, apiRef }: CanvasEmbedToolsProps) {
- // Excalidraw types the key of `t()` as a union that TypeScript cannot check a string against.
- const { t, langCode } = useI18n() as unknown as { t: Translate; langCode: string };
+ const { t, langCode } = useExcalidrawTranslation();
const [ tools ] = useState(() => new CanvasTools(rootRef));
useEffect(() => tools.setTranslate(t), [ tools, t, langCode ]);
@@ -74,6 +73,12 @@ export default function CanvasEmbedTools({ rootRef, apiRef }: CanvasEmbedToolsPr
return null;
}
+/** Excalidraw's `t()` and language, for a component that renders inside `<Excalidraw>`. */
+export function useExcalidrawTranslation() {
+ // Excalidraw types the key of `t()` as a union that TypeScript cannot check a string against.
+ return useI18n() as unknown as { t: Translate; langCode: string };
+}
+
/** Excalidraw's tools and commands, as buttons of the toolbar of the embed with the drawing. */
export class CanvasTools implements ContentEmbedToolProvider {
private api: ExcalidrawImperativeAPI | null = null;
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.ts
index 72d1689695..25f82c4db6 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
@@ -4,17 +4,18 @@ import { dirname, join } from "node:path";
import { describe, expect, it, vi } from "vitest";
-vi.mock("@excalidraw/excalidraw", () => ({ useI18n: vi.fn() }));
+vi.mock("@excalidraw/excalidraw", () => ({ MainMenu: {}, useI18n: vi.fn() }));
const {
HISTORY_ACTIONS, LOCK, MAX_ZOOM, MIN_ZOOM, TOOLS, ZOOM_STEP, ZOOMS
} = await import("./CanvasEmbedTools");
+const { LIBRARY_SIDEBAR } = await import("./CanvasDrawingMenu");
/** The classes of Trilium that `CanvasDrawing.css` selects. Every other class is Excalidraw's. */
const TRILIUM_CLASSES = new Set([
"note-detail", "canvas-drawing", "canvas-drawing-editor", "canvas-render",
"canvas-drawing-picture", "toolbar-over-panel", "include-note", "include-note-body",
- "include-note-content", "active"
+ "include-note-content", "active", "tn-icon"
]);
/** The classes of Excalidraw that `CanvasDrawing.tsx` and `CanvasEmbedTools.tsx` look up. */
@@ -79,6 +80,12 @@ describe("Excalidraw contract of the canvas drawing", () => {
expect(source).toContain("baseScrollX=appState.scrollX+(appLayerX-appLayerX/currentZoom)");
expect(source).toContain("zoomOffsetScrollX=-(appLayerX-appLayerX/nextZoom)");
});
+
+ it("opens the library in the sidebar that the main menu names", () => {
+ const source = readBundle(developmentDir, [ ".js" ]).replace(/\s+/g, "");
+
+ expect(source).toContain(`varDEFAULT_SIDEBAR={name:"${LIBRARY_SIDEBAR}"`);
+ });
});
/** The text of the scripts or stylesheets in `dir`, without its locales and fonts. */