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: add icons for the toolbar
Adorian Doranb36a9a728a67
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsxindex d90a639ef5..e9d6e2c33c 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx@@ -15,9 +15,10 @@ const {} = await import("../text/content_embed_tools");const MAIN_TOOL_IDS = [- "hand", "rectangle", "diamond", "ellipse", "arrow", "line", "freedraw", "text", "image"+ "hand", "selection", "rectangle", "diamond", "ellipse", "arrow", "line", "freedraw", "text",+ "image", "eraser"];-const EXTRA_TOOL_IDS = [ "selection", "eraser", "frame", "embeddable", "laser" ];+const EXTRA_TOOL_IDS = [ "frame", "embeddable", "laser" ];const ICON = expect.stringContaining("<svg");describe("CanvasEmbedTools", () => {@@ -72,23 +73,26 @@ describe("CanvasEmbedTools", () => {const tools = provider.getTools();const count = MAIN_TOOL_IDS.length;expect(tools[0]).toEqual({- id: "lock", label: "t:toolBar.lock", text: "1", isOn: false, group: "lock"+ id: "lock", label: "t:toolBar.lock", icon: ICON, isOn: false, group: "lock"});- expect(tools.slice(1, count + 1)).toEqual(MAIN_TOOL_IDS.map((id, index) => ({+ expect(tools.slice(1, count + 1)).toEqual(MAIN_TOOL_IDS.map((id) => ({id,label: `t:toolBar.${id}`,- text: String(index + 2),- isOn: false,+ icon: ICON,+ isOn: id === "selection",group: "tools"})));+ expect(tools.find((tool) => tool.id === "diamond")?.icon).toContain("rotate(45)");expect(tools[count + 1]).toEqual({id: "moreTools",label: "t:toolBar.extraTools",icon: ICON,- isOn: true,+ isOn: false,group: "moreTools",children: EXTRA_TOOL_IDS.map((id) => ({- id, label: `t:toolBar.${id}`, isOn: id === "selection"+ id,+ label: `t:toolBar.${id}`,+ isOn: id === "selection"}))});expect(tools.slice(count + 2)).toEqual([@@ -153,9 +157,9 @@ describe("CanvasEmbedTools", () => {expect(listener).toHaveBeenCalledTimes(2);expect(getOn()).toEqual([ "lock", "rectangle" ]);- api.change({ activeTool: { type: "eraser", locked: true } });+ api.change({ activeTool: { type: "laser", locked: true } });expect(listener).toHaveBeenCalledTimes(3);- expect(getOn()).toEqual([ "lock", "moreTools", "eraser" ]);+ expect(getOn()).toEqual([ "lock", "moreTools", "laser" ]);api.change({ zoom: { value: 30 } });expect(listener).toHaveBeenCalledTimes(4);diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsxindex c83ecef639..07e7a4d492 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx@@ -3,11 +3,23 @@ import type {AppState, ExcalidrawImperativeAPI, NormalizedZoomValue, ToolType} from "@excalidraw/excalidraw/types";import moreToolsIcon from "boxicons/svg/regular/bx-category.svg?raw";+import ellipseIcon from "boxicons/svg/regular/bx-circle.svg?raw";+import eraserIcon from "boxicons/svg/regular/bx-eraser.svg?raw";+import textIcon from "boxicons/svg/regular/bx-font.svg?raw";+import imageIcon from "boxicons/svg/regular/bx-image.svg?raw";+import lockIcon from "boxicons/svg/regular/bx-lock-open-alt.svg?raw";+import lineIcon from "boxicons/svg/regular/bx-minus.svg?raw";+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";import zoomInIcon from "boxicons/svg/regular/bx-zoom-in.svg?raw";import zoomOutIcon from "boxicons/svg/regular/bx-zoom-out.svg?raw";+import handIcon from "boxicons/svg/solid/bxs-hand.svg?raw";import type { RefObject } from "preact";import { useEffect, useState } from "preact/hooks";@@ -19,11 +31,12 @@ import {/** The tools with a button of their own, in the order of their buttons. */export const MAIN_TOOLS = [- "hand", "rectangle", "diamond", "ellipse", "arrow", "line", "freedraw", "text", "image"+ "hand", "selection", "rectangle", "diamond", "ellipse", "arrow", "line", "freedraw", "text",+ "image", "eraser"] as const satisfies readonly ToolType[];/** The tools in the "More tools" menu. */export const EXTRA_TOOLS = [- "selection", "eraser", "frame", "embeddable", "laser"+ "frame", "embeddable", "laser"] as const satisfies readonly ToolType[];const TOOLS = [ ...MAIN_TOOLS, ...EXTRA_TOOLS ];export const LOCK = "lock";@@ -48,6 +61,28 @@ type HistoryAction = typeof HISTORY_ACTIONS[number];/** Excalidraw's `t()`, for a key such as `toolBar.rectangle`. */type Translate = (key: string) => string;+/** Boxicons' square turned by 45°, and scaled down to stay inside the box of the icon. */+const diamondIcon = squareIcon.replace(+ "<path ",+ "<path transform=\"translate(12 12) rotate(45) scale(0.8) translate(-12 -12)\" "+);++/** The icons of the tools, for their buttons and their entries in the "More tools" menu. */+const ICONS: Partial<Record<Tool | typeof LOCK, string>> = {+ [LOCK]: lockIcon,+ hand: handIcon,+ selection: selectionIcon,+ rectangle: rectangleIcon,+ diamond: diamondIcon,+ ellipse: ellipseIcon,+ arrow: arrowIcon,+ line: lineIcon,+ freedraw: drawIcon,+ text: textIcon,+ image: imageIcon,+ eraser: eraserIcon+};+interface CanvasEmbedToolsProps {/** The element of the drawing, inside the embed whose toolbar shows the tools. */rootRef: RefObject<HTMLElement>;@@ -141,14 +176,14 @@ export class CanvasTools implements ContentEmbedToolProvider {{id: LOCK,label: this.t("toolBar.lock"),- text: "1",+ icon: ICONS[LOCK],isOn: activeTool.locked,group: "lock"},- ...MAIN_TOOLS.map((type, index) => ({+ ...MAIN_TOOLS.map((type) => ({id: type,label: this.t(`toolBar.${type}`),- text: String(index + 2),+ icon: ICONS[type],isOn: activeTool.type === type,group: "tools"})),@@ -161,6 +196,7 @@ export class CanvasTools implements ContentEmbedToolProvider {children: EXTRA_TOOLS.map((type) => ({id: type,label: this.t(`toolBar.${type}`),+ icon: ICONS[type],isOn: activeTool.type === type}))},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 b2787fa8bc..c08efba470 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@@ -37,7 +37,7 @@ const MORE_TOOLS: Tool = {isOn: false,group: "more",children: [- { id: "eraser", label: "Eraser", isOn: false },+ { id: "eraser", label: "Eraser", icon: ICON, isOn: false },{ id: "laser", label: "Laser pointer", isOn: true }]};@@ -160,6 +160,8 @@ describe("ContentEmbedTools", () => {...menu.panelView.element?.querySelectorAll(".ck-button") ?? []].map((item) => `${item.textContent}:${item.classList.contains("ck-on")}`);expect(getMenuItems()).toEqual([ "Eraser:false", "Laser pointer:true" ]);+ expect([ ...menu.panelView.element?.querySelectorAll(".ck-button") ?? [] ]+ .map((item) => !!item.querySelector(".ck-button__icon"))).toEqual([ true, false ]);expect(menu.buttonView.class).toBeUndefined();const children = [diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.tsindex d3f263ccb5..7d1cbcaa47 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts@@ -221,6 +221,7 @@ export class ContentEmbedToolsView extends View {for (const [ index, child ] of (tool.children ?? []).entries()) {models[index]?.set({label: child.label,+ icon: child.icon,isOn: child.isOn ?? false,isEnabled: child.isEnabled ?? true});