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 a slash command for inserting a drawing canvas
Adorian Dorana337daa22df1
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex b79e8b44b8..3ee999adcb 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4418,7 +4418,8 @@"show-title": "Show title","more-actions": "More actions","drawing-canvas": "Drawing canvas",- "editable": "Editable"+ "editable": "Editable",+ "insert-a-canvas-to-draw-on": "Insert a canvas to draw on."},"task-state-dropped-title": "Invalid task states","task-state-dropped-message_one": "A custom task state definition is invalid and was ignored.",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 5d270e08f7..e6a93c7228 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@@ -169,8 +169,10 @@<li>To insert a drawing canvas, open the <span class="tn-icon cke cke-plus"></span> <em>Insert</em> menuof the <a class="reference-link" href="#root/_help_nRhnJkTT8cPs">Formatting toolbar</a> andchoose <span class="tn-icon bx bx-pen"></span> <em>Drawing canvas</em>, above <em>Mermaid diagram</em>.- The canvas is added empty and editable, in a <em>Medium</em> box with its- title hidden, and takes the focus, ready for drawing.</li>+ It is also available as <em>Drawing canvas</em> in the <a class="reference-link"+ href="#root/_help_ZlN4nump6EbW">Slash Commands</a>. The canvas is added+ empty and editable, in a <em>Medium</em> box with its title hidden, and takes+ the focus, ready for drawing.</li><li>To draw on a canvas that does not have the focus, click it first. Untilthen, the mouse wheel and touch gestures scroll the note. The canvas thenhas the tools, menus and keyboard shortcuts of a Canvas note. The toolbars,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 913a1b040f..6930fd61e3 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@@ -106,7 +106,7 @@ In a _Small_ or _Medium_ box, the image is scaled down to fit the height of theA drawing canvas is an Excalidraw canvas, like the one of a <a class="reference-link" href="../Canvas.md">Canvas</a> note, drawn directly in the text note. It is not a note of its own: the drawing is kept in a `Canvas.excalidraw` [attachment](../../Basic%20Concepts%20and%20Features/Notes/Attachments.md) of the text note and is saved together with the text.-* To insert a drawing canvas, open the <span class="tn-icon cke cke-plus"></span> _Insert_ menu of the <a class="reference-link" href="Formatting%20toolbar.md">Formatting toolbar</a> and choose <span class="tn-icon bx bx-pen"></span> _Drawing canvas_, above _Mermaid diagram_. The canvas is added empty and editable, in a _Medium_ box with its title hidden, and takes the focus, ready for drawing.+* To insert a drawing canvas, open the <span class="tn-icon cke cke-plus"></span> _Insert_ menu of the <a class="reference-link" href="Formatting%20toolbar.md">Formatting toolbar</a> and choose <span class="tn-icon bx bx-pen"></span> _Drawing canvas_, above _Mermaid diagram_. It is also available as _Drawing canvas_ in the <a class="reference-link" href="Slash%20Commands.md">Slash Commands</a>. The canvas is added empty and editable, 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.* To protect a finished drawing from accidental changes, select it and turn off the <span class="tn-icon bx bx-edit-alt"></span> _Editable_ button at the start of its toolbar, or uncheck _Editable_ in its <span class="tn-icon cke cke-three-vertical-dots"></span> _More actions_ menu. The drawing can then be panned and zoomed but not changed, and its toolbar holds only the zoom buttons. To draw again, turn _Editable_ back on.diff --git a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.tsindex 597740a43e..c429d1f00d 100644--- a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts+++ b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts@@ -13,6 +13,7 @@ import {ListView,Plugin,SplitButtonView,+ type Editor,type FileInputViewDoneEvent,type Locale} from "ckeditor5";@@ -96,21 +97,7 @@ export default class FileUploadUI extends Plugin {button.isEnabled = false;}- button.on("execute", () => {- const file = new File([ EMPTY_CANVAS_CONTENT ], CANVAS_ATTACHMENT_TITLE, {- type: CANVAS_ATTACHMENT_MIME- });- editor.execute("fileUpload", {- file: [ file ],- asEmbed: true,- boxSize: "medium",- hideTitle: true,- editable: true,- quiet: true,- focusEmbed: true- });- editor.editing.view.focus();- });+ button.on("execute", () => insertDrawingCanvas(editor));return button;});@@ -142,3 +129,23 @@ export default class FileUploadUI extends Plugin {});}}++/**+ * Attaches an empty canvas drawing and embeds it, editable and untitled, with the focus in it, as+ * the `drawingCanvas` button does.+ */+export function insertDrawingCanvas(editor: Editor) {+ const file = new File([ EMPTY_CANVAS_CONTENT ], CANVAS_ATTACHMENT_TITLE, {+ type: CANVAS_ATTACHMENT_MIME+ });+ editor.execute("fileUpload", {+ file: [ file ],+ asEmbed: true,+ boxSize: "medium",+ hideTitle: true,+ editable: true,+ quiet: true,+ focusEmbed: true+ });+ editor.editing.view.focus();+}diff --git a/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts b/packages/ckeditor5/src/plugins/mention/slash_commands.spec.tsindex 0e6689367c..efafd59f99 100644--- a/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts+++ b/packages/ckeditor5/src/plugins/mention/slash_commands.spec.ts@@ -624,6 +624,7 @@ describe("buildTriliumSlashCommands", () => {[ "internal-link", "Internal link", INTERNAL_LINK_COMMAND ],[ "attach-file", "Attach file as a link", "fileUpload" ],[ "attach-and-embed-file", "Attach and embed file", "fileUpload" ],+ [ "drawing-canvas", "Drawing canvas", "fileUpload" ],[ "content-embed", "Include note", CONTENT_EMBED_COMMAND ],[ "page-break", "Page break", "pageBreak" ],[ "markdown-import", "Markdown import", MARKDOWN_IMPORT_COMMAND ],@@ -669,25 +670,48 @@ describe("buildTriliumSlashCommands", () => {click.mockRestore();});- it("offers embedding a file only where both a file and an embed can go", () => {- const fileUpload = { isEnabled: true };- const contentEmbed = { isEnabled: true };- const { fake } = makeFakeEditor({- fileUpload, [CONTENT_EMBED_COMMAND]: contentEmbed- });- const isEnabled = definition("attach-and-embed-file").isEnabled;- expect(isEnabled?.(fake)).toBe(true);-- contentEmbed.isEnabled = false;- expect(isEnabled?.(fake)).toBe(false);+ it("inserts a drawing canvas as its button does, and returns the focus", () => {+ const execute = vi.spyOn(editor, "execute").mockReturnValue(undefined);+ const focus = vi.spyOn(editor.editing.view, "focus");- contentEmbed.isEnabled = true;- fileUpload.isEnabled = false;- expect(isEnabled?.(fake)).toBe(false);+ definition("drawing-canvas").execute?.(editor);- expect(isEnabled?.(makeFakeEditor().fake)).toBe(false);+ expect(execute).toHaveBeenCalledExactlyOnceWith("fileUpload", {+ file: [ expect.objectContaining({+ name: "Canvas.excalidraw", type: "application/vnd.excalidraw+json"+ }) ],+ asEmbed: true,+ boxSize: "medium",+ hideTitle: true,+ editable: true,+ quiet: true,+ focusEmbed: true+ });+ expect(focus).toHaveBeenCalled();});+ it.each([ "attach-and-embed-file", "drawing-canvas" ])(+ "offers %s only where both a file and an embed can go",+ (id) => {+ const fileUpload = { isEnabled: true };+ const contentEmbed = { isEnabled: true };+ const { fake } = makeFakeEditor({+ fileUpload, [CONTENT_EMBED_COMMAND]: contentEmbed+ });+ const isEnabled = definition(id).isEnabled;+ expect(isEnabled?.(fake)).toBe(true);++ contentEmbed.isEnabled = false;+ expect(isEnabled?.(fake)).toBe(false);++ contentEmbed.isEnabled = true;+ fileUpload.isEnabled = false;+ expect(isEnabled?.(fake)).toBe(false);++ expect(isEnabled?.(makeFakeEditor().fake)).toBe(false);+ }+ );+it.each([[ "align-left", "left" ],[ "align-center", "center" ],diff --git a/packages/ckeditor5/src/plugins/mention/slash_commands.ts b/packages/ckeditor5/src/plugins/mention/slash_commands.tsindex d7a0ce6ec7..d3c8acfd36 100644--- a/packages/ckeditor5/src/plugins/mention/slash_commands.ts+++ b/packages/ckeditor5/src/plugins/mention/slash_commands.ts@@ -32,6 +32,7 @@ import bxError from "boxicons/svg/regular/bx-error.svg?raw";import bxErrorCircle from "boxicons/svg/regular/bx-error-circle.svg?raw";import bxInfoCircle from "boxicons/svg/regular/bx-info-circle.svg?raw";import bxNetworkChart from "boxicons/svg/regular/bx-network-chart.svg?raw";+import bxPen from "boxicons/svg/regular/bx-pen.svg?raw";import bxSticker from "boxicons/svg/regular/bx-sticker.svg?raw";import { BookmarkUI, type Editor, type MentionFeedObjectItem, Plugin } from "ckeditor5";@@ -46,6 +47,7 @@ import { ADMONITION_TYPE_NAMES, type AdmonitionType } from "../admonition/admoniimport { getAdmonitionTitle } from "../admonition/admonition_ui.js";import aiIcon from "../ai_assistant/theme/icons/ai.svg?raw";import { COMMAND_NAME as CONTENT_EMBED_COMMAND } from "../content_embed/content_embed.js";+import { insertDrawingCanvas } from "../file_upload/file_upload_ui.js";import { INSERT_ICON_COMMAND } from "../inline_icon/inline_icon_editing.js";import InlineIconUI from "../inline_icon/inline_icon_ui.js";import InsertDateTimePlugin, { COMMAND_NAME as INSERT_DATE_TIME_COMMAND, getDateTimeFormatOptions } from "../insert_date_time.js";@@ -326,6 +328,16 @@ export function buildTriliumSlashCommands(editor: Editor): SlashCommandDefinitio...buildListSlashCommands(editor),...buildAlignmentSlashCommands(editor),...buildAdmonitionSlashCommands(editor),+ {+ id: "drawing-canvas",+ title: t("Drawing canvas"),+ description: t("Insert a canvas to draw on."),+ aliases: [ "excalidraw", "sketch", "whiteboard" ],+ icon: bxPen,+ commandName: "fileUpload",+ isEnabled: canEmbedFiles,+ execute: insertDrawingCanvas+ },...buildMermaidSlashCommands(editor),{id: "ai-assistant",@@ -381,9 +393,7 @@ export function buildTriliumSlashCommands(editor: Editor): SlashCommandDefinitioaliases: [ "attachment", "upload", "embed" ],icon: IconPaperClip,commandName: "fileUpload",- // Embedding goes wherever the "Include note" button can insert an embed.- isEnabled: (target: Editor) => !!target.commands.get("fileUpload")?.isEnabled- && !!target.commands.get(CONTENT_EMBED_COMMAND)?.isEnabled,+ isEnabled: canEmbedFiles,execute: (target: Editor) => pickFiles("", (files) => {target.execute("fileUpload", { file: files, asEmbed: true });target.editing.view.focus();@@ -738,6 +748,12 @@ function buildImageUploadCommand(editor: Editor): SlashCommandDefinition {};}+/** Whether files can be embedded at the selection: wherever "Include note" can insert an embed. */+function canEmbedFiles(target: Editor) {+ return !!target.commands.get("fileUpload")?.isEnabled+ && !!target.commands.get(CONTENT_EMBED_COMMAND)?.isEnabled;+}+/*** Opens the browser's file picker and passes the picked files to `onPick`.*