mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 10:02:59 +08:00
feat(text-snippets): handle content changes
This commit is contained in:
parent
17ede00fb2
commit
421e125882
@ -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;
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user