fix(state): dragging a pane's only tab onto its own edge no longer loses the tab

moveTabToEdge 对边缘 zone 先把 tab 从源窗格摘除:若源窗格因此清空,
removeLeafAt 会把该叶子从树上删除;随后的 insertLeafAt 再以同一 id
为目标时已找不到叶子,携带被拖 tab 的新叶子成为孤儿被整体丢弃——
tab 从会话中消失(未保存的编辑器草稿一并丢失),activePane 悬挂,
且布局在 200ms 防抖后把无 tab 状态持久化到 localStorage。

改为边缘 zone 先切分目标窗格、再摘 tab:insertLeafAt 完成后目标
叶子仍在树上(自身拖拽时以 target 副本保留原 id),摘除/清空删除
逻辑不变;center 路径行为保持原样。

新增回归测试:单 tab 自拖边缘(旧代码丢 tab,新代码原地切分)、
多 tab 自拖边缘(被拖 tab 进入新叶子,其余留在原叶子)。
This commit is contained in:
Melvin committed 2026-09-25 02:56:46 +08:00
1 parent d641d2e211
commit b772e6c134
2 files changed
+55 -9

No files matched your search

+21 -9
View File
@@ -270,23 +270,35 @@ export function moveTabToEdge(
const source = leafWithTab(node, tabId)
if (source === undefined) return state
const tab = source.tabs.find(candidate => candidate.id === tabId)!
let emptied = false
let splits = mapLeaf(node, source.id, (leaf) => {
leaf.tabs = leaf.tabs.filter(candidate => candidate.id !== tabId)
if (leaf.active === tabId) leaf.active = leaf.tabs[leaf.tabs.length - 1]?.id ?? null
if (leaf.tabs.length === 0) emptied = true
})
if (emptied) splits = removeLeafAt(splits, source.id)
if (zone === 'center') {
let emptied = false
let splits = mapLeaf(node, source.id, (leaf) => {
leaf.tabs = leaf.tabs.filter(candidate => candidate.id !== tabId)
if (leaf.active === tabId) leaf.active = leaf.tabs[leaf.tabs.length - 1]?.id ?? null
if (leaf.tabs.length === 0) emptied = true
})
if (emptied) splits = removeLeafAt(splits, source.id)
splits = mapLeaf(splits, toPane, (leaf) => {
leaf.tabs = [...leaf.tabs, tab]
leaf.active = tab.id
})
return { ...state, bottomSplits: splits, activePane: toPane }
}
// Edge zones split the target pane first, then lift the tab out of its
// source pane. Doing it the other way around breaks a self-drop: removing
// the tab empties the source leaf and removeLeafAt deletes it from the
// tree, so insertLeafAt targets an id that no longer exists and the
// dragged tab is silently discarded.
const dir = zone === 'left' || zone === 'right' ? 'row' : 'col'
const result = insertLeafAt(splits, toPane, dir, tab, zone === 'left' || zone === 'up')
return { ...state, bottomSplits: result.node, activePane: result.leafId }
const inserted = insertLeafAt(node, toPane, dir, tab, zone === 'left' || zone === 'up')
let emptied = false
let splits = mapLeaf(inserted.node, source.id, (leaf) => {
leaf.tabs = leaf.tabs.filter(candidate => candidate.id !== tabId)
if (leaf.active === tabId) leaf.active = leaf.tabs[leaf.tabs.length - 1]?.id ?? null
if (leaf.tabs.length === 0) emptied = true
})
if (emptied) splits = removeLeafAt(splits, source.id)
return { ...state, bottomSplits: splits, activePane: inserted.leafId }
}
/**
+34
View File
@@ -247,6 +247,40 @@ describe('sidebar state', () => {
expect(after.tabs).toHaveLength(2)
})
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' })
s = splitPane(s, 'col')
const split = s.bottomSplits as Extract<SplitNode, { kind: 'split' }>
const paneA = split.children[0] as { id: string; tabs: { id: string }[] }
const paneB = split.children[1] as { id: string }
const tabId = paneA.tabs[0]!.id
s = moveTab(s, paneA.id, tabId, paneB.id)
// paneB now holds the single tab; dropping it onto paneB's own edge
// used to empty paneB, delete its leaf, and discard the tab entirely.
s = moveTabToEdge(s, paneB.id, tabId, paneB.id, 'left')
const leaves = allLeaves(s.bottomSplits)
expect(leaves).toHaveLength(1)
expect(leaves[0]!.tabs.map(t => t.id)).toEqual([tabId])
expect(s.activePane).toBe(leaves[0]!.id)
})
it('dragging one of several tabs onto its own pane edge splits the pane with the dragged tab', () => {
let s = state()
s = openTabInBottomPane(s, { id: 'git', type: 'git', title: 'Git' })
s = openTabInBottomPane(s, { id: 't2', type: 'terminal', title: 'T2' })
const leaf = s.bottomSplits as { id: string; tabs: { id: string }[] }
const first = leaf.tabs[0]!.id
s = moveTabToEdge(s, leaf.id, first, leaf.id, 'right')
const leaves = allLeaves(s.bottomSplits)
expect(leaves).toHaveLength(2)
const dragged = leaves.find(candidate => candidate.tabs.some(t => t.id === first))
const kept = leaves.find(candidate => candidate !== dragged)
expect(dragged!.tabs.map(t => t.id)).toEqual([first])
expect(kept!.tabs.map(t => t.id)).toEqual([leaf.tabs[1]!.id])
expect(s.activePane).toBe(dragged!.id)
})
it('closing the last tab removes the pane (promotes the sibling)', () => {
let s = state()
s = openTabInBottomPane(s, { id: 'git', type: 'git', title: 'Git' })