mirror of
https://github.com/TriliumNext/Notes.git
synced 2025-07-27 18:12:29 +08:00
feat(text-snippets): support description
This commit is contained in:
parent
59e0857bb5
commit
7e399cc10c
@ -5,10 +5,12 @@ import search from "../../../services/search.js";
|
|||||||
import type { TemplateDefinition } from "@triliumnext/ckeditor5";
|
import type { TemplateDefinition } from "@triliumnext/ckeditor5";
|
||||||
import appContext from "../../../components/app_context.js";
|
import appContext from "../../../components/app_context.js";
|
||||||
import TemplateIcon from "@ckeditor/ckeditor5-icons/theme/icons/template.svg?raw";
|
import TemplateIcon from "@ckeditor/ckeditor5-icons/theme/icons/template.svg?raw";
|
||||||
|
import type FNote from "../../../entities/fnote.js";
|
||||||
|
|
||||||
interface TemplateData {
|
interface TemplateData {
|
||||||
title: string;
|
title: string;
|
||||||
content: string | undefined;
|
description?: string;
|
||||||
|
content?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
let templateCache: Map<string, TemplateData> = new Map();
|
let templateCache: Map<string, TemplateData> = new Map();
|
||||||
@ -24,20 +26,29 @@ export default async function getTemplates() {
|
|||||||
const snippets = await search.searchForNotes("#textSnippet");
|
const snippets = await search.searchForNotes("#textSnippet");
|
||||||
const definitions: TemplateDefinition[] = [];
|
const definitions: TemplateDefinition[] = [];
|
||||||
for (const snippet of snippets) {
|
for (const snippet of snippets) {
|
||||||
templateCache.set(snippet.noteId, {
|
const { description } = await invalidateCacheFor(snippet);
|
||||||
title: snippet.title,
|
|
||||||
content: await snippet.getContent()
|
|
||||||
});
|
|
||||||
|
|
||||||
definitions.push({
|
definitions.push({
|
||||||
title: snippet.title,
|
title: snippet.title,
|
||||||
data: () => templateCache.get(snippet.noteId)?.content ?? "",
|
data: () => templateCache.get(snippet.noteId)?.content ?? "",
|
||||||
icon: TemplateIcon
|
icon: TemplateIcon,
|
||||||
})
|
description
|
||||||
|
});
|
||||||
}
|
}
|
||||||
return definitions;
|
return definitions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function invalidateCacheFor(snippet: FNote) {
|
||||||
|
const description = snippet.getLabelValue("textSnippetDescription");
|
||||||
|
const data: TemplateData = {
|
||||||
|
title: snippet.title,
|
||||||
|
description: description ?? undefined,
|
||||||
|
content: await snippet.getContent()
|
||||||
|
};
|
||||||
|
templateCache.set(snippet.noteId, data);
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
|
||||||
function handleFullReload() {
|
function handleFullReload() {
|
||||||
console.warn("Full text editor reload needed");
|
console.warn("Full text editor reload needed");
|
||||||
appContext.triggerCommand("reloadTextEditor");
|
appContext.triggerCommand("reloadTextEditor");
|
||||||
@ -65,10 +76,7 @@ async function handleContentUpdate(affectedNoteIds: string[]) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
templateCache.set(affectedTemplateNoteId, {
|
await invalidateCacheFor(template);
|
||||||
title: template.title,
|
|
||||||
content: await template.getContent()
|
|
||||||
});
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
// If a note was not found while updating the cache, it means we need to do a full reload.
|
// If a note was not found while updating the cache, it means we need to do a full reload.
|
||||||
fullReloadNeeded = true;
|
fullReloadNeeded = true;
|
||||||
@ -86,7 +94,7 @@ export function updateTemplateCache(loadResults: LoadResults): boolean {
|
|||||||
// React to creation or deletion of text snippets.
|
// React to creation or deletion of text snippets.
|
||||||
if (loadResults.getAttributeRows().find((attr) =>
|
if (loadResults.getAttributeRows().find((attr) =>
|
||||||
attr.type === "label" &&
|
attr.type === "label" &&
|
||||||
attr.name === "textSnippet")) {
|
(attr.name === "textSnippet" || attr.name === "textSnippetDescription"))) {
|
||||||
handleFullReload();
|
handleFullReload();
|
||||||
} else if (affectedNoteIds.length > 0) {
|
} else if (affectedNoteIds.length > 0) {
|
||||||
// Update content and titles if one of the template notes were updated.
|
// Update content and titles if one of the template notes were updated.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user