Notes/src/services/keyboard_actions.js

266 lines
6.8 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-18 23:01:31 +01:00
const ELECTRON = "electron";
2019-11-19 20:53:04 +01:00
const DEFAULT_KEYBOARD_ACTIONS = [
2019-11-20 23:10:41 +01:00
{
separator: "Note navigation"
},
{
actionName: "BackInNoteHistory",
defaultShortcuts: ["Alt+Left"]
},
{
actionName: "ForwardInNoteHistory",
defaultShortcuts: ["Alt+Right"]
},
2019-11-18 23:01:31 +01:00
{
2019-11-19 20:53:04 +01:00
actionName: "JumpToNote",
defaultShortcuts: ["CommandOrControl+J"],
2019-11-18 23:01:31 +01:00
description: 'Open "Jump to note" dialog'
},
2019-11-20 23:10:41 +01:00
2019-11-18 23:01:31 +01:00
{
2019-11-20 23:10:41 +01:00
separator: "Tabs"
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "NewTab",
defaultShortcuts: ["CommandOrControl+T"],
2019-11-18 23:01:31 +01:00
only: ELECTRON
},
{
2019-11-19 20:53:04 +01:00
actionName: "CloseTab",
defaultShortcuts: ["CommandOrControl+W"],
2019-11-18 23:01:31 +01:00
only: ELECTRON
},
{
2019-11-19 20:53:04 +01:00
actionName: "NextTab",
defaultShortcuts: ["CommandOrControl+Tab"],
2019-11-18 23:01:31 +01:00
only: ELECTRON
},
{
2019-11-19 20:53:04 +01:00
actionName: "PreviousTab",
defaultShortcuts: ["CommandOrControl+Shift+Tab"],
2019-11-18 23:01:31 +01:00
only: ELECTRON
},
2019-11-20 23:10:41 +01:00
{
separator: "Dialogs"
},
{
actionName: "ShowAttributes",
defaultShortcuts: ["Alt+A"]
},
{
actionName: "ShowNoteInfo",
defaultShortcuts: []
},
{
actionName: "ShowNoteSource",
defaultShortcuts: []
},
{
actionName: "ShowLinkMap",
defaultShortcuts: []
},
{
actionName: "ShowOptions",
defaultShortcuts: []
},
{
actionName: "ShowNoteRevisions",
defaultShortcuts: []
},
{
actionName: "ShowRecentChanges",
defaultShortcuts: []
},
{
actionName: "ShowHelp",
defaultShortcuts: ["F1"]
},
2019-11-18 23:01:31 +01:00
{
2019-11-19 20:53:04 +01:00
actionName: "CreateNoteAfter",
defaultShortcuts: ["CommandOrControl+O"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01: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-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ScrollToActiveNote",
defaultShortcuts: ["CommandOrControl+."]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "CollapseTree",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Alt+C"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "FocusNote",
2019-11-18 23:01:31 +01:00
defaultShortcuts: ["return"]
},
{
2019-11-19 20:53:04 +01:00
actionName: "RunCurrentNote",
defaultShortcuts: ["CommandOrControl+return"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ClipboardCopy",
defaultShortcuts: ["CommandOrControl+C"],
2019-11-20 23:10:41 +01:00
description: "Copy selected notes to the clipboard"
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ClipboardPaste",
defaultShortcuts: ["CommandOrControl+V"],
2019-11-20 23:10:41 +01:00
description: "Paste notes from the clipboard into active note"
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ClipboardCut",
defaultShortcuts: ["CommandOrControl+X"],
2019-11-20 23:10:41 +01:00
description: "Copy selected notes to the clipboard"
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "SelectAllNotesInParent",
defaultShortcuts: ["CommandOrControl+A"],
2019-11-20 23:10:41 +01:00
description: "Select all notes from the current note level"
},
{
separator: "Text note operations"
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "Undo",
defaultShortcuts: ["CommandOrControl+Z"],
2019-11-20 23:10:41 +01:00
description: "Undo last text operation (applicable on MacOS only)"
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "Redo",
defaultShortcuts: ["CommandOrControl+Y"],
2019-11-20 23:10:41 +01:00
description: "Undo last text operation (applicable on MacOS only)"
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "AddLinkToText",
defaultShortcuts: ["CommandOrControl+L"],
2019-11-20 23:10:41 +01:00
description: "Open dialog to add link to the text"
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "CloneNotesTo",
defaultShortcuts: ["CommandOrControl+Shift+C"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "MoveNotesTo",
defaultShortcuts: ["CommandOrControl+Shift+C"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "SearchNotes",
defaultShortcuts: ["CommandOrControl+S"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 23:02:54 +01:00
actionName: "ShowSQLConsole",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Alt+O"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-20 23:10:41 +01:00
actionName: "RunSQL",
defaultShortcuts: ["CommandOrControl+return"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "InsertDateTime",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Alt+T"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ReloadApp",
defaultShortcuts: ["F5", "CommandOrControl+R"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "OpenDevTools",
defaultShortcuts: ["CommandOrControl+Shift+I"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "FindInText",
defaultShortcuts: ["CommandOrControl+F"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ToggleFullscreen",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["F11"]
2019-11-18 23:01:31 +01:00
},
2019-11-20 23:10:41 +01:00
{
actionName: "ToggleZenMode",
defaultShortcuts: ["Alt+M"]
},
2019-11-18 23:01:31 +01:00
{
2019-11-19 20:53:04 +01:00
actionName: "ZoomOut",
defaultShortcuts: ["CommandOrControl+-"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ZoomIn",
defaultShortcuts: ["CommandOrControl+="]
2019-11-20 23:10:41 +01:00
},
{
actionName: "MarkdownToHTML",
defaultShortcuts: ["CommandOrControl+Return"]
2019-11-20 23:10:41 +01:00
},
2019-11-18 23:01:31 +01:00
];
2019-11-19 23:02:54 +01:00
if (process.platform === "darwin") {
2019-11-20 21:35:18 +01:00
for (const action of DEFAULT_KEYBOARD_ACTIONS) {
2019-11-20 23:10:41 +01:00
if (action.defaultShortcuts) {
action.defaultShortcuts = action.defaultShortcuts.map(shortcut => shortcut.replace("CommandOrControl", "Meta"));
2019-11-20 23:10:41 +01:00
}
2019-11-20 21:35:18 +01:00
}
2019-11-19 23:02:54 +01:00
// Mac has a different history navigation shortcuts - https://github.com/zadam/trilium/issues/376
2019-11-20 21:35:18 +01:00
DEFAULT_KEYBOARD_ACTIONS.find(ka => ka.actionName === 'BackInNoteHistory').defaultShortcuts = ["Meta+Left"];
DEFAULT_KEYBOARD_ACTIONS.find(ka => ka.actionName === 'ForwardInNoteHistory').defaultShortcuts = ["Meta+Right"];
}
else {
for (const action of DEFAULT_KEYBOARD_ACTIONS) {
2019-11-20 23:10:41 +01:00
if (action.defaultShortcuts) {
action.defaultShortcuts = action.defaultShortcuts.map(shortcut => shortcut.replace("CommandOrControl", "Ctrl"));
2019-11-20 23:10:41 +01:00
}
2019-11-20 21:35:18 +01:00
}
2019-11-19 23:02:54 +01:00
}
2019-11-19 20:53:04 +01:00
async function getKeyboardActions() {
const actions = JSON.parse(JSON.stringify(DEFAULT_KEYBOARD_ACTIONS));
for (const action of actions) {
2019-11-20 23:10:41 +01:00
if (action.defaultShortcuts) {
action.effectiveShortcuts = action.defaultShortcuts.slice();
}
2019-11-19 20:53:04 +01: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} not found.`);
}
}
}
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
2019-11-18 23:01:31 +01:00
};