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
feat(text-snippets): handle content changes
Elian Doran421e12588241
diff --git a/apps/client/src/widgets/type_widgets/ckeditor/snippets.ts b/apps/client/src/widgets/type_widgets/ckeditor/snippets.tsindex 31be5ae24c..9072194a59 100644--- a/apps/client/src/widgets/type_widgets/ckeditor/snippets.ts+++ b/apps/client/src/widgets/type_widgets/ckeditor/snippets.ts@@ -1,19 +1,56 @@-import search from "../../../services/search";+import froca from "../../../services/froca.js";+import type LoadResults from "../../../services/load_results.js";+import search from "../../../services/search.js";import type { TemplateDefinition } from "@triliumnext/ckeditor5";+let templateCache: Record<string, string> = {};+/*** Generates the list of snippets based on the user's notes to be passed down to the CKEditor configuration.** @returns the list of templates.*/export default async function getTemplates() {- const definitions: TemplateDefinition[] = [];+ // Build the definitions and populate the cache.const snippets = await search.searchForNotes("#textSnippet");+ const definitions: TemplateDefinition[] = [];for (const snippet of snippets) {+ templateCache[snippet.noteId] = await (snippet.getContent()) ?? "";+definitions.push({title: snippet.title,- data: await snippet.getContent() ?? ""+ data: () => templateCache[snippet.noteId]})}return definitions;}++async function handleUpdate(affectedNoteIds: string[]) {+ const updatedNoteIds = new Set(affectedNoteIds);+ const templateNoteIds = new Set(Object.keys(templateCache));+ const affectedTemplateNoteIds = templateNoteIds.intersection(updatedNoteIds);++ console.log("Got ", affectedTemplateNoteIds);++ await froca.getNotes(affectedNoteIds);++ for (const affectedTemplateNoteId of affectedTemplateNoteIds) {+ const template = await froca.getNote(affectedTemplateNoteId);+ if (!template) {+ console.warn("Unable to obtain template with ID ", affectedTemplateNoteId);+ continue;+ }++ templateCache[affectedTemplateNoteId] = await template.getContent() ?? "";+ }+}++export function updateTemplateCache(loadResults: LoadResults): boolean {+ const affectedNoteIds = loadResults.getNoteIds();+ if (affectedNoteIds.length > 0) {+ handleUpdate(affectedNoteIds);+ }+++ return false;+}diff --git a/apps/client/src/widgets/type_widgets/editable_text.ts b/apps/client/src/widgets/type_widgets/editable_text.tsindex aa7902776a..824b5c320d 100644--- a/apps/client/src/widgets/type_widgets/editable_text.ts+++ b/apps/client/src/widgets/type_widgets/editable_text.ts@@ -18,6 +18,7 @@ import { getMermaidConfig } from "../../services/mermaid.js";import { PopupEditor, ClassicEditor, EditorWatchdog, type CKTextEditor, type MentionFeed, type WatchdogConfig } from "@triliumnext/ckeditor5";import "@triliumnext/ckeditor5/index.css";import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";+import { updateTemplateCache } from "./ckeditor/snippets.js";const mentionSetup: MentionFeed[] = [{@@ -584,9 +585,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {async entitiesReloadedEvent(e: EventData<"entitiesReloaded">) {await super.entitiesReloadedEvent(e);- if (e.loadResults.getAttributeRows().find((attr) =>- attr.type === "label" &&- attr.name === "textSnippet")) {+ if (updateTemplateCache(e.loadResults)) {await this.reinitialize();}}