|
| 1 | +import debounce from "debounce"; |
| 2 | +import froca from "../../../services/froca.js"; |
| 3 | +import type LoadResults from "../../../services/load_results.js"; |
| 4 | +import search from "../../../services/search.js"; |
| 5 | +import type { TemplateDefinition } from "@triliumnext/ckeditor5"; |
| 6 | +import appContext from "../../../components/app_context.js"; |
| 7 | +import TemplateIcon from "@ckeditor/ckeditor5-icons/theme/icons/template.svg?raw"; |
| 8 | +import type FNote from "../../../entities/fnote.js"; |
| 9 | + |
| 10 | +interface TemplateData { |
| 11 | + title: string; |
| 12 | + description?: string; |
| 13 | + content?: string; |
| 14 | +} |
| 15 | + |
| 16 | +let templateCache: Map<string, TemplateData> = new Map(); |
| 17 | +const debouncedHandleContentUpdate = debounce(handleContentUpdate, 1000); |
| 18 | + |
| 19 | +/** |
| 20 | + * Generates the list of snippets based on the user's notes to be passed down to the CKEditor configuration. |
| 21 | + * |
| 22 | + * @returns the list of templates. |
| 23 | + */ |
| 24 | +export default async function getTemplates() { |
| 25 | + // Build the definitions and populate the cache. |
| 26 | + const snippets = await search.searchForNotes("#textSnippet"); |
| 27 | + const definitions: TemplateDefinition[] = []; |
| 28 | + for (const snippet of snippets) { |
| 29 | + const { description } = await invalidateCacheFor(snippet); |
| 30 | + |
| 31 | + definitions.push({ |
| 32 | + title: snippet.title, |
| 33 | + data: () => templateCache.get(snippet.noteId)?.content ?? "", |
| 34 | + icon: TemplateIcon, |
| 35 | + description |
| 36 | + }); |
| 37 | + } |
| 38 | + return definitions; |
| 39 | +} |
| 40 | + |
| 41 | +async function invalidateCacheFor(snippet: FNote) { |
| 42 | + const description = snippet.getLabelValue("textSnippetDescription"); |
| 43 | + const data: TemplateData = { |
| 44 | + title: snippet.title, |
| 45 | + description: description ?? undefined, |
| 46 | + content: await snippet.getContent() |
| 47 | + }; |
| 48 | + templateCache.set(snippet.noteId, data); |
| 49 | + return data; |
| 50 | +} |
| 51 | + |
| 52 | +function handleFullReload() { |
| 53 | + console.warn("Full text editor reload needed"); |
| 54 | + appContext.triggerCommand("reloadTextEditor"); |
| 55 | +} |
| 56 | + |
| 57 | +async function handleContentUpdate(affectedNoteIds: string[]) { |
| 58 | + const updatedNoteIds = new Set(affectedNoteIds); |
| 59 | + const templateNoteIds = new Set(templateCache.keys()); |
| 60 | + const affectedTemplateNoteIds = templateNoteIds.intersection(updatedNoteIds); |
| 61 | + |
| 62 | + await froca.getNotes(affectedNoteIds); |
| 63 | + |
| 64 | + let fullReloadNeeded = false; |
| 65 | + for (const affectedTemplateNoteId of affectedTemplateNoteIds) { |
| 66 | + try { |
| 67 | + const template = await froca.getNote(affectedTemplateNoteId); |
| 68 | + if (!template) { |
| 69 | + console.warn("Unable to obtain template with ID ", affectedTemplateNoteId); |
| 70 | + continue; |
| 71 | + } |
| 72 | + |
| 73 | + const newTitle = template.title; |
| 74 | + if (templateCache.get(affectedTemplateNoteId)?.title !== newTitle) { |
| 75 | + fullReloadNeeded = true; |
| 76 | + break; |
| 77 | + } |
| 78 | + |
| 79 | + await invalidateCacheFor(template); |
| 80 | + } catch (e) { |
| 81 | + // If a note was not found while updating the cache, it means we need to do a full reload. |
| 82 | + fullReloadNeeded = true; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + if (fullReloadNeeded) { |
| 87 | + handleFullReload(); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +export function updateTemplateCache(loadResults: LoadResults): boolean { |
| 92 | + const affectedNoteIds = loadResults.getNoteIds(); |
| 93 | + |
| 94 | + // React to creation or deletion of text snippets. |
| 95 | + if (loadResults.getAttributeRows().find((attr) => |
| 96 | + attr.type === "label" && |
| 97 | + (attr.name === "textSnippet" || attr.name === "textSnippetDescription"))) { |
| 98 | + handleFullReload(); |
| 99 | + } else if (affectedNoteIds.length > 0) { |
| 100 | + // Update content and titles if one of the template notes were updated. |
| 101 | + debouncedHandleContentUpdate(affectedNoteIds); |
| 102 | + } |
| 103 | + |
| 104 | + return false; |
| 105 | +} |
0 commit comments