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
fix(client): keep template title writes per template, never wait to open
Elian Doran018a61c8b3d6
diff --git a/apps/client/src/widgets/dialogs/template_properties.spec.tsx b/apps/client/src/widgets/dialogs/template_properties.spec.tsxindex 3211c1bcd0..855084c7fa 100644--- a/apps/client/src/widgets/dialogs/template_properties.spec.tsx+++ b/apps/client/src/widgets/dialogs/template_properties.spec.tsx@@ -142,6 +142,31 @@ describe("TemplatePropertiesDialog", () => {setLabel.mockRestore();});+ it("opens another template while a write waits on updates that never come, and reopens with what was typed", async () => {+ vi.spyOn(ws, "waitForMaxKnownEntityChangeId").mockReturnValue(new Promise<void>(() => {}));+ const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue();+ const first = buildNote({ title: "Meeting", "#template": "" });+ const second = buildNote({ title: "Call", "#template": "", "#template:titleTemplate": "Call notes" });++ const { host, container, field } = await open(first.noteId);+ type(field, "Typed");+ await act(async () => {+ field.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));+ });+ await vi.waitFor(() => expect(setLabel).toHaveBeenCalledWith(first.noteId, "template:titleTemplate", "Typed"));++ const fieldValue = async (noteId: string) => {+ await act(async () => {+ await host.handleEventInChildren("showTemplateProperties", { noteId });+ });+ return container.querySelector<HTMLInputElement>(".template-properties-dialog .title-template-stub")?.value;+ };+ expect(await fieldValue(second.noteId)).toBe("Call notes");+ // froca has not caught up with the first template's write, which the field shows anyway.+ expect(await fieldValue(first.noteId)).toBe("Typed");+ setLabel.mockRestore();+ });+it("previews the title below the field, filled in against the template's parent", async () => {buildNote({title: "Projects", children: [ { id: "tplPreview", title: "Meeting", "#template": "" } ]diff --git a/apps/client/src/widgets/dialogs/template_properties.tsx b/apps/client/src/widgets/dialogs/template_properties.tsxindex b4f577aa9c..131d6faf96 100644--- a/apps/client/src/widgets/dialogs/template_properties.tsx+++ b/apps/client/src/widgets/dialogs/template_properties.tsx@@ -26,41 +26,47 @@ export default function TemplatePropertiesDialog() {const [ shown, setShown ] = useState(false);const [ titleTemplate, setTitleTemplate ] = useState("");const [ defaultParentId, setDefaultParentId ] = useState<string>();- /** The title template as last written, which froca can lag behind while a write is under way. */- const savedTitleTemplate = useRef("");- const titleWrites = useRef<Promise<void>>(Promise.resolve());+ const titleWrites = useRef(new Map<string, TitleWrites>());const preview = useTitlePreview(titleTemplate, note);useTriliumEvent("showTemplateProperties", async ({ noteId }) => {- // The writes of the template shown before go first, so that what is read below includes them.- await titleWrites.current.catch(() => {});const templateNote = await froca.getNote(noteId);if (!templateNote) return;- const ownTitleTemplate = templateNote.getOwnedLabelValue("template:titleTemplate") ?? "";- savedTitleTemplate.current = ownTitleTemplate;- setTitleTemplate(ownTitleTemplate);+ // froca can lag behind writes still queued, so the template's own state is kept until they end.+ let writes = titleWrites.current.get(noteId);+ if (!writes?.pending) {+ const ownTitleTemplate = templateNote.getOwnedLabelValue("template:titleTemplate") ?? "";+ writes = { saved: ownTitleTemplate, latest: ownTitleTemplate, pending: 0, queue: Promise.resolve() };+ titleWrites.current.set(noteId, writes);+ }+ setTitleTemplate(writes.latest);setDefaultParentId(templateNote.getOwnedRelationValue("template:newNoteDefaultParent") ?? undefined);setNote(templateNote);setShown(true);});/**- * Queues writing the title template. The writes run one after another, each waiting for froca to- * hold the one before it, so the last value wins and removing finds the label just created.+ * Queues writing the title template. A template's writes run one after another, each waiting for+ * froca to hold the one before it, so the last value wins and removing finds the label just created.*/function saveTitleTemplate() {- if (!note) return;+ const writes = note && titleWrites.current.get(note.noteId);+ if (!note || !writes) return;const templateNote = note;const value = titleTemplate;- titleWrites.current = titleWrites.current.catch(() => {}).then(async () => {- if (value === savedTitleTemplate.current) return;+ writes.latest = value;+ writes.pending++;+ writes.queue = writes.queue.catch(() => {}).then(async () => {+ if (value === writes.saved) return;if (value) {await attributes.setLabel(templateNote.noteId, "template:titleTemplate", value);} else {await attributes.removeOwnedLabelByName(templateNote, "template:titleTemplate");}- savedTitleTemplate.current = value;+ writes.saved = value;await ws.waitForMaxKnownEntityChangeId();+ }).finally(() => {+ writes.pending--;});}@@ -117,6 +123,16 @@ export default function TemplatePropertiesDialog() {);}+/** A template's title template as the dialog writes it, which froca can lag behind. */+interface TitleWrites {+ /** As last written. */+ saved: string;+ /** As last queued, which the field shows while writes are pending. */+ latest: string;+ pending: number;+ queue: Promise<void>;+}+/*** The title a note created from `templateNote` would get from `template` now, or `undefined`* while it cannot be told. The template's own parent stands in for `parentNote`, since a template