Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
note type context submenu now works end to end
zadam33f2aaa650aa
diff --git a/src/entities/note.js b/src/entities/note.jsindex 2db352ec5f..24d86b0d8d 100644--- a/src/entities/note.js+++ b/src/entities/note.js@@ -74,7 +74,8 @@ class Note extends Entity {}if (this.isStringNote()) {- this.noteContent.content = this.noteContent.content.toString("UTF-8");+ this.noteContent.content = this.noteContent.content === null+ ? "" : this.noteContent.content.toString("UTF-8");}}diff --git a/src/public/javascripts/desktop.js b/src/public/javascripts/desktop.jsindex 0f5f55da86..556fe96ecf 100644--- a/src/public/javascripts/desktop.js+++ b/src/public/javascripts/desktop.js@@ -122,7 +122,7 @@ if (utils.isElectron()) {setTimeout(async () => {const parentNode = treeService.getCurrentNode();- const {note} = await treeService.createNote(parentNode, parentNode.data.noteId, 'into', parentNode.data.isProtected);+ const {note} = await treeService.createNote(parentNode, parentNode.data.noteId, 'into', "text", parentNode.data.isProtected);await treeService.activateNote(note.noteId);diff --git a/src/public/javascripts/mobile.js b/src/public/javascripts/mobile.jsindex b529c64d70..86566ddd88 100644--- a/src/public/javascripts/mobile.js+++ b/src/public/javascripts/mobile.js@@ -93,10 +93,10 @@ $("#note-menu-button").click(async e => {const parentNoteId = node.data.parentNoteId;const isProtected = treeUtils.getParentProtectedStatus(node);- treeService.createNote(node, parentNoteId, 'after', isProtected);+ treeService.createNote(node, parentNoteId, 'after', null, isProtected);}else if (cmd === "insertChildNote") {- treeService.createNote(node, node.data.noteId, 'into');+ treeService.createNote(node, node.data.noteId, 'into', null);}else if (cmd === "delete") {treeChangesService.deleteNodes([node]);diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.jsindex 42b128848c..de7c45f259 100644--- a/src/public/javascripts/services/tree.js+++ b/src/public/javascripts/services/tree.js@@ -551,10 +551,13 @@ async function createNewTopLevelNote() {const rootNode = getNodesByNoteId(hoistedNoteId)[0];- await createNote(rootNode, hoistedNoteId, "into", false);+ await createNote(rootNode, hoistedNoteId, "into", null, false);}-async function createNote(node, parentNoteId, target, isProtected, saveSelection = false) {+/**+ * @param type - type can be falsy - in that case it will be chosen automatically based on parent note+ */+async function createNote(node, parentNoteId, target, type, isProtected, saveSelection = false) {utils.assertArguments(node, parentNoteId, target);// if isProtected isn't available (user didn't enter password yet), then note is created as unencrypted@@ -586,7 +589,8 @@ async function createNote(node, parentNoteId, target, isProtected, saveSelectioncontent: content,target: target,target_branchId: node.data.branchId,- isProtected: isProtected+ isProtected: isProtected,+ type: type});if (saveSelection) {@@ -695,13 +699,13 @@ utils.bindShortcut('ctrl+o', async () => {return;}- createNote(node, parentNoteId, 'after', isProtected, true);+ createNote(node, parentNoteId, 'after', null, isProtected, true);});function createNoteInto() {const node = getCurrentNode();- createNote(node, node.data.noteId, 'into', node.data.isProtected, true);+ createNote(node, node.data.noteId, 'into', null, node.data.isProtected, true);}window.glob.createNoteInto = createNoteInto;diff --git a/src/public/javascripts/services/tree_context_menu.js b/src/public/javascripts/services/tree_context_menu.jsindex 90bba0dbda..30b0610024 100644--- a/src/public/javascripts/services/tree_context_menu.js+++ b/src/public/javascripts/services/tree_context_menu.js@@ -77,38 +77,42 @@ function cut(nodes) {infoService.showMessage("Note(s) have been cut into clipboard.");}-const noteTypeItems = [- {title: "Plain text", cmd: "insertNoteAfter", uiIcon: "file"},- {title: "Terminal", cmd: "insertNoteAfter", uiIcon: "terminal"},- {title: "Saved search", cmd: "insertNoteAfter", uiIcon: "search-folder"},- {title: "Relation Map", cmd: "insertNoteAfter", uiIcon: "map"},- {title: "Render HTML note", cmd: "insertNoteAfter", uiIcon: "play"}-];--const contextMenuItems = [- {title: "Insert note after <kbd>Ctrl+O</kbd>", cmd: "insertNoteAfter", uiIcon: "plus", items: noteTypeItems},- {title: "Insert child note <kbd>Ctrl+P</kbd>", cmd: "insertChildNote", uiIcon: "plus", items: noteTypeItems},- {title: "Delete <kbd>Delete</kbd>", cmd: "delete", uiIcon: "trash"},- {title: "----"},- {title: "Hoist note <kbd>Ctrl-H</kbd>", cmd: "hoist", uiIcon: "arrow-up"},- {title: "Unhoist note <kbd>Ctrl-H</kbd>", cmd: "unhoist", uiIcon: "arrow-up"},- {title: "Edit branch prefix <kbd>F2</kbd>", cmd: "editBranchPrefix", uiIcon: "pencil"},- {title: "----"},- {title: "Protect subtree", cmd: "protectSubtree", uiIcon: "shield-check"},- {title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield-close"},- {title: "----"},- {title: "Copy / clone <kbd>Ctrl+C</kbd>", cmd: "copy", uiIcon: "files"},- {title: "Cut <kbd>Ctrl+X</kbd>", cmd: "cut", uiIcon: "scissors"},- {title: "Paste into <kbd>Ctrl+V</kbd>", cmd: "pasteInto", uiIcon: "clipboard"},- {title: "Paste after", cmd: "pasteAfter", uiIcon: "clipboard"},- {title: "----"},- {title: "Export", cmd: "export", uiIcon: "arrow-up-right"},- {title: "Import into note", cmd: "importIntoNote", uiIcon: "arrow-down-left"},- {title: "----"},- {title: "Collapse subtree <kbd>Alt+-</kbd>", cmd: "collapseSubtree", uiIcon: "align-justify"},- {title: "Force note sync", cmd: "forceNoteSync", uiIcon: "refresh"},- {title: "Sort alphabetically <kbd>Alt+S</kbd>", cmd: "sortAlphabetically", uiIcon: "arrows-v"}-];+function getNoteTypeItems(baseCmd) {+ return [+ { title: "Text", cmd: baseCmd + "_text", uiIcon: "file" },+ { title: "Code", cmd: baseCmd + "_code", uiIcon: "terminal" },+ { title: "Saved search", cmd: baseCmd + "_search", uiIcon: "search-folder" },+ { title: "Relation Map", cmd: baseCmd + "_relation-map", uiIcon: "map" },+ { title: "Render HTML note", cmd: baseCmd + "_render", uiIcon: "play" }+ ];+}++function getTopLevelItems(note) {+ return [+ { title: "Insert note after <kbd>Ctrl+O</kbd>", cmd: "insertNoteAfter", uiIcon: "plus", items: note.type !== 'search' ? getNoteTypeItems("insertNoteAfter") : null },+ { title: "Insert child note <kbd>Ctrl+P</kbd>", cmd: "insertChildNote", uiIcon: "plus", items: note.type !== 'search' ? getNoteTypeItems("insertChildNote") : null },+ { title: "Delete <kbd>Delete</kbd>", cmd: "delete", uiIcon: "trash" },+ { title: "----" },+ { title: "Hoist note <kbd>Ctrl-H</kbd>", cmd: "hoist", uiIcon: "arrow-up" },+ { title: "Unhoist note <kbd>Ctrl-H</kbd>", cmd: "unhoist", uiIcon: "arrow-up" },+ { title: "Edit branch prefix <kbd>F2</kbd>", cmd: "editBranchPrefix", uiIcon: "pencil" },+ { title: "----" },+ { title: "Protect subtree", cmd: "protectSubtree", uiIcon: "shield-check" },+ { title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield-close" },+ { title: "----" },+ { title: "Copy / clone <kbd>Ctrl+C</kbd>", cmd: "copy", uiIcon: "files" },+ { title: "Cut <kbd>Ctrl+X</kbd>", cmd: "cut", uiIcon: "scissors" },+ { title: "Paste into <kbd>Ctrl+V</kbd>", cmd: "pasteInto", uiIcon: "clipboard" },+ { title: "Paste after", cmd: "pasteAfter", uiIcon: "clipboard" },+ { title: "----" },+ { title: "Export", cmd: "export", uiIcon: "arrow-up-right" },+ { title: "Import into note", cmd: "importIntoNote", uiIcon: "arrow-down-left" },+ { title: "----" },+ { title: "Collapse subtree <kbd>Alt+-</kbd>", cmd: "collapseSubtree", uiIcon: "align-justify" },+ { title: "Force note sync", cmd: "forceNoteSync", uiIcon: "refresh" },+ { title: "Sort alphabetically <kbd>Alt+S</kbd>", cmd: "sortAlphabetically", uiIcon: "arrows-v" }+ ];+}async function getContextMenuItems(event) {const node = $.ui.fancytree.getNode(event);@@ -118,7 +122,7 @@ async function getContextMenuItems(event) {const isNotRoot = note.noteId !== 'root';const isHoisted = note.noteId === await hoistedNoteService.getHoistedNoteId();- const itemsContainer = new ContextMenuItemsContainer(contextMenuItems);+ const itemsContainer = new ContextMenuItemsContainer(getTopLevelItems(note));// Modify menu entries depending on node statusitemsContainer.enableItem("insertNoteAfter", isNotRoot && !isHoisted && parentNote.type !== 'search');@@ -151,14 +155,17 @@ function selectContextMenuItem(event, cmd) {// context menu is always triggered on current nodeconst node = treeService.getCurrentNode();- if (cmd === "insertNoteAfter") {+ if (cmd.startsWith("insertNoteAfter")) {const parentNoteId = node.data.parentNoteId;const isProtected = treeUtils.getParentProtectedStatus(node);+ const type = cmd.split("_")[1];- treeService.createNote(node, parentNoteId, 'after', isProtected);+ treeService.createNote(node, parentNoteId, 'after', type, isProtected);}- else if (cmd === "insertChildNote") {- treeService.createNote(node, node.data.noteId, 'into');+ else if (cmd.startsWith("insertChildNote")) {+ const type = cmd.split("_")[1];++ treeService.createNote(node, node.data.noteId, 'into', type);}else if (cmd === "editBranchPrefix") {branchPrefixDialog.showDialog(node);diff --git a/src/services/notes.js b/src/services/notes.jsindex 00d732af09..49db804393 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -81,10 +81,6 @@ async function createNewNote(parentNoteId, noteData) {noteData.type = noteData.type || parentNote.type;noteData.mime = noteData.mime || parentNote.mime;- if (noteData.type === 'text' || noteData.type === 'code') {- noteData.content = noteData.content || "";- }-const note = await new Note({noteId: noteData.noteId, // optionally can force specific noteIdtitle: noteData.title,@@ -93,6 +89,10 @@ async function createNewNote(parentNoteId, noteData) {mime: noteData.mime || 'text/html'}).save();+ if (note.isStringNote()) {+ noteData.content = noteData.content || "";+ }+note.noteContent = await new NoteContent({noteId: note.noteId,content: noteData.content