Skip to content

Commit

Permalink
fix reopening the very last tab, closes #3397
Browse files Browse the repository at this point in the history
  • Loading branch information
zadam committed Dec 10, 2022
1 parent c44bc60 commit 494f8d2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 26 deletions.
6 changes: 4 additions & 2 deletions src/public/app/components/note_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class NoteContext extends Component {
});
}

isEmpty() {
return !this.noteId;
}

async setNote(inputNotePath, triggerSwitchEvent = true) {
const resolvedNotePath = await this.getResolvedNotePath(inputNotePath);

Expand Down Expand Up @@ -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

Expand Down
56 changes: 32 additions & 24 deletions src/public/app/components/tab_manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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 = {};

Expand Down Expand Up @@ -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) {
Expand All @@ -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);
}
}

Expand Down

0 comments on commit 494f8d2

Please sign in to comment.