Skip to content

Build your personal knowledge base with Trilium Notes

tree keyboard shortcuts

zadam465c3b87a76f
diff --git a/src/public/javascripts/services/keyboard_actions.js b/src/public/javascripts/services/keyboard_actions.js
index 1cfd492447..1498788f69 100644
--- a/src/public/javascripts/services/keyboard_actions.js
+++ b/src/public/javascripts/services/keyboard_actions.js
@@ -52,6 +52,16 @@ function setActionHandler(actionName, handler) {
}
async function triggerAction(actionName) {
+ const action = getAction(actionName);
+
+ if (!action.handler) {
+ throw new Error(`Action ${actionName} has no handler`);
+ }
+
+ await action.handler();
+}
+
+async function getAction(actionName) {
await keyboardActionsLoaded;
const action = keyboardActionRepo[actionName];
@@ -60,14 +70,11 @@ async function triggerAction(actionName) {
throw new Error(`Cannot find action ${actionName}`);
}
- if (!action.handler) {
- throw new Error(`Action ${actionName} has no handler`);
- }
-
- await action.handler();
+ return action;
}
export default {
setActionHandler,
- triggerAction
+ triggerAction,
+ getAction
};
\ No newline at end of file
diff --git a/src/public/javascripts/services/tree.js b/src/public/javascripts/services/tree.js
index d56f791080..de8f8f67dc 100644
--- a/src/public/javascripts/services/tree.js
+++ b/src/public/javascripts/services/tree.js
@@ -9,9 +9,7 @@ import server from './server.js';
import treeCache from './tree_cache.js';
import toastService from "./toast.js";
import treeBuilder from "./tree_builder.js";
-import treeKeyBindings from "./tree_keybindings.js";
-import Branch from '../entities/branch.js';
-import NoteShort from '../entities/note_short.js';
+import treeKeyBindingService from "./tree_keybindings.js";
import hoistedNoteService from '../services/hoisted_note.js';
import optionsService from "../services/options.js";
import TreeContextMenu from "./tree_context_menu.js";
@@ -430,7 +428,7 @@ async function treeInitialized() {
setFrontendAsLoaded();
}
-function initFancyTree(tree) {
+async function initFancyTree(tree) {
utils.assertArguments(tree);
$tree.fancytree({
@@ -473,7 +471,7 @@ function initFancyTree(tree) {
collapse: (event, data) => setExpandedToServer(data.node.data.branchId, false),
init: (event, data) => treeInitialized(), // don't collapse to short form
hotkeys: {
- keydown: treeKeyBindings
+ keydown: await treeKeyBindingService.getKeyboardBindings()
},
dnd5: dragAndDropSetup,
lazyLoad: function(event, data) {
@@ -754,7 +752,7 @@ async function sortAlphabetically(noteId) {
async function showTree() {
const tree = await loadTree();
- initFancyTree(tree);
+ await initFancyTree(tree);
}
ws.subscribeToMessages(message => {
diff --git a/src/public/javascripts/services/tree_keybindings.js b/src/public/javascripts/services/tree_keybindings.js
index 1ce0b43c9d..4d3ecb14aa 100644
--- a/src/public/javascripts/services/tree_keybindings.js
+++ b/src/public/javascripts/services/tree_keybindings.js
@@ -5,12 +5,39 @@ import hoistedNoteService from "./hoisted_note.js";
import clipboard from "./clipboard.js";
import treeCache from "./tree_cache.js";
import searchNoteService from "./search_notes.js";
+import keyboardActionService from "./keyboard_actions.js";
-const keyBindings = {
- "del": node => {
+const fixedKeyBindings = {
+ // code below shouldn't be necessary normally, however there's some problem with interaction with context menu plugin
+ // after opening context menu, standard shortcuts don't work, but they are detected here
+ // so we essentially takeover the standard handling with our implementation.
+ "left": node => {
+ node.navigate($.ui.keyCode.LEFT, true).then(treeService.clearSelectedNodes);
+
+ return false;
+ },
+ "right": node => {
+ node.navigate($.ui.keyCode.RIGHT, true).then(treeService.clearSelectedNodes);
+
+ return false;
+ },
+ "up": node => {
+ node.navigate($.ui.keyCode.UP, true).then(treeService.clearSelectedNodes);
+
+ return false;
+ },
+ "down": node => {
+ node.navigate($.ui.keyCode.DOWN, true).then(treeService.clearSelectedNodes);
+
+ return false;
+ }
+};
+
+const templates = {
+ "DeleteNote": node => {
treeChangesService.deleteNodes(treeService.getSelectedOrActiveNodes(node));
},
- "ctrl+up": node => {
+ "MoveNoteUp": node => {
const beforeNode = node.getPrevSibling();
if (beforeNode !== null) {
@@ -19,7 +46,7 @@ const keyBindings = {
return false;
},
- "ctrl+down": node => {
+ "MoveNoteDown": node => {
const afterNode = node.getNextSibling();
if (afterNode !== null) {
treeChangesService.moveAfterNode([node], afterNode);
@@ -27,12 +54,12 @@ const keyBindings = {
return false;
},
- "ctrl+left": node => {
+ "MoveNoteUpInHierarchy": node => {
treeChangesService.moveNodeUpInHierarchy(node);
return false;
},
- "ctrl+right": node => {
+ "MoveNoteDownInHierarchy": node => {
const toNode = node.getPrevSibling();
if (toNode !== null) {
@@ -41,7 +68,7 @@ const keyBindings = {
return false;
},
- "shift+up": () => {
+ "AddNoteAboveToSelection": () => {
const node = treeService.getFocusedNode();
if (!node) {
@@ -64,7 +91,7 @@ const keyBindings = {
return false;
},
- "shift+down": () => {
+ "AddNoteBelowToSelection": () => {
const node = treeService.getFocusedNode();
if (!node) {
@@ -87,51 +114,53 @@ const keyBindings = {
return false;
},
- "f2": async node => {
+ // TODO: this shouldn't be tree specific shortcut
+ "EditBranchPrefix": async node => {
const editBranchPrefixDialog = await import("../dialogs/branch_prefix.js");
editBranchPrefixDialog.showDialog(node);
},
- "alt+-": node => {
+ "CollapseSubtree": node => {
treeService.collapseTree(node);
},
- "alt+s": node => {
+ "SortChildNotes": node => {
treeService.sortAlphabetically(node.data.noteId);
return false;
},
- "ctrl+a": node => {
+ "SelectAllNotesInParent": node => {
for (const child of node.getParent().getChildren()) {
child.setSelected(true);
}
return false;
},
- "ctrl+c": node => {
+ "CopyNotesToClipboard": node => {
clipboard.copy(treeService.getSelectedOrActiveNodes(node));
return false;
},
- "ctrl+x": node => {
+ "CutNotesToClipboard": node => {
clipboard.cut(treeService.getSelectedOrActiveNodes(node));
return false;
},
- "ctrl+v": node => {
+ "PasteNotesFromClipboard": node => {
clipboard.pasteInto(node);
return false;
},
- "return": node => {
+ "EditNoteTitle": node => {
noteDetailService.focusOnTitle();
return false;
},
- "backspace": async node => {
+ "ActivateParentNote": async node => {
if (!await hoistedNoteService.isRootNode(node)) {
node.getParent().setActive().then(treeService.clearSelectedNodes);
}
},
- "ctrl+h": node => {
+ // TODO: this should probably be app-global shortcut
+ "ToggleNoteHoisting": node => {
hoistedNoteService.getHoistedNoteId().then(async hoistedNoteId => {
if (node.data.noteId === hoistedNoteId) {
hoistedNoteService.unhoist();
@@ -147,34 +176,27 @@ const keyBindings = {
return false;
},
- // code below shouldn't be necessary normally, however there's some problem with interaction with context menu plugin
- // after opening context menu, standard shortcuts don't work, but they are detected here
- // so we essentially takeover the standard handling with our implementation.
- "left": node => {
- node.navigate($.ui.keyCode.LEFT, true).then(treeService.clearSelectedNodes);
-
- return false;
- },
- "right": node => {
- node.navigate($.ui.keyCode.RIGHT, true).then(treeService.clearSelectedNodes);
+ "SearchInSubtree": node => {
+ searchNoteService.searchInSubtree(node.data.noteId);
return false;
- },
- "up": node => {
- node.navigate($.ui.keyCode.UP, true).then(treeService.clearSelectedNodes);
+ }
+};
- return false;
- },
- "down": node => {
- node.navigate($.ui.keyCode.DOWN, true).then(treeService.clearSelectedNodes);
+async function getKeyboardBindings() {
+ const bindings = Object.assign({}, fixedKeyBindings);
- return false;
- },
- "ctrl+shift+s": node => {
- searchNoteService.searchInSubtree(node.data.noteId);
+ for (const actionName in templates) {
+ const action = await keyboardActionService.getAction(actionName);
- return false;
+ for (const shortcut of action.effectiveShortcuts || []) {
+ bindings[shortcut] = templates[actionName];
+ }
}
-};
-export default keyBindings;
\ No newline at end of file
+ return bindings;
+}
+
+export default {
+ getKeyboardBindings
+};
\ No newline at end of file
diff --git a/src/services/keyboard_actions.js b/src/services/keyboard_actions.js
index b757406ba8..18a65a7063 100644
--- a/src/services/keyboard_actions.js
+++ b/src/services/keyboard_actions.js
@@ -114,37 +114,97 @@ const DEFAULT_KEYBOARD_ACTIONS = [
defaultShortcuts: ["CommandOrControl+return"]
},
{
- actionName: "ClipboardCopy",
+ actionName: "DeleteNote",
+ defaultShortcuts: ["Delete"],
+ description: "Delete note"
+ },
+ {
+ actionName: "MoveNoteUp",
+ defaultShortcuts: ["Ctrl+Up"],
+ description: "Move note up"
+ },
+ {
+ actionName: "MoveNoteDown",
+ defaultShortcuts: ["Ctrl+Down"],
+ description: "Move note down"
+ },
+ {
+ actionName: "MoveNoteUpInHierarchy",
+ defaultShortcuts: ["Ctrl+Left"],
+ description: "Move note up in hierarchy"
+ },
+ {
+ actionName: "MoveNoteDownInHierarchy",
+ defaultShortcuts: ["Ctrl+Right"],
+ description: "Move note down in hierarchy"
+ },
+ {
+ actionName: "AddNoteAboveToSelection",
+ defaultShortcuts: ["Shift+Up"],
+ description: "Add note above to the selection"
+ },
+ {
+ actionName: "AddNoteBelowToSelection",
+ defaultShortcuts: ["Shift+Down"],
+ description: "Add note above to the selection"
+ },
+ {
+ actionName: "CopyNotesToClipboard",
defaultShortcuts: ["CommandOrControl+C"],
description: "Copy selected notes to the clipboard"
},
{
- actionName: "ClipboardPaste",
+ actionName: "PasteNotesFromClipboard",
defaultShortcuts: ["CommandOrControl+V"],
description: "Paste notes from the clipboard into active note"
},
{
- actionName: "ClipboardCut",
+ actionName: "CutNotesToClipboard",
defaultShortcuts: ["CommandOrControl+X"],
- description: "Copy selected notes to the clipboard"
+ description: "Cut selected notes to the clipboard"
},
{
- actionName: "SelectAllNotesInParent",
- defaultShortcuts: ["CommandOrControl+A"],
- description: "Select all notes from the current note level"
+ actionName: "EditBranchPrefix",
+ defaultShortcuts: ["F2"],
+ description: "Show Edit branch prefix dialog"
},
{
- separator: "Text note operations"
+ actionName: "CollapseSubtree",
+ defaultShortcuts: ["Alt+-"],
+ description: "Collapses subtree of current note"
+ },
+ {
+ actionName: "SortChildNotes",
+ defaultShortcuts: ["Alt+s"],
+ description: "Sort child notes"
+ },
+ {
+ actionName: "ActivateParentNote",
+ defaultShortcuts: ["Backspace"],
+ description: "Activates parent note of currently active note"
+ },
+ {
+ actionName: "ToggleNoteHoisting",
+ defaultShortcuts: ["Alt+h"],
+ description: "Toggles note hoisting of active note"
},
{
- actionName: "Undo",
- defaultShortcuts: ["CommandOrControl+Z"],
- description: "Undo last text operation (applicable on MacOS only)"
+ actionName: "SearchInSubtree",
+ defaultShortcuts: ["CommandOrControl+Shift+S"],
+ description: "Search for notes in the active note's subtree"
},
{
- actionName: "Redo",
- defaultShortcuts: ["CommandOrControl+Y"],
- description: "Undo last text operation (applicable on MacOS only)"
+ actionName: "EditNoteTitle",
+ defaultShortcuts: ["return"],
+ description: "Edit active note title"
+ },
+ {
+ actionName: "SelectAllNotesInParent",
+ defaultShortcuts: ["CommandOrControl+A"],
+ description: "Select all notes from the current note level"
+ },
+ {
+ separator: "Text note operations"
},
{
actionName: "AddLinkToText",