Notes/src/services/keyboard_actions.js

446 lines
12 KiB
JavaScript
Raw Normal View History

2019-11-19 20:53:04 +01:00
"use strict";
const optionService = require('./options');
const log = require('./log');
2019-11-24 09:50:19 +01:00
const utils = require('./utils');
2019-11-19 20:53:04 +01:00
2019-11-24 09:50:19 +01:00
const isMac = process.platform === "darwin";
const isElectron = utils.isElectron();
2019-11-18 23:01:31 +01:00
2019-11-19 20:53:04 +01:00
const DEFAULT_KEYBOARD_ACTIONS = [
2019-11-20 23:10:41 +01:00
{
separator: "Note navigation"
},
{
2020-02-19 20:54:14 +01:00
actionName: "backInNoteHistory",
2019-11-24 09:50:19 +01:00
// Mac has a different history navigation shortcuts - https://github.com/zadam/trilium/issues/376
defaultShortcuts: isMac ? ["Meta+Left"] : ["Alt+Left"],
scope: "window"
2019-11-20 23:10:41 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "forwardInNoteHistory",
2019-11-24 09:50:19 +01:00
// Mac has a different history navigation shortcuts - https://github.com/zadam/trilium/issues/376
defaultShortcuts: isMac ? ["Meta+Right"] : ["Alt+Right"],
scope: "window"
2019-11-20 23:10:41 +01:00
},
2019-11-18 23:01:31 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "jumpToNote",
defaultShortcuts: ["CommandOrControl+J"],
description: 'Open "Jump to note" dialog',
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "scrollToActiveNote",
defaultShortcuts: ["CommandOrControl+."],
2020-06-03 11:06:45 +02:00
scope: "window"
2019-11-20 23:10:41 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "searchNotes",
defaultShortcuts: ["CommandOrControl+S"],
scope: "window"
2019-11-20 23:10:41 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "searchInSubtree",
defaultShortcuts: ["CommandOrControl+Shift+S"],
description: "Search for notes in the active note's subtree",
scope: "note-tree"
2019-11-20 23:10:41 +01:00
},
{
actionName: "expandSubtree",
defaultShortcuts: [],
description: "Expand subtree of current note",
scope: "note-tree"
},
2019-11-20 23:10:41 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "collapseTree",
defaultShortcuts: ["Alt+C"],
description: "Collapses the complete note tree",
2020-06-03 11:06:45 +02:00
scope: "window"
2019-11-20 23:10:41 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "collapseSubtree",
defaultShortcuts: ["Alt+-"],
description: "Collapses subtree of current note",
scope: "note-tree"
2019-11-20 23:10:41 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "sortChildNotes",
defaultShortcuts: ["Alt+S"],
description: "Sort child notes",
scope: "note-tree"
2019-11-20 23:10:41 +01:00
},
{
separator: "Creating and moving notes"
},
2019-11-18 23:01:31 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "createNoteAfter",
defaultShortcuts: ["CommandOrControl+O"],
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "createNoteInto",
defaultShortcuts: ["CommandOrControl+P"],
scope: "window"
},
{
2020-02-19 20:54:14 +01:00
actionName: "createNoteIntoDayNote",
defaultShortcuts: ["global:CommandOrControl+Alt+P"],
description: "Create and open subnote of a current day note",
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "deleteNotes",
2019-11-21 22:24:07 +01:00
defaultShortcuts: ["Delete"],
description: "Delete note",
scope: "note-tree"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "moveNoteUp",
defaultShortcuts: ["CommandOrControl+Up"],
description: "Move note up",
scope: "note-tree"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "moveNoteDown",
defaultShortcuts: ["CommandOrControl+Down"],
description: "Move note down",
scope: "note-tree"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "moveNoteUpInHierarchy",
defaultShortcuts: ["CommandOrControl+Left"],
description: "Move note up in hierarchy",
scope: "note-tree"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "moveNoteDownInHierarchy",
defaultShortcuts: ["CommandOrControl+Right"],
description: "Move note down in hierarchy",
scope: "note-tree"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "editNoteTitle",
defaultShortcuts: ["Enter"],
description: "Jump from tree to the note detail and edit title",
scope: "note-tree"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "editBranchPrefix",
defaultShortcuts: ["F2"],
description: "Show Edit branch prefix dialog",
scope: "window"
},
{
2020-02-19 20:54:14 +01:00
actionName: "cloneNotesTo",
defaultShortcuts: ["CommandOrControl+Shift+C"],
scope: "window"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "moveNotesTo",
defaultShortcuts: ["CommandOrControl+Shift+X"],
scope: "window"
},
{
separator: "Note clipboard"
},
2019-11-21 22:24:07 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "copyNotesToClipboard",
defaultShortcuts: ["CommandOrControl+C"],
description: "Copy selected notes to the clipboard",
scope: "note-tree"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "pasteNotesFromClipboard",
defaultShortcuts: ["CommandOrControl+V"],
description: "Paste notes from the clipboard into active note",
scope: "note-tree"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "cutNotesToClipboard",
defaultShortcuts: ["CommandOrControl+X"],
description: "Cut selected notes to the clipboard",
scope: "note-tree"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "selectAllNotesInParent",
defaultShortcuts: ["CommandOrControl+A"],
description: "Select all notes from the current note level",
scope: "note-tree"
2019-11-20 23:10:41 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "addNoteAboveToSelection",
defaultShortcuts: ["Shift+Up"],
description: "Add note above to the selection",
scope: "note-tree"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "addNoteBelowToSelection",
defaultShortcuts: ["Shift+Down"],
description: "Add note above to the selection",
scope: "note-tree"
2019-11-21 22:24:07 +01:00
},
2019-11-21 22:24:07 +01:00
{
separator: "Tabs & Windows"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "openNewTab",
2019-11-24 22:15:33 +01:00
defaultShortcuts: isElectron ? ["CommandOrControl+T"] : [],
description: "Opens new tab",
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "closeActiveTab",
2019-11-24 22:15:33 +01:00
defaultShortcuts: isElectron ? ["CommandOrControl+W"] : [],
description: "Closes active tab",
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "activateNextTab",
2019-11-24 22:15:33 +01:00
defaultShortcuts: isElectron ? ["CommandOrControl+Tab"] : [],
description: "Activates tab on the right",
scope: "window"
2019-11-21 22:24:07 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "activatePreviousTab",
2019-11-24 22:15:33 +01:00
defaultShortcuts: isElectron ? ["CommandOrControl+Shift+Tab"] : [],
description: "Activates tab on the left",
scope: "window"
2019-11-21 22:24:07 +01:00
},
{
actionName: "openNewWindow",
defaultShortcuts: [],
description: "Open new empty window",
scope: "window"
},
2019-11-21 22:24:07 +01:00
{
separator: "Dialogs"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "showNoteInfo",
2019-11-24 22:15:33 +01:00
defaultShortcuts: [],
description: "Shows Note Info dialog",
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "showNoteSource",
2019-11-24 22:15:33 +01:00
defaultShortcuts: [],
description: "Shows Note Source dialog",
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "showLinkMap",
2019-11-24 22:15:33 +01:00
defaultShortcuts: [],
description: "Shows Link Map dialog",
scope: "window"
},
{
2020-02-19 20:54:14 +01:00
actionName: "showOptions",
2019-11-24 22:15:33 +01:00
defaultShortcuts: [],
description: "Shows Options dialog",
scope: "window"
},
{
2020-02-19 20:54:14 +01:00
actionName: "showNoteRevisions",
2019-11-24 22:15:33 +01:00
defaultShortcuts: [],
description: "Shows Note Revisions dialog",
scope: "window"
},
{
2020-02-19 20:54:14 +01:00
actionName: "showRecentChanges",
2019-11-24 22:15:33 +01:00
defaultShortcuts: [],
description: "Shows Recent Changes dialog",
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "showSQLConsole",
2019-11-24 22:15:33 +01:00
defaultShortcuts: ["Alt+O"],
description: "Shows SQL Console dialog",
scope: "window"
2019-11-18 23:01:31 +01:00
},
2019-12-05 21:25:36 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "showBackendLog",
2019-12-05 21:25:36 +01:00
defaultShortcuts: [],
description: "Shows Backend Log dialog",
scope: "window"
2019-12-05 21:25:36 +01:00
},
2019-11-18 23:01:31 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "showHelp",
2019-11-24 22:15:33 +01:00
defaultShortcuts: ["F1"],
description: "Shows built-in Help / cheatsheet",
scope: "window"
},
{
separator: "Text note operations"
},
{
2020-02-19 20:54:14 +01:00
actionName: "addLinkToText",
defaultShortcuts: ["CommandOrControl+L"],
description: "Open dialog to add link to the text",
scope: "text-detail"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "insertDateTimeToText",
defaultShortcuts: ["Alt+T"],
scope: "text-detail"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "pasteMarkdownIntoText",
defaultShortcuts: [],
description: "Pastes Markdown from clipboard into text note",
scope: "text-detail"
},
2020-01-01 22:59:51 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "cutIntoNote",
2020-01-01 22:59:51 +01:00
defaultShortcuts: [],
description: "Cuts the selection from the current note and creates subnote with the selected text",
scope: "text-detail"
2020-01-01 22:59:51 +01:00
},
{
separator: "Attributes (labels & relations)"
},
{
actionName: "focusOnAttributes",
defaultShortcuts: ["Alt+A"],
description: "Put focus into attribute editor",
scope: "window"
},
{
actionName: "addNewLabel",
defaultShortcuts: ["Alt+L"],
description: "Create new label",
scope: "window"
},
{
actionName: "addNewRelation",
defaultShortcuts: ["Alt+R"],
description: "Create new relation",
scope: "window"
},
{
separator: "Other"
},
{
2020-02-19 20:54:14 +01:00
actionName: "printActiveNote",
defaultShortcuts: [],
scope: "window"
},
{
2020-02-19 20:54:14 +01:00
actionName: "runActiveNote",
defaultShortcuts: ["CommandOrControl+Enter"],
description: "Run active JavaScript (frontend/backend) code note",
scope: "code-detail"
},
{
2020-02-19 20:54:14 +01:00
actionName: "toggleNoteHoisting",
defaultShortcuts: ["Alt+H"],
description: "Toggles note hoisting of active note",
scope: "window"
},
{
actionName: "unhoist",
defaultShortcuts: ["Alt+U"],
description: "Unhoist from anywhere",
scope: "window"
},
2019-11-18 23:01:31 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "reloadFrontendApp",
defaultShortcuts: ["F5", "CommandOrControl+R"],
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "openDevTools",
defaultShortcuts: isElectron ? ["CommandOrControl+Shift+I"] : [],
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "findInText",
2020-09-13 00:19:50 +02:00
defaultShortcuts: isElectron ? ["CommandOrControl+F"] : [],
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "toggleFullscreen",
defaultShortcuts: ["F11"],
scope: "window"
2019-11-18 23:01:31 +01:00
},
2019-11-20 23:10:41 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "toggleZenMode",
defaultShortcuts: ["Alt+M"],
scope: "window"
2019-11-20 23:10:41 +01:00
},
2019-11-18 23:01:31 +01:00
{
2020-02-19 20:54:14 +01:00
actionName: "zoomOut",
2020-08-24 23:33:27 +02:00
defaultShortcuts: isElectron ? ["CommandOrControl+-"] : [],
scope: "window"
2019-11-18 23:01:31 +01:00
},
{
2020-02-19 20:54:14 +01:00
actionName: "zoomIn",
2020-08-24 23:33:27 +02:00
defaultShortcuts: isElectron ? ["CommandOrControl+="] : [],
scope: "window"
},
{
2020-02-19 20:54:14 +01:00
actionName: "copyWithoutFormatting",
defaultShortcuts: ["CommandOrControl+Alt+C"],
scope: "text-detail"
}
2019-11-18 23:01:31 +01:00
];
2019-11-24 09:50:19 +01:00
const platformModifier = isMac ? 'Meta' : 'Ctrl';
2019-11-20 21:35:18 +01:00
2019-11-24 09:50:19 +01:00
for (const action of DEFAULT_KEYBOARD_ACTIONS) {
if (action.defaultShortcuts) {
action.defaultShortcuts = action.defaultShortcuts.map(shortcut => shortcut.replace("CommandOrControl", platformModifier));
2019-11-20 21:35:18 +01:00
}
2019-11-19 23:02:54 +01:00
}
2020-06-20 12:31:38 +02:00
function getKeyboardActions() {
2019-11-19 20:53:04 +01:00
const actions = JSON.parse(JSON.stringify(DEFAULT_KEYBOARD_ACTIONS));
for (const action of actions) {
action.effectiveShortcuts = action.effectiveShortcuts ? action.defaultShortcuts.slice() : [];
2019-11-19 20:53:04 +01:00
}
2020-06-20 12:31:38 +02:00
for (const option of optionService.getOptions()) {
2019-11-19 20:53:04 +01:00
if (option.name.startsWith('keyboardShortcuts')) {
2020-02-19 20:54:14 +01:00
let actionName = option.name.substr(17);
actionName = actionName.charAt(0).toLowerCase() + actionName.slice(1);
2019-11-19 20:53:04 +01:00
const action = actions.find(ea => ea.actionName === actionName);
if (action) {
try {
action.effectiveShortcuts = JSON.parse(option.value);
}
catch (e) {
log.error(`Could not parse shortcuts for action ${actionName}`);
}
}
else {
log.info(`Keyboard action ${actionName} found in database, but not in action definition.`);
2019-11-19 20:53:04 +01:00
}
}
}
2019-11-19 23:02:54 +01:00
return actions;
2019-11-19 20:53:04 +01:00
}
2019-11-18 23:01:31 +01:00
module.exports = {
2019-11-19 20:53:04 +01:00
DEFAULT_KEYBOARD_ACTIONS,
getKeyboardActions
2020-06-03 11:06:45 +02:00
};