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 toolbar
Adorian Doran55e9fdf8f5b4
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsxindex b2b0203b47..1233de1255 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.spec.tsx@@ -70,6 +70,9 @@ describe("CanvasEmbedTools", () => {provider.subscribe(listener);expect(getContentEmbedTools(document.createElement("figure"))).toBeNull();+ expect(provider.hiddenToolbarItems).toEqual([+ "contentEmbedBoxSizeDropdown", "toggleContentEmbedTitle", "convertEmbedToLink"+ ]);const tools = provider.getTools();const count = MAIN_TOOL_IDS.length;expect(tools[0]).toEqual({diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsxindex a37edd3bcb..f5f53d9eb7 100644--- a/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasEmbedTools.tsx@@ -26,6 +26,7 @@ import { useEffect, useState } from "preact/hooks";import {type ContentEmbedTool,+ type ContentEmbedToolbarItem,type ContentEmbedToolProvider,registerContentEmbedTools} from "../text/content_embed_tools";@@ -125,6 +126,9 @@ export function useExcalidrawTranslation() {/** Excalidraw's tools and commands, as buttons of the toolbar of the embed with the drawing. */export class CanvasTools implements ContentEmbedToolProvider {+ readonly hiddenToolbarItems: readonly ContentEmbedToolbarItem[] = [+ "contentEmbedBoxSizeDropdown", "toggleContentEmbedTitle", "convertEmbedToLink"+ ];private api: ExcalidrawImperativeAPI | null = null;private translate: Translate | null = null;private listeners = new Set<() => void>();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 bee3ae19e6..d7a69f281c 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@@ -19,12 +19,18 @@ export interface ContentEmbedTool {class?: string;}+/** An item of the embed toolbar that the content of an embed can hide. */+export type ContentEmbedToolbarItem =+ "contentEmbedBoxSizeDropdown" | "toggleContentEmbedTitle" | "convertEmbedToLink";+/** The buttons that the content of an embed, such as a canvas drawing, adds to its toolbar. */export interface ContentEmbedToolProvider {getTools(): ContentEmbedTool[];execute(id: string): void;/** Calls `callback` when `getTools()` changes, until the returned function is called. */subscribe(callback: () => void): () => void;+ /** The items of the embed toolbar to hide. The menu of the embed still offers them. */+ hiddenToolbarItems?: readonly ContentEmbedToolbarItem[];}const providers = new Map<HTMLElement, ContentEmbedToolProvider>();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.htmlindex 5fd9baf596..7be47480f7 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@@ -179,8 +179,13 @@<li>A new drawing has the background color of the note. A background chosenfrom the Excalidraw menu replaces it.</li><li>The box sizes, resizing, 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>+ note. While the drawing can be changed, its toolbar holds the drawing tools+ and leaves the box size, <em>Show title</em> and <em>Convert to link</em> to+ its <span class="tn-icon cke cke-three-vertical-dots"></span> <em>More actions</em> menu.</li>+ <li>In the desktop layout, Excalidraw keeps its desktop interface however+ small the box is. In the mobile layout, a narrow or short box switches+ it to a compact interface. 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><li>Pictures added to the drawing are stored in the same attachment.</li><li>In a <a href="#root/_help_CoFPLs3dRlXc">read-only note</a>, the drawingcan be viewed but not changed.</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.mdindex c101190f8b..ae13ed81c8 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@@ -109,7 +109,8 @@ A drawing canvas is an Excalidraw canvas, like the one of a <a class="reference-* 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, and takes the focus, ready for drawing.* To draw on a canvas that does not have the focus, click it 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, resizing, 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_.+* The box sizes, resizing, the caption and the title work as for an included note. While the drawing can be changed, its toolbar holds the drawing tools and leaves the box size, _Show title_ and _Convert to link_ to its <span class="tn-icon cke cke-three-vertical-dots"></span> _More actions_ menu.+* In the desktop layout, Excalidraw keeps its desktop interface however small the box is. In the mobile layout, a narrow or short box switches it to a compact interface. 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.diff --git a/packages/ckeditor5/src/augmentation.ts b/packages/ckeditor5/src/augmentation.tsindex a78687a0b0..02084411bc 100644--- a/packages/ckeditor5/src/augmentation.ts+++ b/packages/ckeditor5/src/augmentation.ts@@ -51,12 +51,18 @@ declare global {class?: string;}+ /** An item of the embed toolbar that the content of an embed can hide. */+ type ContentEmbedToolbarItem =+ "contentEmbedBoxSizeDropdown" | "toggleContentEmbedTitle" | "convertEmbedToLink";+/** The buttons that the content of an embed, such as a canvas drawing, adds to its toolbar. */interface ContentEmbedToolProvider {getTools(): ContentEmbedTool[];execute(id: string): void;/** Calls `callback` when `getTools()` changes, until the returned function is called. */subscribe(callback: () => void): () => void;+ /** The items of the embed toolbar to hide. The menu of the embed still offers them. */+ hiddenToolbarItems?: readonly ContentEmbedToolbarItem[];}interface EditorComponent extends Component {diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.tsindex 8583f7620d..4315403733 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts@@ -81,6 +81,9 @@ export function getBoxSizeLabel(t: (message: string) => string, size: BoxSizeVal}export default class ContentEmbed extends Plugin {+ /** The buttons that the content of the selected embed adds to its toolbar, or `null`. */+ declare public selectedEmbedTools: ContentEmbedToolProvider | null;+static get requires() {return [ ContentEmbedEditing, ContentEmbedUI ];}@@ -89,6 +92,19 @@ export default class ContentEmbed extends Plugin {return 'ContentEmbed' as const;}+ constructor( editor: Editor ) {+ super( editor );+ // Set before `init()` of the plugins it requires, whose toolbar items bind to it.+ this.set( 'selectedEmbedTools', null );+ }++ init() {+ // Runs before `WidgetToolbarRepository` positions the toolbar from its width.+ this.listenTo( this.editor.ui, 'update', () => {+ this.selectedEmbedTools = getSelectedEmbedTools( this.editor );+ }, { priority: 'high' } );+ }+/*** Selects the embed that `domElement` is part of, so that commands act on the embed a* context menu is opened on. Returns `false` when `domElement` is not part of an embed of@@ -128,11 +144,6 @@ export default class ContentEmbed extends Plugin {};}- /** The rendered `<figure>` of the selected embed, or `null`. */- getSelectedEmbedDom(): HTMLElement | null {- return getSelectedContentEmbedDom( this.editor );- }-/** Whether `domElement` is a reference link that `convertLinkToEmbed` turns into an embed. */canConvertLinkToEmbed( domElement: HTMLElement ): boolean {return !!getLinkToEmbed( this.editor, domElement );@@ -190,7 +201,9 @@ class ContentEmbedUI extends Plugin {// Shown only on an embed that names a note or an attachment.if ( command ) {buttonView.bind( 'isEnabled' ).to( command );- buttonView.bind( 'isVisible' ).to( command, 'isEnabled' );+ bindToolbarItemVisibility(+ editor, buttonView, command, CONVERT_EMBED_TO_LINK_COMMAND+ );}this.listenTo( buttonView, 'execute', () => {@@ -238,7 +251,7 @@ class ContentEmbedUI extends Plugin {if ( command ) {buttonView.bind( 'isOn', 'isEnabled' ).to( command, 'value', 'isEnabled' );- buttonView.bind( 'isVisible' ).to( command, 'isEnabled' );+ bindToolbarItemVisibility( editor, buttonView, command, TOGGLE_TITLE_COMMAND_NAME );}this.listenTo( buttonView, 'execute', () => {@@ -897,6 +910,40 @@ function getTitleAttributes( element: ModelElement ): Record<string, string> {return element.getAttribute( 'hideTitle' ) ? { 'data-hide-title': 'true' } : {};}+/** Whether the content of the selected embed, whose buttons are `tools`, hides `name`. */+export function isToolbarItemHidden(+ tools: ContentEmbedToolProvider | null,+ name: ContentEmbedToolbarItem+) {+ return !!tools?.hiddenToolbarItems?.includes( name );+}++/**+ * Shows `button` while `command` is enabled, unless the content of the selected embed hides the+ * toolbar item `name`.+ */+function bindToolbarItemVisibility(+ editor: Editor, button: ButtonView, command: Command, name: ContentEmbedToolbarItem+) {+ button.bind( 'isVisible' ).to(+ command, 'isEnabled',+ editor.plugins.get( ContentEmbed ), 'selectedEmbedTools',+ ( isEnabled, tools ) => isEnabled && !isToolbarItemHidden( tools, name )+ );+}++/** The buttons that the content of the selected embed adds to its toolbar, or `null`. */+function getSelectedEmbedTools( editor: Editor ): ContentEmbedToolProvider | null {+ const embed = getSelectedContentEmbedDom( editor );+ if ( !embed ) {+ return null;+ }++ const component: EditorComponent | undefined =+ glob.getComponentByEl<EditorComponent>( editor.editing.view.getDomRoot() );+ return component?.getContentEmbedTools?.( embed ) ?? null;+}+/** Has the host open the context menu of the selected embed, below `anchor`. */function openContentEmbedMenu( editor: Editor, anchor: HTMLElement | null ) {const domElement = getSelectedContentEmbedDom( editor );diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_box_size_dropdown.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_box_size_dropdown.tsindex 0710ea7597..3f1e2d2c0d 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_box_size_dropdown.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_box_size_dropdown.ts@@ -1,5 +1,9 @@import { Plugin, type ListDropdownButtonDefinition, Collection, ViewModel, createDropdown, addListToDropdown, DropdownButtonView, type Command } from "ckeditor5";-import ContentEmbed, { BOX_SIZE_COMMAND_NAME, BOX_SIZES, type BoxSizeValue, getBoxSizeLabel } from "./content_embed.js";+import ContentEmbed, {+ BOX_SIZE_COMMAND_NAME, BOX_SIZES, type BoxSizeValue, getBoxSizeLabel, isToolbarItemHidden+} from "./content_embed.js";++export const CONTENT_EMBED_BOX_SIZE_DROPDOWN = "contentEmbedBoxSizeDropdown";/*** Toolbar item which displays the list of box sizes for embeds in a dropdown.@@ -16,8 +20,9 @@ export default class ContentEmbedBoxSizeDropdown extends Plugin {const itemDefinitions = this._getBoxSizeListItemDefinitions();const command = editor.commands.get(BOX_SIZE_COMMAND_NAME) as Command & { value: BoxSizeValue | null };+ const contentEmbed = editor.plugins.get(ContentEmbed);- componentFactory.add("contentEmbedBoxSizeDropdown", _locale => {+ componentFactory.add(CONTENT_EMBED_BOX_SIZE_DROPDOWN, _locale => {const dropdownView = createDropdown(editor.locale, DropdownButtonView);const boxSizeLabel = editor.t("Box size");dropdownView.buttonView.set({@@ -26,6 +31,10 @@ export default class ContentEmbedBoxSizeDropdown extends Plugin {label: boxSizeLabel});dropdownView.bind("isEnabled").to(command, "isEnabled");+ dropdownView.bind("class").to(contentEmbed, "selectedEmbedTools", (tools) => {+ const isHidden = isToolbarItemHidden(tools, CONTENT_EMBED_BOX_SIZE_DROPDOWN);+ return isHidden ? "ck-hidden" : undefined;+ });dropdownView.buttonView.bind("label").to(command, "value", (value) => {if (!value) return boxSizeLabel;return getBoxSizeLabel(editor.t, value);diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.tsindex 9dbe81d1a5..500c558897 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_toolbar.ts@@ -5,7 +5,9 @@ import ContentEmbed, {TOGGLE_CAPTION_COMMAND_NAME,TOGGLE_TITLE_COMMAND_NAME} from "./content_embed.js";-import ContentEmbedBoxSizeDropdown from "./content_embed_box_size_dropdown.js";+import ContentEmbedBoxSizeDropdown, {+ CONTENT_EMBED_BOX_SIZE_DROPDOWN+} from "./content_embed_box_size_dropdown.js";import ContentEmbedTools, { CONTENT_EMBED_TOOLS } from "./content_embed_tools.js";export default class ContentEmbedToolbar extends Plugin {@@ -23,7 +25,7 @@ export default class ContentEmbedToolbar extends Plugin {widgetToolbarRepository.register("contentEmbed", {items: [CONTENT_EMBED_TOOLS,- "contentEmbedBoxSizeDropdown",+ CONTENT_EMBED_BOX_SIZE_DROPDOWN,TOGGLE_TITLE_COMMAND_NAME,TOGGLE_CAPTION_COMMAND_NAME,CONVERT_EMBED_TO_LINK_COMMAND,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 f156b9f64b..cf49788a39 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@@ -1,11 +1,14 @@import {- ClassicEditor, DropdownView, Essentials, Paragraph, Widget, _setModelData as setModelData+ ClassicEditor, DropdownView, Essentials, LinkEditing, Paragraph, Widget,+ _setModelData as setModelData} from "ckeditor5";import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";import { createTestEditor } from "../../../test/editor-kit.js";import { installGlobMock } from "../../../test/globals-test-kit.js";+import ReferenceLink from "../referencelink.js";import ContentEmbed from "./content_embed.js";+import ContentEmbedBoxSizeDropdown from "./content_embed_box_size_dropdown.js";import ContentEmbedTools, {CONTENT_EMBED_TOOLS, ContentEmbedToolsView} from "./content_embed_tools.js";@@ -57,7 +60,8 @@ describe("ContentEmbedTools", () => {});editor = await createTestEditor([- Essentials, Paragraph, Widget, ContentEmbed, ContentEmbedTools+ Essentials, Paragraph, Widget, LinkEditing, ReferenceLink, ContentEmbed,+ ContentEmbedBoxSizeDropdown, ContentEmbedTools]);});@@ -234,6 +238,33 @@ describe("ContentEmbedTools", () => {expect(tools.isVisible).toBe(false);});+ it("hides the items of the embed toolbar that the content leaves to the embed menu", () => {+ const names = [+ "contentEmbedBoxSizeDropdown", "toggleContentEmbedTitle", "convertEmbedToLink",+ "toggleContentEmbedCaption"+ ];+ const items = names.map((name) => {+ const item = editor.ui.componentFactory.create(name);+ item.render();+ return item;+ });+ const getShown = () => items.map((item) => !item.element?.classList.contains("ck-hidden"));+ selectEmbed();+ expect(getShown()).toEqual([ true, true, true, true ]);++ provider = createProvider([ SELECTION ], names.slice(0, 3));+ editor.ui.update();+ expect(getShown()).toEqual([ false, false, false, true ]);+ const embed = editor.editing.view.getDomRoot()?.querySelector("figure.include-note");+ expect(embed && editor.plugins.get(ContentEmbed).getEmbedStateAt(embed)).toMatchObject({+ boxSize: "medium", isTitleToggleable: true, isConvertibleToLink: true+ });++ provider = createProvider([ SELECTION ]);+ editor.ui.update();+ expect(getShown()).toEqual([ true, true, true, true ]);+ });+it("focuses its first button, and stops following the provider once destroyed", () => {const tools = createView();selectEmbed();@@ -250,13 +281,14 @@ describe("ContentEmbedTools", () => {});/** A provider whose tools the spec changes. */-function createProvider(initialTools: Tool[]) {+function createProvider(initialTools: Tool[], hiddenToolbarItems?: string[]) {let tools = initialTools;let listener: (() => void) | undefined;const unsubscribe = vi.fn();return {unsubscribe,+ hiddenToolbarItems,getTools: () => tools,execute: vi.fn(),subscribe: vi.fn((callback: () => void) => {diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.tsindex 82af46f2e6..326b98fc68 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed_tools.ts@@ -27,13 +27,14 @@ export default class ContentEmbedTools extends Plugin {init() {const editor = this.editor;+ const contentEmbed = editor.plugins.get(ContentEmbed);editor.ui.componentFactory.add(CONTENT_EMBED_TOOLS, locale => {const view = new ContentEmbedToolsView(locale);+ view.setProvider(contentEmbed.selectedEmbedTools);- // Runs before `WidgetToolbarRepository` positions the toolbar from its width.- this.listenTo(editor.ui, "update", () => view.setProvider(this.getProvider()), {- priority: "high"+ this.listenTo(contentEmbed, "change:selectedEmbedTools", () => {+ view.setProvider(contentEmbed.selectedEmbedTools);});this.listenTo<ContentEmbedToolsResizeEvent>(view, "resize", () => editor.ui.update());@@ -41,19 +42,6 @@ export default class ContentEmbedTools extends Plugin {});}- /** The buttons that the content of the selected embed adds, or `null`. */- private getProvider() {- const embed = this.editor.plugins.get(ContentEmbed).getSelectedEmbedDom();- if (!embed) {- return null;- }-- const editorEl = this.editor.editing.view.getDomRoot();- const component: EditorComponent | undefined =- glob.getComponentByEl<EditorComponent>(editorEl);- return component?.getContentEmbedTools?.(embed) ?? null;- }-}/** Fired when the buttons are created again, which changes the width of the toolbar. */