mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	notes with #keyboardShortcut don't need reload to take effect
This commit is contained in:
		| @@ -12,6 +12,7 @@ import keyboardActionsService from "../services/keyboard_actions.js"; | |||||||
| import MobileScreenSwitcherExecutor from "./mobile_screen_switcher.js"; | import MobileScreenSwitcherExecutor from "./mobile_screen_switcher.js"; | ||||||
| import MainTreeExecutors from "./main_tree_executors.js"; | import MainTreeExecutors from "./main_tree_executors.js"; | ||||||
| import toast from "../services/toast.js"; | import toast from "../services/toast.js"; | ||||||
|  | import ShortcutComponent from "./shortcut_component.js"; | ||||||
|  |  | ||||||
| class AppContext extends Component { | class AppContext extends Component { | ||||||
|     constructor(isMainWindow) { |     constructor(isMainWindow) { | ||||||
| @@ -46,7 +47,8 @@ class AppContext extends Component { | |||||||
|             this.tabManager, |             this.tabManager, | ||||||
|             new RootCommandExecutor(), |             new RootCommandExecutor(), | ||||||
|             new Entrypoints(), |             new Entrypoints(), | ||||||
|             new MainTreeExecutors() |             new MainTreeExecutors(), | ||||||
|  |             new ShortcutComponent() | ||||||
|         ]; |         ]; | ||||||
|  |  | ||||||
|         if (utils.isMobile()) { |         if (utils.isMobile()) { | ||||||
|   | |||||||
| @@ -2,18 +2,39 @@ import appContext from "./app_context.js"; | |||||||
| import shortcutService from "../services/shortcuts.js"; | import shortcutService from "../services/shortcuts.js"; | ||||||
| import server from "../services/server.js"; | import server from "../services/server.js"; | ||||||
| import Component from "./component.js"; | import Component from "./component.js"; | ||||||
|  | import froca from "../services/froca.js"; | ||||||
|  |  | ||||||
| export default class ShortcutComponent extends Component { | export default class ShortcutComponent extends Component { | ||||||
|     constructor() { |     constructor() { | ||||||
|  |         super(); | ||||||
|  |  | ||||||
|         server.get('keyboard-shortcuts-for-notes').then(shortcutAttributes => { |         server.get('keyboard-shortcuts-for-notes').then(shortcutAttributes => { | ||||||
|             for (const attr in shortcutAttributes) { |             for (const attr of shortcutAttributes) { | ||||||
|                 bindNoteShortcutHandler(attr); |                 this.bindNoteShortcutHandler(attr); | ||||||
|             } |             } | ||||||
|  |         }); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     bindNoteShortcutHandler(attr) { |     bindNoteShortcutHandler(attr) { | ||||||
|         const handler = async () => appContext.tabManager.getActiveContext().setNote(attr.noteId); |         const handler = () => appContext.tabManager.getActiveContext().setNote(attr.noteId); | ||||||
|  |         const namespace = attr.attributeId; | ||||||
|  |  | ||||||
|         shortcutService.bindGlobalShortcut(attr.value, handler, attr.attributeId); |         if (attr.isDeleted) { | ||||||
|  |             shortcutService.removeGlobalShortcut(namespace); | ||||||
|  |         } else { | ||||||
|  |             shortcutService.bindGlobalShortcut(attr.value, handler, namespace); | ||||||
|  |         } | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     async entitiesReloadedEvent({loadResults}) { | ||||||
|  |         for (const attr of loadResults.getAttributes()) { | ||||||
|  |             if (attr.type === 'label' && attr.name === 'keyboardShortcut') { | ||||||
|  |                 const note = await froca.getNote(attr.noteId); | ||||||
|  |                 // launcher shortcuts are handled specifically | ||||||
|  |                 if (note && note.type !== 'launcher') { | ||||||
|  |                     this.bindNoteShortcutHandler(attr); | ||||||
|  |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -44,24 +44,6 @@ getActionsForScope("window").then(actions => { | |||||||
| 	} | 	} | ||||||
| }); | }); | ||||||
|  |  | ||||||
| function setElementActionHandler($el, actionName, handler) { |  | ||||||
| 	keyboardActionsLoaded.then(() => { |  | ||||||
| 		const action = keyboardActionRepo[actionName]; |  | ||||||
|  |  | ||||||
| 		if (!action) { |  | ||||||
| 			throw new Error(`Cannot find keyboard action '${actionName}'`); |  | ||||||
| 		} |  | ||||||
|  |  | ||||||
| 		// not setting action.handler since this is not global |  | ||||||
|  |  | ||||||
| 		for (const shortcut of action.effectiveShortcuts) { |  | ||||||
| 			if (shortcut) { |  | ||||||
| 				shortcutService.bindElShortcut($el, shortcut, handler); |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	}); |  | ||||||
| } |  | ||||||
|  |  | ||||||
| async function getAction(actionName, silent = false) { | async function getAction(actionName, silent = false) { | ||||||
| 	await keyboardActionsLoaded; | 	await keyboardActionsLoaded; | ||||||
|  |  | ||||||
| @@ -108,10 +90,8 @@ function updateDisplayedShortcuts($container) { | |||||||
| } | } | ||||||
|  |  | ||||||
| export default { | export default { | ||||||
| 	setElementActionHandler, |  | ||||||
| 	updateDisplayedShortcuts, | 	updateDisplayedShortcuts, | ||||||
| 	setupActionsForElement, | 	setupActionsForElement, | ||||||
| 	getActions, | 	getActions, | ||||||
| 	getActionsForScope, | 	getActionsForScope | ||||||
| 	getAction |  | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -1,5 +1,9 @@ | |||||||
| import utils from "./utils.js"; | import utils from "./utils.js"; | ||||||
|  |  | ||||||
|  | function removeGlobalShortcut(namespace) { | ||||||
|  |     bindGlobalShortcut('', null, namespace); | ||||||
|  | } | ||||||
|  |  | ||||||
| function bindGlobalShortcut(keyboardShortcut, handler, namespace = null) { | function bindGlobalShortcut(keyboardShortcut, handler, namespace = null) { | ||||||
|     bindElShortcut($(document), keyboardShortcut, handler, namespace); |     bindElShortcut($(document), keyboardShortcut, handler, namespace); | ||||||
| } | } | ||||||
| @@ -48,5 +52,6 @@ function normalizeShortcut(shortcut) { | |||||||
| export default { | export default { | ||||||
|     bindGlobalShortcut, |     bindGlobalShortcut, | ||||||
|     bindElShortcut, |     bindElShortcut, | ||||||
|  |     removeGlobalShortcut, | ||||||
|     normalizeShortcut |     normalizeShortcut | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user