From 2694195f861ea524a9893389bad4d60a3e035ead Mon Sep 17 00:00:00 2001 From: kelvinwww <1970138194@qq.com> Date: Sun, 11 Oct 2026 23:05:50 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=90=8C=E7=AA=97?= =?UTF-8?q?=E6=A0=BC=E5=94=AF=E4=B8=80=E6=A0=87=E7=AD=BE=E6=8B=96=E5=9B=9E?= =?UTF-8?q?=E5=90=8E=E4=B8=A2=E5=A4=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/client/state.ts | 20 +++++++++++++++++--- tests/state.spec.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 3 deletions(-) diff --git a/src/client/state.ts b/src/client/state.ts index 4c6ec327..96530173 100644 --- a/src/client/state.ts +++ b/src/client/state.ts @@ -439,10 +439,24 @@ export function openTabInBottomPane(state: SidebarState, tab: SidebarTab): Sideb } } -/** Move a tab from one pane to another (insert at index; -1 appends). - * The panes may live in DIFFERENT trees — dragging a tab between the two - * panels removes it from its own tree and lands it in the other one. */ +/** + * 在底部工作台窗格之间移动标签,或在同一窗格内重排。 + * index 对应移动前的目标标签位置,-1 表示追加。 + */ export function moveTab(state: SidebarState, fromPane: string, tabId: string, toPane: string, index = -1): SidebarState { + if (fromPane === toPane) { + const source = leafWithTab(state.bottomSplits, tabId) + if (source?.id !== fromPane) return state + const sourceIndex = source.tabs.findIndex(tab => tab.id === tabId) + const tabs = source.tabs.filter(tab => tab.id !== tabId) + const insertAt = index < 0 || index > source.tabs.length ? tabs.length : sourceIndex < index ? index - 1 : index + tabs.splice(insertAt, 0, source.tabs[sourceIndex]!) + const splits = mapLeaf(state.bottomSplits, fromPane, leaf => { + leaf.tabs = tabs + leaf.active = tabId + }) + return { ...state, bottomSplits: splits, activePane: fromPane } + } const fromKey = 'bottomSplits' const toKey = 'bottomSplits' if (fromKey !== toKey) { diff --git a/tests/state.spec.ts b/tests/state.spec.ts index 55935993..7d1b3b92 100644 --- a/tests/state.spec.ts +++ b/tests/state.spec.ts @@ -247,6 +247,38 @@ describe('sidebar state', () => { expect(after.tabs).toHaveLength(2) }) + /** 同一分栏窗格内的唯一标签经过各放回入口后仍保留原窗格。 */ + it.each(['center', 'strip', 'chip'] as const)('keeps the only tab when dropped on its own %s', (destination) => { + let s = state() + s = openTabInBottomPane(s, { id: 'keep', type: 'editor', title: 'keep' }) + s = openTabInBottomPane(s, { id: 'draft', type: 'editor', title: 'draft' }) + const rootPane = (s.bottomSplits as { id: string }).id + s = moveTabToEdge(s, rootPane, 'draft', rootPane, 'right') + const pane = allLeaves(s.bottomSplits).find(leaf => leaf.tabs.some(tab => tab.id === 'draft'))! + + const after = destination === 'center' + ? moveTabToEdge(s, pane.id, 'draft', pane.id, 'center') + : moveTab(s, pane.id, 'draft', pane.id, destination === 'chip' ? 0 : -1) + + expect(after.bottomSplits).toEqual(s.bottomSplits) + expect(after.activePane).toBe(pane.id) + expect(allLeaves(after.bottomSplits).find(leaf => leaf.id === pane.id)?.active).toBe('draft') + }) + + /** 标签按钮提供原顺序中的目标位置,同窗格重排需保留目标标签之前的语义。 */ + it('reorders a tab before another tab in the same pane', () => { + let s = state() + for (const id of ['first', 'middle', 'last']) { + s = openTabInBottomPane(s, { id, type: 'editor', title: id }) + } + const paneId = (s.bottomSplits as { id: string }).id + + const after = moveTab(s, paneId, 'first', paneId, 2) + + expect(allLeaves(after.bottomSplits)[0]!.tabs.map(tab => tab.id)).toEqual(['middle', 'first', 'last']) + expect(after.activePane).toBe(paneId) + }) + it('dragging a pane\'s only tab onto its own edge splits in place instead of losing the tab', () => { let s = state() s = openTabInBottomPane(s, { id: 'git', type: 'git', title: 'Git' })