Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:bug: Preserve cross-window tab order after tab eviction https://github.com/siyuan-note/siyuan/issues/14736

Daniel469810a053a4
diff --git a/app/src/layout/Wnd.ts b/app/src/layout/Wnd.ts
index 668f2a94c0..ab08ad8c0d 100644
--- a/app/src/layout/Wnd.ts
+++ b/app/src/layout/Wnd.ts
@@ -54,7 +54,7 @@ import {recordBeforeResizeTop} from "../protyle/util/resize";
import {sanitizeClosedTabs, setStorageVal} from "../protyle/util/compatibility";
import {setTitle} from "../util/processTitle";
import {dragOverScroll} from "../boot/globalEvent/dragover";
-import {clearTabDragPreview, reorderTabItems} from "./tabDrag";
+import {clearTabDragPreview, findNextTabId, reorderTabItems} from "./tabDrag";
const createDragTabPlaceholder = () => {
const dragTab = window.siyuan.dragTab;
@@ -303,8 +303,15 @@ export class Wnd {
clearTabDragPreview(it);
return;
}
- const nextTabHeaderElement = dragTabHeaderElement.nextElementSibling as HTMLElement;
- const nextTabId = nextTabHeaderElement?.getAttribute("data-id") || undefined;
+ const nextTabIds: string[] = [];
+ let nextTabHeaderElement = dragTabHeaderElement.nextElementSibling as HTMLElement;
+ while (nextTabHeaderElement) {
+ const nextTabId = nextTabHeaderElement.getAttribute("data-id");
+ if (nextTabId) {
+ nextTabIds.push(nextTabId);
+ }
+ nextTabHeaderElement = nextTabHeaderElement.nextElementSibling as HTMLElement;
+ }
const tabData = JSON.parse(event.dataTransfer.getData(Constants.SIYUAN_DROP_TAB));
let oldTab = getInstanceById(tabData.id) as Tab;
const wnd = getInstanceById(it.parentElement.getAttribute("data-id")) as Wnd;
@@ -325,6 +332,7 @@ export class Wnd {
clearTabDragPreview(it);
return;
}
+ const nextTabId = findNextTabId(wnd.children, nextTabIds);
const oldWnd = oldTab.parent;
if (cloneTabElement) {
cloneTabElement.before(oldTab.headElement);
diff --git a/app/src/layout/tabDrag.test.ts b/app/src/layout/tabDrag.test.ts
index f5e18c6143..41f5c370ef 100644
--- a/app/src/layout/tabDrag.test.ts
+++ b/app/src/layout/tabDrag.test.ts
@@ -1,6 +1,6 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
-import {reorderTabItems} from "./tabDrag";
+import {findNextTabId, reorderTabItems} from "./tabDrag";
const createItems = () => [
{id: "a"},
@@ -10,6 +10,19 @@ const createItems = () => [
];
describe("tab drag ordering", () => {
+ it("uses the first surviving successor after tabs are removed", () => {
+ const items = createItems();
+ items.splice(1, 1);
+
+ assert.equal(findNextTabId(items, ["b", "c", "d"]), "c");
+ });
+
+ it("moves to the end when no successor survives", () => {
+ const items = createItems().slice(0, 1);
+
+ assert.equal(findNextTabId(items, ["b", "c", "d"]), undefined);
+ });
+
it("moves a tab before the requested successor", () => {
const items = createItems();
diff --git a/app/src/layout/tabDrag.ts b/app/src/layout/tabDrag.ts
index 68ff28dff4..3a62da9459 100644
--- a/app/src/layout/tabDrag.ts
+++ b/app/src/layout/tabDrag.ts
@@ -31,3 +31,8 @@ export const reorderTabItems = <T extends { id: string }>(items: T[], item: T, n
}
return true;
};
+
+export const findNextTabId = <T extends { id: string }>(items: T[], candidateIds: string[]) => {
+ const existingIds = new Set(items.map((item) => item.id));
+ return candidateIds.find((id) => existingIds.has(id));
+};