Skip to content

Build your personal knowledge base with Trilium Notes

client/note attachments page: properly preview and open Excalidraw files

Adorian Doran818bfcd70e73
diff --git a/apps/client/src/services/content_renderer.spec.ts b/apps/client/src/services/content_renderer.spec.ts
index 74dd1e6ed7..ea1631bbc5 100644
--- a/apps/client/src/services/content_renderer.spec.ts
+++ b/apps/client/src/services/content_renderer.spec.ts
@@ -127,7 +127,8 @@ import {
disposeInteractiveContent,
getEmbedBoxSize,
getRenderedContent as rawGetRenderedContent,
- getUploadBoxSize
+ getUploadBoxSize,
+ hasRenderedPreview
} from "./content_renderer.js";
import froca from "./froca.js";
import server from "./server.js";
@@ -217,6 +218,23 @@ describe("getEmbedBoxSize", () => {
});
});
+describe("hasRenderedPreview", () => {
+ it("tells a file it draws, such as a canvas drawing, from one it shows as an icon", () => {
+ expect([
+ buildAttachment({ role: "file", mime: "application/vnd.excalidraw+json" }),
+ buildAttachment({ role: "importSource", mime: "application/json" }),
+ buildAttachment({ role: "file", mime: "application/pdf" }),
+ buildNote({ title: "Site", type: "webView", "#webViewSrc": "https://example.com" })
+ ].map(hasRenderedPreview)).toEqual([ true, true, true, true ]);
+
+ expect([
+ buildAttachment({ role: "file", mime: "text/plain" }),
+ buildAttachment({ role: "file", mime: "application/zip" }),
+ buildNote({ title: "Blank page", type: "webView" })
+ ].map(hasRenderedPreview)).toEqual([ false, false, false ]);
+ });
+});
+
describe("getRenderedContent dispatch", () => {
it("renders text/book via renderText and applies css class", async () => {
const note = buildNote({ title: "T", type: "text", "#cssClass": "my-class" });
diff --git a/apps/client/src/services/content_renderer.ts b/apps/client/src/services/content_renderer.ts
index 4af8f3836a..0bbea011b6 100644
--- a/apps/client/src/services/content_renderer.ts
+++ b/apps/client/src/services/content_renderer.ts
@@ -761,12 +761,18 @@ async function showRenderError($content: JQuery<HTMLElement>, error: unknown, no
* it, `small` for audio, `full` for code and `medium` for the rest.
*/
export function getEmbedBoxSize(entity: FNote | FAttachment): BoxSize {
+ return getBoxSize(getContentType(entity), hasRenderedPreview(entity));
+}
+
+/**
+ * Whether an interactive `getRenderedContent()` shows a preview of `entity`, rather than an icon
+ * or the actions of a file.
+ */
+export function hasRenderedPreview(entity: FNote | FAttachment) {
const type = getContentType(entity);
- const hasPreview = type === "webView"
+ return type === "webView"
? entity instanceof FNote && entity.hasLabel("webViewSrc")
: PREVIEWED_TYPES.has(type);
-
- return getBoxSize(type, hasPreview);
}
/** The box size of a new embed of a file being uploaded, from the attachment it becomes. */
diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsx
index 7a4bdc24d1..1aa043a57f 100644
--- a/apps/client/src/widgets/react/hooks.tsx
+++ b/apps/client/src/widgets/react/hooks.tsx
@@ -185,26 +185,34 @@ export function useEditorSpacedUpdate({ note, noteType, noteContext, getData, on
spacedUpdate.setUpdateInterval(updateInterval);
}, [ updateInterval ]);
- // Save if needed upon switching tabs.
+ useSaveBeforeLeaving(spacedUpdate, noteContext);
+
+ return spacedUpdate;
+}
+
+/**
+ * Saves the pending changes of `spacedUpdate` before the note of `noteContext` switches, before
+ * its tab closes and before the window closes.
+ */
+export function useSaveBeforeLeaving<T>(
+ spacedUpdate: SpacedUpdate<T>,
+ noteContext: NoteContext | null | undefined
+) {
useTriliumEvent("beforeNoteSwitch", async ({ noteContext: eventNoteContext }) => {
if (eventNoteContext.ntxId !== noteContext?.ntxId) return;
await spacedUpdate.updateNowIfNecessary();
});
- // Save if needed upon tab closing.
useTriliumEvent("beforeNoteContextRemove", async ({ ntxIds }) => {
if (!noteContext?.ntxId || !ntxIds.includes(noteContext.ntxId)) return;
await spacedUpdate.updateNowIfNecessary();
});
- // Save if needed upon window/browser closing.
useEffect(() => {
const listener = () => spacedUpdate.isAllSavedAndTriggerUpdate();
appContext.addBeforeUnloadListener(listener);
return () => appContext.removeBeforeUnloadListener(listener);
- }, []);
-
- return spacedUpdate;
+ }, [ spacedUpdate ]);
}
export function useBlobEditorSpacedUpdate({ note, noteType, noteContext, getData, onContentChange, dataSaved, updateInterval, replaceWithoutRevision }: {
@@ -278,24 +286,7 @@ export function useBlobEditorSpacedUpdate({ note, noteType, noteContext, getData
spacedUpdate.setUpdateInterval(updateInterval);
}, [ updateInterval ]);
- // Save if needed upon switching tabs.
- useTriliumEvent("beforeNoteSwitch", async ({ noteContext: eventNoteContext }) => {
- if (eventNoteContext.ntxId !== noteContext?.ntxId) return;
- await spacedUpdate.updateNowIfNecessary();
- });
-
- // Save if needed upon tab closing.
- useTriliumEvent("beforeNoteContextRemove", async ({ ntxIds }) => {
- if (!noteContext?.ntxId || !ntxIds.includes(noteContext.ntxId)) return;
- await spacedUpdate.updateNowIfNecessary();
- });
-
- // Save if needed upon window/browser closing.
- useEffect(() => {
- const listener = () => spacedUpdate.isAllSavedAndTriggerUpdate();
- appContext.addBeforeUnloadListener(listener);
- return () => appContext.removeBeforeUnloadListener(listener);
- }, []);
+ useSaveBeforeLeaving(spacedUpdate, noteContext);
return spacedUpdate;
}
diff --git a/apps/client/src/widgets/type_widgets/Attachment.css b/apps/client/src/widgets/type_widgets/Attachment.css
index 1ba08e7301..c64f03b5a3 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.css
+++ b/apps/client/src/widgets/type_widgets/Attachment.css
@@ -152,6 +152,12 @@
max-height: 220px;
object-fit: contain;
}
+
+ .attachment-content-wrapper .canvas-drawing-picture {
+ display: block;
+ max-height: 220px;
+ margin-inline: auto;
+ }
}
/* The header stays put and only the listing under it scrolls, so the owning note and the upload actions
@@ -350,6 +356,14 @@ body[dir=rtl] .attachment-icon-marker {
overflow: hidden;
}
+.attachment-content-wrapper.attachment-canvas-drawing {
+ overflow: hidden;
+
+ > .canvas-render {
+ height: 100%;
+ }
+}
+
/*
* The countdown to an unlinked attachment's erasure.
*
diff --git a/apps/client/src/widgets/type_widgets/Attachment.spec.tsx b/apps/client/src/widgets/type_widgets/Attachment.spec.tsx
index c1ac7b4d91..6046d33879 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/Attachment.spec.tsx
@@ -1,7 +1,19 @@
+import { useEffect } from "preact/hooks";
+import { act } from "preact/test-utils";
import { describe, expect, it, vi } from "vitest";
+import Component from "../../components/component";
import FAttachment from "../../entities/fattachment";
+import froca from "../../services/froca";
+import LoadResults from "../../services/load_results";
import { renderInto } from "../../test/render";
+import { ParentComponent } from "../react/react_utils";
+
+const CANVAS_MIME = "application/vnd.excalidraw+json";
+const { getRenderedContent, drawingMounts } = vi.hoisted(() => ({
+ getRenderedContent: vi.fn(async () => ({ $renderedContent: [] })),
+ drawingMounts: vi.fn()
+}));
// Nothing initialises i18next in a unit test, and an uninitialised one answers every lookup with an
// empty string — which would make the two placeholder messages indistinguishable. Answering with the
@@ -15,8 +27,16 @@ vi.mock("i18next", () => {
// were built at all.
vi.mock("../../services/content_renderer", () => ({
default: {
- getRenderedContent: async () => ({ $renderedContent: [] }),
+ getRenderedContent,
disposeInteractiveContent: () => {}
+ },
+ hasRenderedPreview: (attachment: FAttachment) => attachment.mime === CANVAS_MIME
+}));
+// Counts the mounts of the drawing editor, which remounts to load a change saved elsewhere.
+vi.mock("./canvas/CanvasDrawing", () => ({
+ CanvasDrawingDetail: () => {
+ useEffect(() => drawingMounts(), []);
+ return <div className="drawing-detail-stub" />;
}
}));
// Bootstrap's dropdown and the note link both reach well past this widget; the compression dialog only
@@ -25,7 +45,7 @@ vi.mock("../react/Dropdown", () => ({ default: () => <div class="dropdown-stub"
vi.mock("../react/NoteLink", () => ({ default: () => <span class="note-link-stub" /> }));
vi.mock("../dialogs/image_compression/image_compression_dialog", () => ({ showImageCompressionDialog: vi.fn() }));
-const { AttachmentList } = await import("./Attachment");
+const { AttachmentDetail, AttachmentList } = await import("./Attachment");
describe("AttachmentList", () => {
it("lists what the reader placed, with nothing to unfold where the app has made nothing", async () => {
@@ -61,6 +81,17 @@ describe("AttachmentList", () => {
expect(container.querySelector(".attachment-system-group .collapsible-title")?.getAttribute("aria-expanded")).toBe("false");
});
+ it("previews a file as text only where the renderer draws nothing of it", async () => {
+ const drawingBlob = vi.fn(async () => ({ content: "{}" }));
+ const drawing = { ...role("file"), mime: CANVAS_MIME, getBlob: drawingBlob };
+ const text = { ...role("file"), mime: "text/plain", getBlob: async () => ({ content: "hi" }) };
+ const container = await mount([ drawing, text ] as FAttachment[]);
+
+ await vi.waitFor(() => expect(container.querySelector(".file-preview-content")).not.toBeNull());
+ expect(container.querySelectorAll(".file-preview-content")).toHaveLength(1);
+ expect(drawingBlob).not.toHaveBeenCalled();
+ });
+
it("says plainly that there is nothing where a note carries nothing at all", async () => {
const container = await mount([]);
@@ -72,6 +103,47 @@ describe("AttachmentList", () => {
});
});
+describe("AttachmentDetail", () => {
+ it("edits a canvas drawing, loading again only what another component saved", async () => {
+ const drawing = { ...role("file"), mime: CANVAS_MIME } as FAttachment;
+ vi.spyOn(froca, "getAttachment").mockResolvedValue(drawing);
+ getRenderedContent.mockClear();
+ drawingMounts.mockClear();
+
+ const component = new Component();
+ const props = {
+ note: { noteId: "note1" },
+ viewScope: { viewMode: "attachments", attachmentId: drawing.attachmentId }
+ } as unknown as Parameters<typeof AttachmentDetail>[0];
+ const container = renderInto(
+ <ParentComponent.Provider value={component}>
+ <AttachmentDetail {...props} />
+ </ParentComponent.Provider>
+ );
+
+ await vi.waitFor(() => expect(drawingMounts).toHaveBeenCalledOnce());
+ expect(container.querySelector(".attachment-canvas-drawing .drawing-detail-stub")).not.toBeNull();
+ expect(getRenderedContent).not.toHaveBeenCalled();
+
+ // Its own save, and a change to another attachment, keep the editor as it is.
+ await reloadAttachment(component, drawing.attachmentId, component.componentId);
+ await reloadAttachment(component, "otherAttachment", "otherComponent");
+ expect(drawingMounts).toHaveBeenCalledOnce();
+
+ await reloadAttachment(component, drawing.attachmentId, "otherComponent");
+ await vi.waitFor(() => expect(drawingMounts).toHaveBeenCalledTimes(2));
+ });
+});
+
+/** Reports a saved change to an attachment, as the server does after a save from `componentId`. */
+async function reloadAttachment(component: Component, attachmentId: string, componentId: string) {
+ const loadResults = new LoadResults([]);
+ loadResults.addAttachmentRow({ attachmentId, ownerId: "note1" } as never, componentId);
+ await act(async () => {
+ await component.handleEvent("entitiesReloaded", { loadResults });
+ });
+}
+
function role(role: string) {
return {
attachmentId: `att-${role}-${Math.random().toString(36).slice(2)}`,
diff --git a/apps/client/src/widgets/type_widgets/Attachment.tsx b/apps/client/src/widgets/type_widgets/Attachment.tsx
index 29f3a0142e..894053228d 100644
--- a/apps/client/src/widgets/type_widgets/Attachment.tsx
+++ b/apps/client/src/widgets/type_widgets/Attachment.tsx
@@ -1,8 +1,11 @@
import "./Attachment.css";
-import { attachmentIcon, isImageAttachmentRole } from "@triliumnext/commons";
+import {
+ type AttachmentRow, attachmentIcon, CANVAS_ATTACHMENT_MIME, isImageAttachmentRole
+} from "@triliumnext/commons";
import { t } from "i18next";
import { Fragment } from "preact";
+import { lazy, Suspense } from "preact/compat";
import { useContext, useEffect, useMemo, useRef, useState } from "preact/hooks";
import type NoteContext from "../../components/note_context";
@@ -16,11 +19,12 @@ import {
} from "../../services/attachment_actions";
import { partitionAttachmentsByGroup } from "../../services/attachment_groups";
import { attachmentRoleLabel } from "../../services/attachment_role_names";
-import content_renderer from "../../services/content_renderer";
+import content_renderer, { hasRenderedPreview } from "../../services/content_renderer";
import froca from "../../services/froca";
import image from "../../services/image";
import { type ViewScope } from "../../services/link";
import options from "../../services/options";
+import protected_session_holder from "../../services/protected_session_holder";
import utils from "../../services/utils";
import { showImageCompressionDialog } from "../dialogs/image_compression/image_compression_dialog";
import ActionButton from "../react/ActionButton";
@@ -41,6 +45,9 @@ import { TextPreview } from "./File";
import MediaPreview from "./file/MediaPreview";
import { TypeWidgetProps } from "./type_widget";
+const CanvasDrawingDetail = lazy(() =>
+ import("./canvas/CanvasDrawing").then((module) => module.CanvasDrawingDetail));
+
/**
* Displays the full list of attachments of a note and allows the user to interact with them.
*/
@@ -198,6 +205,7 @@ export function AttachmentDetail({ note, viewScope, noteContext }: TypeWidgetPro
}
function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, viewScope }: { attachment: FAttachment, isFullDetail?: boolean, ownerNote?: FNote, noteContext?: NoteContext, viewScope?: ViewScope }) {
+ const parentComponent = useContext(ParentComponent);
const contentWrapper = useRef<HTMLDivElement>(null);
const imageViewerWrapper = useRef<HTMLDivElement>(null);
const [ title, setTitle ] = useState(attachment.title);
@@ -209,9 +217,12 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view
// Same reason, for the content itself: replacing an attachment changes neither its id nor its title, so
// without this nothing here re-renders and the viewer/player would keep showing what it first loaded.
const [ modified, setModified ] = useState(attachment.utcDateModified);
+ // Counts the changes that another component saved, which the drawing editor loads again.
+ const [ externalRevision, setExternalRevision ] = useState(0);
// "importSource" attachments (e.g. OneNote debug source) behave like ordinary files for
// preview, OCR and link-copying purposes.
const isFileLike = isFileLikeAttachment(attachment);
+ const hasTextPreview = isFileLike && !hasRenderedPreview(attachment);
const isPicture = isImageAttachmentRole(attachment.role);
// Opened in full detail, an image gets the interactive zoom/pan viewer and audio/video the full media
@@ -220,7 +231,11 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view
// in either view, is rendered imperatively via the content renderer.
const isZoomableImage = !!isFullDetail && isPicture;
const isPlayableMedia = !!isFullDetail && (attachment.mime.startsWith("audio/") || attachment.mime.startsWith("video/"));
- const rendersItself = isZoomableImage || isPlayableMedia;
+ // A canvas drawing gets the Excalidraw editor, which saves with `useAttachmentEditor()`.
+ const isEditableDrawing = !!isFullDetail && !!ownerNote && isFileLike
+ && attachment.mime === CANVAS_ATTACHMENT_MIME
+ && (!attachment.isProtected || protected_session_holder.isProtectedSessionAvailable());
+ const rendersItself = isZoomableImage || isPlayableMedia || isEditableDrawing;
const imageSrc = `api/attachments/${attachment.attachmentId}/image/${encodeURIComponent(attachment.title)}?${modified}`;
/** Unmounts whatever the content renderer previously mounted here (a media player), so that replacing
@@ -241,7 +256,7 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view
});
}
- if (isFileLike) {
+ if (hasTextPreview) {
attachment.getBlob().then(blob => setTextContent(blob?.content ?? null));
}
@@ -255,9 +270,13 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view
return disposeContent;
}, [ attachment, rendersItself ]);
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
- if (loadResults.getAttachmentRows().find(attachment => attachment.attachmentId)) {
- refresh();
+ const isThis = (row: AttachmentRow) => row.attachmentId === attachment.attachmentId;
+ if (!loadResults.getAttachmentRows().some(isThis)) return;
+
+ if (loadResults.getAttachmentRows(parentComponent?.componentId).some(isThis)) {
+ setExternalRevision((revision) => revision + 1);
}
+ refresh();
});
// Electron right-click menu (copy image / reference) for the interactive image viewer.
@@ -330,6 +349,20 @@ function AttachmentInfo({ attachment, isFullDetail, ownerNote, noteContext, view
viewScope={viewScope}
/>
</div>
+ ) : isEditableDrawing && ownerNote ? (
+ <div
+ key="canvas-drawing"
+ className="attachment-content-wrapper attachment-canvas-drawing"
+ >
+ <Suspense fallback={null}>
+ <CanvasDrawingDetail
+ key={`${attachment.attachmentId}-${externalRevision}`}
+ attachment={attachment}
+ note={ownerNote}
+ noteContext={noteContext}
+ />
+ </Suspense>
+ </div>
) : (
<div key="rendered" ref={contentWrapper} className="attachment-content-wrapper" />
)}
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
index cb38602062..6139378c49 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.spec.tsx
@@ -1,9 +1,11 @@
+import { exportToSvg } from "@excalidraw/excalidraw";
import { type ComponentChildren, render, toChildArray, type VNode } from "preact";
import { useEffect, useRef, useState } from "preact/hooks";
import { act } from "preact/test-utils";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import type { AttachmentEditor } from "../../../services/content_renderer";
+import { buildNote } from "../../../test/easy-froca";
vi.mock("@excalidraw/excalidraw", () => ({ exportToSvg: vi.fn() }));
vi.mock("./Canvas", () => ({ CanvasEditor: MockCanvasEditor }));
@@ -11,8 +13,12 @@ vi.mock("./persistence", () => ({
useCanvasDrawingPersistence: (...args: unknown[]) => {
persistenceArgs(...args);
return {};
- }
+ },
+ parseContent: (content: string) => JSON.parse(content),
+ getInlineFiles: () => ({})
}));
+const detailEditor = vi.hoisted(() => ({ canEdit: () => true, release: () => {} }));
+vi.mock("../text/attachment_saves", () => ({ useAttachmentEditor: () => detailEditor }));
const canvasEditorProps = vi.fn();
const persistenceArgs = vi.fn();
@@ -43,7 +49,8 @@ globalThis.ResizeObserver = class {
} as unknown as typeof ResizeObserver;
const {
- default: CanvasDrawing, useIsToolbarOverPanel, useSidePanels
+ default: CanvasDrawing, CanvasDrawingDetail, renderCanvasDrawingPicture, useIsToolbarOverPanel,
+ useSidePanels
} = await import("./CanvasDrawing");
const { default: CanvasDrawingMenu } = await import("./CanvasDrawingMenu");
const { default: CanvasEmbedTools } = await import("./CanvasEmbedTools");
@@ -404,6 +411,28 @@ describe("CanvasDrawing", () => {
editable.remove();
});
+ it("edits the drawing in the detail of its attachment unless its note is read-only", async () => {
+ async function mountDetail(title: string, labels: Record<string, string> = {}) {
+ const note = buildNote({ title, ...labels });
+ await act(async () => {
+ render(<CanvasDrawingDetail attachment={ATTACHMENT} note={note} />, box);
+ });
+ }
+
+ await mountDetail("Owner");
+ expect(getEditingState()).toEqual({ isReadOnly: false, menu: true, tools: undefined });
+ expect(persistenceArgs).toHaveBeenLastCalledWith(
+ ATTACHMENT, detailEditor, expect.anything(), expect.anything()
+ );
+
+ render(null, box);
+ await mountDetail("Locked owner", { "#readOnly": "" });
+ expect(getEditingState()).toEqual({ isReadOnly: true, menu: false, tools: undefined });
+ expect(persistenceArgs).toHaveBeenLastCalledWith(
+ ATTACHMENT, undefined, expect.anything(), expect.anything()
+ );
+ });
+
it("keeps the desktop layout of Excalidraw in the desktop layout of Trilium only", async () => {
const device = window.glob.device;
await mount();
@@ -420,3 +449,26 @@ describe("CanvasDrawing", () => {
);
});
});
+
+describe("renderCanvasDrawingPicture", () => {
+ it("draws in the dark colors of a dark theme on screen, and in the light ones in print", async () => {
+ vi.mocked(exportToSvg).mockImplementation(async () =>
+ document.createElementNS("http://www.w3.org/2000/svg", "svg"));
+ const content = JSON.stringify({ elements: [ { id: "e1" } ], appState: {} });
+ const drawing = { getBlob: async () => ({ content }) } as never;
+ const { theme, device } = window.glob;
+
+ window.glob.theme = "next-dark";
+ const picture = await renderCanvasDrawingPicture(drawing);
+ window.glob.device = "print";
+ await renderCanvasDrawingPicture(drawing);
+ window.glob.theme = "next-light";
+ window.glob.device = device;
+ await renderCanvasDrawingPicture(drawing);
+ window.glob.theme = theme;
+
+ expect(picture?.classList.contains("canvas-drawing-picture")).toBe(true);
+ expect(vi.mocked(exportToSvg).mock.calls.map(([ { appState } ]) => appState?.exportWithDarkMode))
+ .toEqual([ true, false, false ]);
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
index 4e40f494c0..d4003164f5 100644
--- a/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
+++ b/apps/client/src/widgets/type_widgets/canvas/CanvasDrawing.tsx
@@ -6,12 +6,15 @@ import clsx from "clsx";
import type { RefObject } from "preact";
import { useEffect, useRef, useState } from "preact/hooks";
+import type NoteContext from "../../../components/note_context";
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 { getEffectiveThemeStyle } from "../../../services/theme";
import { isDesktop } from "../../../services/utils";
-import { useColorScheme } from "../../react/hooks";
+import { useColorScheme, useEffectiveReadOnly } from "../../react/hooks";
+import { useAttachmentEditor } from "../text/attachment_saves";
import { useIsContentEmbedEditable } from "../text/content_embed_tools";
import { CanvasEditor } from "./Canvas";
import CanvasDrawingMenu from "./CanvasDrawingMenu";
@@ -79,7 +82,12 @@ export async function renderCanvasDrawingPicture(entity: FNote | FAttachment) {
const svg = await exportToSvg({
elements,
- appState: { ...content.appState, exportBackground: true },
+ appState: {
+ ...content.appState,
+ exportBackground: true,
+ // Follows the theme as the editor does, except in print, which stays light.
+ exportWithDarkMode: glob.device !== "print" && getEffectiveThemeStyle() === "dark"
+ },
files: getInlineFiles(content),
exportPadding: 5
});
@@ -87,6 +95,43 @@ export async function renderCanvasDrawingPicture(entity: FNote | FAttachment) {
return svg;
}
+interface CanvasDrawingDetailProps {
+ attachment: FAttachment;
+ /** The note that owns the attachment. */
+ note: FNote;
+ noteContext?: NoteContext;
+}
+
+/**
+ * A canvas drawing saved in an attachment, edited in the full detail of the attachment. It is
+ * read-only while its note is.
+ */
+export function CanvasDrawingDetail({ attachment, note, noteContext }: CanvasDrawingDetailProps) {
+ const apiRef = useRef<ExcalidrawImperativeAPI>(null);
+ const colorScheme = useColorScheme();
+ const editor = useAttachmentEditor(note, noteContext);
+ const isNoteReadOnly = useEffectiveReadOnly(note, noteContext);
+ const canEdit = editor.canEdit(attachment) && !isNoteReadOnly
+ && !options.is("databaseReadonly");
+ const persistence = useCanvasDrawingPersistence(
+ attachment,
+ canEdit ? editor : undefined,
+ apiRef,
+ colorScheme
+ );
+
+ return (
+ <CanvasEditor
+ apiRef={apiRef}
+ isReadOnly={!canEdit}
+ colorScheme={colorScheme}
+ persistence={persistence}
+ >
+ <CanvasDrawingMenu apiRef={apiRef} isEditable={canEdit} />
+ </CanvasEditor>
+ );
+}
+
/**
* Moves the focus into Excalidraw when the embed box around the drawing holds it. Excalidraw
* renders its container only once its language loads, which can be after the box took the focus.
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.ts
index 5abba4dad0..157db4cfab 100644
--- a/apps/client/src/widgets/type_widgets/text/attachment_saves.spec.ts
+++ b/apps/client/src/widgets/type_widgets/text/attachment_saves.spec.ts
@@ -1,7 +1,19 @@
+import { h, render } from "preact";
+import { act } from "preact/test-utils";
import { describe, expect, it, vi } from "vitest";
+import Component from "../../../components/component";
+import type NoteContext from "../../../components/note_context";
import type FAttachment from "../../../entities/fattachment";
-import AttachmentSaves from "./attachment_saves";
+import type FNote from "../../../entities/fnote";
+import type { AttachmentEditor } from "../../../services/content_renderer";
+import { ParentComponent } from "../../react/react_utils";
+import AttachmentSaves, { useAttachmentEditor } from "./attachment_saves";
+
+const serverPost = vi.hoisted(() => vi.fn(async () => undefined));
+vi.mock("../../../services/server", () => ({
+ default: { get: async () => [], post: serverPost }
+}));
const drawing = {
attachmentId: "drawing1",
@@ -96,3 +108,49 @@ describe("AttachmentSaves", () => {
expect(saves.getUnsavedContent("drawing1")).toBeUndefined();
});
});
+
+describe("useAttachmentEditor", () => {
+ const note = { noteId: "note1", isProtected: false } as FNote;
+ const noteContext = { ntxId: "ntx1", setContextData: vi.fn() } as unknown as NoteContext;
+
+ function savedRequest(content: string) {
+ return [ "notes/note1/attachments", {
+ attachmentId: "drawing1",
+ role: "file",
+ mime: "application/vnd.excalidraw+json",
+ title: "Canvas.excalidraw",
+ content
+ }, component.componentId ];
+ }
+
+ let component: Component;
+ let editor: AttachmentEditor | undefined;
+ function Probe() {
+ editor = useAttachmentEditor(note, noteContext);
+ return null;
+ }
+
+ it("saves on its own before the note switches, and once the content goes away", async () => {
+ component = new Component();
+ const container = document.createElement("div");
+ await act(() => {
+ render(h(ParentComponent.Provider, { value: component }, h(Probe, {})), container);
+ });
+ expect(editor?.canEdit(drawing)).toBe(true);
+
+ editor?.scheduleSave(drawing, () => "first");
+ await act(async () => {
+ await component.handleEvent("beforeNoteSwitch", { noteContext } as never);
+ });
+ expect(serverPost).toHaveBeenCalledExactlyOnceWith(...savedRequest("first"));
+ expect(editor?.getUnsavedContent("drawing1")).toBeUndefined();
+ expect(noteContext.setContextData).toHaveBeenLastCalledWith("saveState", { state: "saved" });
+
+ editor?.scheduleSave(drawing, () => "second");
+ editor?.release("drawing1");
+ // Sent at once, not once the timer of the spaced update runs out.
+ await act(() => render(null, container));
+ expect(serverPost).toHaveBeenCalledTimes(2);
+ expect(serverPost).toHaveBeenLastCalledWith(...savedRequest("second"));
+ });
+});
diff --git a/apps/client/src/widgets/type_widgets/text/attachment_saves.ts b/apps/client/src/widgets/type_widgets/text/attachment_saves.ts
index b11b4c0cea..906dbf864e 100644
--- a/apps/client/src/widgets/type_widgets/text/attachment_saves.ts
+++ b/apps/client/src/widgets/type_widgets/text/attachment_saves.ts
@@ -1,6 +1,14 @@
+import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";
+
+import type NoteContext from "../../../components/note_context";
import type FAttachment from "../../../entities/fattachment";
+import type FNote from "../../../entities/fnote";
import type { AttachmentEditor } from "../../../services/content_renderer";
-import type { SavedData } from "../../react/hooks";
+import protected_session_holder from "../../../services/protected_session_holder";
+import server from "../../../services/server";
+import SpacedUpdate from "../../../services/spaced_update";
+import { type SavedData, useSaveBeforeLeaving } from "../../react/hooks";
+import { ParentComponent } from "../../react/react_utils";
type SavedAttachments = NonNullable<SavedData["attachments"]>;
@@ -15,8 +23,8 @@ interface PendingSave {
}
/**
- * 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.
+ * The attachment changes that content, such as a canvas drawing, makes. A text note saves them
+ * together with its own content, and `useAttachmentEditor()` saves them on their own.
*/
export default class AttachmentSaves implements AttachmentEditor {
private noteId: string | undefined;
@@ -86,6 +94,60 @@ export default class AttachmentSaves implements AttachmentEditor {
}
}
+/**
+ * Saves the changes that content shown outside a text note, such as a canvas drawing in the full
+ * detail of its attachment, makes to the attachments of `note`.
+ */
+export function useAttachmentEditor(
+ note: FNote,
+ noteContext: NoteContext | undefined
+): AttachmentEditor {
+ const parentComponent = useContext(ParentComponent);
+ const [ saves ] = useState(() => {
+ const saves = new AttachmentSaves(() => spacedUpdate.scheduleUpdate());
+ saves.setNoteId(note.noteId);
+ return saves;
+ });
+ const noteContextRef = useRef(noteContext);
+ noteContextRef.current = noteContext;
+
+ const prepare = useCallback(() => saves.collect(), [ saves ]);
+ const commit = useCallback(async (attachments: SavedAttachments) => {
+ protected_session_holder.touchProtectedSessionIfNecessary(note);
+ for (const attachment of attachments) {
+ await server.post(
+ `notes/${note.noteId}/attachments`,
+ attachment,
+ parentComponent?.componentId
+ );
+ }
+ saves.markSaved(attachments);
+ }, [ note, parentComponent, saves ]);
+
+ const [ spacedUpdate ] = useState(() => new SpacedUpdate<SavedAttachments>(
+ { key: note.noteId, prepare, commit },
+ undefined,
+ (state) => noteContextRef.current?.setContextData("saveState", { state })
+ ));
+
+ // `rebind()` takes the changes to the previous note before `setNoteId()` drops them.
+ useEffect(() => {
+ spacedUpdate.rebind(note.noteId, prepare, commit);
+ saves.setNoteId(note.noteId);
+ });
+
+ useSaveBeforeLeaving(spacedUpdate, noteContext);
+
+ // Saves what is left once the content goes away.
+ useEffect(() => () => {
+ spacedUpdate.updateNowIfNecessary().catch(() => {
+ // Failures are logged by `SpacedUpdate` and retried.
+ });
+ }, [ spacedUpdate ]);
+
+ return saves;
+}
+
function readContent(save: PendingSave) {
return save.getContent ? save.getContent() : save.content ?? "";
}