Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix dragging out tab creating multiple windows, closes #2944
zadam33c272e86a09
diff --git a/src/public/app/services/tab_manager.js b/src/public/app/services/tab_manager.jsindex 59318d6e3d..2156400c7b 100644--- a/src/public/app/services/tab_manager.js+++ b/src/public/app/services/tab_manager.js@@ -142,6 +142,11 @@ export default class TabManager extends Component {return this.noteContexts;}+ /** @returns {NoteContext[]} */+ getMainNoteContexts() {+ return this.noteContexts.filter(nc => nc.isMainContext());+ }+/** @returns {NoteContext} */getNoteContextById(ntxId) {const noteContext = this.noteContexts.find(nc => nc.ntxId === ntxId);@@ -294,11 +299,23 @@ export default class TabManager extends Component {this.setCurrentNotePathToHash();}+ /**+ * @param ntxId+ * @returns {Promise<boolean>} true if note context has been removed, false otherwise+ */async removeNoteContext(ntxId) {// removing note context is async process which can take some time, if users presses CTRL-W quickly, two// close events could interleave which would then lead to attempting to activate already removed context.- await this.mutex.runExclusively(async () => {- const noteContextToRemove = this.getNoteContextById(ntxId);+ return await this.mutex.runExclusively(async () => {+ let noteContextToRemove;++ try {+ noteContextToRemove = this.getNoteContextById(ntxId);+ }+ catch {+ // note context not found+ return false;+ }if (noteContextToRemove.isMainContext()) {// forbid removing last main note context@@ -308,7 +325,7 @@ export default class TabManager extends Component {if (mainNoteContexts.length === 1) {await this.clearLastMainNoteContext(noteContextToRemove);- return;+ return false;}}@@ -338,6 +355,8 @@ export default class TabManager extends Component {}this.removeNoteContexts(noteContextsToRemove);++ return true;});}@@ -445,12 +464,14 @@ export default class TabManager extends Component {}}- moveTabToNewWindowCommand({ntxId}) {+ async moveTabToNewWindowCommand({ntxId}) {const {notePath, hoistedNoteId} = this.getNoteContextById(ntxId);- this.removeNoteContext(ntxId);+ const removed = await this.removeNoteContext(ntxId);- this.triggerCommand('openInWindow', {notePath, hoistedNoteId});+ if (removed) {+ this.triggerCommand('openInWindow', {notePath, hoistedNoteId});+ }}async reopenLastTabCommand() {diff --git a/src/public/app/utils/mutex.js b/src/public/app/utils/mutex.jsindex a48b2660ee..483e0f345c 100644--- a/src/public/app/utils/mutex.js+++ b/src/public/app/utils/mutex.js@@ -19,7 +19,7 @@ export default class Mutex {const unlock = await this.lock();try {- await cb();+ return await cb();}finally {unlock();