feat(layout): 左右停靠栏(P1 布局 + P2 图标拖拽)v3.25.35

- 新增 src/ui-dock.js(IWBDockArea):左=节点组/浏览器/风格库,右=提示词库/图库;
  默认左浏览器、右图库;独立开合、宽度自由拖动(最小 100px、无上限)、状态持久化。
- 图标横排:点击切换 / 拖动排序 / 拖到另一栏按位置插入;每栏至少保留一个面板。
- 画布不动;工具栏/小地图/提示按 --iwb-dock-l/--iwb-dock-r 让位;停靠栏从顶栏下方开始。
- 顶部资源中心按钮与 Esc 继续控制右栏;右栏默认图库。
- 更新 boundary 快照、架构文档计数与 3 个外壳结构测试。
This commit is contained in:
qiqi committed 2026-11-19 22:32:56 +08:00
1 parent 640591fda3
commit 6e7c22123d
15 files changed
+535 -100

No files matched your search

+358
View File
@@ -0,0 +1,358 @@
/* 左右停靠栏(v3.25.35)
*
* 结构(对齐 Obsidian):
* [左栏] 头部:开关图标 + 各面板图标(横排,可点击切换 / 拖动排序 / 拖到另一栏)
* 主体:当前面板
* 右缘:拖宽手柄
* [右栏] 头部:各面板图标(横排)+ 开关图标(最右)
* 主体:当前面板
* 左缘:拖宽手柄
* 折叠后:窗口边缘保留 14px 窄条(上面的开关图标 = 展开入口)
*
* 约束(用户裁定):
* - 画布不动;只有工具栏/小地图按 --iwb-dock-l / --iwb-dock-r 让位(CSS 负责)
* - 宽度自由拖动、无上限;最小 100px
* - 每栏至少保留一个面板(拖走最后一个 → 阻止并提示)
* - 状态(开合/宽度/面板顺序/当前面板)持久化到 localStorage
*/
;(function (global) {
'use strict'
const React = global.React
const el = React.createElement
const useState = React.useState
const useEffect = React.useEffect
const useRef = React.useRef
const MIN_W = 100
const STRIP_W = 14
const DEFAULT_KEY = 'iwb.dock.v1'
// 存储统一走 IWBStorageLocal 门面(src 内不得直调 localStorage)
const _store = () => (global.IWBStorageLocal && typeof global.IWBStorageLocal.getStore === 'function') ? global.IWBStorageLocal.getStore() : null
function normalizeSide (side, def) {
const s = side && typeof side === 'object' ? side : {}
const list = Array.isArray(s.list) && s.list.length ? s.list.map(String) : def.list.slice()
const active = list.indexOf(String(s.active || '')) >= 0 ? String(s.active) : (list.indexOf(def.active) >= 0 ? def.active : list[0])
return {
open: s.open === undefined ? def.open !== false : !!s.open,
w: Math.max(MIN_W, Number(s.w) || def.w || 300),
list,
active,
}
}
function loadState (key, defaults) {
const def = {
left: normalizeSide(defaults && defaults.left, { open: true, w: 318, list: [], active: '' }),
right: normalizeSide(defaults && defaults.right, { open: true, w: 330, list: [], active: '' }),
}
try {
const st = _store()
const raw = st ? st.rawGet(key, null) : null
if (!raw) return def
const v = JSON.parse(raw)
return { left: normalizeSide(v && v.left, def.left), right: normalizeSide(v && v.right, def.right) }
} catch (e) {
return def
}
}
function saveState (key, s) {
try { const st = _store(); if (st) st.rawSet(key, JSON.stringify(s)) } catch (e) { /* ignore */ }
}
// 插件面板宿主:只在激活时挂载(切走即卸载)。挂载失败不影响其它面板。
function PluginBox (props) {
const ref = useRef(null)
const mount = props.mount
useEffect(() => {
const node = ref.current
if (!node || typeof mount !== 'function') return undefined
try { mount(node) } catch (e) { /* 插件挂载异常不得冒泡 */ }
return undefined
}, [mount])
return el('div', { className: 'iwb-dock-plugin', ref })
}
function IWBDockArea (props) {
const panels = props.panels || {}
const renderIcon = props.renderIcon || function () { return null }
const key = props.storageKey || DEFAULT_KEY
const defaults = props.defaults || { left: { open: true, w: 318, list: [], active: '' }, right: { open: true, w: 330, list: [], active: '' } }
const [dock, setDock] = useState(function () { return loadState(key, defaults) })
const [dropInfo, setDropInfo] = useState(null) // { side, index }
const [ghost, setGhost] = useState(null) // { id, x, y }
const dragRef = useRef(null)
const dropRef = useRef(null)
const rootRef = useRef(null)
// 顶部栏/Esc 控制右栏开合;外部 tab 变化时同步右栏激活面板
useEffect(function () {
if (props.rightOpen === undefined) return
setDock(function (d) { return d.right.open === !!props.rightOpen ? d : { left: d.left, right: Object.assign({}, d.right, { open: !!props.rightOpen }) } })
}, [props.rightOpen])
useEffect(function () {
if (!props.rightActive) return
setDock(function (d) { return d.right.active === props.rightActive ? d : { left: d.left, right: Object.assign({}, d.right, { active: props.rightActive }) } })
}, [props.rightActive])
// 面板注册可能是异步的(插件稍后注册):订阅插件宿主,注册后重渲染
const PluginHost = props.PluginHost
const [rev, setRev] = useState(0)
useEffect(function () {
if (!PluginHost || typeof PluginHost.on !== 'function') return undefined
return PluginHost.on(function () { setRev(function (v) { return v + 1 }) })
}, [PluginHost])
// 面板可用性兜底:插件是异步注册的,先给 1.2s 窗口;仍不可用才切到本栏第一个可用面板
useEffect(function () {
const ok = function (id) {
const p = panels[id]
if (!p) return false
if (p.node) return true
return !!(p.pluginId && props.pluginMount && props.pluginMount(p.pluginId))
}
const t = setTimeout(function () {
setDock(function (d) {
let changed = false
const next = { left: Object.assign({}, d.left), right: Object.assign({}, d.right) }
;['left', 'right'].forEach(function (side) {
const s = next[side]
if (!ok(s.active)) {
const first = s.list.find(ok)
if (first) { s.active = first; changed = true }
}
})
return changed ? next : d
})
}, 1200)
return function () { clearTimeout(t) }
}, [rev, panels])
useEffect(function () {
// 拖宽时每帧都会变:节流写盘,避免 localStorage 同步写造成卡顿
const t = setTimeout(function () { saveState(key, dock) }, 180)
return function () { clearTimeout(t) }
}, [dock])
// 顶栏高度是运行时值:停靠栏从顶栏下方开始(避免盖住 名称/缩放/节点数)
useEffect(function () {
const apply = function () {
const root = global.document && global.document.documentElement
const area = rootRef.current
if (!root || !area) return
const bar = global.document.querySelector('.iwb-canvas-topbar')
const host = area.parentElement
if (!bar || !host) return
const top = Math.max(0, Math.round(bar.getBoundingClientRect().bottom - host.getBoundingClientRect().top))
root.style.setProperty('--iwb-dock-top', top + 'px')
}
apply()
if (global.requestAnimationFrame) global.requestAnimationFrame(apply)
global.addEventListener('resize', apply)
const t = setTimeout(apply, 600)
return function () { global.removeEventListener('resize', apply); clearTimeout(t) }
}, [])
// 把两侧实际占位宽度写给 CSS(画布不动,只让其它浮层让位)
useEffect(function () {
const r = global.document && global.document.documentElement
if (!r) return
const lw = dock.left.open ? dock.left.w : STRIP_W
const rw = dock.right.open ? dock.right.w : STRIP_W
r.style.setProperty('--iwb-dock-l', lw + 'px')
r.style.setProperty('--iwb-dock-r', rw + 'px')
}, [dock.left.open, dock.left.w, dock.right.open, dock.right.w])
const setSide = function (side, patch) {
setDock(function (d) {
const next = { left: Object.assign({}, d.left), right: Object.assign({}, d.right) }
next[side] = Object.assign({}, next[side], patch)
return next
})
}
const toggle = function (side) {
const next = !dock[side].open
setSide(side, { open: next })
if (side === 'right' && typeof props.onRightOpen === 'function') props.onRightOpen(next)
}
// ---- 拖宽(内缘手柄) ----
const startResize = function (side, e) {
e.preventDefault()
e.stopPropagation()
const startX = e.clientX
const startW = dock[side].w
const move = function (ev) {
const dx = ev.clientX - startX
const w = side === 'left' ? startW + dx : startW - dx
setSide(side, { w: Math.max(MIN_W, Math.round(w)) })
}
const up = function () {
global.removeEventListener('pointermove', move)
global.removeEventListener('pointerup', up)
}
global.addEventListener('pointermove', move)
global.addEventListener('pointerup', up)
}
// ---- 图标拖拽:同栏排序 + 跨栏移动(按鼠标位置插入) ----
const iconCenters = function (side) {
const root = rootRef.current
if (!root) return []
const nodes = root.querySelectorAll('.iwb-dock[data-side="' + side + '"] .iwb-dock-btn')
const out = []
for (let i = 0; i < nodes.length; i++) {
const r = nodes[i].getBoundingClientRect()
out.push({ id: nodes[i].getAttribute('data-panel'), x: r.left + r.width / 2 })
}
return out
}
const indexForX = function (side, x) {
const arr = iconCenters(side)
for (let i = 0; i < arr.length; i++) if (x < arr[i].x) return i
return arr.length
}
const onIconPointerDown = function (side, id, e) {
if (e.button !== 0) return
e.preventDefault()
e.stopPropagation()
const sx = e.clientX
const sy = e.clientY
let started = false
dragRef.current = { side, id }
const move = function (ev) {
if (!started) {
if (Math.abs(ev.clientX - sx) < 4 && Math.abs(ev.clientY - sy) < 4) return
started = true
dragRef.current = { side, id, dragging: true }
}
const rect = rootRef.current ? rootRef.current.getBoundingClientRect() : { left: 0, width: global.innerWidth || 0 }
const mid = rect.left + rect.width / 2
let target = ev.clientX < mid ? 'left' : 'right'
if (!dock[target].open) target = target === 'left' ? 'right' : 'left'
const info = { side: target, index: indexForX(target, ev.clientX) }
dropRef.current = info
setGhost({ id, x: ev.clientX, y: ev.clientY })
setDropInfo(info)
}
const up = function () {
global.removeEventListener('pointermove', move)
global.removeEventListener('pointerup', up)
const d = dragRef.current
dragRef.current = null
setGhost(null)
const info = dropRef.current
dropRef.current = null
setDropInfo(null)
if (!d || !d.dragging || !info) return
commitMove(d.side, d.id, info.side, info.index)
}
global.addEventListener('pointermove', move)
global.addEventListener('pointerup', up)
}
const commitMove = function (fromSide, id, toSide, toIndex) {
if (fromSide === toSide && dock[fromSide].list.length === 1) return
if (fromSide !== toSide && dock[fromSide].list.length === 1) {
if (props.onReject) props.onReject('该栏至少保留一个面板')
return
}
setDock(function (d) {
const next = { left: Object.assign({}, d.left, { list: d.left.list.slice() }), right: Object.assign({}, d.right, { list: d.right.list.slice() }) }
const src = next[fromSide].list
const at = src.indexOf(id)
if (at >= 0) src.splice(at, 1)
let idx = Math.max(0, Math.min(toIndex, next[toSide].list.length))
if (fromSide === toSide && at >= 0 && at < idx) idx -= 1
next[toSide].list.splice(idx, 0, id)
next[toSide].active = id
if (!next[fromSide].list.length) next[fromSide].list.push(id) // 双保险:绝不留空栏
if (next[fromSide].list.indexOf(next[fromSide].active) < 0) next[fromSide].active = next[fromSide].list[0]
return next
})
}
const renderHead = function (side) {
const s = dock[side]
const isLeft = side === 'left'
const toggleBtn = el('button', {
key: 'toggle',
className: 'iwb-dock-btn iwb-dock-toggle',
title: isLeft ? '收起左栏' : '收起右栏',
'aria-label': isLeft ? '收起左栏' : '收起右栏',
onClick: function () { toggle(side) },
}, renderIcon(isLeft ? 'chevronLeft' : 'chevronRight'))
const icons = s.list.map(function (id) {
const p = panels[id]
return el('button', {
key: id,
className: 'iwb-dock-btn' + (s.active === id ? ' on' : ''),
'data-panel': id,
'aria-label': (p && p.title) || id,
onPointerDown: function (e) { onIconPointerDown(side, id, e) },
onClick: function (e) { e.stopPropagation(); if (!dragRef.current) setSide(side, { active: id }) },
}, renderIcon((p && p.icon) || 'panel'))
})
return el('div', { className: 'iwb-dock-head' }, isLeft ? [toggleBtn].concat(icons) : icons.concat([toggleBtn]))
}
const renderBody = function (side) {
const s = dock[side]
const p = panels[s.active]
if (!p) return el('div', { className: 'iwb-dock-empty' }, '面板加载中…')
if (p.node) return p.node
if (p.pluginId) {
const mount = (props.pluginMount && props.pluginMount(p.pluginId)) || null
if (!mount) return el('div', { className: 'iwb-dock-empty' }, (p.title || '插件') + ' 未加载')
return el(PluginBox, { mount })
}
return el('div', { className: 'iwb-dock-empty' }, '面板不可用')
}
const renderDock = function (side) {
const s = dock[side]
if (!s.open) {
return el('div', {
key: side,
className: 'iwb-dock-strip ' + side,
title: side === 'left' ? '展开左栏' : '展开右栏',
onClick: function () { toggle(side) },
}, renderIcon(side === 'left' ? 'chevronRight' : 'chevronLeft'))
}
const isLeft = side === 'left'
return el('div', {
key: side,
className: 'iwb-dock ' + side,
'data-side': side,
style: { width: s.w },
},
renderHead(side),
el('div', { className: 'iwb-dock-body' }, renderBody(side)),
el('div', { className: 'iwb-dock-resize', onPointerDown: function (e) { startResize(side, e) } }),
(dropInfo && dropInfo.side === side) ? el('div', {
className: 'iwb-dock-drop',
style: { left: (8 + dropInfo.index * 30) + 'px' },
}) : null
)
}
return el('div', { className: 'iwb-dockarea', ref: rootRef },
renderDock('left'),
renderDock('right'),
ghost ? el('div', { className: 'iwb-dock-ghost', style: { left: ghost.x + 12, top: ghost.y + 12 } },
renderIcon((panels[ghost.id] && panels[ghost.id].icon) || 'panel'),
el('span', null, (panels[ghost.id] && panels[ghost.id].title) || '')
) : null
)
}
global.IWBDockArea = IWBDockArea
})(typeof window !== 'undefined' ? window : globalThis)