Skip to content

Build your personal knowledge base with Trilium Notes

fix(presentation): show a canvas slide's drawing once editing ends

Elian Doran61bd60811e4c
diff --git a/apps/client/src/widgets/collections/presentation/index.spec.tsx b/apps/client/src/widgets/collections/presentation/index.spec.tsx
index e8b7da7775..a9f6b56fb0 100644
--- a/apps/client/src/widgets/collections/presentation/index.spec.tsx
+++ b/apps/client/src/widgets/collections/presentation/index.spec.tsx
@@ -4,12 +4,14 @@ import { afterEach, describe, expect, it, vi } from "vitest";
import Component from "../../../components/component";
import NoteContext from "../../../components/note_context";
import FNote from "../../../entities/fnote";
+import LoadResults from "../../../services/load_results";
import note_create from "../../../services/note_create";
import options from "../../../services/options";
import { buildNote } from "../../../test/easy-froca";
import { renderInto } from "../../../test/render";
import { NoteContextContext, ParentComponent } from "../../react/react_utils";
import PresentationView from "./index";
+import { buildPresentationModel } from "./model";
// i18next is never initialized under test, so `t()` looks keys up in the English catalog itself.
vi.mock("../../../services/i18n", async (importOriginal) => {
@@ -22,6 +24,11 @@ vi.mock("../../../services/i18n", async (importOriginal) => {
};
});
+vi.mock("./model", async (importOriginal) => {
+ const actual = await importOriginal<typeof import("./model")>();
+ return { ...actual, buildPresentationModel: vi.fn(actual.buildPresentationModel) };
+});
+
vi.mock("./themes", async (importOriginal) => ({
...await importOriginal<typeof import("./themes")>(),
loadPresentationTheme: async () => ""
@@ -89,16 +96,46 @@ describe("an empty presentation", () => {
});
});
-function renderPresentation(note: FNote, onReady: () => void) {
+describe("reloading the presentation", () => {
+ it("builds the slides again when an attachment of a slide changes, as a canvas's picture does", async () => {
+ const note = buildNote({ title: "Deck", type: "book", "#viewType": "presentation" });
+ const parent = new Component();
+ const container = renderPresentation(note, vi.fn(), { parent, noteIds: [ "drawing" ] });
+ await vi.waitFor(() => expect(container.querySelector(".no-items")).not.toBeNull());
+ const builds = vi.mocked(buildPresentationModel).mock.calls.length;
+
+ const attachmentChanged = (ownerId: string) => {
+ const loadResults = new LoadResults([]);
+ loadResults.addAttachmentRow({
+ attachmentId: `picture-of-${ownerId}`, ownerId, role: "image", mime: "image/svg+xml", title: "canvas-export.svg"
+ });
+ return loadResults;
+ };
+ await act(async () => {
+ await parent.handleEvent("entitiesReloaded", { loadResults: attachmentChanged("elsewhere") });
+ });
+ expect(buildPresentationModel).toHaveBeenCalledTimes(builds);
+
+ await act(async () => {
+ await parent.handleEvent("entitiesReloaded", { loadResults: attachmentChanged("drawing") });
+ });
+ expect(buildPresentationModel).toHaveBeenCalledTimes(builds + 1);
+ });
+});
+
+function renderPresentation(
+ note: FNote, onReady: () => void,
+ { parent = new Component(), noteIds = [] }: { parent?: Component; noteIds?: string[] } = {}
+) {
const noteContext = new NoteContext("ntx-presentation");
noteContext.noteId = note.noteId;
return renderInto(
- <ParentComponent.Provider value={new Component()}>
+ <ParentComponent.Provider value={parent}>
<NoteContextContext.Provider value={noteContext}>
<PresentationView
note={note}
notePath={note.noteId}
- noteIds={[]}
+ noteIds={noteIds}
highlightedTokens={undefined}
viewConfig={undefined}
saveConfig={() => {}}
diff --git a/apps/client/src/widgets/collections/presentation/index.tsx b/apps/client/src/widgets/collections/presentation/index.tsx
index a4334a9f29..87b0bcf19b 100644
--- a/apps/client/src/widgets/collections/presentation/index.tsx
+++ b/apps/client/src/widgets/collections/presentation/index.tsx
@@ -156,7 +156,9 @@ export default function PresentationView({ note, noteIds, media, onReady, onProg
}
useTriliumEvent("entitiesReloaded", ({ loadResults }) => {
+ // A canvas slide shows the picture that its note keeps as an attachment.
if (loadResults.getNoteIds().find(noteId => noteIds.includes(noteId)) ||
+ loadResults.getAttachmentRows().some(({ ownerId }) => ownerId && noteIds.includes(ownerId)) ||
loadResults.getAttributeRows().find(attr => attr.noteId && noteIds.includes(attr.noteId)
&& (attr.name?.startsWith("slide:") || attr.name === "readOnly"))) {
refresh();
diff --git a/apps/client/src/widgets/collections/presentation/slide_canvas_editor.spec.tsx b/apps/client/src/widgets/collections/presentation/slide_canvas_editor.spec.tsx
index ba3cc8b60a..9707c67095 100644
--- a/apps/client/src/widgets/collections/presentation/slide_canvas_editor.spec.tsx
+++ b/apps/client/src/widgets/collections/presentation/slide_canvas_editor.spec.tsx
@@ -2,7 +2,12 @@ import { render } from "preact";
import { act } from "preact/test-utils";
import { describe, expect, it, vi } from "vitest";
-const announceClosing = vi.hoisted(() => vi.fn());
+const announceClosing = vi.hoisted(() => vi.fn(async () => {}));
+const drawPicture = vi.hoisted(() => vi.fn());
+vi.mock("../../type_widgets/canvas/picture_schedule", async (importOriginal) => ({
+ ...await importOriginal<typeof import("../../type_widgets/canvas/picture_schedule")>(),
+ drawPicture
+}));
vi.mock("../../EmbeddedNotePane", async (importOriginal) => ({
...await importOriginal<typeof import("../../EmbeddedNotePane")>(),
announceEmbeddedNoteClosing: announceClosing
@@ -63,6 +68,8 @@ describe("SlideCanvasEditor", () => {
expect(announceClosing).not.toHaveBeenCalled();
act(() => render(null, container));
expect(announceClosing).toHaveBeenCalledOnce();
+ // The slide shows the picture of the canvas, which is drawn at once rather than once the user idles.
+ await vi.waitFor(() => expect(drawPicture).toHaveBeenCalledWith("drawing"));
container.remove();
(appContext as unknown as { tabManager: unknown }).tabManager = undefined;
});
diff --git a/apps/client/src/widgets/collections/presentation/slide_canvas_editor.tsx b/apps/client/src/widgets/collections/presentation/slide_canvas_editor.tsx
index ae96119a93..7d09cca2e2 100644
--- a/apps/client/src/widgets/collections/presentation/slide_canvas_editor.tsx
+++ b/apps/client/src/widgets/collections/presentation/slide_canvas_editor.tsx
@@ -6,6 +6,7 @@ import FNote from "../../../entities/fnote";
import { announceEmbeddedNoteClosing, EmbeddedNoteScope, useEmbeddedNoteContext } from "../../EmbeddedNotePane";
import { useNoteContext } from "../../react/hooks";
import Canvas from "../../type_widgets/canvas/Canvas";
+import { drawPicture } from "../../type_widgets/canvas/picture_schedule";
import { getCanvasEditorSlotName } from "./slides";
interface SlideCanvasEditorProps {
@@ -24,8 +25,14 @@ interface SlideCanvasEditorProps {
export default function SlideCanvasEditor({ note, onFinish }: SlideCanvasEditorProps) {
const { noteContext, component, ntxId } = useEmbeddedNoteContext(note, "_slide", { skipRecentNotes: true });
- // A layout cleanup runs while Excalidraw is still mounted, so it saves what is left.
- useLayoutEffect(() => () => void announceEmbeddedNoteClosing(component, ntxId), [ component, ntxId ]);
+ // A layout cleanup runs while Excalidraw is still mounted, so it saves what is left. The slide
+ // shows the picture of the canvas, which the save schedules, so it is drawn at once.
+ useLayoutEffect(() => () => {
+ void (async () => {
+ await announceEmbeddedNoteClosing(component, ntxId);
+ drawPicture(note.noteId);
+ })();
+ }, [ component, ntxId ]);
return (
<div
diff --git a/apps/client/src/widgets/type_widgets/canvas/picture_schedule.spec.ts b/apps/client/src/widgets/type_widgets/canvas/picture_schedule.spec.ts
index 943030a302..2f23b7ae9c 100644
--- a/apps/client/src/widgets/type_widgets/canvas/picture_schedule.spec.ts
+++ b/apps/client/src/widgets/type_widgets/canvas/picture_schedule.spec.ts
@@ -3,12 +3,13 @@ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
describe("schedulePicture", () => {
let schedulePicture: typeof import("./picture_schedule").schedulePicture;
let watchPictures: typeof import("./picture_schedule").watchPictures;
+ let drawPicture: typeof import("./picture_schedule").drawPicture;
beforeEach(async () => {
vi.useFakeTimers();
// A new module, without the input and the pictures of the other tests.
vi.resetModules();
- ({ schedulePicture, watchPictures } = await import("./picture_schedule"));
+ ({ schedulePicture, watchPictures, drawPicture } = await import("./picture_schedule"));
});
afterEach(() => {
@@ -33,6 +34,18 @@ describe("schedulePicture", () => {
expect(first).not.toHaveBeenCalled();
});
+ it("draws a waiting picture at once when asked to, as when its editor closes", async () => {
+ const draw = vi.fn(async () => {});
+ schedulePicture("d1", draw, 5000);
+ drawPicture("d1");
+ drawPicture("nothing waits");
+ await vi.advanceTimersByTimeAsync(0);
+ expect(draw).toHaveBeenCalledOnce();
+
+ await vi.advanceTimersByTimeAsync(10000);
+ expect(draw).toHaveBeenCalledOnce();
+ });
+
it("waits until the user gives no input for a while, as during panning", async () => {
const draw = vi.fn(async () => {});
schedulePicture("d1", draw, 1000);
diff --git a/apps/client/src/widgets/type_widgets/canvas/picture_schedule.ts b/apps/client/src/widgets/type_widgets/canvas/picture_schedule.ts
index a9a78b7e98..959db70643 100644
--- a/apps/client/src/widgets/type_widgets/canvas/picture_schedule.ts
+++ b/apps/client/src/widgets/type_widgets/canvas/picture_schedule.ts
@@ -57,8 +57,11 @@ function drawWhenIdle(key: string) {
}
}
-/** Starts the draw of `key` that waits, once the running draw of `key` ends. */
-function drawPicture(key: string) {
+/**
+ * Starts the draw of `key` that waits, once the running draw of `key` ends, without waiting for
+ * its delay or for the user to idle.
+ */
+export function drawPicture(key: string) {
const pending = pendingPictures.get(key);
if (!pending) return;
pendingPictures.delete(key);