trilium/src/services/keyboard_actions.js

340 lines
9 KiB
JavaScript
Raw Normal View History

2019-11-20 03:53:04 +08:00
"use strict";
const optionService = require('./options');
const log = require('./log');
2019-11-24 16:50:19 +08:00
const utils = require('./utils');
2019-11-20 03:53:04 +08:00
2019-11-24 16:50:19 +08:00
const isMac = process.platform === "darwin";
const isElectron = utils.isElectron();
2019-11-19 06:01:31 +08:00
2019-11-20 03:53:04 +08:00
const DEFAULT_KEYBOARD_ACTIONS = [
2019-11-21 06:10:41 +08:00
{
separator: "Note navigation"
},
{
actionName: "BackInNoteHistory",
2019-11-24 16:50:19 +08:00
// Mac has a different history navigation shortcuts - https://github.com/zadam/trilium/issues/376
defaultShortcuts: isMac ? ["Meta+Left"] : ["Alt+Left"]
2019-11-21 06:10:41 +08:00
},
{
actionName: "ForwardInNoteHistory",
2019-11-24 16:50:19 +08:00
// Mac has a different history navigation shortcuts - https://github.com/zadam/trilium/issues/376
defaultShortcuts: isMac ? ["Meta+Right"] : ["Alt+Right"]
2019-11-21 06:10:41 +08:00
},
2019-11-19 06:01:31 +08:00
{
2019-11-20 03:53:04 +08:00
actionName: "JumpToNote",
defaultShortcuts: ["CommandOrControl+J"],
2019-11-19 06:01:31 +08:00
description: 'Open "Jump to note" dialog'
},
{
actionName: "ScrollToActiveNote",
defaultShortcuts: ["CommandOrControl+."]
2019-11-21 06:10:41 +08:00
},
{
actionName: "SearchNotes",
defaultShortcuts: ["CommandOrControl+S"]
2019-11-21 06:10:41 +08:00
},
{
actionName: "SearchInSubtree",
defaultShortcuts: ["CommandOrControl+Shift+S"],
description: "Search for notes in the active note's subtree"
2019-11-21 06:10:41 +08:00
},
{
actionName: "CollapseTree",
defaultShortcuts: ["Alt+C"]
2019-11-21 06:10:41 +08:00
},
{
actionName: "CollapseSubtree",
defaultShortcuts: ["Alt+-"],
description: "Collapses subtree of current note"
2019-11-21 06:10:41 +08:00
},
{
actionName: "ActivateParentNote",
defaultShortcuts: ["Backspace"],
description: "Activates the parent note of currently active note"
2019-11-21 06:10:41 +08:00
},
{
actionName: "SortChildNotes",
defaultShortcuts: ["Alt+S"],
description: "Sort child notes"
2019-11-21 06:10:41 +08:00
},
{
separator: "Creating and moving notes"
},
2019-11-19 06:01:31 +08:00
{
2019-11-20 03:53:04 +08:00
actionName: "CreateNoteAfter",
defaultShortcuts: ["CommandOrControl+O"]
2019-11-19 06:01:31 +08:00
},
{
2019-11-20 03:53:04 +08:00
actionName: "CreateNoteInto",
defaultShortcuts: ["CommandOrControl+P"]
},
{
actionName: "CreateNoteIntoDayNote",
defaultShortcuts: ["global:CommandOrControl+Alt+P"],
description: "Create and open subnote of a current day note"
2019-11-19 06:01:31 +08:00
},
{
actionName: "DeleteNotes",
2019-11-22 05:24:07 +08:00
defaultShortcuts: ["Delete"],
description: "Delete note"
},
{
actionName: "MoveNoteUp",
defaultShortcuts: ["CommandOrControl+Up"],
2019-11-22 05:24:07 +08:00
description: "Move note up"
},
{
actionName: "MoveNoteDown",
defaultShortcuts: ["CommandOrControl+Down"],
2019-11-22 05:24:07 +08:00
description: "Move note down"
},
{
actionName: "MoveNoteUpInHierarchy",
defaultShortcuts: ["CommandOrControl+Left"],
2019-11-22 05:24:07 +08:00
description: "Move note up in hierarchy"
},
{
actionName: "MoveNoteDownInHierarchy",
defaultShortcuts: ["CommandOrControl+Right"],
2019-11-22 05:24:07 +08:00
description: "Move note down in hierarchy"
},
{
actionName: "EditNoteTitle",
defaultShortcuts: ["Enter"],
description: "Jump from tree to the note detail and edit title"
2019-11-22 05:24:07 +08:00
},
{
actionName: "EditBranchPrefix",
defaultShortcuts: ["F2"],
description: "Show Edit branch prefix dialog"
},
{
actionName: "CloneNotesTo",
defaultShortcuts: ["CommandOrControl+Shift+C"]
2019-11-22 05:24:07 +08:00
},
{
actionName: "MoveNotesTo",
defaultShortcuts: ["CommandOrControl+Shift+X"]
},
{
separator: "Note clipboard"
},
2019-11-22 05:24:07 +08:00
{
actionName: "CopyNotesToClipboard",
defaultShortcuts: ["CommandOrControl+C"],
2019-11-21 06:10:41 +08:00
description: "Copy selected notes to the clipboard"
2019-11-19 06:01:31 +08:00
},
{
2019-11-22 05:24:07 +08:00
actionName: "PasteNotesFromClipboard",
defaultShortcuts: ["CommandOrControl+V"],
2019-11-21 06:10:41 +08:00
description: "Paste notes from the clipboard into active note"
2019-11-19 06:01:31 +08:00
},
{
2019-11-22 05:24:07 +08:00
actionName: "CutNotesToClipboard",
defaultShortcuts: ["CommandOrControl+X"],
2019-11-22 05:24:07 +08:00
description: "Cut selected notes to the clipboard"
2019-11-19 06:01:31 +08:00
},
{
actionName: "SelectAllNotesInParent",
defaultShortcuts: ["CommandOrControl+A"],
description: "Select all notes from the current note level"
2019-11-21 06:10:41 +08:00
},
{
actionName: "AddNoteAboveToSelection",
defaultShortcuts: ["Shift+Up"],
description: "Add note above to the selection"
2019-11-22 05:24:07 +08:00
},
{
actionName: "AddNoteBelowToSelection",
defaultShortcuts: ["Shift+Down"],
description: "Add note above to the selection"
2019-11-22 05:24:07 +08:00
},
2019-11-22 05:24:07 +08:00
{
separator: "Tabs"
2019-11-22 05:24:07 +08:00
},
{
actionName: "OpenNewTab",
2019-11-25 05:15:33 +08:00
defaultShortcuts: isElectron ? ["CommandOrControl+T"] : [],
description: "Opens new tab"
2019-11-19 06:01:31 +08:00
},
{
actionName: "CloseActiveTab",
2019-11-25 05:15:33 +08:00
defaultShortcuts: isElectron ? ["CommandOrControl+W"] : [],
description: "Closes active tab"
2019-11-19 06:01:31 +08:00
},
{
actionName: "ActivateNextTab",
2019-11-25 05:15:33 +08:00
defaultShortcuts: isElectron ? ["CommandOrControl+Tab"] : [],
description: "Activates tab on the right"
2019-11-22 05:24:07 +08:00
},
{
actionName: "ActivatePreviousTab",
2019-11-25 05:15:33 +08:00
defaultShortcuts: isElectron ? ["CommandOrControl+Shift+Tab"] : [],
description: "Activates tab on the left"
2019-11-22 05:24:07 +08:00
},
2019-11-22 05:24:07 +08:00
{
separator: "Dialogs"
2019-11-19 06:01:31 +08:00
},
{
actionName: "ShowAttributes",
2019-11-25 05:15:33 +08:00
defaultShortcuts: ["Alt+A"],
description: "Shows Attributes dialog"
2019-11-19 06:01:31 +08:00
},
{
actionName: "ShowNoteInfo",
2019-11-25 05:15:33 +08:00
defaultShortcuts: [],
description: "Shows Note Info dialog"
2019-11-19 06:01:31 +08:00
},
{
actionName: "ShowNoteSource",
2019-11-25 05:15:33 +08:00
defaultShortcuts: [],
description: "Shows Note Source dialog"
2019-11-19 06:01:31 +08:00
},
{
actionName: "ShowLinkMap",
2019-11-25 05:15:33 +08:00
defaultShortcuts: [],
description: "Shows Link Map dialog"
},
{
actionName: "ShowOptions",
2019-11-25 05:15:33 +08:00
defaultShortcuts: [],
description: "Shows Options dialog"
},
{
actionName: "ShowNoteRevisions",
2019-11-25 05:15:33 +08:00
defaultShortcuts: [],
description: "Shows Note Revisions dialog"
},
{
actionName: "ShowRecentChanges",
2019-11-25 05:15:33 +08:00
defaultShortcuts: [],
description: "Shows Recent Changes dialog"
2019-11-19 06:01:31 +08:00
},
{
2019-11-20 06:02:54 +08:00
actionName: "ShowSQLConsole",
2019-11-25 05:15:33 +08:00
defaultShortcuts: ["Alt+O"],
description: "Shows SQL Console dialog"
2019-11-19 06:01:31 +08:00
},
{
actionName: "ShowHelp",
2019-11-25 05:15:33 +08:00
defaultShortcuts: ["F1"],
description: "Shows built-in Help / cheatsheet"
},
{
separator: "Text note operations"
},
{
actionName: "AddLinkToText",
defaultShortcuts: ["CommandOrControl+L"],
description: "Open dialog to add link to the text"
2019-11-19 06:01:31 +08:00
},
{
actionName: "InsertDateTimeToText",
2019-11-21 04:35:18 +08:00
defaultShortcuts: ["Alt+T"]
2019-11-19 06:01:31 +08:00
},
{
separator: "Other"
},
{
actionName: "PrintActiveNote",
defaultShortcuts: []
},
{
actionName: "RunActiveNote",
defaultShortcuts: ["CommandOrControl+Enter"],
description: "Run active JavaScript (frontend/backend) code note"
},
{
actionName: "ToggleNoteHoisting",
defaultShortcuts: ["Alt+H"],
description: "Toggles note hoisting of active note"
},
2019-11-19 06:01:31 +08:00
{
actionName: "ReloadFrontendApp",
defaultShortcuts: ["F5", "CommandOrControl+R"]
2019-11-19 06:01:31 +08:00
},
{
2019-11-20 03:53:04 +08:00
actionName: "OpenDevTools",
defaultShortcuts: ["CommandOrControl+Shift+I"]
2019-11-19 06:01:31 +08:00
},
{
2019-11-20 03:53:04 +08:00
actionName: "FindInText",
defaultShortcuts: ["CommandOrControl+F"]
2019-11-19 06:01:31 +08:00
},
{
2019-11-20 03:53:04 +08:00
actionName: "ToggleFullscreen",
2019-11-21 04:35:18 +08:00
defaultShortcuts: ["F11"]
2019-11-19 06:01:31 +08:00
},
2019-11-21 06:10:41 +08:00
{
actionName: "ToggleZenMode",
defaultShortcuts: ["Alt+M"]
},
2019-11-19 06:01:31 +08:00
{
2019-11-20 03:53:04 +08:00
actionName: "ZoomOut",
defaultShortcuts: ["CommandOrControl+-"]
2019-11-19 06:01:31 +08:00
},
{
2019-11-20 03:53:04 +08:00
actionName: "ZoomIn",
defaultShortcuts: ["CommandOrControl+="]
}
2019-11-19 06:01:31 +08:00
];
2019-11-24 16:50:19 +08:00
const platformModifier = isMac ? 'Meta' : 'Ctrl';
2019-11-21 04:35:18 +08:00
2019-11-24 16:50:19 +08:00
for (const action of DEFAULT_KEYBOARD_ACTIONS) {
if (action.defaultShortcuts) {
action.defaultShortcuts = action.defaultShortcuts.map(shortcut => shortcut.replace("CommandOrControl", platformModifier));
2019-11-21 04:35:18 +08:00
}
2019-11-20 06:02:54 +08:00
}
2019-11-20 03:53:04 +08:00
async function getKeyboardActions() {
const actions = JSON.parse(JSON.stringify(DEFAULT_KEYBOARD_ACTIONS));
for (const action of actions) {
2019-11-21 06:10:41 +08:00
if (action.defaultShortcuts) {
action.effectiveShortcuts = action.defaultShortcuts.slice();
}
2019-11-20 03:53:04 +08:00
}
for (const option of await optionService.getOptions()) {
if (option.name.startsWith('keyboardShortcuts')) {
const actionName = option.name.substr(17);
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-20 03:53:04 +08:00
}
}
}
2019-11-20 06:02:54 +08:00
return actions;
2019-11-20 03:53:04 +08:00
}
2019-11-19 06:01:31 +08:00
module.exports = {
2019-11-20 03:53:04 +08:00
DEFAULT_KEYBOARD_ACTIONS,
getKeyboardActions
2019-11-19 06:01:31 +08:00
};