From 9bc1f5af45906d850ccbba41525d0bd0344f95f1 Mon Sep 17 00:00:00 2001 From: zadam Date: Sun, 19 Jan 2020 22:05:45 +0100 Subject: [PATCH] WIP --- .../javascripts/services/app_context.js | 28 ---------- .../javascripts/services/keyboard_actions.js | 53 +++++++++++++------ .../javascripts/services/note_detail.js | 4 +- src/public/javascripts/widgets/tab_row.js | 2 +- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/src/public/javascripts/services/app_context.js b/src/public/javascripts/services/app_context.js index 3a532b240..d982b0d23 100644 --- a/src/public/javascripts/services/app_context.js +++ b/src/public/javascripts/services/app_context.js @@ -382,32 +382,4 @@ class AppContext { const appContext = new AppContext(); -keyboardActionService.setGlobalActionHandler('OpenNewTab', () => { - appContext.openEmptyTab(); -}); - -keyboardActionService.setGlobalActionHandler('CloseActiveTab', () => { - if (this.tabRow.activeTabEl) { - this.tabRow.removeTab(this.tabRow.activeTabId); - } -}); - -keyboardActionService.setGlobalActionHandler('ActivateNextTab', () => { - const nextTab = this.tabRow.nextTabEl; - - if (nextTab) { - // FIXME - this.tabRow.activateTab(nextTab); - } -}); - -keyboardActionService.setGlobalActionHandler('ActivatePreviousTab', () => { - const prevTab = this.tabRow.previousTabEl; - - if (prevTab) { - // FIXME - this.tabRow.activateTab(prevTab); - } -}); - export default appContext; \ No newline at end of file diff --git a/src/public/javascripts/services/keyboard_actions.js b/src/public/javascripts/services/keyboard_actions.js index 5aeee022c..6efe54746 100644 --- a/src/public/javascripts/services/keyboard_actions.js +++ b/src/public/javascripts/services/keyboard_actions.js @@ -1,11 +1,20 @@ import server from "./server.js"; import utils from "./utils.js"; +import appContext from "./app_context.js"; const keyboardActionRepo = {}; const keyboardActionsLoaded = server.get('keyboard-actions').then(actions => { for (const action of actions) { keyboardActionRepo[action.actionName] = action; + + for (const shortcut of action.effectiveShortcuts || []) { + if (shortcut && !shortcut.startsWith("global:")) { // global shortcuts should be handled in the electron code + const eventName = action.actionName.charAt(0).toUpperCase() + action.actionName.slice(1); + + utils.bindGlobalShortcut(shortcut, appContext.trigger(eventName)); + } + } } }); @@ -20,21 +29,7 @@ server.get('keyboard-shortcuts-for-notes').then(shortcutForNotes => { }); function setGlobalActionHandler(actionName, handler) { - keyboardActionsLoaded.then(() => { - const action = keyboardActionRepo[actionName]; - - if (!action) { - throw new Error(`Cannot find keyboard action '${actionName}'`); - } - - action.handler = handler; - - for (const shortcut of action.effectiveShortcuts) { - if (shortcut && !shortcut.startsWith("global:")) { // global shortcuts should be handled in the electron code - utils.bindGlobalShortcut(shortcut, handler); - } - } - }); + console.log("Useless handler for " + actionName); } function setElementActionHandler($el, actionName, handler) { @@ -108,6 +103,34 @@ function updateDisplayedShortcuts($container) { $(() => updateDisplayedShortcuts($(document))); +setGlobalActionHandler('OpenNewTab', () => { + appContext.openEmptyTab(); +}); + +setGlobalActionHandler('CloseActiveTab', () => { + if (this.tabRow.activeTabEl) { + this.tabRow.removeTab(this.tabRow.activeTabId); + } +}); + +setGlobalActionHandler('ActivateNextTab', () => { + const nextTab = this.tabRow.nextTabEl; + + if (nextTab) { + // FIXME + this.tabRow.activateTab(nextTab); + } +}); + +setGlobalActionHandler('ActivatePreviousTab', () => { + const prevTab = this.tabRow.previousTabEl; + + if (prevTab) { + // FIXME + this.tabRow.activateTab(prevTab); + } +}); + export default { setGlobalActionHandler, setElementActionHandler, diff --git a/src/public/javascripts/services/note_detail.js b/src/public/javascripts/services/note_detail.js index 9fa0d49cc..34da72985 100644 --- a/src/public/javascripts/services/note_detail.js +++ b/src/public/javascripts/services/note_detail.js @@ -200,7 +200,9 @@ function noteChanged() { // this makes sure that when user e.g. reloads the page or navigates away from the page, the note's content is saved // this sends the request asynchronously and doesn't wait for result // FIXME -$(window).on('beforeunload', () => { saveNotesIfChanged(); }); // don't convert to short form, handler doesn't like returned promise +$(window).on('beforeunload', () => { + //saveNotesIfChanged(); + }); export default { reload, diff --git a/src/public/javascripts/widgets/tab_row.js b/src/public/javascripts/widgets/tab_row.js index 22de57098..757c84034 100644 --- a/src/public/javascripts/widgets/tab_row.js +++ b/src/public/javascripts/widgets/tab_row.js @@ -636,7 +636,7 @@ export default class TabRowWidget extends BasicWidget { const {note} = this.appContext.getTabContextById(tabId); - if (!note) { + if (!note || !$tab.length) { return; }