feat(text-snippets): handle content changes

This commit is contained in:
Elian Doran 2025-06-17 16:36:05 +03:00
parent 17ede00fb2
commit 421e125882
No known key found for this signature in database
2 changed files with 42 additions and 6 deletions

View File

@ -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"; 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. * Generates the list of snippets based on the user's notes to be passed down to the CKEditor configuration.
* *
* @returns the list of templates. * @returns the list of templates.
*/ */
export default async function getTemplates() { export default async function getTemplates() {
const definitions: TemplateDefinition[] = []; // Build the definitions and populate the cache.
const snippets = await search.searchForNotes("#textSnippet"); const snippets = await search.searchForNotes("#textSnippet");
const definitions: TemplateDefinition[] = [];
for (const snippet of snippets) { for (const snippet of snippets) {
templateCache[snippet.noteId] = await (snippet.getContent()) ?? "";
definitions.push({ definitions.push({
title: snippet.title, title: snippet.title,
data: await snippet.getContent() ?? "" data: () => templateCache[snippet.noteId]
}) })
} }
return definitions; 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;
}

View File

@ -18,6 +18,7 @@ import { getMermaidConfig } from "../../services/mermaid.js";
import { PopupEditor, ClassicEditor, EditorWatchdog, type CKTextEditor, type MentionFeed, type WatchdogConfig } from "@triliumnext/ckeditor5"; import { PopupEditor, ClassicEditor, EditorWatchdog, type CKTextEditor, type MentionFeed, type WatchdogConfig } from "@triliumnext/ckeditor5";
import "@triliumnext/ckeditor5/index.css"; import "@triliumnext/ckeditor5/index.css";
import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons"; import { normalizeMimeTypeForCKEditor } from "@triliumnext/commons";
import { updateTemplateCache } from "./ckeditor/snippets.js";
const mentionSetup: MentionFeed[] = [ const mentionSetup: MentionFeed[] = [
{ {
@ -584,9 +585,7 @@ export default class EditableTextTypeWidget extends AbstractTextTypeWidget {
async entitiesReloadedEvent(e: EventData<"entitiesReloaded">) { async entitiesReloadedEvent(e: EventData<"entitiesReloaded">) {
await super.entitiesReloadedEvent(e); await super.entitiesReloadedEvent(e);
if (e.loadResults.getAttributeRows().find((attr) => if (updateTemplateCache(e.loadResults)) {
attr.type === "label" &&
attr.name === "textSnippet")) {
await this.reinitialize(); await this.reinitialize();
} }
} }