Skip to content

Build your personal knowledge base with Trilium Notes

refactor(tabs): address PR review on tab placement

Elian Doran09639e01c254
diff --git a/apps/client/src/components/tab_manager.ts b/apps/client/src/components/tab_manager.ts
index aec9148515..11e5e42fd3 100644
--- a/apps/client/src/components/tab_manager.ts
+++ b/apps/client/src/components/tab_manager.ts
@@ -299,7 +299,7 @@ export default class TabManager extends Component {
}
/**
- * Index in the flat `children` array at which a newly created (unpinned) tab should be inserted.
+ * Index in `children` where a newly created (unpinned) tab is inserted.
* `"end"` appends; `"afterCurrent"` inserts right after the active tab and all of its splits,
* clamped so an unpinned tab never lands inside the pinned group (which `reorderPinnedFirst`
* keeps grouped at the front of the row).
@@ -309,7 +309,12 @@ export default class TabManager extends Component {
return this.children.length;
}
- const activeMainNtxId = this.getActiveMainContext()?.ntxId;
+ // Resolve the active main context without getActiveMainContext(): it throws on a stale
+ // activeNtxId, which would bypass the activeIndex === -1 fallback below.
+ const activeContext = this.activeNtxId
+ ? this.noteContexts.find((nc) => nc.ntxId === this.activeNtxId)
+ : undefined;
+ const activeMainNtxId = activeContext?.getMainContext().ntxId;
const activeIndex = activeMainNtxId
? this.children.findIndex((nc) => nc.ntxId === activeMainNtxId)
: -1;
@@ -324,7 +329,7 @@ export default class TabManager extends Component {
index++;
}
- // Pinned tabs stay grouped at the front, so never insert the new (unpinned) tab inside them.
+ // Pinned tabs stay grouped first, so never insert the new (unpinned) tab inside them.
const firstUnpinnedIndex = this.children.findIndex((nc) => !this.isPartOfPinnedTab(nc));
const minIndex = firstUnpinnedIndex === -1 ? this.children.length : firstUnpinnedIndex;
@@ -332,11 +337,7 @@ export default class TabManager extends Component {
}
private isPartOfPinnedTab(noteContext: NoteContext): boolean {
- const mainContext = noteContext.mainNtxId
- ? this.children.find((nc) => nc.ntxId === noteContext.mainNtxId)
- : noteContext;
-
- return !!mainContext?.pinned;
+ return !!noteContext.getMainContext().pinned;
}
async openInNewTab(targetNoteId: string, hoistedNoteId: string | null = null, activate: boolean = false) {
diff --git a/apps/client/src/widgets/tab_row.ts b/apps/client/src/widgets/tab_row.ts
index 8d2e954c44..b49c0feda1 100644
--- a/apps/client/src/widgets/tab_row.ts
+++ b/apps/client/src/widgets/tab_row.ts
@@ -614,9 +614,12 @@ export default class TabRowWidget extends BasicWidget {
setTimeout(() => $tab.removeClass("note-tab-was-just-added"), 500);
this.$containerAnchor.before($tab);
- // The model may insert this tab before the end (e.g. opened from a link, after the
- // active tab), so mirror the model order in the DOM rather than leaving it appended.
- this.syncTabOrder();
+ // When opened from a link the model may place this tab before the end; mirror that order in
+ // the DOM. When appended (the common case) it is already correct, so skip the O(n) walk.
+ const mainContexts = appContext.tabManager.getMainNoteContexts();
+ if (mainContexts[mainContexts.length - 1]?.ntxId !== ntxId) {
+ this.syncTabOrder();
+ }
this.setVisibility();
this.setTabCloseEvent($tab);
this.updateTitle($tab, t("tab_row.new_tab"));