Notes/src/services/keyboard_actions.js

232 lines
5.4 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-18 23:01:31 +01:00
{
2019-11-19 20:53:04 +01:00
actionName: "JumpToNote",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+J"],
2019-11-18 23:01:31 +01:00
description: 'Open "Jump to note" dialog'
},
{
2019-11-19 20:53:04 +01:00
actionName: "MarkdownToHTML",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+Return"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "NewTab",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+T"],
2019-11-18 23:01:31 +01:00
only: ELECTRON
},
{
2019-11-19 20:53:04 +01:00
actionName: "CloseTab",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+W"],
2019-11-18 23:01:31 +01:00
only: ELECTRON
},
{
2019-11-19 20:53:04 +01:00
actionName: "NextTab",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+Tab"],
2019-11-18 23:01:31 +01:00
only: ELECTRON
},
{
2019-11-19 20:53:04 +01:00
actionName: "PreviousTab",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+SHIFT+Tab"],
2019-11-18 23:01:31 +01:00
only: ELECTRON
},
{
2019-11-19 20:53:04 +01:00
actionName: "CreateNoteAfter",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+O"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "CreateNoteInto",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+P"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ScrollToActiveNote",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+."]
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: "RunSQL",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+return"]
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",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+return"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ClipboardCopy",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+C"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ClipboardPaste",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+V"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ClipboardCut",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+X"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "SelectAllNotesInParent",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+A"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "Undo",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+Z"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "Redo",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+Y"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "AddLinkToText",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+L"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "CloneNotesTo",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+Shift+C"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "MoveNotesTo",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+Shift+C"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "SearchNotes",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+S"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ShowAttributes",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Alt+A"]
2019-11-18 23:01:31 +01:00
},
2019-11-19 23:02:54 +01:00
{
actionName: "ShowNoteInfo",
defaultShortcuts: []
},
{
actionName: "ShowNoteSource",
defaultShortcuts: []
},
{
actionName: "ShowLinkMap",
defaultShortcuts: []
},
{
actionName: "ShowOptions",
defaultShortcuts: []
},
{
actionName: "ShowNoteRevisions",
defaultShortcuts: []
},
{
actionName: "ShowRecentChanges",
defaultShortcuts: []
},
2019-11-18 23:01:31 +01:00
{
2019-11-19 20:53:04 +01:00
actionName: "ShowHelp",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["F1"]
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-19 20:53:04 +01:00
actionName: "BackInNoteHistory",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Alt+Left"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ForwardInNoteHistory",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Alt+Right"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ToggleZenMode",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Alt+M"]
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",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["F5", "Mod+R"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "OpenDevTools",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+SHIFT+I"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "FindInText",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+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-19 20:53:04 +01:00
actionName: "ZoomOut",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+-"]
2019-11-18 23:01:31 +01:00
},
{
2019-11-19 20:53:04 +01:00
actionName: "ZoomIn",
2019-11-20 21:35:18 +01:00
defaultShortcuts: ["Mod+="]
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) {
action.defaultShortcuts = action.defaultShortcuts.map(shortcut => shortcut.replace("Mod", "Meta"));
}
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) {
action.defaultShortcuts = action.defaultShortcuts.map(shortcut => shortcut.replace("Mod", "Ctrl"));
}
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) {
action.effectiveShortcuts = action.defaultShortcuts.slice();
}
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
};