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/canvas: tweak the toolbar
Adorian Doran382225068602
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.css b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.cssnew file mode 100644index 0000000000..2fb4963ef4--- /dev/null+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.css@@ -0,0 +1,7 @@+/* The zoom level keeps the width of "3000%", the largest zoom, in a CKEditor text button. */+.ck.ck-toolbar .ck.ck-button.canvas-drawing-zoom-level {+ justify-content: center;+ padding-inline: var(--ck-spacing-small);+ width: calc(6ch + 2 * var(--ck-spacing-small) + 2px);+ font-variant-numeric: tabular-nums;+}diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsxindex e9d6e2c33c..b2b0203b47 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx@@ -18,7 +18,7 @@ const MAIN_TOOL_IDS = ["hand", "selection", "rectangle", "diamond", "ellipse", "arrow", "line", "freedraw", "text","image", "eraser"];-const EXTRA_TOOL_IDS = [ "frame", "embeddable", "laser" ];+const EXTRA_TOOL_IDS = [ "frame", "embeddable" ];const ICON = expect.stringContaining("<svg");describe("CanvasEmbedTools", () => {@@ -105,7 +105,13 @@ describe("CanvasEmbedTools", () => {isEnabled: true,group: "zoom"},- { id: "resetZoom", label: "t:buttons.resetZoom (100%)", icon: ICON, group: "zoom" },+ {+ id: "resetZoom",+ label: "t:buttons.resetZoom",+ text: "100%",+ class: "canvas-drawing-zoom-level",+ group: "zoom"+ },{ id: "zoomIn", label: "t:buttons.zoomIn", icon: ICON, isEnabled: true, group: "zoom" }]);@@ -157,15 +163,16 @@ describe("CanvasEmbedTools", () => {expect(listener).toHaveBeenCalledTimes(2);expect(getOn()).toEqual([ "lock", "rectangle" ]);- api.change({ activeTool: { type: "laser", locked: true } });+ api.change({ activeTool: { type: "frame", locked: true } });expect(listener).toHaveBeenCalledTimes(3);- expect(getOn()).toEqual([ "lock", "moreTools", "laser" ]);+ expect(getOn()).toEqual([ "lock", "moreTools", "frame" ]);api.change({ zoom: { value: 30 } });expect(listener).toHaveBeenCalledTimes(4);expect(getEnabled()).toEqual(["undo:true", "redo:false", "zoomOut:true", "resetZoom:true", "zoomIn:false"]);+ expect(provider.getTools().find((tool) => tool.id === "resetZoom")?.text).toBe("3000%");await act(async () => {container.querySelector("[data-testid=button-redo]")?.removeAttribute("disabled");@@ -186,8 +193,8 @@ describe("CanvasEmbedTools", () => {expect(api.setActiveTool).toHaveBeenCalledWith({ type: "rectangle" });expect(document.activeElement).toBe(container.querySelector(".excalidraw-container"));- provider.execute("laser");- expect(api.setActiveTool).toHaveBeenLastCalledWith({ type: "laser" });+ provider.execute("frame");+ expect(api.setActiveTool).toHaveBeenLastCalledWith({ type: "frame" });provider.execute("lock");expect(api.updateScene).toHaveBeenLastCalledWith({@@ -214,6 +221,7 @@ describe("CanvasEmbedTools", () => {});provider.execute("magicframe");+ provider.execute("laser");provider.execute("moreTools");expect(api.setActiveTool).toHaveBeenCalledTimes(2);expect(api.updateScene).toHaveBeenCalledTimes(3);diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsxindex 07e7a4d492..a37edd3bcb 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx@@ -1,3 +1,5 @@+import "./CanvasEmbedTools.css";+import { useI18n } from "@excalidraw/excalidraw";import type {AppState, ExcalidrawImperativeAPI, NormalizedZoomValue, ToolType@@ -13,7 +15,6 @@ import drawIcon from "boxicons/svg/regular/bx-pencil.svg?raw";import selectionIcon from "boxicons/svg/regular/bx-pointer.svg?raw";import rectangleIcon from "boxicons/svg/regular/bx-rectangle.svg?raw";import redoIcon from "boxicons/svg/regular/bx-redo.svg?raw";-import resetZoomIcon from "boxicons/svg/regular/bx-reset.svg?raw";import arrowIcon from "boxicons/svg/regular/bx-right-arrow-alt.svg?raw";import squareIcon from "boxicons/svg/regular/bx-square.svg?raw";import undoIcon from "boxicons/svg/regular/bx-undo.svg?raw";@@ -35,9 +36,7 @@ export const MAIN_TOOLS = ["image", "eraser"] as const satisfies readonly ToolType[];/** The tools in the "More tools" menu. */-export const EXTRA_TOOLS = [- "frame", "embeddable", "laser"-] as const satisfies readonly ToolType[];+export const EXTRA_TOOLS = [ "frame", "embeddable" ] as const satisfies readonly ToolType[];const TOOLS = [ ...MAIN_TOOLS, ...EXTRA_TOOLS ];export const LOCK = "lock";/** The button that opens the "More tools" menu, named by Excalidraw's `toolBar.extraTools`. */@@ -211,8 +210,9 @@ export class CanvasTools implements ContentEmbedToolProvider {},{id: "resetZoom",- label: `${this.t("buttons.resetZoom")} (${zoomPercent}%)`,- icon: resetZoomIcon,+ label: this.t("buttons.resetZoom"),+ text: `${zoomPercent}%`,+ class: "canvas-drawing-zoom-level",group: "zoom"},{diff --git a/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts b/apps/client/src/widgets/type_widgets/text/content_embed_tools.tsindex f752358f27..bee3ae19e6 100644--- a/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts+++ b/apps/client/src/widgets/type_widgets/text/content_embed_tools.ts@@ -15,6 +15,8 @@ export interface ContentEmbedTool {group?: string;/** The tools of the menu that the button opens. */children?: ContentEmbedTool[];+ /** A class of the button, for the content to style it. */+ class?: string;}/** The buttons that the content of an embed, such as a canvas drawing, adds to its toolbar. */diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.tsindex ba5727b560..a78687a0b0 100644--- a/packages/ckeditor5/src/augmentation.ts+++ b/packages/ckeditor5/src/augmentation.ts@@ -47,6 +47,8 @@ declare global {group?: string;/** The tools of the menu that the button opens. */children?: ContentEmbedTool[];+ /** A class of the button, for the content to style it. */+ class?: string;}/** The buttons that the content of an embed, such as a canvas drawing, adds to its toolbar. */diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.spec.tsindex c08efba470..f718da3280 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.spec.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.spec.ts@@ -28,6 +28,7 @@ interface Tool {isEnabled?: boolean;group?: string;children?: Tool[];+ class?: string;}const MORE_TOOLS: Tool = {@@ -139,8 +140,9 @@ describe("ContentEmbedTools", () => {.toEqual([ "Undo", false, ICON, "Undo", "Undo", false, false, false ]);expect(getItems(tools)).toEqual([ "1", "2", "|", "Undo" ]);- provider.change([ SELECTION, RECTANGLE, { ...UNDO, isEnabled: true } ]);+ provider.change([ SELECTION, RECTANGLE, { ...UNDO, isEnabled: true, class: "undo" } ]);expect(getButtons(tools)[2]?.isEnabled).toBe(true);+ expect(getButtons(tools)[2]?.element?.classList.contains("undo")).toBe(true);});it("shows the children of a tool in a menu, without taking the focus", () => {@@ -168,10 +170,10 @@ describe("ContentEmbedTools", () => {{ id: "eraser", label: "Eraser", isOn: true },{ id: "laser", label: "Laser pointer", isOn: false }];- provider.change([ RECTANGLE, { ...MORE_TOOLS, isOn: true, children } ]);+ provider.change([ RECTANGLE, { ...MORE_TOOLS, isOn: true, children, class: "more" } ]);expect(tools.views[1]).toBe(menu);expect(getMenuItems()).toEqual([ "Eraser:true", "Laser pointer:false" ]);- expect(menu.buttonView.class).toBe("ck-on");+ expect(menu.buttonView.class).toBe("more ck-on");for (const element of [ menu.buttonView.element, menu.panelView.element ]) {const mousedown = new MouseEvent("mousedown", { bubbles: true, cancelable: true });diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.tsindex 7d1cbcaa47..a370482d57 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts@@ -179,6 +179,7 @@ export class ContentEmbedToolsView extends View {view: button,update: (tool) => button.set({...getButtonFace(tool),+ class: tool.class,isToggleable: tool.isOn !== undefined,isOn: tool.isOn ?? false,isEnabled: tool.isEnabled ?? true@@ -213,9 +214,10 @@ export class ContentEmbedToolsView extends View {view: dropdown,update: (tool) => {// `createDropdown()` binds `isOn` of the button to the dropdown being open.+ const classes = [ tool.class, tool.isOn && "ck-on" ].filter(Boolean);dropdown.buttonView.set({...getButtonFace(tool),- class: tool.isOn ? "ck-on" : undefined+ class: classes.length ? classes.join(" ") : undefined});dropdown.isEnabled = tool.isEnabled ?? true;for (const [ index, child ] of (tool.children ?? []).entries()) {