From 494f8d2153171188401f6a75711d3149e49a1c9c Mon Sep 17 00:00:00 2001 From: zadam Date: Sat, 10 Dec 2022 14:35:58 +0100 Subject: [PATCH] fix reopening the very last tab, closes #3397 --- src/public/app/components/note_context.js | 6 ++- src/public/app/components/tab_manager.js | 56 +++++++++++++---------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/src/public/app/components/note_context.js b/src/public/app/components/note_context.js index 6b4bc62b6..0b6eab882 100644 --- a/src/public/app/components/note_context.js +++ b/src/public/app/components/note_context.js @@ -32,6 +32,10 @@ class NoteContext extends Component { }); } + isEmpty() { + return !this.noteId; + } + async setNote(inputNotePath, triggerSwitchEvent = true) { const resolvedNotePath = await this.getResolvedNotePath(inputNotePath); @@ -59,8 +63,6 @@ class NoteContext extends Component { }); } - console.log(resolvedNotePath, "resolvedNotePath"); - if (this.hoistedNoteId === 'root' && this.notePath.startsWith("root/hidden")) { // hidden subtree displays only when hoisted so it doesn't make sense to keep root as hoisted note diff --git a/src/public/app/components/tab_manager.js b/src/public/app/components/tab_manager.js index 30c1e0d4e..e370ec2db 100644 --- a/src/public/app/components/tab_manager.js +++ b/src/public/app/components/tab_manager.js @@ -324,14 +324,16 @@ export default class TabManager extends Component { } if (noteContextToRemove.isMainContext()) { - // forbid removing last main note context - // this was previously allowed (was replaced with empty tab) but this proved to be prone to race conditions const mainNoteContexts = this.getNoteContexts().filter(nc => nc.isMainContext()); if (mainNoteContexts.length === 1) { - await this.clearLastMainNoteContext(noteContextToRemove); + if (noteContextToRemove.isEmpty()) { + // this is already the empty note context, no point in closing it and replacing with another + // empty tab + return false; + } - return false; + await this.openEmptyTab(); } } @@ -366,35 +368,26 @@ export default class TabManager extends Component { }); } - async clearLastMainNoteContext(noteContextToClear) { - noteContextToClear.setEmpty(); - - // activate main split - await this.activateNoteContext(noteContextToClear.ntxId); - - // remove all other splits - const noteContextsToRemove = noteContextToClear.getSubContexts() - .filter(ntx => ntx.ntxId !== noteContextToClear.ntxId); - - const ntxIdsToRemove = noteContextsToRemove.map(ntx => ntx.ntxId); - - await this.triggerEvent('beforeNoteContextRemove', {ntxIds: ntxIdsToRemove}); - - this.removeNoteContexts(noteContextsToRemove); - } - removeNoteContexts(noteContextsToRemove) { const ntxIdsToRemove = noteContextsToRemove.map(nc => nc.ntxId); this.children = this.children.filter(nc => !ntxIdsToRemove.includes(nc.ntxId)); - this.recentlyClosedTabs.push(noteContextsToRemove); + this.addToRecentlyClosedTabs(noteContextsToRemove); this.triggerEvent('noteContextRemoved', {ntxIds: ntxIdsToRemove}); this.tabsUpdate.scheduleUpdate(); } + addToRecentlyClosedTabs(noteContexts) { + if (noteContexts.length === 1 && noteContexts[0].isEmpty()) { + return; + } + + this.recentlyClosedTabs.push(noteContexts);console.log(this.recentlyClosedTabs); + } + tabReorderEvent({ntxIdsInOrder}) { const order = {}; @@ -481,7 +474,18 @@ export default class TabManager extends Component { } async reopenLastTabCommand() { - if (this.recentlyClosedTabs.length > 0) { + let closeLastEmptyTab = null; + + await this.mutex.runExclusively(async () => { + if (this.recentlyClosedTabs.length === 0) { + return; + } + + if (this.noteContexts.length === 1 && this.noteContexts[0].isEmpty()) { + // new empty tab is created after closing the last tab, this reverses the empty tab creation + closeLastEmptyTab = this.noteContexts[0]; + } + const noteContexts = this.recentlyClosedTabs.pop(); for (const noteContext of noteContexts) { @@ -494,12 +498,16 @@ export default class TabManager extends Component { ? noteContexts[0] : noteContexts.find(nc => nc.isMainContext()); - this.activateNoteContext(noteContextToActivate.ntxId); + await this.activateNoteContext(noteContextToActivate.ntxId); await this.triggerEvent('noteSwitched', { noteContext: noteContextToActivate, notePath: noteContextToActivate.notePath }); + }); + + if (closeLastEmptyTab) { + await this.removeNoteContext(closeLastEmptyTab.ntxId); } }