From 95d1952896ace4e9845e9956dfee23dcf6fa2a37 Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 29 Feb 2020 16:26:46 +0100 Subject: [PATCH] wip --- .../javascripts/services/entrypoints.js | 4 +-- src/public/javascripts/services/link.js | 9 ++--- .../javascripts/services/tab_manager.js | 13 ++++--- .../javascripts/services/tree_context_menu.js | 3 +- src/public/javascripts/widgets/note_tree.js | 6 ++-- .../javascripts/widgets/tab_caching_widget.js | 36 ++++++++++++------- .../widgets/type_widgets/relation_map.js | 3 +- 7 files changed, 37 insertions(+), 37 deletions(-) diff --git a/src/public/javascripts/services/entrypoints.js b/src/public/javascripts/services/entrypoints.js index 64a356926..4b328dc26 100644 --- a/src/public/javascripts/services/entrypoints.js +++ b/src/public/javascripts/services/entrypoints.js @@ -69,9 +69,7 @@ export default class Entrypoints extends Component { await treeService.expandToNote(note.noteId); - const tabContext = appContext.tabManager.openEmptyTab(); - appContext.tabManager.activateTab(tabContext.tabId); - await tabContext.setNote(note.noteId); + const tabContext = await appContext.tabManager.openTabWithNote(note.noteId, true); appContext.triggerCommand('focusAndSelectTitle'); } diff --git a/src/public/javascripts/services/link.js b/src/public/javascripts/services/link.js index a9b9958d0..7879c171e 100644 --- a/src/public/javascripts/services/link.js +++ b/src/public/javascripts/services/link.js @@ -77,8 +77,7 @@ function goToLink(e) { if (notePath) { if ((e.which === 1 && e.ctrlKey) || e.which === 2) { - const tabContext = appContext.tabManager.openEmptyTab(); - tabContext.setNote(notePath); + appContext.tabManager.openTabWithNote(notePath); } else if (e.which === 1) { const activeTabContext = appContext.tabManager.getActiveTabContext(); @@ -118,8 +117,7 @@ function newTabContextMenu(e) { ], selectMenuItemHandler: ({command}) => { if (command === 'openNoteInNewTab') { - const tabContext = appContext.tabManager.openEmptyTab(); - tabContext.setNote(notePath); + appContext.tabManager.openTabWithNote(notePath); } } }); @@ -139,8 +137,7 @@ $(document).on('mousedown', '.note-detail-text a', function (e) { e.preventDefault(); if (notePath) { - const tabContext = appContext.tabManager.openEmptyTab(); - tabContext.setNote(notePath); + appContext.tabManager.openTabWithNote(notePath, false); } else { const address = $link.attr('href'); diff --git a/src/public/javascripts/services/tab_manager.js b/src/public/javascripts/services/tab_manager.js index d7494c1be..0dd606132 100644 --- a/src/public/javascripts/services/tab_manager.js +++ b/src/public/javascripts/services/tab_manager.js @@ -163,31 +163,31 @@ export default class TabManager extends Component { async switchToTab(tabId, notePath) { const tabContext = this.tabContexts.find(tc => tc.tabId === tabId) - || this.openEmptyTab(); + || await this.openEmptyTab(); this.activateTab(tabContext.tabId); await tabContext.setNote(notePath); } async openAndActivateEmptyTab() { - const tabContext = this.openEmptyTab(); + const tabContext = await this.openEmptyTab(); await this.activateTab(tabContext.tabId); await tabContext.setEmpty(); } - openEmptyTab(tabId) { + async openEmptyTab(tabId) { const tabContext = new TabContext(tabId); this.child(tabContext); - this.triggerEvent('newTabOpened', {tabId: tabContext.tabId}); + await this.triggerEvent('newTabOpened', {tabId: tabContext.tabId}); return tabContext; } async openTabWithNote(notePath, activate, tabId = null) { - const tabContext = this.openEmptyTab(tabId); + const tabContext = await this.openEmptyTab(tabId); await tabContext.setNote(notePath, !activate); // if activate is false then send normal noteSwitched event @@ -211,8 +211,7 @@ export default class TabManager extends Component { // if no tab with this note has been found we'll create new tab - const tabContext = this.openEmptyTab(); - await tabContext.setNote(noteId); + await this.openTabWithNote(noteId); } activateTab(tabId, triggerEvent = true) { diff --git a/src/public/javascripts/services/tree_context_menu.js b/src/public/javascripts/services/tree_context_menu.js index 3768f53af..6db23878f 100644 --- a/src/public/javascripts/services/tree_context_menu.js +++ b/src/public/javascripts/services/tree_context_menu.js @@ -108,8 +108,7 @@ class TreeContextMenu { const notePath = treeService.getNotePath(this.node); if (command === 'openInTab') { - const tabContext = appContext.tabManager.openEmptyTab(); - tabContext.setNote(notePath); + appContext.tabManager.openTabWithNote(notePath); } else if (command === "insertNoteAfter") { const parentNoteId = this.node.data.parentNoteId; diff --git a/src/public/javascripts/widgets/note_tree.js b/src/public/javascripts/widgets/note_tree.js index a5f15767a..3d044bcfe 100644 --- a/src/public/javascripts/widgets/note_tree.js +++ b/src/public/javascripts/widgets/note_tree.js @@ -47,8 +47,7 @@ export default class NoteTreeWidget extends TabAwareWidget { const notePath = treeService.getNotePath(node); if (notePath) { - const tabContext = appContext.tabManager.openEmptyTab(); - tabContext.setNote(notePath); + appContext.tabManager.openTabWithNote(notePath); } e.stopPropagation(); @@ -81,9 +80,8 @@ export default class NoteTreeWidget extends TabAwareWidget { node.setFocus(true); } else if (event.ctrlKey) { - const tabContext = appContext.tabManager.openEmptyTab(); const notePath = treeService.getNotePath(node); - tabContext.setNote(notePath); + appContext.tabManager.openTabWithNote(notePath); } else { node.setActive(); diff --git a/src/public/javascripts/widgets/tab_caching_widget.js b/src/public/javascripts/widgets/tab_caching_widget.js index 8f9b5950a..9731bf9f8 100644 --- a/src/public/javascripts/widgets/tab_caching_widget.js +++ b/src/public/javascripts/widgets/tab_caching_widget.js @@ -28,6 +28,23 @@ export default class TabCachingWidget extends TabAwareWidget { } } + async newTabOpenedEvent({tabId}) { + if (this.widgets[tabId]) { + return; + } + + this.widgets[tabId] = this.widgetFactory(); + + const $renderedWidget = this.widgets[tabId].render(); + this.$widget.after($renderedWidget); + + keyboardActionsService.updateDisplayedShortcuts($renderedWidget); + + await this.widgets[tabId].handleEvent('setTabContext', {tabContext: this.tabContext}); + + this.child(this.widgets[tabId]); // add as child only once it is ready (rendered with tabContext) + } + async refreshWithNote() { for (const widget of Object.values(this.widgets)) { widget.toggle(false); @@ -39,21 +56,14 @@ export default class TabCachingWidget extends TabAwareWidget { return; } - let widget = this.widgets[this.tabContext.tabId]; + const widget = this.widgets[this.tabContext.tabId]; - if (!widget) { - widget = this.widgets[this.tabContext.tabId] = this.widgetFactory(); - - const $renderedWidget = widget.render(); - this.$widget.after($renderedWidget); - - keyboardActionsService.updateDisplayedShortcuts($renderedWidget); - - this.child(widget); // add as child only once it is ready (also rendered) - await widget.handleEvent('setTabContext', {tabContext: this.tabContext}); + if (widget) { + widget.toggle(widget.isEnabled()); + } + else { + console.error(`Widget for tab ${this.tabContext.tabId} not found.`); } - - widget.toggle(widget.isEnabled()); } tabRemovedEvent({tabId}) { diff --git a/src/public/javascripts/widgets/type_widgets/relation_map.js b/src/public/javascripts/widgets/type_widgets/relation_map.js index b6724cbb4..65cdcbf3d 100644 --- a/src/public/javascripts/widgets/type_widgets/relation_map.js +++ b/src/public/javascripts/widgets/type_widgets/relation_map.js @@ -196,8 +196,7 @@ export default class RelationMapTypeWidget extends TypeWidget { const noteId = this.idToNoteId($noteBox.prop("id")); if (command === "openInNewTab") { - const tabContext = appContext.tabManager.openEmptyTab(); - tabContext.setNote(noteId); + appContext.tabManager.openTabWithNote(noteId); } else if (command === "remove") { const confirmDialog = await import('../../dialogs/confirm.js');