mirror of
				https://github.com/zadam/trilium.git
				synced 2025-11-03 20:06:08 +01:00 
			
		
		
		
	dynamically translating kbd based on actions
This commit is contained in:
		@@ -1,3 +1,4 @@
 | 
				
			|||||||
 | 
					import keyboardActionService from './keyboard_actions.js';
 | 
				
			||||||
const $contextMenuContainer = $("#context-menu-container");
 | 
					const $contextMenuContainer = $("#context-menu-container");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
let dateContextMenuOpenedMs = 0;
 | 
					let dateContextMenuOpenedMs = 0;
 | 
				
			||||||
@@ -69,6 +70,8 @@ async function initContextMenu(event, contextMenu) {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    addItems($contextMenuContainer, await contextMenu.getContextMenuItems());
 | 
					    addItems($contextMenuContainer, await contextMenu.getContextMenuItems());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    keyboardActionService.updateKbdElements($contextMenuContainer);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // code below tries to detect when dropdown would overflow from page
 | 
					    // code below tries to detect when dropdown would overflow from page
 | 
				
			||||||
    // in such case we'll position it above click coordinates so it will fit into client
 | 
					    // in such case we'll position it above click coordinates so it will fit into client
 | 
				
			||||||
    const clickPosition = event.pageY;
 | 
					    const clickPosition = event.pageY;
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -121,7 +121,7 @@ function registerEntrypoints() {
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $("#reload-frontend-button").on('click', utils.reloadApp);
 | 
					    $("#reload-frontend-button").on('click', utils.reloadApp);
 | 
				
			||||||
    keyboardActionService.setActionHandler("ReloadApp", utils.reloadApp);
 | 
					    keyboardActionService.setActionHandler("ReloadFrontendApp", utils.reloadApp);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    $("#open-dev-tools-button").toggle(utils.isElectron());
 | 
					    $("#open-dev-tools-button").toggle(utils.isElectron());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -61,20 +61,39 @@ async function triggerAction(actionName) {
 | 
				
			|||||||
	await action.handler();
 | 
						await action.handler();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
async function getAction(actionName) {
 | 
					async function getAction(actionName, silent = false) {
 | 
				
			||||||
	await keyboardActionsLoaded;
 | 
						await keyboardActionsLoaded;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const action = keyboardActionRepo[actionName];
 | 
						const action = keyboardActionRepo[actionName];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (!action) {
 | 
						if (!action) {
 | 
				
			||||||
 | 
							if (silent) {
 | 
				
			||||||
 | 
								console.log(`Cannot find action ${actionName}`);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							else {
 | 
				
			||||||
			throw new Error(`Cannot find action ${actionName}`);
 | 
								throw new Error(`Cannot find action ${actionName}`);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return action;
 | 
						return action;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function updateKbdElements($container) {
 | 
				
			||||||
 | 
						$container.find('kbd[data-kb-action]').each(async (i, el) => {
 | 
				
			||||||
 | 
							const actionName = $(el).attr('data-kb-action');
 | 
				
			||||||
 | 
							const action = await getAction(actionName, true);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							if (action) {
 | 
				
			||||||
 | 
								$(el).text(action.effectiveShortcuts.join(', '));
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						});
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					$(() => updateKbdElements($(document)));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default {
 | 
					export default {
 | 
				
			||||||
	setActionHandler,
 | 
						setActionHandler,
 | 
				
			||||||
	triggerAction,
 | 
						triggerAction,
 | 
				
			||||||
	getAction
 | 
						getAction,
 | 
				
			||||||
 | 
						updateKbdElements
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
@@ -46,39 +46,39 @@ class TreeContextMenu {
 | 
				
			|||||||
        const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch;
 | 
					        const insertNoteAfterEnabled = isNotRoot && !isHoisted && parentNotSearch;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return [
 | 
					        return [
 | 
				
			||||||
            { title: "Open in new tab", cmd: "openInTab", uiIcon: "empty", enabled: noSelectedNotes },
 | 
					            { title: 'Open in new tab', cmd: "openInTab", uiIcon: "empty", enabled: noSelectedNotes },
 | 
				
			||||||
            { title: "Insert note after <kbd>Ctrl+O</kbd>", cmd: "insertNoteAfter", uiIcon: "plus",
 | 
					            { title: 'Insert note after <kbd data-kb-action="CreateNoteAfter"></kbd>', cmd: "insertNoteAfter", uiIcon: "plus",
 | 
				
			||||||
                items: insertNoteAfterEnabled ? this.getNoteTypeItems("insertNoteAfter") : null,
 | 
					                items: insertNoteAfterEnabled ? this.getNoteTypeItems("insertNoteAfter") : null,
 | 
				
			||||||
                enabled: insertNoteAfterEnabled && noSelectedNotes },
 | 
					                enabled: insertNoteAfterEnabled && noSelectedNotes },
 | 
				
			||||||
            { title: "Insert child note <kbd>Ctrl+P</kbd>", cmd: "insertChildNote", uiIcon: "plus",
 | 
					            { title: 'Insert child note <kbd data-kb-action="CreateNoteInto"></kbd>', cmd: "insertChildNote", uiIcon: "plus",
 | 
				
			||||||
                items: notSearch ? this.getNoteTypeItems("insertChildNote") : null,
 | 
					                items: notSearch ? this.getNoteTypeItems("insertChildNote") : null,
 | 
				
			||||||
                enabled: notSearch && noSelectedNotes },
 | 
					                enabled: notSearch && noSelectedNotes },
 | 
				
			||||||
            { title: "Delete <kbd>Delete</kbd>", cmd: "delete", uiIcon: "trash",
 | 
					            { title: 'Delete <kbd data-kb-action="DeleteNotes"></kbd>', cmd: "delete", uiIcon: "trash",
 | 
				
			||||||
                enabled: isNotRoot && !isHoisted && parentNotSearch },
 | 
					                enabled: isNotRoot && !isHoisted && parentNotSearch },
 | 
				
			||||||
            { title: "----" },
 | 
					            { title: "----" },
 | 
				
			||||||
            { title: "Search in subtree <kbd>Ctrl+Shift+S</kbd>", cmd: "searchInSubtree", uiIcon: "search",
 | 
					            { title: 'Search in subtree <kbd data-kb-action="SearchInSubtree"></kbd>', cmd: "searchInSubtree", uiIcon: "search",
 | 
				
			||||||
                enabled: notSearch && noSelectedNotes },
 | 
					                enabled: notSearch && noSelectedNotes },
 | 
				
			||||||
            isHoisted ? null : { title: "Hoist note <kbd>Ctrl-H</kbd>", cmd: "hoist", uiIcon: "empty", enabled: noSelectedNotes && notSearch },
 | 
					            isHoisted ? null : { title: 'Hoist note <kbd data-kb-action="ToggleNoteHoisting"></kbd>', cmd: "hoist", uiIcon: "empty", enabled: noSelectedNotes && notSearch },
 | 
				
			||||||
            !isHoisted || !isNotRoot ? null : { title: "Unhoist note <kbd>Ctrl-H</kbd>", cmd: "unhoist", uiIcon: "arrow-up" },
 | 
					            !isHoisted || !isNotRoot ? null : { title: 'Unhoist note <kbd data-kb-action="ToggleNoteHoisting"></kbd>', cmd: "unhoist", uiIcon: "arrow-up" },
 | 
				
			||||||
            { title: "Edit branch prefix <kbd>F2</kbd>", cmd: "editBranchPrefix", uiIcon: "empty",
 | 
					            { title: 'Edit branch prefix <kbd data-kb-action="EditBranchPrefix"></kbd>', cmd: "editBranchPrefix", uiIcon: "empty",
 | 
				
			||||||
                enabled: isNotRoot && parentNotSearch && noSelectedNotes},
 | 
					                enabled: isNotRoot && parentNotSearch && noSelectedNotes},
 | 
				
			||||||
            { title: "----" },
 | 
					            { title: "----" },
 | 
				
			||||||
            { title: "Protect subtree", cmd: "protectSubtree", uiIcon: "check-shield", enabled: noSelectedNotes },
 | 
					            { title: "Protect subtree", cmd: "protectSubtree", uiIcon: "check-shield", enabled: noSelectedNotes },
 | 
				
			||||||
            { title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield", enabled: noSelectedNotes },
 | 
					            { title: "Unprotect subtree", cmd: "unprotectSubtree", uiIcon: "shield", enabled: noSelectedNotes },
 | 
				
			||||||
            { title: "----" },
 | 
					            { title: "----" },
 | 
				
			||||||
            { title: "Copy / clone <kbd>Ctrl+C</kbd>", cmd: "copy", uiIcon: "copy",
 | 
					            { title: 'Copy / clone <kbd data-kb-action="CopyNotesToClipboard"></kbd>', cmd: "copy", uiIcon: "copy",
 | 
				
			||||||
                enabled: isNotRoot && !isHoisted },
 | 
					                enabled: isNotRoot && !isHoisted },
 | 
				
			||||||
            { title: "Clone to ... <kbd>Ctrl+Shift+C</kbd>", cmd: "cloneTo", uiIcon: "empty",
 | 
					            { title: 'Clone to ... <kbd data-kb-action="CloneNotesTo"></kbd>', cmd: "cloneTo", uiIcon: "empty",
 | 
				
			||||||
                enabled: isNotRoot && !isHoisted },
 | 
					                enabled: isNotRoot && !isHoisted },
 | 
				
			||||||
            { title: "Cut <kbd>Ctrl+X</kbd>", cmd: "cut", uiIcon: "cut",
 | 
					            { title: 'Cut <kbd data-kb-action="CutNotesToClipboard"></kbd>', cmd: "cut", uiIcon: "cut",
 | 
				
			||||||
                enabled: isNotRoot && !isHoisted && parentNotSearch },
 | 
					                enabled: isNotRoot && !isHoisted && parentNotSearch },
 | 
				
			||||||
            { title: "Move to ... <kbd>Ctrl+Shift+X</kbd>", cmd: "moveTo", uiIcon: "empty",
 | 
					            { title: 'Move to ... <kbd data-kb-action="MoveNotesTo">Ctrl+Shift+X</kbd>', cmd: "moveTo", uiIcon: "empty",
 | 
				
			||||||
                enabled: isNotRoot && !isHoisted && parentNotSearch },
 | 
					                enabled: isNotRoot && !isHoisted && parentNotSearch },
 | 
				
			||||||
            { title: "Paste into <kbd>Ctrl+V</kbd>", cmd: "pasteInto", uiIcon: "paste",
 | 
					            { title: 'Paste into <kbd data-kb-action="PasteNotesFromClipboard">Ctrl+V</kbd>', cmd: "pasteInto", uiIcon: "paste",
 | 
				
			||||||
                enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes },
 | 
					                enabled: !clipboard.isClipboardEmpty() && notSearch && noSelectedNotes },
 | 
				
			||||||
            { title: "Paste after", cmd: "pasteAfter", uiIcon: "paste",
 | 
					            { title: 'Paste after', cmd: "pasteAfter", uiIcon: "paste",
 | 
				
			||||||
                enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes },
 | 
					                enabled: !clipboard.isClipboardEmpty() && isNotRoot && !isHoisted && parentNotSearch && noSelectedNotes },
 | 
				
			||||||
            { title: "Duplicate note here", cmd: "duplicateNote", uiIcon: "empty",
 | 
					            { title: 'Duplicate note here', cmd: "duplicateNote", uiIcon: "empty",
 | 
				
			||||||
                enabled: noSelectedNotes && parentNotSearch && (!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) },
 | 
					                enabled: noSelectedNotes && parentNotSearch && (!note.isProtected || protectedSessionHolder.isProtectedSessionAvailable()) },
 | 
				
			||||||
            { title: "----" },
 | 
					            { title: "----" },
 | 
				
			||||||
            { title: "Export", cmd: "export", uiIcon: "empty",
 | 
					            { title: "Export", cmd: "export", uiIcon: "empty",
 | 
				
			||||||
@@ -86,9 +86,9 @@ class TreeContextMenu {
 | 
				
			|||||||
            { title: "Import into note", cmd: "importIntoNote", uiIcon: "empty",
 | 
					            { title: "Import into note", cmd: "importIntoNote", uiIcon: "empty",
 | 
				
			||||||
                enabled: notSearch && noSelectedNotes },
 | 
					                enabled: notSearch && noSelectedNotes },
 | 
				
			||||||
            { title: "----" },
 | 
					            { title: "----" },
 | 
				
			||||||
            { title: "Collapse subtree <kbd>Alt+-</kbd>", cmd: "collapseSubtree", uiIcon: "align-justify", enabled: noSelectedNotes },
 | 
					            { title: 'Collapse subtree <kbd data-kb-action="CollapseSubtree">Alt+-</kbd>', cmd: "collapseSubtree", uiIcon: "align-justify", enabled: noSelectedNotes },
 | 
				
			||||||
            { title: "Force note sync", cmd: "forceNoteSync", uiIcon: "recycle", enabled: noSelectedNotes },
 | 
					            { title: "Force note sync", cmd: "forceNoteSync", uiIcon: "recycle", enabled: noSelectedNotes },
 | 
				
			||||||
            { title: "Sort alphabetically <kbd>Alt+S</kbd>", cmd: "sortAlphabetically", uiIcon: "empty", enabled: noSelectedNotes && notSearch }
 | 
					            { title: 'Sort alphabetically <kbd data-kb-action="SortChildNotes">Alt+S</kbd>', cmd: "sortAlphabetically", uiIcon: "empty", enabled: noSelectedNotes && notSearch }
 | 
				
			||||||
        ].filter(row => row !== null);
 | 
					        ].filter(row => row !== null);
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,7 +34,7 @@ const fixedKeyBindings = {
 | 
				
			|||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const templates = {
 | 
					const templates = {
 | 
				
			||||||
    "DeleteNote": node => {
 | 
					    "DeleteNotes": node => {
 | 
				
			||||||
        treeChangesService.deleteNodes(treeService.getSelectedOrActiveNodes(node));
 | 
					        treeChangesService.deleteNodes(treeService.getSelectedOrActiveNodes(node));
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    "MoveNoteUp": node => {
 | 
					    "MoveNoteUp": node => {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -114,7 +114,7 @@ const DEFAULT_KEYBOARD_ACTIONS = [
 | 
				
			|||||||
        defaultShortcuts: ["CommandOrControl+return"]
 | 
					        defaultShortcuts: ["CommandOrControl+return"]
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        actionName: "DeleteNote",
 | 
					        actionName: "DeleteNotes",
 | 
				
			||||||
        defaultShortcuts: ["Delete"],
 | 
					        defaultShortcuts: ["Delete"],
 | 
				
			||||||
        description: "Delete note"
 | 
					        description: "Delete note"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
@@ -185,7 +185,7 @@ const DEFAULT_KEYBOARD_ACTIONS = [
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        actionName: "ToggleNoteHoisting",
 | 
					        actionName: "ToggleNoteHoisting",
 | 
				
			||||||
        defaultShortcuts: ["Alt+h"],
 | 
					        defaultShortcuts: ["Alt+H"],
 | 
				
			||||||
        description: "Toggles note hoisting of active note"
 | 
					        description: "Toggles note hoisting of active note"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@@ -195,7 +195,7 @@ const DEFAULT_KEYBOARD_ACTIONS = [
 | 
				
			|||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        actionName: "EditNoteTitle",
 | 
					        actionName: "EditNoteTitle",
 | 
				
			||||||
        defaultShortcuts: ["return"],
 | 
					        defaultShortcuts: ["Return"],
 | 
				
			||||||
        description: "Edit active note title"
 | 
					        description: "Edit active note title"
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
@@ -236,7 +236,7 @@ const DEFAULT_KEYBOARD_ACTIONS = [
 | 
				
			|||||||
        defaultShortcuts: ["Alt+T"]
 | 
					        defaultShortcuts: ["Alt+T"]
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        actionName: "ReloadApp",
 | 
					        actionName: "ReloadFrontendApp",
 | 
				
			||||||
        defaultShortcuts: ["F5", "CommandOrControl+R"]
 | 
					        defaultShortcuts: ["F5", "CommandOrControl+R"]
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -66,37 +66,37 @@
 | 
				
			|||||||
                    <a class="dropdown-item" id="open-dev-tools-button">
 | 
					                    <a class="dropdown-item" id="open-dev-tools-button">
 | 
				
			||||||
                        <span class="bx bx-terminal"></span>
 | 
					                        <span class="bx bx-terminal"></span>
 | 
				
			||||||
                        Open Dev Tools
 | 
					                        Open Dev Tools
 | 
				
			||||||
                        <kbd>CTRL+SHIFT+I</kbd>
 | 
					                        <kbd data-kb-action="OpenDevTools"></kbd>
 | 
				
			||||||
                    </a>
 | 
					                    </a>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    <a class="dropdown-item" id="open-sql-console-button">
 | 
					                    <a class="dropdown-item" id="open-sql-console-button">
 | 
				
			||||||
                        <span class="bx bx-data"></span>
 | 
					                        <span class="bx bx-data"></span>
 | 
				
			||||||
                        Open SQL Console
 | 
					                        Open SQL Console
 | 
				
			||||||
                        <kbd>ALT+O</kbd>
 | 
					                        <kbd data-kb-action="ShowSQLConsole"></kbd>
 | 
				
			||||||
                    </a>
 | 
					                    </a>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    <a class="dropdown-item" id="reload-frontend-button" title="Reload can help with some visual glitches without restarting the whole app.">
 | 
					                    <a class="dropdown-item" id="reload-frontend-button" title="Reload can help with some visual glitches without restarting the whole app.">
 | 
				
			||||||
                        <span class="bx bx-empty"></span>
 | 
					                        <span class="bx bx-empty"></span>
 | 
				
			||||||
                        Reload frontend
 | 
					                        Reload frontend
 | 
				
			||||||
                        <kbd>CTRL+R</kbd>
 | 
					                        <kbd data-kb-action="ReloadFrontendApp"></kbd>
 | 
				
			||||||
                    </a>
 | 
					                    </a>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    <a class="dropdown-item" id="toggle-zen-mode-button">
 | 
					                    <a class="dropdown-item" id="toggle-zen-mode-button">
 | 
				
			||||||
                        <span class="bx bx-empty"></span>
 | 
					                        <span class="bx bx-empty"></span>
 | 
				
			||||||
                        Toggle Zen mode
 | 
					                        Toggle Zen mode
 | 
				
			||||||
                        <kbd>ALT+M</kbd>
 | 
					                        <kbd data-kb-action="ToggleZenMode"></kbd>
 | 
				
			||||||
                    </a>
 | 
					                    </a>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    <a class="dropdown-item" id="toggle-fullscreen-button">
 | 
					                    <a class="dropdown-item" id="toggle-fullscreen-button">
 | 
				
			||||||
                        <span class="bx bx-empty"></span>
 | 
					                        <span class="bx bx-empty"></span>
 | 
				
			||||||
                        Toggle fullscreen
 | 
					                        Toggle fullscreen
 | 
				
			||||||
                        <kbd>F11</kbd>
 | 
					                        <kbd data-kb-action="ToggleFullscreen"></kbd>
 | 
				
			||||||
                    </a>
 | 
					                    </a>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    <a class="dropdown-item" id="show-help-button">
 | 
					                    <a class="dropdown-item" id="show-help-button">
 | 
				
			||||||
                        <span class="bx bx-info-circle"></span>
 | 
					                        <span class="bx bx-info-circle"></span>
 | 
				
			||||||
                        Show Help
 | 
					                        Show Help
 | 
				
			||||||
                        <kbd>F1</kbd>
 | 
					                        <kbd data-kb-action="ShowHelp"></kbd>
 | 
				
			||||||
                    </a>
 | 
					                    </a>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    <a class="dropdown-item" id="show-about-dialog-button">
 | 
					                    <a class="dropdown-item" id="show-about-dialog-button">
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user