背景:动态插件面板此前一律回填到右栏,左侧留白只能靠用户手动拖过去。 实现: 1. app.js:registerPanel 接受 panel.side('left' 缺省 right),side 内联进面板条目 (app.js 行数棘轮不变,仍 8325)。 2. src/ui-dock-panels.js:build() 把 side 透传到面板清单(缺省 right)。 3. src/ui-dock-sanitize.js:回填按 side 分栏;「用户移除过」改判任一侧 removed (避免插件换侧后已移除的面板又冒出来)。 验证: - tests/ui-dock-sanitize-test.js 新增 side 回填 + 左栏 removed + 清单透传断言 - tests/_plugin-host-probe.spec.js:注册带 side:'left' 的面板 → 按钮出现在左栏、点击挂载出 LEFT-MOUNTED、右栏无该按钮、停用后左右两边都消失 - npm run verify:ci 全绿(含 e2e 冒烟 31 条 / 45s)
73 lines
3.9 KiB
JavaScript
73 lines
3.9 KiB
JavaScript
/* ui-dock-sanitize.js —— 停靠栏状态的纯函数整理(v3.26.21)
|
|
*
|
|
* 从 ui-dock.js 抽出来,便于单测(jsdom 里跑 React + 拖拽太重)。规则:
|
|
* 1. 只保留当前存在(plugins 有注册)的面板;
|
|
* 2. 同侧去重(历史重复注册残留);
|
|
* 3. 默认面板回填:removed 里的不补、另一侧已有的不补;
|
|
* 4. 同一面板同时出现在左右两侧 → 只保留左侧。
|
|
*/
|
|
;(function (global) {
|
|
'use strict'
|
|
function sanitize (dock, panels, defaults) {
|
|
if (!dock || !panels) return dock
|
|
var changed = false
|
|
var next = { left: Object.assign({}, dock.left), right: Object.assign({}, dock.right) }
|
|
;['left', 'right'].forEach(function (side) {
|
|
var s0 = dock[side] || {}
|
|
var removed = Array.isArray(s0.removed) ? s0.removed : []
|
|
var defList = (defaults && defaults[side] && defaults[side].list) || []
|
|
// v3.26.22:不再丢弃“当前没注册”的面板 id(启动瞬间插件未加载完,清掉会覆盖用户摆放)
|
|
var list = (s0.list || []).slice()
|
|
list = list.filter(function (id, i) { return list.indexOf(id) === i })
|
|
defList.forEach(function (id) {
|
|
if (!panels[id]) return
|
|
if (removed.indexOf(id) >= 0) return
|
|
var other = side === 'left' ? 'right' : 'left'
|
|
var otherList = (dock[other] && dock[other].list) || []
|
|
if (otherList.indexOf(id) >= 0) return
|
|
if (list.indexOf(id) >= 0) return
|
|
list.push(id)
|
|
})
|
|
var usable = list.filter(function (id) { return !!panels[id] })
|
|
// v3.26.23:保存的 active 只要还在 list 里就保持 —— 插件还没加载完时不要切走(否则重启后当前面板会变)
|
|
var keepActive = !!s0.active && list.indexOf(s0.active) >= 0
|
|
var active = keepActive ? s0.active : (usable[0] || list[0] || '')
|
|
// 用内容比较而不是长度:去重(减 1) + 默认回填(加 1) 时长度可能不变,但内容已不同
|
|
if (list.join('|') !== (s0.list || []).join('|') || active !== s0.active) {
|
|
next[side] = Object.assign({}, s0, { list: list, active: active })
|
|
changed = true
|
|
}
|
|
})
|
|
// v3.26.108:动态插件面板(plugin:*)不在 defaults 里 ⇒ 按注册时的 side 偏好回填(默认右栏);
|
|
// 用户移除过的(任一侧 removed 记录过)不补 —— 尊重「我不要这个面板」的意图,避免换侧后又冒出来。
|
|
var placed = (next.left.list || []).concat(next.right.list || [])
|
|
var removedAny = (Array.isArray(next.left.removed) ? next.left.removed : [])
|
|
.concat(Array.isArray(next.right.removed) ? next.right.removed : [])
|
|
var dynLeft = []
|
|
var dynRight = []
|
|
Object.keys(panels).forEach(function (id) {
|
|
if (id.indexOf('plugin:') !== 0) return
|
|
if (placed.indexOf(id) >= 0 || removedAny.indexOf(id) >= 0) return
|
|
var prefer = (panels[id] && panels[id].side === 'left') ? 'left' : 'right'
|
|
if (prefer === 'left') dynLeft.push(id); else dynRight.push(id)
|
|
})
|
|
;[['left', dynLeft], ['right', dynRight]].forEach(function (pair) {
|
|
var side = pair[0]
|
|
var ids = pair[1]
|
|
if (!ids.length) return
|
|
var cur = next[side]
|
|
next[side] = Object.assign({}, cur, { list: (cur.list || []).concat(ids) })
|
|
if (!next[side].active) next[side].active = next[side].list[0] || ''
|
|
changed = true
|
|
})
|
|
var dup = (next.left.list || []).filter(function (id) { return (next.right.list || []).indexOf(id) >= 0 })
|
|
if (dup.length) {
|
|
next.right = Object.assign({}, next.right, { list: next.right.list.filter(function (id) { return dup.indexOf(id) < 0 }) })
|
|
if (dup.indexOf(next.right.active) >= 0) next.right.active = next.right.list[0] || ''
|
|
changed = true
|
|
}
|
|
return changed ? next : dock
|
|
}
|
|
global.IWBUiDockSanitize = { sanitize: sanitize }
|
|
})(typeof window !== 'undefined' ? window : globalThis)
|