Skip to content

Build your personal knowledge base with Trilium Notes

feat(tree): group the snippets in a Snippet submenu of the insert menus

Elian Dorane48dc813d4ed
diff --git a/apps/client/src/services/note_types.spec.ts b/apps/client/src/services/note_types.spec.ts
index 3f2dc71f00..7fa47c9f09 100644
--- a/apps/client/src/services/note_types.spec.ts
+++ b/apps/client/src/services/note_types.spec.ts
@@ -436,6 +436,47 @@ describe("new template badges", () => {
restore();
}
});
+
+ it("puts the snippets in a submenu of the menu, and the AI quick action beside the AI chat", async () => {
+ withTemplates();
+ const restore = withTemplatesRoot([
+ fakeTemplate("tpl-text-snippet", [ "template", "textSnippet" ], "Text snippet"),
+ fakeTemplate("tpl-code-snippet", [ "template", "snippet" ], "Code snippet"),
+ fakeTemplate("tpl-ai", [ "template", "aiQuickAction" ], "AI quick action")
+ ]);
+ const titles = (items: any[]) => items.map((i) => i.templateNoteId ?? i.type ?? i.kind);
+ try {
+ llmFlag.mockReturnValue(true);
+ const data = await noteTypesService.loadNoteTypeData();
+ const menu: any[] = noteTypesService.buildNoteTypeItems(data, "insertChildNote" as never);
+
+ const submenu = menu.find((i) => i.title === "note_types.snippet");
+ expect(submenu).toBeDefined();
+ expect(submenu.command).toBeUndefined();
+ expect(titles(submenu.items)).toEqual([ "tpl-text-snippet", "tpl-code-snippet" ]);
+ expect(submenu.items.every((i: any) => i.command === "insertChildNote")).toBe(true);
+ // Only in the submenu, and followed by no other group of built-in templates.
+ expect(menu.some((i) => i.templateNoteId === "tpl-code-snippet")).toBe(false);
+ const types = titles(menu);
+ expect(types.indexOf("tpl-ai")).toBe(types.indexOf("llmChat") + 1);
+
+ // The chooser's flat list keeps the snippets inline, after a separator.
+ const list: any[] = await noteTypesService.getNoteTypeItems();
+ expect(list.some((i) => i.items)).toBe(false);
+ const listed = titles(list);
+ expect(listed.slice(listed.indexOf("tpl-text-snippet") - 1, listed.indexOf("tpl-text-snippet") + 2))
+ .toEqual([ "separator", "tpl-text-snippet", "tpl-code-snippet" ]);
+ expect(listed.indexOf("tpl-ai")).toBe(listed.indexOf("llmChat") + 1);
+
+ // Without the AI features, neither the chat nor the quick action is offered.
+ llmFlag.mockReturnValue(false);
+ const withoutAi = titles(noteTypesService.buildNoteTypeItems(data, "insertChildNote" as never));
+ expect(withoutAi).not.toContain("llmChat");
+ expect(withoutAi).not.toContain("tpl-ai");
+ } finally {
+ restore();
+ }
+ });
});
describe("isCurrentNoteType", () => {
diff --git a/apps/client/src/services/note_types.ts b/apps/client/src/services/note_types.ts
index bd648eca15..128e5e13d9 100644
--- a/apps/client/src/services/note_types.ts
+++ b/apps/client/src/services/note_types.ts
@@ -217,22 +217,31 @@ async function loadNoteTypeData(): Promise<NoteTypeData> {
}
/**
- * The note type items. In a `"menu"` the collections form a submenu; a `"list"`, such as the note
- * type chooser draws, cannot open one, so there they follow a header instead.
+ * The note type items. In a `"menu"` the snippets and the collections each form a submenu; a
+ * `"list"`, such as the note type chooser draws, cannot open one, so there they stay inline. The AI
+ * quick action template stands beside the AI chat note type, and is offered only alongside it.
*/
function buildNoteTypeItems(data: NoteTypeData, command?: TreeCommandNames, layout: "menu" | "list" = "menu") {
const { builtInTemplateNotes, userTemplateNotes, newTemplates } = data;
- const collections = getBuiltInTemplates(command, builtInTemplateNotes, true, newTemplates);
+ const builtIn = (group: BuiltInTemplateGroup) =>
+ getBuiltInTemplates(command, builtInTemplateNotes, group, newTemplates);
+ const snippets = builtIn("snippet");
+ const collections = builtIn("collection");
- const items: MenuItem<TreeCommandNames>[] = [
- ...getBlankNoteTypes(command),
- ...withLeading(SEPARATOR, getBuiltInTemplates(command, builtInTemplateNotes, false, newTemplates))
- ];
+ const items: MenuItem<TreeCommandNames>[] = [];
+ for (const blankType of getBlankNoteTypes(command)) {
+ items.push(blankType);
+ if ("type" in blankType && blankType.type === "llmChat") items.push(...builtIn("aiQuickAction"));
+ }
if (layout === "list") {
+ items.push(...withLeading(SEPARATOR, [ ...snippets, ...builtIn("other") ]));
items.push(...withLeading({ title: t("note_types.collections"), kind: "header" }, collections));
- } else if (collections.length > 0) {
- items.push(SEPARATOR, { title: t("note_types.book"), uiIcon: "bx bx-book", items: collections });
+ } else {
+ items.push(...withLeading(SEPARATOR, builtIn("other")));
+ if (snippets.length > 0 || collections.length > 0) items.push(SEPARATOR);
+ if (snippets.length > 0) items.push({ title: t("note_types.snippet"), uiIcon: "bx bx-align-left", items: snippets });
+ if (collections.length > 0) items.push({ title: t("note_types.book"), uiIcon: "bx bx-book", items: collections });
}
items.push(...getUserTemplates(command, userTemplateNotes, newTemplates));
@@ -310,12 +319,21 @@ async function getBuiltInTemplateNotes() {
return await templatesRoot.getChildNotes();
}
-function getBuiltInTemplates(command: TreeCommandNames | undefined, childNotes: FNote[], filterCollections: boolean, newTemplates: Set<string>) {
+type BuiltInTemplateGroup = "collection" | "snippet" | "aiQuickAction" | "other";
+
+/** The group of the note type menus a built-in template stands in, told by the labels it carries. */
+function builtInTemplateGroup(templateNote: FNote): BuiltInTemplateGroup {
+ if (templateNote.hasLabel("collection")) return "collection";
+ if (templateNote.hasLabel("snippet") || templateNote.hasLabel("textSnippet")) return "snippet";
+ if (templateNote.hasLabel("aiQuickAction")) return "aiQuickAction";
+ return "other";
+}
+
+function getBuiltInTemplates(command: TreeCommandNames | undefined, childNotes: FNote[], group: BuiltInTemplateGroup, newTemplates: Set<string>) {
const items: MenuItem<TreeCommandNames>[] = [];
for (const templateNote of childNotes) {
- if (templateNote.hasLabel("collection") !== filterCollections ||
- !templateNote.hasLabel("template")) {
+ if (!templateNote.hasLabel("template") || builtInTemplateGroup(templateNote) !== group) {
continue;
}
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 419df07666..6302b724aa 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -2581,7 +2581,8 @@
"collections": "Collections",
"spreadsheet": "Spreadsheet",
"note-types": "Note types",
- "built-in-templates": "Built-in templates"
+ "built-in-templates": "Built-in templates",
+ "snippet": "Snippet"
},
"template_selection": {
"add-existing": "Add existing template",
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Snippets/Code Snippets.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Snippets/Code Snippets.html
index a36034636b..742db0c7ac 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Snippets/Code Snippets.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Snippets/Code Snippets.html
@@ -6,8 +6,8 @@
<p>In the&nbsp;<a class="reference-link" href="#root/_help_oPVyFC7WL2Lp">Note Tree</a>: </p>
<ol>
<li>Right-click the note where you want to place the snippet.</li>
- <li>Select <strong>Insert child</strong> <em>note</em>.</li>
- <li>Select <em><strong>Code snippet</strong></em>.</li>
+ <li>Select <em>Insert child note</em>.</li>
+ <li>Select <em>Snippet</em> → <em>Code snippet</em>.</li>
</ol>
<p>Then simply type the desired code into the note and set the correct language
mode.</p>
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Snippets/Markdown Snippets.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Snippets/Markdown Snippets.html
index cd4179b550..85f47f152c 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Snippets/Markdown Snippets.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Advanced Usage/Snippets/Markdown Snippets.html
@@ -7,8 +7,8 @@
<p>In the&nbsp;<a class="reference-link" href="#root/_help_oPVyFC7WL2Lp">Note Tree</a>: </p>
<ol>
<li>Right-click the note where you want to place the snippet.</li>
- <li>Select <strong>Insert child</strong> <em>note</em>.</li>
- <li>Select <em><strong>Markdown snippet</strong></em>.</li>
+ <li>Select <em>Insert child note</em>.</li>
+ <li>Select <em>Snippet</em> → <em>Markdown snippet</em>.</li>
</ol>
<p>Then simply type the desired content into the note.</p>
<p>The note's title becomes the title of the snippet. Optionally, you can
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/UI Elements/Note Tree/Note tree contextual menu.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/UI Elements/Note Tree/Note tree contextual menu.html
index d7fe8f2900..74497715eb 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/UI Elements/Note Tree/Note tree contextual menu.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/UI Elements/Note Tree/Note tree contextual menu.html
@@ -44,8 +44,9 @@
<li><strong>Insert note after</strong>
<ul>
<li>Allows easy creation of a note with a specified <a href="#root/_help_KSZ04uQ2D1St">note type</a>.</li>
- <li><a class="reference-link" href="#root/_help_GTwFsgaA0lCt">Collections</a> are
- grouped in the <em>Collection</em> submenu.</li>
+ <li><a class="reference-link" href="#root/_help_B2ORr7OVEZtc">Snippets</a> and&nbsp;<a
+ class="reference-link" href="#root/_help_GTwFsgaA0lCt">Collections</a> are
+ grouped in the <em>Snippet</em> and <em>Collection</em> submenus.</li>
<li><a class="reference-link" href="#root/_help_KC1HB96bqqHX">Templates</a>&nbsp;will
also be present (if any) at the end of the list.</li>
<li>The note will be added on the same level of hierarchy as the note selected.</li>
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/In-editor AI assistant/Custom AI quick actions.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/In-editor AI assistant/Custom AI quick actions.html
index 38c8339fff..250fc4c7df 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/In-editor AI assistant/Custom AI quick actions.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/In-editor AI assistant/Custom AI quick actions.html
@@ -9,7 +9,8 @@
<ol>
<li>Right click a note where to place the text snippet.</li>
<li>Select <em>Insert child note</em>.</li>
- <li>Select <em>AI Quick Action</em>.</li>
+ <li>Select <em>AI Quick Action</em>, next to <em>AI Chat</em>. Both appear only
+ when AI is enabled.</li>
</ol>
<p>Afterwards, simply type in the content of the note the desired prompt.
The text can be formatted in the same manner as a normal text note and
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Text Snippets.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Text Snippets.html
index 00222971db..0ff26a4855 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Text Snippets.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Note Types/Text/Text Snippets.html
@@ -10,7 +10,7 @@
<ol>
<li>Right click a note where to place the text snippet.</li>
<li>Select <em>Insert child note</em>.</li>
- <li>Select <em>Text snippet</em>.</li>
+ <li>Select <em>Snippet</em> → <em>Text snippet</em>.</li>
</ol>
<p>Afterwards, simply type in the content of the note the desired text. The
text can be formatted in the same manner as a normal text note.</p>
diff --git a/docs/User Guide/User Guide/Advanced Usage/Snippets/Code Snippets.md b/docs/User Guide/User Guide/Advanced Usage/Snippets/Code Snippets.md
index 125ac1a99a..67b18173b8 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Snippets/Code Snippets.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Snippets/Code Snippets.md
@@ -6,8 +6,8 @@ Code Snippets are closely related to <a class="reference-link" href="../Templat
In the <a class="reference-link" href="../../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a>: 
1. Right-click the note where you want to place the snippet.
-2. Select **Insert child** _note_.
-3. Select _**Code snippet**_.
+2. Select _Insert child note_.
+3. Select _Snippet_ → _Code snippet_.
Then simply type the desired code into the note and set the correct language mode.
diff --git a/docs/User Guide/User Guide/Advanced Usage/Snippets/Markdown Snippets.md b/docs/User Guide/User Guide/Advanced Usage/Snippets/Markdown Snippets.md
index c32e3908b8..391ea4b076 100644
--- a/docs/User Guide/User Guide/Advanced Usage/Snippets/Markdown Snippets.md
+++ b/docs/User Guide/User Guide/Advanced Usage/Snippets/Markdown Snippets.md
@@ -6,8 +6,8 @@ Markdown Snippets are closely related to <a class="reference-link" href="../Tem
In the <a class="reference-link" href="../../Basic%20Concepts%20and%20Features/UI%20Elements/Note%20Tree.md">Note Tree</a>: 
1. Right-click the note where you want to place the snippet.
-2. Select **Insert child** _note_.
-3. Select _**Markdown snippet**_.
+2. Select _Insert child note_.
+3. Select _Snippet_ → _Markdown snippet_.
Then simply type the desired content into the note.
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/UI Elements/Note Tree/Note tree contextual menu.md b/docs/User Guide/User Guide/Basic Concepts and Features/UI Elements/Note Tree/Note tree contextual menu.md
index 193b62384e..f4ef0d6ea3 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/UI Elements/Note Tree/Note tree contextual menu.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/UI Elements/Note Tree/Note tree contextual menu.md
@@ -24,7 +24,7 @@ The contextual menu can operate:
* Will focus the note tree on this note. See <a class="reference-link" href="../../Navigation/Note%20Hoisting.md">Note Hoisting</a> for more information.
* **Insert note after**
* Allows easy creation of a note with a specified [note type](../../../Note%20Types.md).
- * <a class="reference-link" href="../../../Collections.md">Collections</a> are grouped in the _Collection_ submenu.
+ * <a class="reference-link" href="../../../Advanced%20Usage/Snippets.md">Snippets</a> and <a class="reference-link" href="../../../Collections.md">Collections</a> are grouped in the _Snippet_ and _Collection_ submenus.
* <a class="reference-link" href="../../../Advanced%20Usage/Templates.md">Templates</a> will also be present (if any) at the end of the list.
* The note will be added on the same level of hierarchy as the note selected.
* **Insert child note**
diff --git a/docs/User Guide/User Guide/Note Types/Text/In-editor AI assistant/Custom AI quick actions.md b/docs/User Guide/User Guide/Note Types/Text/In-editor AI assistant/Custom AI quick actions.md
index 081d1426ac..57a39ccbc5 100644
--- a/docs/User Guide/User Guide/Note Types/Text/In-editor AI assistant/Custom AI quick actions.md
+++ b/docs/User Guide/User Guide/Note Types/Text/In-editor AI assistant/Custom AI quick actions.md
@@ -7,7 +7,7 @@ In the <a class="reference-link" href="../../../Basic%20Concepts%20and%20Featur
1. Right click a note where to place the text snippet.
2. Select _Insert child note_.
-3. Select _AI Quick Action_.
+3. Select _AI Quick Action_, next to _AI Chat_. Both appear only when AI is enabled.
Afterwards, simply type in the content of the note the desired prompt. The text can be formatted in the same manner as a normal text note and it will be passed to the LLM as Markdown.
diff --git a/docs/User Guide/User Guide/Note Types/Text/Text Snippets.md b/docs/User Guide/User Guide/Note Types/Text/Text Snippets.md
index 89d84b4d4a..4fbe31bd3c 100644
--- a/docs/User Guide/User Guide/Note Types/Text/Text Snippets.md
+++ b/docs/User Guide/User Guide/Note Types/Text/Text Snippets.md
@@ -9,7 +9,7 @@ In the <a class="reference-link" href="../../Basic%20Concepts%20and%20Features/
1. Right click a note where to place the text snippet.
2. Select _Insert child note_.
-3. Select _Text snippet_.
+3. Select _Snippet_ → _Text snippet_.
Afterwards, simply type in the content of the note the desired text. The text can be formatted in the same manner as a normal text note.