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 support for interactive canvas in text notes
Adorian Dorane2e030157c93
diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.tsindex 5818326cd0..74dd1e6ed7 100644--- a/apps/client/src/services/content_renderer.spec.ts+++ b/apps/client/src/services/content_renderer.spec.ts@@ -97,6 +97,14 @@ vi.mock("../widgets/collections/NoteList", () => ({ EmbeddedNoteList: embeddedNoconst iconPackPreviewComponent = vi.fn((_props: any) => null);vi.mock("../widgets/type_widgets/icon_pack/IconPackPreview", () => ({ IconPackPreview: iconPackPreviewComponent }));+const canvasDrawingComponent = vi.fn((_props: any): VNode<any> =>+ h("span", { class: "mock-canvas-drawing-marker" }));+const renderCanvasDrawingPicture = vi.fn(async (_entity: unknown): Promise<Element | null> => null);+vi.mock("../widgets/type_widgets/canvas/CanvasDrawing", () => ({+ default: canvasDrawingComponent,+ renderCanvasDrawingPicture: (entity: unknown) => renderCanvasDrawingPicture(entity)+}));+const chatPreviewComponent = vi.fn((props: any): VNode<any> =>h("span", { class: "mock-chat-marker" }, `messages:${props.messages.length}`));vi.mock("../widgets/type_widgets/llm_chat/ChatPreview", () => ({ default: chatPreviewComponent }));@@ -194,16 +202,17 @@ describe("getEmbedBoxSize", () => {buildNote({ title: "Site", type: "webView", "#webViewSrc": "https://example.com" }),buildNote({ title: "Clip", type: "file", mime: "video/mp4" }),buildAttachment({ role: "file", mime: "application/pdf" }),- buildAttachment({ role: "image", mime: "image/png" })- ].map(getEmbedBoxSize)).toEqual(Array(8).fill("medium"));+ buildAttachment({ role: "image", mime: "image/png" }),+ buildAttachment({ role: "file", mime: "application/vnd.excalidraw+json" })+ ].map(getEmbedBoxSize)).toEqual(Array(9).fill("medium"));});it("sizes an upload by its media type, as the attachment it becomes", () => {expect(["application/zip", "", "audio/mpeg", "application/json", "application/pdf",- "image/png", "video/mp4"+ "image/png", "video/mp4", "application/vnd.excalidraw+json"].map(getUploadBoxSize)).toEqual([- "tiny", "tiny", "small", "full", "medium", "medium", "medium"+ "tiny", "tiny", "small", "full", "medium", "medium", "medium", "medium"]);});});@@ -851,6 +860,44 @@ describe("generic FNote fallback / webView", () => {});});+describe("getRenderedContent canvas drawing rendering", () => {+ const canvasMime = "application/vnd.excalidraw+json";++ it("mounts the editor for an interactive attachment, with its saving editor", async () => {+ const att = buildAttachment({ role: "file", mime: canvasMime });+ const attachmentEditor = { canEdit: vi.fn() } as any;++ const { type, $renderedContent } = await getRenderedContent(att, {+ interactive: true,+ attachmentEditor+ });++ expect(type).toBe("canvasDrawing");+ const $drawing = $renderedContent.find(".canvas-drawing[data-interactive-mount]");+ expect($drawing.find(".mock-canvas-drawing-marker").length).toBe(1);+ expect(canvasDrawingComponent).toHaveBeenCalledWith(+ { attachment: att, editor: attachmentEditor }, expect.anything());+ expect(renderCanvasDrawingPicture).not.toHaveBeenCalled();+ });++ it("shows a picture of the drawing elsewhere, and an icon for an empty drawing", async () => {+ const att = buildAttachment({ role: "file", mime: canvasMime });+ const picture = document.createElement("svg");+ renderCanvasDrawingPicture.mockResolvedValueOnce(picture);++ const { type, $renderedContent } = await getRenderedContent(att);+ expect(type).toBe("canvasDrawing");+ expect($renderedContent.children().get(0)).toBe(picture);+ expect(canvasDrawingComponent).not.toHaveBeenCalled();++ const note = buildNote({ title: "Drawing", type: "file", mime: canvasMime });+ const { $renderedContent: $empty } = await getRenderedContent(note, { interactive: true });+ expect(renderCanvasDrawingPicture).toHaveBeenLastCalledWith(note);+ expect($empty.hasClass("no-preview")).toBe(true);+ expect($empty.find(".bx-pen").length).toBe(1);+ });+});+describe("interactive content disposal", () => {it("tags an interactive mount and disposes it, unmounting the widget", async () => {const note = buildNote({ title: "WI", type: "webView", "#webViewSrc": "https://example.com" });diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.tsindex 38db6d47e9..4af8f3836a 100644--- a/apps/client/src/services/content_renderer.ts+++ b/apps/client/src/services/content_renderer.ts@@ -2,6 +2,7 @@ import "./content_renderer.css";import {attachmentIcon,+ CANVAS_ATTACHMENT_MIME,isAcceptedImageMime,isImageAttachmentRole,isOfficeMimeType,@@ -83,6 +84,23 @@ export interface RenderOptions {* the attachment full-detail view opts in. The viewer remains read-only either way.*/pdfToolbar?: boolean;+ /**+ * Saves the changes that interactive content makes to an attachment, such as a canvas drawing.+ * Without it, that content is read-only.+ */+ attachmentEditor?: AttachmentEditor;+}++/** Saves the changes that rendered content makes to attachments of the note that shows it. */+export interface AttachmentEditor {+ /** Whether the rendered content can change `attachment`. */+ canEdit(attachment: FAttachment): boolean;+ /** The content of the attachment that is not saved yet, or `undefined`. */+ getUnsavedContent(attachmentId: string): string | undefined;+ /** Schedules a save of `attachment`. The save reads the content from `getContent`. */+ scheduleSave(attachment: FAttachment, getContent: () => string): void;+ /** Reads the unsaved content of the attachment now, before its editor unmounts. */+ release(attachmentId: string): void;}const CODE_MIME_TYPES = new Set(["application/json"]);@@ -92,8 +110,9 @@ const CODE_MIME_TYPES = new Set(["application/json"]);* icon, or as the actions of a file. Keep in step with the branches there.*/const PREVIEWED_TYPES = new Set([- "book", "search", "text", "markdown", "code", "iconPack", "image", "canvas", "mindMap",- "spreadsheet", "office", "pdf", "audio", "video", "mermaid", "render", "doc", "llmChat"+ "book", "search", "text", "markdown", "code", "iconPack", "image", "canvas", "canvasDrawing",+ "mindMap", "spreadsheet", "office", "pdf", "audio", "video", "mermaid", "render", "doc",+ "llmChat"]);export async function getRenderedContent(this: {} | { ctx: string }, entity: FNote | FAttachment, options: RenderOptions = {}) {@@ -124,6 +143,8 @@ export async function getRenderedContent(this: {} | { ctx: string }, entity: FNoawait renderCode(entity, $renderedContent);} else if (type === "iconPack" && !options.tooltip && entity instanceof FNote) {await renderIconPack(entity, $renderedContent, options);+ } else if (type === "canvasDrawing") {+ await renderCanvasDrawing(entity, $renderedContent, options);} else if (type === "image" && options.mediaEnvironment === "embedded") {await renderImageViewer(entity, $renderedContent);} else if (["image", "canvas", "mindMap", "spreadsheet"].includes(type)) {@@ -315,6 +336,41 @@ async function renderImageViewer($renderedContent.append($container);}+/**+ * Renders a canvas drawing kept in a file or an attachment. Interactive content of an attachment+ * mounts the Excalidraw editor; anything else shows a picture of the drawing.+ */+async function renderCanvasDrawing(+ entity: FNote | FAttachment,+ $renderedContent: JQuery<HTMLElement>,+ options: RenderOptions+) {+ const { default: CanvasDrawing, renderCanvasDrawingPicture } =+ await import("../widgets/type_widgets/canvas/CanvasDrawing");++ if (options.interactive && entity instanceof FAttachment) {+ const $container = $('<div class="canvas-drawing">');+ const container = $container.get(0);+ if (container) {+ await mountInteractiveWidget(h(CanvasDrawing, {+ attachment: entity,+ editor: options.attachmentEditor+ }), container);+ }+ $renderedContent.append($container);+ return;+ }++ const picture = await renderCanvasDrawingPicture(entity);+ if (picture) {+ $renderedContent.append(picture);+ } else {+ $renderedContent.addClass("no-preview");+ const $icon = $("<span>").addClass(attachmentIcon("file", entity.mime));+ $renderedContent.append($("<div>").append($icon));+ }+}+function getImageUrl(entity: FNote | FAttachment) {const encodedTitle = encodeURIComponent(entity.title);@@ -772,6 +828,7 @@ function getContentType(entity: FNote | FAttachment) {/** Narrows a file, or a `viewConfig` attachment, to the kind of file its media type names. */function getFileContentType(type: string, mime: string) {if (type === "file" && mime === "application/pdf") return "pdf";+ if (type === "file" && mime === CANVAS_ATTACHMENT_MIME) return "canvasDrawing";if ((type === "file" || type === "viewConfig") && CODE_MIME_TYPES.has(mime)) return "code";if (type === "file" && mime.startsWith("audio/")) return "audio";if (type === "file" && mime.startsWith("video/")) return "video";diff --git a/apps/client/src/services/froca_updater.ts b/apps/client/src/services/froca_updater.tsindex 8be1eceada..48fdbbdc8f 100644--- a/apps/client/src/services/froca_updater.ts+++ b/apps/client/src/services/froca_updater.ts@@ -288,7 +288,7 @@ function processAttachment(loadResults: LoadResults, ec: EntityChange) {note.attachments = note.attachments.filter((att) => att.attachmentId !== attachment.attachmentId);}- loadResults.addAttachmentRow(attachmentEntity);+ loadResults.addAttachmentRow(attachmentEntity, ec.componentId);delete froca.attachments[ec.entityId];}@@ -309,7 +309,7 @@ function processAttachment(loadResults: LoadResults, ec: EntityChange) {}}- loadResults.addAttachmentRow(attachmentEntity);+ loadResults.addAttachmentRow(attachmentEntity, ec.componentId);}export default {diff --git a/apps/client/src/services/load_results.spec.ts b/apps/client/src/services/load_results.spec.tsindex 970e317b2f..111c4d15f2 100644--- a/apps/client/src/services/load_results.spec.ts+++ b/apps/client/src/services/load_results.spec.ts@@ -118,6 +118,12 @@ describe("LoadResults", () => {const attachment = { attachmentId: "att1" } as never;lr.addAttachmentRow(attachment);expect(lr.getAttachmentRows()).toEqual([attachment]);++ const savedByComp1 = { attachmentId: "att2" } as never;+ lr.addAttachmentRow(savedByComp1, "comp1");+ expect(lr.getAttachmentRows()).toEqual([attachment, savedByComp1]);+ expect(lr.getAttachmentRows("comp1")).toEqual([attachment]);+ expect(lr.getAttachmentRows("comp2")).toEqual([attachment, savedByComp1]);});it("reports attribute-related changes from branches or attributes", () => {diff --git a/apps/client/src/services/load_results.ts b/apps/client/src/services/load_results.tsindex 871ab77439..eb26528e8f 100644--- a/apps/client/src/services/load_results.ts+++ b/apps/client/src/services/load_results.ts@@ -79,7 +79,7 @@ export default class LoadResults {private noteReorderings: string[];private contentNoteIdToComponentId: ContentNoteIdToComponentIdRow[];private optionNames: OptionNames[];- private attachmentRows: AttachmentRow[];+ private attachmentRows: { attachment: AttachmentRow; componentId?: string | null }[];public hasEtapiTokenChanges: boolean = false;constructor(entityChanges: EntityChange[]) {@@ -218,12 +218,15 @@ export default class LoadResults {return this.optionNames;}- addAttachmentRow(attachment: AttachmentRow) {- this.attachmentRows.push(attachment);+ addAttachmentRow(attachment: AttachmentRow, componentId?: string | null) {+ this.attachmentRows.push({ attachment, componentId });}- getAttachmentRows() {- return this.attachmentRows;+ /** The changed attachments, without the ones that the component `componentId` changed. */+ getAttachmentRows(componentId?: string) {+ return this.attachmentRows+ .filter((row) => !componentId || row.componentId !== componentId)+ .map((row) => row.attachment);}/**diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex 5a761ad153..a8cc3f41d3 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -1239,7 +1239,8 @@ svg.ck-icon.note-icon {margin: 0 !important;}-.ck-content a:hover {+/* `:where(:not(.excalidraw *))` leaves out the menus of an embedded canvas drawing. */+.ck-content a:hover:where(:not(.excalidraw *)) {text-decoration: underline;}@@ -1563,7 +1564,8 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t}.include-note[data-box-size] .include-note-content:is(- .type-image, .type-canvas, .type-mindMap, .type-spreadsheet, .type-mermaid, .type-video+ .type-image, .type-canvas, .type-canvasDrawing, .type-mindMap, .type-spreadsheet, .type-mermaid,+ .type-video) {max-height: none;}@@ -1761,6 +1763,7 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-t.include-note-content.type-image,.include-note-content.type-video,.include-note-content.type-canvas,+.include-note-content.type-canvasDrawing,.include-note-content.type-webView,.include-note-content.type-book:has(.note-list-widget.full-height),.include-note-content.type-search:has(.note-list-widget.full-height) {diff --git a/apps/client/src/stylesheets/theme-next/dialogs.css b/apps/client/src/stylesheets/theme-next/dialogs.cssindex 24fd3be9f8..ba7f0e72a3 100644--- a/apps/client/src/stylesheets/theme-next/dialogs.css+++ b/apps/client/src/stylesheets/theme-next/dialogs.css@@ -380,9 +380,10 @@ div.tn-tool-dialog {margin-top: 0.4em;}-/* Keyboard shortcut. Tooltips have their own palette — see the TOOLTIPS section in base.css. */+/* Keyboard shortcut. Tooltips have their own palette — see the TOOLTIPS section in base.css.+ `:where(:not(.excalidraw *))` leaves out the menus of an embedded canvas drawing. */.help-dialog .help-cards kbd,-.ck-content kbd {+.ck-content kbd:where(:not(.excalidraw *)) {display: inline-block;margin: 0 2px 2px !important;border: 1px solid var(--help-kbd-shortcut-border);diff --git a/apps/client/src/stylesheets/theme-next/forms.css b/apps/client/src/stylesheets/theme-next/forms.cssindex 6076693501..0692b4ea68 100644--- a/apps/client/src/stylesheets/theme-next/forms.css+++ b/apps/client/src/stylesheets/theme-next/forms.css@@ -776,12 +776,12 @@ div.switch-widget {--switch-on-thumb-background: var(--main-background-color);}-/* Links */+/* Links. `:where(:not(.excalidraw *))` leaves out the menus of an embedded canvas drawing. */body a.tn-link,body a.tn-link:visited,-.use-tn-links a,-.use-tn-links a:visited {+.use-tn-links a:where(:not(.excalidraw *)),+.use-tn-links a:visited:where(:not(.excalidraw *)) {--background: transparent;box-shadow: 0 0 0 0 var(--background);@@ -798,13 +798,13 @@ body a.tn-link:visited,}body a.tn-link:focus-visible,-.use-tn-links a:focus-visible {+.use-tn-links a:focus-visible:where(:not(.excalidraw *)) {outline: 2px solid var(--input-focus-outline-color);outline-offset: 2px;}body a.tn-link:hover,-.use-tn-links a:hover,+.use-tn-links a:hover:where(:not(.excalidraw *)),.use-tn-links a.ck-widget_selected {box-shadow: 0 0 0 4px var(--link-hover-background);--background: var(--link-hover-background);@@ -820,9 +820,9 @@ body a.tn-link:hover,a.tn-link.external:not(.no-arrow)::after,a.tn-link[href^="http://"]:not(.no-arrow)::after,a.tn-link[href^="https://"]:not(.no-arrow)::after,-.use-tn-links a.external:not(.no-arrow)::after,-.use-tn-links a[href^="http://"]:not(.no-arrow)::after,-.use-tn-links a[href^="https://"]:not(.no-arrow)::after+.use-tn-links a.external:not(.no-arrow):where(:not(.excalidraw *))::after,+.use-tn-links a[href^="http://"]:not(.no-arrow):where(:not(.excalidraw *))::after,+.use-tn-links a[href^="https://"]:not(.no-arrow):where(:not(.excalidraw *))::after{display: inline-block;opacity: 0.5;@@ -840,9 +840,9 @@ a.tn-link[href^="https://"]:not(.no-arrow)::after,a.tn-link:hover.external:not(.no-arrow)::after,a.tn-link:hover[href^="http://"]:not(.no-arrow)::after,a.tn-link:hover[href^="https://"]:not(.no-arrow)::after,-.use-tn-links a:hover.external:not(.no-arrow)::after,-.use-tn-links a:hover[href^="http://"]:not(.no-arrow)::after,-.use-tn-links a:hover[href^="https://"]:not(.no-arrow)::after+.use-tn-links a:hover.external:not(.no-arrow):where(:not(.excalidraw *))::after,+.use-tn-links a:hover[href^="http://"]:not(.no-arrow):where(:not(.excalidraw *))::after,+.use-tn-links a:hover[href^="https://"]:not(.no-arrow):where(:not(.excalidraw *))::after{animation: link-arrow-blink 500ms linear alternate infinite;}diff --git a/apps/client/src/stylesheets/theme-next/notes/text.css b/apps/client/src/stylesheets/theme-next/notes/text.cssindex 1671545279..8805befa1a 100644--- a/apps/client/src/stylesheets/theme-next/notes/text.css+++ b/apps/client/src/stylesheets/theme-next/notes/text.css@@ -797,12 +797,8 @@ html .note-detail-editable-text > :not(figure, .include-note, hr):first-child {inset-inline-end: 0.35em;}-.ck-content h1,-.ck-content h2,-.ck-content h3,-.ck-content h4,-.ck-content h5,-.ck-content h6 {+/* `:where(:not(.excalidraw *))` leaves out the UI of an embedded canvas drawing. */+.ck-content :is(h1, h2, h3, h4, h5, h6):where(:not(.excalidraw *)) {font-weight: 300;}@@ -810,7 +806,8 @@ html .note-detail-editable-text > :not(figure, .include-note, hr):first-child {font-weight: 600;}-:root .ck-content hr {+/* `:where(:not(.excalidraw *))` leaves out the menus of an embedded canvas drawing. */+:root .ck-content hr:where(:not(.excalidraw *)) {margin-block: 5px;height: 0;border: thin solid var(--main-border-color);diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 61d35d03f6..42138e5a0c 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4415,7 +4415,8 @@"some-rows-were-sorted-together-because-merged-cells-tie-them-to-each-other": "Some rows were sorted together because merged cells tie them to each other","tiny": "Tiny","show-title": "Show title",- "more-actions": "More actions"+ "more-actions": "More actions",+ "insert-drawing-canvas": "Insert drawing canvas"},"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/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex 4ce82392b2..7a4bdc24d1 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -99,11 +99,13 @@ export function useSpacedUpdate(callback: () => void | Promise<void>, interval =export interface SavedData {content: string;attachments?: {+ /** The attachment to update. Without it, the attachment is matched by its title. */+ attachmentId?: string;role: string;title: string;mime: string;content: string;- position: number;+ position?: number;encoding?: "base64";}[];}diff --git a/apps/client/src/widgets/type_widgets/canvas/Canvas.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/Canvas.spec.tsxnew file mode 100644index 0000000000..ec0e37e492--- /dev/null+++ b/apps/client/src/widgets/type_widgets/canvas/Canvas.spec.tsx@@ -0,0 +1,75 @@+import type { ExcalidrawImperativeAPI, ExcalidrawProps } from "@excalidraw/excalidraw/types";+import { type RefObject, render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++const excalidrawProps = vi.fn((_props: ExcalidrawProps) => undefined);+vi.mock("@excalidraw/excalidraw", () => ({+ Excalidraw: (props: ExcalidrawProps) => {+ excalidrawProps(props);+ return <div className="mock-excalidraw" />;+ }+}));+vi.mock("../../react/hooks", async (importOriginal) => ({+ ...await importOriginal<typeof import("../../react/hooks")>(),+ useTriliumOption: () => [ "en" ]+}));++const { CanvasEditor } = await import("./Canvas");++describe("CanvasEditor", () => {+ let container: HTMLElement;+ let refresh: ReturnType<typeof vi.fn>;+ let apiRef: RefObject<ExcalidrawImperativeAPI>;++ beforeEach(() => {+ excalidrawProps.mockClear();+ refresh = vi.fn();+ apiRef = { current: { refresh } as unknown as ExcalidrawImperativeAPI };+ container = document.createElement("div");+ document.body.appendChild(container);+ });++ afterEach(() => {+ render(null, container);+ container.remove();+ });++ async function mount(isEmbedded?: boolean) {+ await act(async () => {+ render(+ <CanvasEditor+ apiRef={apiRef}+ isReadOnly={false}+ colorScheme="light"+ persistence={{}}+ isEmbedded={isEmbedded}+ />,+ container+ );+ });+ const excalidraw = container.querySelector(".canvas-render .mock-excalidraw");+ expect(excalidraw).not.toBeNull();+ return excalidraw;+ }++ function point(target: Element | null) {+ for (const type of [ "pointerover", "pointermove" ]) {+ target?.dispatchEvent(new PointerEvent(type, { bubbles: true }));+ }+ }++ it("reads its position again before pointer input when it is embedded in a note", async () => {+ point(await mount(true));++ expect(refresh).toHaveBeenCalledTimes(2);+ expect(excalidrawProps).toHaveBeenLastCalledWith(expect.objectContaining({ detectScroll: true }));+ });++ it("leaves the position to Excalidraw in a canvas note, which does not move", async () => {+ point(await mount());++ expect(refresh).not.toHaveBeenCalled();+ expect(excalidrawProps).toHaveBeenLastCalledWith(expect.objectContaining({ detectScroll: false }));+ });+});diff --git a/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx b/apps/client/src/widgets/type_widgets/canvas/Canvas.tsxindex 8b9eb14ec6..dc7f92f44c 100644--- a/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx+++ b/apps/client/src/widgets/type_widgets/canvas/Canvas.tsx@@ -2,8 +2,13 @@ import { Excalidraw } from "@excalidraw/excalidraw";import { TypeWidgetProps } from "../type_widget";import "@excalidraw/excalidraw/index.css";import { useColorScheme, useEffectiveReadOnly, useTriliumOption } from "../../react/hooks";-import { useCallback, useMemo, useRef } from "preact/hooks";-import { type ExcalidrawImperativeAPI, type AppState } from "@excalidraw/excalidraw/types";+import type { RefObject } from "preact";+import { useCallback, useRef } from "preact/hooks";+import {+ type AppState,+ type ExcalidrawImperativeAPI,+ type ExcalidrawProps+} from "@excalidraw/excalidraw/types";import options from "../../../services/options";import "./Canvas.css";import { NonDeleted, NonDeletedExcalidrawElement, ExcalidrawEmbeddableElement } from "@excalidraw/excalidraw/element/types";@@ -23,10 +28,38 @@ export default function Canvas({ note, noteContext }: TypeWidgetProps) {const apiRef = useRef<ExcalidrawImperativeAPI>(null);const isReadOnly = useEffectiveReadOnly(note, noteContext);const colorScheme = useColorScheme();- const [ locale ] = useTriliumOption("locale");const persistence = useCanvasPersistence(note, noteContext, apiRef, colorScheme, isReadOnly);++ return (+ <CanvasEditor+ apiRef={apiRef}+ isReadOnly={isReadOnly}+ colorScheme={colorScheme}+ persistence={persistence}+ />+ );+}++export interface CanvasEditorProps {+ apiRef: RefObject<ExcalidrawImperativeAPI>;+ isReadOnly: boolean;+ colorScheme: AppState["theme"];+ persistence: Partial<ExcalidrawProps>;+ /** Whether the editor moves on the page, as inside a text note that scrolls. */+ isEmbedded?: boolean;+}++/** The Excalidraw editor of a canvas note or drawing, loaded and saved by `persistence`. */+export function CanvasEditor({+ apiRef, isReadOnly, colorScheme, persistence, isEmbedded = false+}: CanvasEditorProps) {+ const [ locale ] = useTriliumOption("locale");const noteDrop = useCanvasNoteDrop(apiRef, isReadOnly);+ // Excalidraw maps the pointer with the container position it read last. An embedded editor+ // moves with the note, so it reads the position again before the pointer acts.+ const refreshPosition = useCallback(() => apiRef.current?.refresh(), [ apiRef ]);+/** Use excalidraw's native zoom instead of the global zoom. */const onWheel = useCallback((e: MouseEvent) => {if (e.ctrlKey) {@@ -63,14 +96,19 @@ export default function Canvas({ note, noteContext }: TypeWidgetProps) {}, []);return (- <div className="canvas-render" onWheel={onWheel}>+ <div+ className="canvas-render"+ onWheel={onWheel}+ onPointerOverCapture={isEmbedded ? refreshPosition : undefined}+ onPointerMoveCapture={isEmbedded ? refreshPosition : undefined}+ ><div className="excalidraw-wrapper" {...noteDrop}><Excalidrawtheme={colorScheme}viewModeEnabled={isReadOnly || options.is("databaseReadonly")}zenModeEnabled={false}isCollaborating={false}- detectScroll={false}+ detectScroll={isEmbedded}handleKeyboardGlobally={false}autoFocus={false}langCode={LANGUAGE_MAPPINGS[locale as DISPLAYABLE_LOCALE_IDS] ?? undefined}diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.cssnew file mode 100644index 0000000000..51e40539ce--- /dev/null+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.css@@ -0,0 +1,49 @@+/* The font size of the note detail in pixels. Excalidraw in a canvas note inherits this size. */+@property --canvas-drawing-font-size {+ syntax: "<length>";+ inherits: true;+ initial-value: 16px;+}++.note-detail {+ --canvas-drawing-font-size: 1em;+}++.canvas-drawing {+ height: 20em;++ /* Resets the typography inherited from the text note. */+ > .canvas-render {+ height: 100%;+ font-size: var(--canvas-drawing-font-size);+ line-height: var(--bs-body-line-height);+ text-align: start;+ overflow-wrap: normal;+ }++ /* Resets CKEditor's content style for `hr`, which matches the menu separators. */+ .excalidraw hr {+ display: block;+ vertical-align: baseline;+ background: none;+ }+}++.include-note {+ &[data-box-size="small"] .canvas-drawing {+ height: 10em;+ }++ &[data-box-size="full"] .canvas-drawing {+ height: 50em;+ }+}++.include-note-content:fullscreen .canvas-drawing {+ height: 100%;+}++.canvas-drawing-picture {+ max-width: 100%;+ height: auto;+}diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsxnew file mode 100644index 0000000000..cfa9a68f23--- /dev/null+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx@@ -0,0 +1,61 @@+import "./CanvasDrawing.css";++import { exportToSvg } from "@excalidraw/excalidraw";+import type { ExcalidrawImperativeAPI } from "@excalidraw/excalidraw/types";+import { useRef } from "preact/hooks";++import type FAttachment from "../../../entities/fattachment";+import type FNote from "../../../entities/fnote";+import type { AttachmentEditor } from "../../../services/content_renderer";+import options from "../../../services/options";+import { useColorScheme } from "../../react/hooks";+import { CanvasEditor } from "./Canvas";+import { getInlineFiles, parseContent, useCanvasDrawingPersistence } from "./persistence";++interface CanvasDrawingProps {+ attachment: FAttachment;+ /** Saves the changes. Without it, or for an attachment of another note, it is read-only. */+ editor?: AttachmentEditor;+}++/** A canvas drawing saved in an attachment, edited in place inside the note that shows it. */+export default function CanvasDrawing({ attachment, editor }: CanvasDrawingProps) {+ const apiRef = useRef<ExcalidrawImperativeAPI>(null);+ const colorScheme = useColorScheme();+ const isEditable = !!editor?.canEdit(attachment) && !options.is("databaseReadonly");+ const persistence = useCanvasDrawingPersistence(+ attachment,+ isEditable ? editor : undefined,+ apiRef,+ colorScheme+ );++ return (+ <CanvasEditor+ apiRef={apiRef}+ isReadOnly={!isEditable}+ colorScheme={colorScheme}+ persistence={persistence}+ isEmbedded+ />+ );+}++/** A picture of the canvas drawing saved in `entity`, or `null` for an empty drawing. */+export async function renderCanvasDrawingPicture(entity: FNote | FAttachment) {+ const blob = await entity.getBlob();+ const content = parseContent(blob?.content ?? "", entity);+ const elements = (content.elements ?? []).filter((element) => !element.isDeleted);+ if (!elements.length) {+ return null;+ }++ const svg = await exportToSvg({+ elements,+ appState: { ...content.appState, exportBackground: true },+ files: getInlineFiles(content),+ exportPadding: 5+ });+ svg.classList.add("canvas-drawing-picture");+ return svg;+}diff --git a/apps/client/src/widgets/type_widgets/canvas/persistence.ts b/apps/client/src/widgets/type_widgets/canvas/persistence.tsindex 5cc9868c08..a0675b0e88 100644--- a/apps/client/src/widgets/type_widgets/canvas/persistence.ts+++ b/apps/client/src/widgets/type_widgets/canvas/persistence.ts@@ -3,10 +3,12 @@ import { ExcalidrawElement, NonDeletedExcalidrawElement } from "@excalidraw/excaimport { AppState, BinaryFileData, BinaryFiles, ExcalidrawImperativeAPI, ExcalidrawInitialDataState, ExcalidrawProps, LibraryItem } from "@excalidraw/excalidraw/types";import { deferred, type DeferredPromise } from "@triliumnext/commons";import { RefObject } from "preact";-import { useRef } from "preact/hooks";+import { useEffect, useRef, useState } from "preact/hooks";import NoteContext from "../../../components/note_context";+import type FAttachment from "../../../entities/fattachment";import FNote from "../../../entities/fnote";+import type { AttachmentEditor } from "../../../services/content_renderer";import server from "../../../services/server";import { SavedData, useEditorSpacedUpdate } from "../../react/hooks";import { buildNewImageAttachments, CANVAS_EXPORT_TITLE, IMAGE_ROLE, loadImageAttachments } from "./image_attachments";@@ -341,7 +343,88 @@ export default function useCanvasPersistence(note: FNote, noteContext: NoteConte};}-function parseContent(newContent: string, note: FNote): CanvasContent {+/**+ * Loads a canvas drawing from an attachment and passes its changes to `editor`, which saves them.+ * Without `editor`, the drawing is read-only.+ */+export function useCanvasDrawingPersistence(+ attachment: FAttachment,+ editor: AttachmentEditor | undefined,+ apiRef: RefObject<ExcalidrawImperativeAPI>,+ theme: AppState["theme"]+): Partial<ExcalidrawProps> {+ const currentSceneVersion = useRef(0);+ /** The saved part of the app state, read from the editor at the first change. */+ const appStateToCompare = useRef<ImportantAppState | null>(null);+ // Same guard as the canvas note: the scene is empty until Excalidraw applies `initialData`.+ const initialSceneAppliedRef = useRef(true);++ const [ initialData ] = useState(async (): Promise<ExcalidrawInitialDataState> => {+ const unsavedContent = editor?.getUnsavedContent(attachment.attachmentId);+ const blobContent = unsavedContent === undefined+ ? (await attachment.getBlob())?.content ?? ""+ : unsavedContent;+ const content = parseContent(blobContent, attachment);++ const elements = content.elements ?? [];+ currentSceneVersion.current = getSceneVersion(elements);+ initialSceneAppliedRef.current = currentSceneVersion.current === 0;++ return {+ elements,+ appState: { ...content.appState, theme },+ files: getInlineFiles(content)+ };+ });++ useEffect(() => () => editor?.release(attachment.attachmentId), [ editor, attachment ]);++ return {+ initialData,+ excalidrawAPI: (api) => {+ apiRef.current = api;+ },+ onChange: () => {+ const api = apiRef.current;+ if (!api || !editor) return;++ const sceneVersion = getSceneVersion(api.getSceneElements());+ if (!initialSceneAppliedRef.current) {+ if (sceneVersion === 0) return;+ initialSceneAppliedRef.current = true;+ }++ const appState = getImportantAppState(api.getAppState());+ const previousAppState = appStateToCompare.current;+ appStateToCompare.current = appState;+ const isAppStateChanged = !!previousAppState+ && (previousAppState.gridModeEnabled !== appState.gridModeEnabled+ || previousAppState.viewBackgroundColor !== appState.viewBackgroundColor);+ if (sceneVersion === currentSceneVersion.current && !isAppStateChanged) return;++ currentSceneVersion.current = sceneVersion;+ editor.scheduleSave(attachment, () => serializeDrawing(api));+ }+ };+}++/** The images kept inline in `content`, keyed by their file ID. */+export function getInlineFiles(content: CanvasContent): BinaryFiles {+ const files: BinaryFiles = {};+ for (const file of Object.values(content.files ?? {})) {+ files[file.id] = file;+ }+ return files;+}++/** The content of a canvas drawing. Unlike a canvas note, a drawing keeps its images inline. */+function serializeDrawing(api: ExcalidrawImperativeAPI) {+ const elements = api.getSceneElements();+ const files = getActiveFiles(elements, api.getFiles());+ return JSON.stringify(buildSceneContent(elements, api.getAppState(), files));+}++export function parseContent(newContent: string, entity: FNote | FAttachment): CanvasContent {let content: CanvasContent = {elements: [],files: [],@@ -351,7 +434,11 @@ function parseContent(newContent: string, note: FNote): CanvasContent {try {content = JSON.parse(newContent) as CanvasContent;} catch (err) {- console.error("Error parsing content. Probably note.type changed. Starting with empty canvas", note, err);+ console.error(+ "Error parsing content. Probably note.type changed. Starting with empty canvas",+ entity,+ err+ );}}return content;@@ -375,39 +462,55 @@ async function getData(api: ExcalidrawImperativeAPI, appStateToCompare: RefObjec});const svgString = svg.outerHTML;- const activeFiles: Record<string, BinaryFileData> = {};- elements.forEach((element: NonDeletedExcalidrawElement) => {- if ("fileId" in element && element.fileId) {- activeFiles[element.fileId] = files[element.fileId];- }- });+ appStateToCompare.current = getImportantAppState(appState);- const importantAppState: ImportantAppState = {- gridModeEnabled: appState.gridModeEnabled,- viewBackgroundColor: appState.viewBackgroundColor+ // Images are persisted as attachments (keyed by fileId), not inline, so the content stays+ // small. Elements keep their `fileId`; the bytes are reattached on load. Kept as an empty+ // object for shape compatibility with loadData() and legacy content.+ const content = buildSceneContent(elements, appState, {});++ return {+ content,+ svg: svgString,+ activeFiles: getActiveFiles(elements, files)};- appStateToCompare.current = importantAppState;+}- const content = {+/** The scene as the content of a canvas note or of a canvas drawing stores it. */+function buildSceneContent(+ elements: readonly NonDeletedExcalidrawElement[],+ appState: AppState,+ files: Record<string, BinaryFileData>+) {+ return {type: "excalidraw",version: 2,elements,- // Images are persisted as attachments (keyed by fileId), not inline, so the content stays- // small. Elements keep their `fileId`; the bytes are reattached on load. Kept as an empty- // object for shape compatibility with loadData() and legacy content.- files: {} as Record<string, BinaryFileData>,+ files,appState: {scrollX: appState.scrollX,scrollY: appState.scrollY,zoom: appState.zoom,- ...importantAppState+ ...getImportantAppState(appState)}};+}+/** The files that an element of the scene uses. */+function getActiveFiles(elements: readonly NonDeletedExcalidrawElement[], files: BinaryFiles) {+ const activeFiles: Record<string, BinaryFileData> = {};+ for (const element of elements) {+ if ("fileId" in element && element.fileId) {+ activeFiles[element.fileId] = files[element.fileId];+ }+ }+ return activeFiles;+}++function getImportantAppState(appState: AppState): ImportantAppState {return {- content,- svg: svgString,- activeFiles+ gridModeEnabled: appState.gridModeEnabled,+ viewBackgroundColor: appState.viewBackgroundColor};}diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.css b/apps/client/src/widgets/type_widgets/text/EditableText.cssindex 9b991701f4..6673420e59 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.css+++ b/apps/client/src/widgets/type_widgets/text/EditableText.css@@ -16,31 +16,45 @@ body.mobile .note-detail-editable-text {cursor: pointer;}-.note-detail-editable-text a[href^="http://"], .note-detail-editable-text a[href^="https://"] {+.note-detail-editable-text :is(a[href^="http://"], a[href^="https://"]):where(:not(.excalidraw *)) {cursor: text !important;}-.note-detail-editable-text *:not(figure, .include-note, hr):first-child {+.note-detail-editable-text *:not(figure, .include-note, hr, .excalidraw *):first-child {margin-top: 0 !important;}-.note-detail-editable-text h2 { font-size: 1.6em; }-.note-detail-editable-text h3 { font-size: 1.4em; }-.note-detail-editable-text h4 { font-size: 1.2em; }-.note-detail-editable-text h5 { font-size: 1.1em; }-.note-detail-editable-text h6 { font-size: 1.0em; }+/* `:where(:not(.excalidraw *))` leaves out the UI of an embedded canvas drawing. */+.note-detail-editable-text :where(:not(.excalidraw *)) {+ &:is(h2) { font-size: 1.6em; }+ &:is(h3) { font-size: 1.4em; }+ &:is(h4) { font-size: 1.2em; }+ &:is(h5) { font-size: 1.1em; }+ &:is(h6) { font-size: 1.0em; }-body.heading-style-markdown .note-detail-editable-text h2::before { content: "##\2004"; color: var(--muted-text-color); }-body.heading-style-markdown .note-detail-editable-text h3::before { content: "###\2004"; color: var(--muted-text-color); }-body.heading-style-markdown .note-detail-editable-text h4:not(.include-note-title)::before { content: "####\2004"; color: var(--muted-text-color); }-body.heading-style-markdown .note-detail-editable-text h5::before { content: "#####\2004"; color: var(--muted-text-color); }-body.heading-style-markdown .note-detail-editable-text h6::before { content: "######\2004"; color: var(--muted-text-color); }+ body.heading-style-markdown & {+ &:is(h2)::before { content: "##\2004"; }+ &:is(h3)::before { content: "###\2004"; }+ &:is(h4):not(.include-note-title)::before { content: "####\2004"; }+ &:is(h5)::before { content: "#####\2004"; }+ &:is(h6)::before { content: "######\2004"; }-body.heading-style-underline .note-detail-editable-text h2 { border-bottom: 1px solid var(--main-border-color); }-body.heading-style-underline .note-detail-editable-text h3 { border-bottom: 1px solid var(--main-border-color); }-body.heading-style-underline .note-detail-editable-text h4:not(.include-note-title) { border-bottom: 1px solid var(--main-border-color); }-body.heading-style-underline .note-detail-editable-text h5 { border-bottom: 1px solid var(--main-border-color); }-body.heading-style-underline .note-detail-editable-text h6 { border-bottom: 1px solid var(--main-border-color); }+ &:is(h2, h3, h5, h6)::before,+ &:is(h4):not(.include-note-title)::before {+ color: var(--muted-text-color);+ }+ }++ body.heading-style-underline & {+ &:is(h2),+ &:is(h3),+ &:is(h4):not(.include-note-title),+ &:is(h5),+ &:is(h6) {+ border-bottom: 1px solid var(--main-border-color);+ }+ }+}.note-detail-editable-text-editor {padding-top: 10px;diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts b/apps/client/src/widgets/type_widgets/text/EditableText.spec.tsindex 9ec8a6b140..ab9497e7ff 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts+++ b/apps/client/src/widgets/type_widgets/text/EditableText.spec.ts@@ -140,7 +140,7 @@ describe("notifyAttachmentChanges", () => {function loadResultsOf(rows: Row[], isContentReloaded = false) {return {- getAttachmentRows: () => rows,+ getAttachmentRows: vi.fn(() => rows),isNoteContentReloaded: vi.fn(() => isContentReloaded)};}@@ -164,6 +164,8 @@ describe("notifyAttachmentChanges", () => {notify(loadResults, { current: undefined });expect(loadResults.isNoteContentReloaded).toHaveBeenCalledWith("note1", "component1");+ // Without the attachments that this editor saved, such as a canvas drawing.+ expect(loadResults.getAttachmentRows).toHaveBeenCalledWith("component1");expect(updateAttachmentLinks).toHaveBeenCalledWith([{ attachmentId: "renamed", isDeleted: false },{ attachmentId: "deleted", isDeleted: true }diff --git a/apps/client/src/widgets/type_widgets/text/EditableText.tsx b/apps/client/src/widgets/type_widgets/text/EditableText.tsxindex 5e4e01fb0b..79a5bd2ff5 100644--- a/apps/client/src/widgets/type_widgets/text/EditableText.tsx+++ b/apps/client/src/widgets/type_widgets/text/EditableText.tsx@@ -31,6 +31,7 @@ import { useEditorSpacedUpdate, useLegacyImperativeHandlers, useNoteLabel, useSeimport IconPicker from "../../react/IconPicker";import { setEditorNoteId } from "../../react/NoteStore";import { TypeWidgetProps } from "../type_widget";+import AttachmentSaves from "./attachment_saves";import CKEditorWithWatchdog, { CKEditorApi, NotificationEventData, NotificationEventInfo } from "./CKEditorWithWatchdog";import getTemplates, { updateTemplateCache } from "./snippets.js";import linkEmbedService from "../../../services/link_embed";@@ -72,6 +73,8 @@ export default function EditableText({ note, parentComponent, ntxId, noteContexttextNoteEditorType});const initialized = useRef(deferred<void>());+ const [ attachmentSaves ] = useState(() =>+ new AttachmentSaves(() => spacedUpdate.scheduleUpdate()));const spacedUpdate = useEditorSpacedUpdate({note,noteContext,@@ -84,14 +87,17 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext}const content = editor.getData() ?? "";+ const attachments = attachmentSaves.collect();// if content is only tags/whitespace (typically <p> </p>), then just make it empty,// this is important when setting a new note to codereturn {- content: utils.isHtmlEmpty(content) ? "" : content+ content: utils.isHtmlEmpty(content) ? "" : content,+ ...(attachments.length ? { attachments } : {})};},onContentChange(newContent) {+ attachmentSaves.setNoteId(note?.noteId);contentRef.current = newContent;contentNoteIdRef.current = note?.noteId;const editor = watchdogRef.current?.editor;@@ -119,6 +125,7 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext// Store back the saved data in order to retrieve it in case the CKEditor crashes.contentRef.current = savedData.content;contentNoteIdRef.current = note?.noteId;+ attachmentSaves.markSaved(savedData.attachments);}});const templates = useTemplates();@@ -193,7 +200,9 @@ export default function EditableText({ note, parentComponent, ntxId, noteContext});},loadEmbeddedNote,- loadEmbeddedAttachment,+ loadEmbeddedAttachment(attachmentId: string, $el: JQuery<HTMLElement>, boxSize?: string) {+ return loadEmbeddedAttachment(attachmentId, $el, boxSize, attachmentSaves);+ },getAttachmentHref,getNoteId() {return note.noteId;@@ -709,7 +718,8 @@ interface PendingAttachmentChanges {/*** Passes the attachments changed in `loadResults` to the editor's reference links, so a link shows- * the new title of its attachment or goes away with it.+ * the new title of its attachment or goes away with it. Changes that this editor saved, such as a+ * canvas drawing, are skipped: redrawing an embed remounts what it shows.** When `loadResults` also reloads the note's content, the changes wait in `pending` until* `applyPendingAttachmentChanges()` runs on the reloaded content.@@ -723,7 +733,8 @@ export function notifyAttachmentChanges(componentId: string | undefined,pending: { current: PendingAttachmentChanges | undefined }) {- const changes = loadResults.getAttachmentRows().flatMap(({ attachmentId, isDeleted }) =>+ const rows = loadResults.getAttachmentRows(componentId);+ const changes = rows.flatMap(({ attachmentId, isDeleted }) =>attachmentId ? [ { attachmentId, isDeleted: !!isDeleted } ] : []);if (!changes.length) {diff --git a/apps/client/src/widgets/type_widgets/text/attachment_saves.spec.ts b/apps/client/src/widgets/type_widgets/text/attachment_saves.spec.tsnew file mode 100644index 0000000000..633104d44f--- /dev/null+++ b/apps/client/src/widgets/type_widgets/text/attachment_saves.spec.ts@@ -0,0 +1,98 @@+import { describe, expect, it, vi } from "vitest";++import type FAttachment from "../../../entities/fattachment";+import AttachmentSaves from "./attachment_saves";++const drawing = {+ attachmentId: "drawing1",+ ownerId: "note1",+ role: "file",+ mime: "application/vnd.excalidraw+json",+ title: "canvas.json"+} as FAttachment;++function setUp() {+ const scheduleUpdate = vi.fn();+ const saves = new AttachmentSaves(scheduleUpdate);+ saves.setNoteId("note1");+ return { saves, scheduleUpdate };+}++describe("AttachmentSaves", () => {+ it("edits only the attachments of the note it saves", () => {+ const { saves } = setUp();++ expect(saves.canEdit(drawing)).toBe(true);+ expect(saves.canEdit({ ...drawing, ownerId: "note2" } as FAttachment)).toBe(false);++ saves.setNoteId(undefined);+ expect(saves.canEdit(drawing)).toBe(false);+ });++ it("schedules a save and reads the content when the note saves", () => {+ const { saves, scheduleUpdate } = setUp();+ let content = "first";++ saves.scheduleSave(drawing, () => content);+ content = "second";++ expect(scheduleUpdate).toHaveBeenCalledOnce();+ expect(saves.getUnsavedContent("drawing1")).toBe("second");+ expect(saves.collect()).toStrictEqual([ {+ attachmentId: "drawing1",+ role: "file",+ mime: "application/vnd.excalidraw+json",+ title: "canvas.json",+ content: "second"+ } ]);+ });++ it("keeps a change made while a save runs", () => {+ const { saves } = setUp();+ saves.scheduleSave(drawing, () => "saved");+ const sent = saves.collect();++ saves.scheduleSave(drawing, () => "changed meanwhile");+ saves.markSaved(sent);+ expect(saves.getUnsavedContent("drawing1")).toBe("changed meanwhile");++ saves.markSaved(saves.collect());+ expect(saves.getUnsavedContent("drawing1")).toBeUndefined();+ expect(saves.collect()).toStrictEqual([]);+ });++ it("ignores a save of other data", () => {+ const { saves } = setUp();+ saves.scheduleSave(drawing, () => "unsaved");++ saves.markSaved(undefined);+ saves.markSaved([ { role: "image", title: "x", mime: "image/png", content: "" } ]);++ expect(saves.getUnsavedContent("drawing1")).toBe("unsaved");+ });++ it("reads the content of a released editor once, and keeps it until the note saves", () => {+ const { saves } = setUp();+ const getContent = vi.fn(() => "last state");+ saves.scheduleSave(drawing, getContent);++ saves.release("drawing1");+ saves.release("unknown");++ expect(getContent).toHaveBeenCalledOnce();+ expect(saves.getUnsavedContent("drawing1")).toBe("last state");+ expect(saves.collect()[0].content).toBe("last state");+ expect(getContent).toHaveBeenCalledOnce();+ });++ it("drops the changes when it saves another note", () => {+ const { saves } = setUp();+ saves.scheduleSave(drawing, () => "unsaved");++ saves.setNoteId("note1");+ expect(saves.getUnsavedContent("drawing1")).toBe("unsaved");++ saves.setNoteId("note2");+ expect(saves.getUnsavedContent("drawing1")).toBeUndefined();+ });+});diff --git a/apps/client/src/widgets/type_widgets/text/attachment_saves.ts b/apps/client/src/widgets/type_widgets/text/attachment_saves.tsnew file mode 100644index 0000000000..b11b4c0cea--- /dev/null+++ b/apps/client/src/widgets/type_widgets/text/attachment_saves.ts@@ -0,0 +1,91 @@+import type FAttachment from "../../../entities/fattachment";+import type { AttachmentEditor } from "../../../services/content_renderer";+import type { SavedData } from "../../react/hooks";++type SavedAttachments = NonNullable<SavedData["attachments"]>;++interface PendingSave {+ attachment: FAttachment;+ /** Reads the content from the mounted editor. */+ getContent?: () => string;+ /** The content read when the editor went away. */+ content?: string;+ /** Counts the changes, so that a save keeps the changes made while it ran. */+ revision: number;+}++/**+ * The attachment changes that embedded content, such as a canvas drawing, makes inside a text note.+ * The text note saves them together with its own content.+ */+export default class AttachmentSaves implements AttachmentEditor {+ private noteId: string | undefined;+ private pending = new Map<string, PendingSave>();+ private sentRevisions = new WeakMap<SavedAttachments, Map<string, number>>();++ /** @param scheduleUpdate schedules a save of the text note. */+ constructor(private scheduleUpdate: () => void) {}++ /** Sets the note whose attachments can change, and drops the changes to any other note. */+ setNoteId(noteId: string | undefined) {+ if (noteId !== this.noteId) {+ this.pending.clear();+ }+ this.noteId = noteId;+ }++ canEdit(attachment: FAttachment) {+ return !!this.noteId && attachment.ownerId === this.noteId;+ }++ getUnsavedContent(attachmentId: string) {+ const save = this.pending.get(attachmentId);+ return save ? readContent(save) : undefined;+ }++ scheduleSave(attachment: FAttachment, getContent: () => string) {+ const revision = (this.pending.get(attachment.attachmentId)?.revision ?? 0) + 1;+ this.pending.set(attachment.attachmentId, { attachment, getContent, revision });+ this.scheduleUpdate();+ }++ release(attachmentId: string) {+ const save = this.pending.get(attachmentId);+ if (save?.getContent) {+ save.content = save.getContent();+ save.getContent = undefined;+ }+ }++ /** The attachments to save with the note, read at the time of the call. */+ collect(): SavedAttachments {+ const attachments: SavedAttachments = [];+ const revisions = new Map<string, number>();+ for (const [ attachmentId, save ] of this.pending) {+ const { role, mime, title } = save.attachment;+ attachments.push({ attachmentId, role, mime, title, content: readContent(save) });+ revisions.set(attachmentId, save.revision);+ }++ this.sentRevisions.set(attachments, revisions);+ return attachments;+ }++ /** Drops the changes that `attachments`, as returned by `collect()`, saved. */+ markSaved(attachments: SavedAttachments | undefined) {+ const revisions = attachments && this.sentRevisions.get(attachments);+ if (!revisions) {+ return;+ }++ for (const [ attachmentId, revision ] of revisions) {+ if (this.pending.get(attachmentId)?.revision === revision) {+ this.pending.delete(attachmentId);+ }+ }+ }+}++function readContent(save: PendingSave) {+ return save.getContent ? save.getContent() : save.content ?? "";+}diff --git a/apps/client/src/widgets/type_widgets/text/toolbar.ts b/apps/client/src/widgets/type_widgets/text/toolbar.tsindex c7ef2a35a7..f4d611037f 100644--- a/apps/client/src/widgets/type_widgets/text/toolbar.ts+++ b/apps/client/src/widgets/type_widgets/text/toolbar.ts@@ -95,6 +95,7 @@ export function buildClassicToolbar(multilineToolbar: boolean, aiAssistant: bool...(aiAssistant ? ["aiAssistant"] : []),"imageUpload","fileUpload",+ "drawingCanvas","blockQuote","admonition","insertTable",@@ -169,6 +170,7 @@ export function buildFloatingToolbar(aiAssistant: boolean) {"|","imageUpload","fileUpload",+ "drawingCanvas","blockQuote","admonition","codeBlock",diff --git a/apps/client/src/widgets/type_widgets/text/utils.ts b/apps/client/src/widgets/type_widgets/text/utils.tsindex d8e936a853..b8a55ec02b 100644--- a/apps/client/src/widgets/type_widgets/text/utils.ts+++ b/apps/client/src/widgets/type_widgets/text/utils.ts@@ -3,7 +3,7 @@ import { h, type JSX } from "preact";import appContext from "../../../components/app_context";import linkContextMenu from "../../../menus/link_context_menu";-import content_renderer from "../../../services/content_renderer";+import content_renderer, { type AttachmentEditor } from "../../../services/content_renderer";import { getEmbedCaption } from "../../../services/content_renderer_text";import froca from "../../../services/froca";import link, { ViewScope } from "../../../services/link";@@ -60,12 +60,14 @@ export async function loadEmbeddedNote(noteId: string, $el: JQuery<HTMLElement>,/*** Fills an embed box with an embedded attachment, under a title linking to it. Without a box- * size of its own, the box takes the one of its embed.+ * size of its own, the box takes the one of its embed. `attachmentEditor` saves the changes that+ * the content makes to the attachment, such as a canvas drawing.*/export async function loadEmbeddedAttachment(attachmentId: string,$el: JQuery<HTMLElement>,- boxSize?: string+ boxSize?: string,+ attachmentEditor?: AttachmentEditor) {const attachment = await froca.getAttachment(attachmentId, true);if (!attachment) return;@@ -98,7 +100,7 @@ export async function loadEmbeddedAttachment(});const { $renderedContent, type } = await content_renderer.getRenderedContent(attachment,- { interactive: true, mediaEnvironment: "embedded" }+ { interactive: true, mediaEnvironment: "embedded", attachmentEditor });const box = h(ContentEmbed, {diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.htmlindex f9e3270933..0ff3ffc5cf 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.html@@ -75,6 +75,9 @@<li>To embed an attachment that is already linked, right-click a link to itin a text note being edited and select <em>Convert link to an embed</em>,at the end of the menu.</li>+ <li>A drawing canvas, inserted with the <span class="tn-icon bx bx-pen"></span> <em>Insert drawing canvas</em> button,+ is an embed of a <code>canvas.json</code> attachment that can be drawn on+ in place (see <em>Drawing canvases</em> in <a class="reference-link" href="#root/_help_nBAXQFj20hS1">Include Note</a>).</li><li>An embed gets the box size that suits the attachment, as a new includednote does (see <em>Box sizes</em> there). To change its size, select theembed and use the box size menu in its toolbar, or the <em>Size</em> submenudiff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Presentation.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Presentation.htmlindex 4f1af11c44..a0db7eb0a3 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Presentation.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Presentation.html@@ -135,4 +135,4 @@ config:</ul><h2>Under the hood</h2><p>The Presentation view uses <a href="https://revealjs.com/">Reveal.js</a> to- handle the navigation and layout of the slides.</p>+ handle the navigation and layout of the slides.</p>\ No newline at end of filediff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Canvas.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Canvas.htmlindex d937524bb8..e812b866b3 100644--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Canvas.html+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Canvas.html@@ -25,4 +25,9 @@href="#root/_help_nBAXQFj20hS1">Include Note</a>: some notes are renderedas images (e.g. <a class="reference-link" href="#root/_help_gBbsAeiuUxI5">Mind Map</a>),whereas some render fully interactive such as <a class="reference-link"- href="#root/_help_GTwFsgaA0lCt">Collections</a>.</p>\ No newline at end of file+ href="#root/_help_GTwFsgaA0lCt">Collections</a>.</p>+<h2>Drawing in a text note</h2>+<p>To draw inside a <a class="reference-link" href="#root/_help_iPIMuisry3hd">Text</a> note+ instead of in a note of its own, insert a drawing canvas with the <span+ class="tn-icon bx bx-pen"></span> <em>Insert drawing canvas</em> button.+ See <em>Drawing canvases</em> in <a class="reference-link" href="#root/_help_nBAXQFj20hS1">Include Note</a>.</p>\ No newline at end of filediff --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 1d580e4104..af5be60d7e 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@@ -132,4 +132,36 @@<li>To return to the fitted size, double-click the image.</li></ul><p>In a <em>Small</em> or <em>Medium</em> box, the image is scaled down to fit- the height of the box. In fullscreen, it fits the screen.</p>\ No newline at end of file+ the height of the box. In fullscreen, it fits the screen.</p>+<h2>Drawing canvases</h2>+<p>A drawing canvas is an Excalidraw canvas, like the one of a <a class="reference-link"+ href="#root/_help_grjYqerjn243">Canvas</a> note, drawn directly in the text+ note. It is not a note of its own: the drawing is kept in a <code>canvas.json</code> <a+ href="#root/_help_0vhv7lsOLy82">attachment</a> of the text note and is saved+ together with the text.</p>+<ul>+ <li>To insert a drawing canvas, press the <span class="tn-icon bx bx-pen"></span> <em>Insert drawing canvas</em> button+ in the <a class="reference-link" href="#root/_help_nRhnJkTT8cPs">Formatting toolbar</a>,+ next to the <span class="tn-icon cke cke-paper-clip"></span> <em>Attach file</em> button.+ The canvas is added empty, in a <em>Medium</em> box with its title hidden.</li>+ <li>To draw, click the canvas 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.</li>+ <li>The box sizes, 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>+ <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 drawing+ can be viewed but not changed.</li>+</ul>+<h3>Limitations</h3>+<ul>+ <li>The Excalidraw shape library is not saved for a drawing canvas.</li>+ <li>Outside the editor, for example in a tooltip, when printing or in the+ list of attachments, the drawing is shown as a picture. On a <a href="#root/_help_R9pX4DGra2Vt">shared page</a>,+ it appears as a link to download <code>canvas.json</code>.</li>+ <li>A copy of a drawing canvas pasted in the same note shares the drawing+ with the original, so a change to one shows in the other only after the+ note is opened again. A copy pasted in another note gets a drawing of its+ own.</li>+</ul>\ No newline at end of filediff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.mdindex 06a6a5e16b..a49f3f87e4 100644--- a/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Notes/Attachments.md@@ -41,6 +41,7 @@ An attachment can be shown in the text instead of linked, the way <a class="ref* To attach files and embed them straight away, select _Attach and embed file_ from the arrow beside the <span class="tn-icon cke cke-paper-clip"></span> _Attach file_ button. Each file gets an embed of its own, which shows the file name while the file uploads.* To embed an attachment that is already linked, right-click a link to it in a text note being edited and select _Convert link to an embed_, at the end of the menu.+* A drawing canvas, inserted with the <span class="tn-icon bx bx-pen"></span> _Insert drawing canvas_ button, is an embed of a `canvas.json` attachment that can be drawn on in place (see _Drawing canvases_ in <a class="reference-link" href="../../Note%20Types/Text/Include%20Note.md">Include Note</a>).* An embed gets the box size that suits the attachment, as a new included note does (see _Box sizes_ there). To change its size, select the embed and use the box size menu in its toolbar, or the _Size_ submenu of its <span class="tn-icon bx bx-dots-vertical-rounded"></span> _More actions_ menu.* An embed can have a caption, as an included note can (see _Caption_ there).* To show only the attachment, select the embed and press the <span class="tn-icon bx bx-window-alt"></span> _Show title_ button in its toolbar, as for an included note (see _Title_ there).diff --git a/docs/User Guide/User Guide/Collections/Presentation.md b/docs/User Guide/User Guide/Collections/Presentation.mdindex 4a1157e0bb..97891a7826 100644--- a/docs/User Guide/User Guide/Collections/Presentation.md+++ b/docs/User Guide/User Guide/Collections/Presentation.md@@ -86,4 +86,4 @@ At slide level:## Under the hood-The Presentation view uses [Reveal.js](https://revealjs.com/) to handle the navigation and layout of the slides.+The Presentation view uses [Reveal.js](https://revealjs.com/) to handle the navigation and layout of the slides.\ No newline at end of filediff --git a/docs/User Guide/User Guide/Note Types/Canvas.md b/docs/User Guide/User Guide/Note Types/Canvas.mdindex c877a089bc..279ffa1b5d 100644--- a/docs/User Guide/User Guide/Note Types/Canvas.md+++ b/docs/User Guide/User Guide/Note Types/Canvas.md@@ -18,4 +18,8 @@ To embed a note:* From the <a class="reference-link" href="../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a>, drag the item onto the canvas.* Manually, by choosing _More tools_ → _Web Embed_ and pasting the link to the note (e.g. `root/MujWAc9fQ1nj`).-Embedding notes in the canvas follows the same rules as <a class="reference-link" href="Text/Include%20Note.md">Include Note</a>: some notes are rendered as images (e.g. <a class="reference-link" href="Mind%20Map.md">Mind Map</a>), whereas some render fully interactive such as <a class="reference-link" href="../Collections.md">Collections</a>.\ No newline at end of file+Embedding notes in the canvas follows the same rules as <a class="reference-link" href="Text/Include%20Note.md">Include Note</a>: some notes are rendered as images (e.g. <a class="reference-link" href="Mind%20Map.md">Mind Map</a>), whereas some render fully interactive such as <a class="reference-link" href="../Collections.md">Collections</a>.++## Drawing in a text note++To draw inside a <a class="reference-link" href="Text.md">Text</a> note instead of in a note of its own, insert a drawing canvas with the <span class="tn-icon bx bx-pen"></span> _Insert drawing canvas_ button. See _Drawing canvases_ in <a class="reference-link" href="Text/Include%20Note.md">Include Note</a>.\ No newline at end of filediff --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 ab63f485e5..f26d4a87f1 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@@ -87,4 +87,20 @@ An included image, whether an image note or an image attachment, is shown in the* To pan an image that is zoomed in, drag it. At its fitted size, dragging the image on a touch screen scrolls the note.* To return to the fitted size, double-click the image.-In a _Small_ or _Medium_ box, the image is scaled down to fit the height of the box. In fullscreen, it fits the screen.\ No newline at end of file+In a _Small_ or _Medium_ box, the image is scaled down to fit the height of the box. In fullscreen, it fits the screen.++## Drawing canvases++A 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.json` [attachment](../../Basic%20Concepts%20and%20Features/Notes/Attachments.md) of the text note and is saved together with the text.++* 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.+* To draw, click the canvas 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 box sizes, 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_.+* 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.++### Limitations++* The Excalidraw shape library is not saved for a drawing canvas.+* Outside the editor, for example in a tooltip, when printing or in the list of attachments, the drawing is shown as a picture. On a [shared page](../../Advanced%20Usage/Sharing.md), it appears as a link to download `canvas.json`.+* A copy of a drawing canvas pasted in the same note shares the drawing with the original, so a change to one shows in the other only after the note is opened again. A copy pasted in another note gets a drawing of its own.\ No newline at end of filediff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.tsindex 09a33c407e..fa6b15b2c1 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.spec.ts@@ -493,6 +493,30 @@ describe("ContentEmbed", () => {expect(keyStop).toHaveBeenCalled();});+ it("lets the embedded content handle a key, then keeps it from the editor", () => {+ insertContentEmbed(editor, "noteKeys", "small");++ const domRoot = editor.editing.view.getDomRoot();+ const wrapper = domRoot?.querySelector("div.include-note-wrapper");+ expect(wrapper).not.toBeNull();+ if (!domRoot || !wrapper) {+ return;+ }++ const content = document.createElement("div");+ wrapper.append(content);+ const contentHeard = vi.fn();+ const rootHeard = vi.fn();+ content.addEventListener("keydown", contentHeard);+ domRoot.addEventListener("keydown", rootHeard);++ content.dispatchEvent(new KeyboardEvent("keydown", { key: "r", bubbles: true }));++ expect(contentHeard).toHaveBeenCalledOnce();+ expect(rootHeard).not.toHaveBeenCalled();+ domRoot.removeEventListener("keydown", rootHeard);+ });+it("does nothing on a mousedown when the wrapper has no enclosing embed", () => {insertContentEmbed(editor, "noteDetached", "small");diff --git a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts b/packages/ckeditor5/src/plugins/content_embed/content_embed.tsindex 7fa21d0620..be562e20f5 100644--- a/packages/ckeditor5/src/plugins/content_embed/content_embed.ts+++ b/packages/ckeditor5/src/plugins/content_embed/content_embed.ts@@ -1043,7 +1043,8 @@ function preventCKEditorHandling( domElement: HTMLElement, editor: Editor ) {domElement.addEventListener( 'focus', stopEventPropagationAndHackRendererFocus, { capture: true } );// Prevents TAB handling or other editor keys listeners which might be executed on editors selection.- domElement.addEventListener( 'keydown', stopEventPropagationAndHackRendererFocus, { capture: true } );+ // Listens in the bubble phase, so the embedded content handles its own keys first.+ domElement.addEventListener( 'keydown', stopEventPropagationAndHackRendererFocus );function stopEventPropagationAndHackRendererFocus( evt: Event ) {evt.stopPropagation();diff --git a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.tsindex 5b54b7a7ee..5469470b7f 100644--- a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts+++ b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.spec.ts@@ -1,4 +1,5 @@import {+ ButtonView,ClassicEditor,Command,DropdownView,@@ -11,6 +12,7 @@ import {SplitButtonView} from "ckeditor5";import bxLink from "boxicons/svg/regular/bx-link.svg?raw";+import bxPen from "boxicons/svg/regular/bx-pen.svg?raw";import bxShapeSquare from "boxicons/svg/regular/bx-shape-square.svg?raw";import { beforeEach, describe, expect, it, vi } from "vitest";@@ -138,4 +140,54 @@ describe("FileUploadUI", () => {const labels = createDropdown().items.map((item) => item.label);expect(labels).toEqual([ "Attach file as a link" ]);});++ describe("drawingCanvas", () => {+ function createCanvasButton() {+ const button = editor.ui.componentFactory.create("drawingCanvas");+ if (!(button instanceof ButtonView)) {+ throw new Error("Expected a button.");+ }+ return button;+ }++ it("embeds an empty canvas drawing, medium and without a title, quietly", async () => {+ const button = createCanvasButton();+ const execute = vi.spyOn(editor, "execute").mockImplementation(() => undefined);+ const focus = vi.spyOn(editor.editing.view, "focus");++ expect([ button.label, button.icon, button.tooltip ])+ .toEqual([ "Insert drawing canvas", bxPen, true ]);++ button.fire("execute");++ expect(execute).toHaveBeenCalledExactlyOnceWith("fileUpload", {+ file: [ expect.any(File) ],+ asEmbed: true,+ boxSize: "medium",+ hideTitle: true,+ quiet: true+ });+ const [ file ] = (execute.mock.calls[0][1] as { file: File[] }).file;+ expect([ file.name, file.type ])+ .toEqual([ "canvas.json", "application/vnd.excalidraw+json" ]);+ expect(JSON.parse(await file.text())).toEqual({+ type: "excalidraw", version: 2, elements: [], files: {}, appState: {}+ });+ expect(focus).toHaveBeenCalled();+ });++ it("is enabled only where a file can be attached and an embed can go", async () => {+ const button = createCanvasButton();+ expect(button.isEnabled).toBe(true);++ embedCommand.forceDisabled("spec");+ expect(button.isEnabled).toBe(false);+ embedCommand.clearForceDisabled("spec");+ command.forceDisabled("spec");+ expect(button.isEnabled).toBe(false);++ editor = await createTestEditor([Essentials, Paragraph, FileUploadUI]);+ expect(createCanvasButton().isEnabled).toBe(false);+ });+ });});diff --git a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.tsindex 0756fc19b2..4eed96066f 100644--- a/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts+++ b/packages/ckeditor5/src/plugins/file_upload/file_upload_ui.ts@@ -1,4 +1,10 @@import {+ CANVAS_ATTACHMENT_MIME,+ CANVAS_ATTACHMENT_TITLE,+ EMPTY_CANVAS_CONTENT+} from "@triliumnext/commons";+import {+ ButtonView,createDropdown,FileDialogButtonView,FileDialogListItemButtonView,@@ -11,6 +17,7 @@ import {type Locale} from "ckeditor5";import bxLink from "boxicons/svg/regular/bx-link.svg?raw";+import bxPen from "boxicons/svg/regular/bx-pen.svg?raw";import bxShapeSquare from "boxicons/svg/regular/bx-shape-square.svg?raw";import type { FileUploadOptions } from "./fileuploadcommand.js";@@ -18,6 +25,8 @@ import type { FileUploadOptions } from "./fileuploadcommand.js";/*** Registers the `fileUpload` split button. The button attaches the picked files and links them;* the list also offers to embed them.+ *+ * Also registers the `drawingCanvas` button, which attaches an empty canvas drawing and embeds it.*/export default class FileUploadUI extends Plugin {static get pluginName() {@@ -72,6 +81,36 @@ export default class FileUploadUI extends Plugin {return dropdownView;});++ editor.ui.componentFactory.add("drawingCanvas", (locale) => {+ const button = new ButtonView(locale);+ button.set({ label: t("Insert drawing canvas"), icon: bxPen, tooltip: true });++ const uploadCommand = editor.commands.get("fileUpload");+ const embedCommand = editor.commands.get("insertContentEmbed");+ if (uploadCommand && embedCommand) {+ button.bind("isEnabled").to(uploadCommand, "isEnabled", embedCommand, "isEnabled",+ (canUpload, canEmbed) => canUpload && canEmbed);+ } else {+ 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,+ quiet: true+ });+ editor.editing.view.focus();+ });++ return button;+ });}private createListButton(diff --git a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.tsindex 7ea7e3cff4..364c945634 100644--- a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts+++ b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.spec.ts@@ -17,7 +17,7 @@ import { createTestEditor } from "../../../test/editor-kit.js";import { installGlobMock } from "../../../test/globals-test-kit.js";import ContentEmbed from "../content_embed/content_embed.js";import { isUploadAsLink } from "../uploadimage.js";-import FileUploadCommand from "./fileuploadcommand.js";+import FileUploadCommand, { isQuietUpload } from "./fileuploadcommand.js";/*** Minimal plugin that registers the 'reference' model schema AND the downcast@@ -184,6 +184,36 @@ describe("FileUploadCommand", () => {expect(getModelData(editor.model)).toMatch(/^<contentEmbed boxSize="medium" /);});+ it("gives the embeds the box size and hidden title the caller asks for", () => {+ const getEmbedBoxSize = vi.fn(() => "tiny");+ installGlobMock({ getComponentByEl: () => ({ getEmbedBoxSize }) });+ setModelData(editor.model, "<paragraph>[]</paragraph>");++ editor.execute("fileUpload", {+ file: [ new File(["{}"], "canvas.json", { type: "application/vnd.excalidraw+json" }) ],+ asEmbed: true,+ boxSize: "medium",+ hideTitle: true+ });++ expect(getModelData(editor.model)).toMatch(new RegExp(+ "^<contentEmbed boxSize=\"medium\" hideTitle=\"true\" uploadFileName=\"canvas.json\""+ ));+ expect(getEmbedBoxSize).not.toHaveBeenCalled();+ });++ it("marks the loaders of a quiet upload, and only those", () => {+ installGlobMock({ getComponentByEl: () => ({}) });+ setModelData(editor.model, "<paragraph>[]</paragraph>");+ const createLoaderSpy = vi.spyOn(editor.plugins.get(FileRepository), "createLoader");++ editor.execute("fileUpload", { file: [ new File(["1"], "one.txt") ] });+ editor.execute("fileUpload", { file: [ new File(["2"], "two.txt") ], quiet: true });++ expect(createLoaderSpy.mock.results.map(({ value }) => isQuietUpload(value as FileLoader)))+ .toEqual([ false, true ]);+ });+it("marks the loader of every file to upload as a link, for an embed too", () => {installGlobMock({ getComponentByEl: () => ({}) });setModelData(editor.model, "<paragraph>[]</paragraph>");diff --git a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.ts b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.tsindex d8f49bc2ab..1d8f93b300 100644--- a/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.ts+++ b/packages/ckeditor5/src/plugins/file_upload/fileuploadcommand.ts@@ -1,11 +1,32 @@-import { Command, FileRepository, type Editor, type Model, type ModelWriter } from "ckeditor5";+import {+ Command,+ FileRepository,+ type Editor,+ type FileLoader,+ type Model,+ type ModelWriter+} from "ckeditor5";+import type { BoxSizeValue } from "../content_embed/content_embed.js";import { uploadAsLink } from "../uploadimage.js";export interface FileUploadOptions {file: File[];/** Embeds each file in a block of its own instead of linking it. */asEmbed?: boolean;+ /** The box size of the embeds, instead of the one the host picks for the file type. */+ boxSize?: BoxSizeValue;+ /** Hides the title row of the embeds. */+ hideTitle?: boolean;+ /** Skips the `upload` event, for a file that the editor created itself. */+ quiet?: boolean;+}++const quietLoaders = new WeakSet<FileLoader>();++/** Whether the upload of `loader` is left out of the `upload` event. */+export function isQuietUpload(loader: FileLoader) {+ return quietLoaders.has(loader);}/**@@ -21,7 +42,7 @@ export default class FileUploadCommand extends Command {this.isEnabled = !!position && model.schema.checkChild(position, "reference");}- override execute({ file: files, asEmbed }: FileUploadOptions) {+ override execute({ file: files, asEmbed, boxSize, hideTitle, quiet }: FileUploadOptions) {const model = this.editor.model;const fileRepository = this.editor.plugins.get(FileRepository);@@ -34,9 +55,15 @@ export default class FileUploadCommand extends Command {}uploadAsLink(loader);+ if (quiet) {+ quietLoaders.add(loader);+ }+if (asEmbed) {- const boxSize = getEmbedBoxSize(this.editor, file);- insertEmbedPlaceholder(writer, model, loader.id, file.name, boxSize);+ insertEmbedPlaceholder(writer, model, loader.id, file.name, {+ boxSize: boxSize ?? getEmbedBoxSize(this.editor, file),+ hideTitle+ });} else {insertPlaceholder(writer, model, loader.id, file.name);}@@ -65,10 +92,11 @@ function insertEmbedPlaceholder(model: Model,uploadId: string,fileName: string,- boxSize: string+ { boxSize, hideTitle }: { boxSize: string; hideTitle?: boolean }) {const placeholder = writer.createElement("contentEmbed", {boxSize,+ ...(hideTitle ? { hideTitle: true } : {}),uploadId,uploadFileName: fileName});diff --git a/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.spec.ts b/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.spec.tsindex bcbb78b9f3..ef792cd016 100644--- a/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.spec.ts+++ b/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.spec.ts@@ -260,6 +260,18 @@ describe("FileUploadEditing", () => {}});+ it("uploads a quiet file without announcing it", async () => {+ const controls = installUploadAdapter(editor);+ const announced = vi.fn();+ editor.plugins.get(FileUploadEditing).on<FileUploadEvent>("upload", announced);+ setModelData(editor.model, "<paragraph>[]</paragraph>");++ editor.execute("fileUpload", { file: [ new File(["{}"], "canvas.json") ], quiet: true });+ await waitFor(() => controls.uploadCalled());++ expect(announced).not.toHaveBeenCalled();+ });+it("aborts the loader when the placeholder is inserted into the graveyard", () => {installUploadAdapter(editor);const fileRepository = editor.plugins.get(FileRepository);diff --git a/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.ts b/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.tsindex 3e4554142f..38b15e9519 100644--- a/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.ts+++ b/packages/ckeditor5/src/plugins/file_upload/fileuploadediting.ts@@ -9,7 +9,7 @@ import {type ViewRange} from 'ckeditor5';import { getAttachmentId } from '../referencelink.js';-import FileUploadCommand from './fileuploadcommand';+import FileUploadCommand, { isQuietUpload } from './fileuploadcommand';/** A file attachment upload, announced by the `upload` event of `FileUploadEditing`. */export interface FileUploadData {@@ -179,11 +179,13 @@ export default class FileUploadEditing extends Plugin {} );} );- this.fire<FileUploadEvent>( 'upload', {- fileName: String( fileElement.getAttribute( 'uploadFileName' ) ?? '' ),- loader,- done: Promise.allSettled( [ upload ] ).then( () => undefined )- } );+ if ( !isQuietUpload( loader ) ) {+ this.fire<FileUploadEvent>( 'upload', {+ fileName: String( fileElement.getAttribute( 'uploadFileName' ) ?? '' ),+ loader,+ done: Promise.allSettled( [ upload ] ).then( () => undefined )+ } );+ }return upload;diff --git a/packages/commons/src/lib/notes.spec.ts b/packages/commons/src/lib/notes.spec.tsindex e74b9ad734..0897516591 100644--- a/packages/commons/src/lib/notes.spec.ts+++ b/packages/commons/src/lib/notes.spec.ts@@ -1,7 +1,8 @@import { describe, expect, it } from "vitest";import {- GEO_LOCATION_ATTRIBUTE, GEO_SHAPE_ATTRIBUTE, getImageAttachmentTitle, getMimeIcon, getNoteIcon,+ CANVAS_ATTACHMENT_MIME, GEO_LOCATION_ATTRIBUTE, GEO_SHAPE_ATTRIBUTE, getImageAttachmentTitle,+ getMimeIcon, getNoteIcon,NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS, parseMindMapNoteLink} from "./notes.js";import { NoteType } from "./rows.js";@@ -243,6 +244,7 @@ describe("getMimeIcon", () => {expect(getMimeIcon("application/gpx+xml")).toBe("bx bx-trip");expect(getMimeIcon("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")).toBe("bx bx-spreadsheet");expect(getMimeIcon("text/csv")).toBe("bx bx-spreadsheet");+ expect(getMimeIcon(CANVAS_ATTACHMENT_MIME)).toBe(NOTE_TYPE_ICONS.canvas);expect(getMimeIcon("text/plain")).toBe("bx bx-file");});diff --git a/packages/commons/src/lib/notes.ts b/packages/commons/src/lib/notes.tsindex 0dcaf4be5b..135dcc9d55 100644--- a/packages/commons/src/lib/notes.ts+++ b/packages/commons/src/lib/notes.ts@@ -55,6 +55,21 @@ export function getImageAttachmentTitle(type: NoteType | null | undefined): strireturn (NOTE_TYPE_IMAGE_ATTACHMENTS as Partial<Record<NoteType, string>>)[type];}+/** The media type of a canvas drawing saved in an attachment, such as one in a text note. */+export const CANVAS_ATTACHMENT_MIME = "application/vnd.excalidraw+json";++/** The title of the attachment a new canvas drawing is saved in. */+export const CANVAS_ATTACHMENT_TITLE = "canvas.json";++/** The content of a new, empty canvas drawing. */+export const EMPTY_CANVAS_CONTENT = JSON.stringify({+ type: "excalidraw",+ version: 2,+ elements: [],+ files: {},+ appState: {}+});+/*** The note a mind map node's link points at, or `null` where it points somewhere else entirely.*@@ -89,6 +104,7 @@ const FILE_MIME_MAPPINGS: Record<string, string> = {"application/vnd.oasis.opendocument.spreadsheet": "bx bx-spreadsheet","application/vnd.ms-excel": "bx bx-spreadsheet","text/csv": "bx bx-spreadsheet",+ [CANVAS_ATTACHMENT_MIME]: NOTE_TYPE_ICONS.canvas};const IMAGE_MIME_MAPPINGS: Record<string, string> = {diff --git a/packages/trilium-core/src/services/notes.spec.ts b/packages/trilium-core/src/services/notes.spec.tsindex 2df6d42b96..cfca4e6f5d 100644--- a/packages/trilium-core/src/services/notes.spec.ts+++ b/packages/trilium-core/src/services/notes.spec.ts@@ -622,6 +622,32 @@ describe("notes service (real DB)", () => {/not available for change/);});++ it("updates an attachment of the note by its ID and skips any other attachment ID", () => {+ const note = createNote("root", { title: "spec-update-att", content: "<p>x</p>" }).note;+ const other = createNote("root", { title: "spec-update-att-other" }).note;+ const attachment = { role: "file", mime: "application/json", title: "canvas.json" };+ const save = (owner: BNote, content: string) =>+ getContext().init(() => owner.saveAttachment({ ...attachment, content }));+ const own = save(note, "old");+ const foreign = save(other, "theirs");+ const row = (attachmentId: string | undefined, content: string) =>+ ({ ...attachment, attachmentId, ownerId: note.noteId, content });+ // `becca.getAttachment()` reads the row again, with its current blob.+ const contentOf = (attachmentId: string | undefined) =>+ attachmentId ? becca.getAttachment(attachmentId)?.getContent() : undefined;++ getContext().init(() => noteService.updateNoteData(note.noteId, "<p>x</p>", [+ row(own.attachmentId, "new"),+ row(foreign.attachmentId, "mine"),+ row("deletedAttachment99", "gone")+ ]));++ expect(contentOf(own.attachmentId)).toBe("new");+ expect(contentOf(foreign.attachmentId)).toBe("theirs");+ expect(note.getAttachments().map((a) => a.attachmentId))+ .toStrictEqual([ own.attachmentId ]);+ });});describe("duplicateSubtree", () => {diff --git a/packages/trilium-core/src/services/notes.ts b/packages/trilium-core/src/services/notes.tsindex 4f9e5f0051..77dc1ab1f5 100644--- a/packages/trilium-core/src/services/notes.ts+++ b/packages/trilium-core/src/services/notes.ts@@ -1105,6 +1105,14 @@ function updateNoteData(noteId: string, content: string, attachments: Attachmentconst existingAttachmentsByTitle = toMap(note.getAttachments(), "title");for (const { attachmentId, role, mime, title, position, content, encoding } of attachments) {+ // An attachment deleted since the client read it, or one of another note, is not saved.+ if (attachmentId && becca.getAttachment(attachmentId)?.ownerId !== noteId) {+ getLog().info(+ `Skipped attachment '${attachmentId}', which note '${noteId}' does not own.`+ );+ continue;+ }+const decodedContent = encoding === "base64" && typeof content === "string"? decodeBase64(content) : content;diff --git a/packages/trilium-core/src/services/utils/index.spec.ts b/packages/trilium-core/src/services/utils/index.spec.tsindex f88c6f7804..c0d630a42b 100644--- a/packages/trilium-core/src/services/utils/index.spec.ts+++ b/packages/trilium-core/src/services/utils/index.spec.ts@@ -303,6 +303,11 @@ describe("#isStringNote", () => {"w/ 'undefined' note type and the InkML mime type, it should return true",[ undefined, "application/inkml+xml" ],true+ ],+ [+ "w/ 'undefined' note type and the mime type of a canvas drawing, it should return true",+ [ undefined, "application/vnd.excalidraw+json" ],+ true]];diff --git a/packages/trilium-core/src/services/utils/index.ts b/packages/trilium-core/src/services/utils/index.tsindex fdb6f277e8..7c4a6636ca 100644--- a/packages/trilium-core/src/services/utils/index.ts+++ b/packages/trilium-core/src/services/utils/index.ts@@ -1,4 +1,5 @@import { isFontMimeType } from "@triliumnext/commons/src/lib/font_mimes.js";+import { CANVAS_ATTACHMENT_MIME } from "@triliumnext/commons/src/lib/notes.js";import { getCrypto } from "../encryption/crypto";import { getPlatform } from "../platform";@@ -18,7 +19,10 @@ export function isLinux() { return getPlatform().isLinux; }// render and book are string note in the sense that they are expected to contain empty stringconst STRING_NOTE_TYPES = new Set(["text", "code", "relationMap", "search", "render", "book", "mermaid", "canvas", "webView"]);-const STRING_MIME_TYPES = new Set(["application/javascript", "application/x-javascript", "application/json", "application/x-sql", "image/svg+xml", "application/inkml+xml"]);+const STRING_MIME_TYPES = new Set([+ "application/javascript", "application/x-javascript", "application/json", "application/x-sql",+ "image/svg+xml", "application/inkml+xml", CANVAS_ATTACHMENT_MIME+]);export function hash(text: string) {return encodeBase64(getCrypto().createHash("sha1", text.normalize()));