Skip to content

Build your personal knowledge base with Trilium Notes

refactor(text-snippets): use a map instead of an object

Elian Doran9f82e0a6d6e6
diff --git a/apps/client/src/widgets/type_widgets/ckeditor/snippets.ts b/apps/client/src/widgets/type_widgets/ckeditor/snippets.ts
index 9072194a59..a24aefaf88 100644
--- a/apps/client/src/widgets/type_widgets/ckeditor/snippets.ts
+++ b/apps/client/src/widgets/type_widgets/ckeditor/snippets.ts
@@ -3,7 +3,7 @@ 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> = {};
+let templateCache: Map<string, string | undefined> = new Map();
/**
* Generates the list of snippets based on the user's notes to be passed down to the CKEditor configuration.
@@ -15,11 +15,11 @@ export default async function getTemplates() {
const snippets = await search.searchForNotes("#textSnippet");
const definitions: TemplateDefinition[] = [];
for (const snippet of snippets) {
- templateCache[snippet.noteId] = await (snippet.getContent()) ?? "";
+ templateCache.set(snippet.noteId, await snippet.getContent());
definitions.push({
title: snippet.title,
- data: () => templateCache[snippet.noteId]
+ data: () => templateCache.get(snippet.noteId) ?? ""
})
}
return definitions;
@@ -27,7 +27,7 @@ export default async function getTemplates() {
async function handleUpdate(affectedNoteIds: string[]) {
const updatedNoteIds = new Set(affectedNoteIds);
- const templateNoteIds = new Set(Object.keys(templateCache));
+ const templateNoteIds = new Set(templateCache.keys());
const affectedTemplateNoteIds = templateNoteIds.intersection(updatedNoteIds);
console.log("Got ", affectedTemplateNoteIds);
@@ -41,7 +41,7 @@ async function handleUpdate(affectedNoteIds: string[]) {
continue;
}
- templateCache[affectedTemplateNoteId] = await template.getContent() ?? "";
+ templateCache.set(affectedTemplateNoteId, await template.getContent());
}
}