Files
image-workbench-desktop/src/ui-vault-browse.js
T
qiqi ec7933e2e5
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 1m42s
feat(vault): 浏览窗口固定/可拖拽 + 复制不闪(v3.26.269)
- 窗口固定 860×620;右下角拖拽调整、双击复位、尺寸持久化;切换分类不再改窗口高度
- 面板加载 effect 只依赖 open+源库路径(actions 用 ref),复制不再触发重新读库
- 批量复制:延迟 setWorkspaces(deferState),结束后一次 commitList(一次渲染+一次写索引)
- 修复 268 接线:app 端 createBoard(b, opts) 之前只传 b,silent/defer 未生效
- 记住上次分类/排序
2026-12-06 19:02:42 +08:00

229 lines
16 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* ui-vault-browse.js —— 跨库浏览(只读)+ 一键复制(v3.26.267/269)
* 样式按已确认稿;269 起固定窗口大小(右下角可拖拽、双击复位、尺寸持久化),
* 复制过程不再重建列表(加载副作用只依赖 open + 源库路径)。
*/
;(function (global) {
'use strict'
const React = global.React || {}
const ReactDOM = global.ReactDOM || {}
const el = (t, p, ...c) => React.createElement(t, p, ...c)
const DEF = { w: 860, h: 620 }
const MIN = { w: 640, h: 420 }
const SIZE_KEY = 'iwb.browseSize.v1'
const PREF_KEY = 'iwb.browsePrefs.v1'
const loadJSON = (k, fb) => { try { const v = global.localStorage.getItem(k); return v ? JSON.parse(v) : fb } catch (e) { return fb } }
const saveJSON = (k, v) => { try { global.localStorage.setItem(k, JSON.stringify(v)) } catch (e) { /* ignore */ } }
const clampSize = (w, h) => {
const vw = (global.innerWidth || 1440), vh = (global.innerHeight || 900)
return { w: Math.max(MIN.w, Math.min(Number(w) || DEF.w, vw - 32)), h: Math.max(MIN.h, Math.min(Number(h) || DEF.h, vh - 32)) }
}
const centerOf = (s) => ({ x: Math.max(8, Math.round(((global.innerWidth || 1440) - s.w) / 2)), y: Math.max(8, Math.round(((global.innerHeight || 900) - s.h) / 2)) })
const S = {
mask: { position: 'fixed', inset: '0', background: 'rgba(0,0,0,.32)', zIndex: 2147483000 },
modal: { position: 'fixed', background: '#fefefe', border: '1px solid #c2c2c2', borderRadius: '14px', boxShadow: '0 24px 60px rgba(0,0,0,.22)', overflow: 'hidden', display: 'flex', flexDirection: 'column' },
head: { display: 'flex', alignItems: 'flex-start', justifyContent: 'space-between', padding: '14px 16px 10px', flexShrink: 0 },
title: { fontSize: '14px', fontWeight: 700 },
sub: { fontSize: '11px', color: '#5c5c5c', marginTop: '3px', wordBreak: 'break-all' },
filters: { display: 'flex', flexWrap: 'wrap', gap: '6px 6px', padding: '0 16px 10px', overflowY: 'auto', maxHeight: '7.5em', flexShrink: 0 },
chip: { display: 'inline-flex', alignItems: 'center', flex: '0 0 auto', fontSize: '11px', lineHeight: '18px', minHeight: '22px', padding: '0 9px', borderRadius: '999px', border: '1px solid #c2c2c2', background: '#fff', color: '#5c5c5c', cursor: 'pointer', whiteSpace: 'nowrap' },
chipOn: { background: 'rgba(59,130,246,.12)', borderColor: '#bcd3f7', color: '#2f5fa8', fontWeight: 600 },
tools: { display: 'flex', gap: '8px', alignItems: 'center', padding: '0 16px 8px', flexShrink: 0 },
search: { flex: 1, height: '34px', borderRadius: '8px', border: '1px solid #c2c2c2', background: '#fff', padding: '0 10px', fontSize: '12px', outline: 'none' },
sortWrap: { position: 'relative' },
sort: { height: '34px', display: 'flex', alignItems: 'center', gap: '6px', padding: '0 10px', border: '1px solid #c2c2c2', borderRadius: '8px', background: '#fff', fontSize: '12px', color: '#5c5c5c', cursor: 'pointer' },
sortMenu: { position: 'absolute', right: 0, top: '38px', zIndex: 6, background: '#fff', border: '1px solid #c2c2c2', borderRadius: '8px', boxShadow: '0 10px 24px rgba(0,0,0,.14)', padding: '4px', minWidth: '170px' },
sortItem: { display: 'block', width: '100%', textAlign: 'left', fontSize: '12px', padding: '6px 8px', border: 0, background: 'transparent', borderRadius: '6px', color: '#404040', cursor: 'pointer' },
list: { padding: '2px 8px 8px', overflow: 'auto', flex: 1, minHeight: '0' },
sec: { fontSize: '11px', fontWeight: 700, color: '#5c5c5c', padding: '8px 8px 4px', letterSpacing: '.4px' },
row: { display: 'flex', alignItems: 'center', gap: '10px', padding: '9px 8px', borderRadius: '8px' },
cb: { width: '15px', height: '15px', border: '1px solid #c2c2c2', borderRadius: '4px', background: '#fff', flex: '0 0 auto', display: 'flex', alignItems: 'center', justifyContent: 'center', color: '#fff', fontSize: '11px', cursor: 'pointer' },
cbOn: { background: '#3b82f6', borderColor: '#3b82f6' },
name: { fontSize: '13px', fontWeight: 600, display: 'flex', gap: '6px', alignItems: 'center', whiteSpace: 'nowrap', overflow: 'hidden', textOverflow: 'ellipsis' },
tag: { fontSize: '10px', padding: '1px 6px', borderRadius: '999px', border: '1px solid #c2c2c2', color: '#5c5c5c', fontWeight: 400 },
meta: { fontSize: '11px', color: '#8a8a8a', marginTop: '3px' },
btn: (primary) => ({ fontSize: '12px', padding: '5px 10px', borderRadius: '7px', border: '1px solid ' + (primary ? '#3b82f6' : '#c2c2c2'), background: primary ? '#3b82f6' : '#fff', color: primary ? '#fff' : '#404040', cursor: 'pointer' }),
batch: { margin: '0 16px 8px', padding: '7px 10px', borderRadius: '8px', background: 'rgba(59,130,246,.08)', border: '1px solid #cfe0fa', fontSize: '12px', color: '#2f5fa8', display: 'flex', alignItems: 'center', justifyContent: 'space-between', flexShrink: 0 },
foot: { borderTop: '1px solid #c2c2c2', background: '#f5f5f5', padding: '10px 14px', display: 'flex', justifyContent: 'space-between', alignItems: 'center', fontSize: '11px', color: '#5c5c5c', gap: '10px', flexShrink: 0 },
}
function VaultBrowsePanel(props) {
const { open, vault, actions, onClose } = props
const [analysis, setAnalysis] = React.useState(null)
const [err, setErr] = React.useState('')
const [q, setQ] = React.useState('')
const [filter, setFilter] = React.useState(() => loadJSON(PREF_KEY, {}).filter || 'all')
const [sort, setSort] = React.useState(() => loadJSON(PREF_KEY, {}).sort || 'recent')
const [sortOpen, setSortOpen] = React.useState(false)
const [sel, setSel] = React.useState(() => new Set())
const [busy, setBusy] = React.useState('')
const [msg, setMsg] = React.useState('')
const [size, setSize] = React.useState(() => clampSize(loadJSON(SIZE_KEY, DEF).w, loadJSON(SIZE_KEY, DEF).h))
const [pos, setPos] = React.useState(() => centerOf(clampSize(loadJSON(SIZE_KEY, DEF).w, loadJSON(SIZE_KEY, DEF).h)))
const actionsRef = React.useRef(actions)
actionsRef.current = actions
const sizeRef = React.useRef(size)
sizeRef.current = size
// 加载:只依赖 open + 源库路径(actions 用 ref),复制/状态更新不会重新读库
React.useEffect(() => {
if (!open || !vault || !vault.path) return
let live = true
const A = actionsRef.current
setErr(''); setMsg(''); setAnalysis(null); setSel(new Set()); setQ(''); setBusy('load')
const s = clampSize(loadJSON(SIZE_KEY, DEF).w, loadJSON(SIZE_KEY, DEF).h)
setSize(s); setPos(centerOf(s))
A.readVault(vault.path).then((a) => { if (live) { setAnalysis(a); setBusy('') } }).catch((e) => { if (live) { setErr('读取失败:' + String((e && e.message) || e)); setBusy('') } })
return () => { live = false }
}, [open, vault && vault.path])
// 记住分类/排序
React.useEffect(() => { saveJSON(PREF_KEY, { filter: filter, sort: sort }) }, [filter, sort])
React.useEffect(() => {
if (!open) return undefined
const onKey = (e) => {
if (e.key !== 'Escape') return
if (sortOpen) { setSortOpen(false); return }
if (q) { setQ(''); return }
onClose && onClose()
}
global.document.addEventListener('keydown', onKey, true)
return () => { try { global.document.removeEventListener('keydown', onKey, true) } catch (e) { /* ignore */ } }
}, [open, q, sortOpen, onClose])
if (!open || !vault) return null
const V = (global.IWBStorageFile && global.IWBStorageFile.vaultBrowse) || null
const view = V && analysis ? V.applyView(analysis.boards, { filter: filter, q: q, sort: sort }) : { list: [], groups: null, counts: { all: 0, recent: 0, starred: 0, uncategorized: 0 }, cats: [] }
const cats = V && analysis ? V.categoriesOf(analysis) : []
const toggle = (id) => setSel((prev) => { const n = new Set(prev); if (n.has(id)) n.delete(id); else n.add(id); return n })
const copyOne = async (b) => {
setBusy('copy'); setMsg('')
try {
const r = await actions.copyOne(vault.path, b.id, b.name)
onClose && onClose()
return r
} catch (e) { setMsg('复制失败:' + String((e && e.message) || e)) }
setBusy('')
}
const copySelected = async () => {
const items = view.list.filter((b) => sel.has(b.id))
if (!items.length) return
setBusy('copy'); setMsg('')
try {
const rs = await actions.copyMany(vault.path, items)
const ok = rs.filter((x) => x && x.ok)
const miss = ok.reduce((n, x) => n + (x.missing || 0), 0)
setMsg('已复制 ' + ok.length + ' 个画板' + (miss ? ' · ' + miss + ' 张图片缺失' : '') + (rs.length > ok.length ? ' · ' + (rs.length - ok.length) + ' 个失败' : ''))
setSel(new Set())
} catch (e) { setMsg('复制失败:' + String((e && e.message) || e)) }
setBusy('')
}
const startResize = (e) => {
try { e.preventDefault(); e.stopPropagation() } catch (err) { /* ignore */ }
const start = { x: e.clientX, y: e.clientY, w: sizeRef.current.w, h: sizeRef.current.h }
const move = (ev) => { setSize(clampSize(start.w + (ev.clientX - start.x), start.h + (ev.clientY - start.y))) }
const up = () => {
try { global.removeEventListener('pointermove', move) } catch (err) { /* ignore */ }
try { global.removeEventListener('pointerup', up) } catch (err) { /* ignore */ }
const s = sizeRef.current
saveJSON(SIZE_KEY, { w: s.w, h: s.h })
}
global.addEventListener('pointermove', move)
global.addEventListener('pointerup', up)
}
const resetSize = () => { const s = clampSize(DEF.w, DEF.h); setSize(s); setPos(centerOf(s)); saveJSON(SIZE_KEY, { w: s.w, h: s.h }) }
const filterChip = (key, label, count) => el('button', {
style: Object.assign({}, S.chip, filter === key ? S.chipOn : null),
onClick: () => setFilter(key),
}, label + (count != null ? ' ' + count : ''))
const rowEl = (b) => el('div', { key: b.id, style: Object.assign({}, S.row, sel.has(b.id) ? { background: 'rgba(59,130,246,.07)' } : null) },
el('div', { style: Object.assign({}, S.cb, sel.has(b.id) ? S.cbOn : null), onClick: () => toggle(b.id) }, sel.has(b.id) ? '✓' : ''),
el('div', { style: { flex: 1, minWidth: 0 } },
el('div', { style: S.name }, b.name, b.category ? el('span', { style: S.tag }, b.category) : null, b.starred ? el('span', { style: { color: '#d9a400' } }, '★') : null),
el('div', { style: S.meta }, b.nodes + ' 个节点 · ' + (b.lastUsed || b.mtime ? new Date(b.lastUsed || b.mtime).toLocaleString('zh-CN', { hour12: false }) : '—')),
),
el('button', { style: S.btn(true), disabled: !!busy, onClick: () => copyOne(b) }, '复制到当前库'),
)
const body = () => {
if (busy === 'load') return el('div', { style: { padding: '18px', fontSize: '12px', color: '#8a8a8a' } }, '正在读取源库…')
if (err) return el('div', { style: { padding: '18px', fontSize: '12px', color: '#b91c1c' } }, err)
if (!view.list.length) return el('div', { style: { padding: '18px', fontSize: '12px', color: '#8a8a8a' } }, q ? '没有匹配的画板' : '这个库里没有可复制的画板')
if (view.groups) return view.groups.map((g) => [el('div', { key: 'g:' + g.label, style: S.sec }, g.label), ...g.items.map(rowEl)])
return view.list.map(rowEl)
}
return ReactDOM.createPortal(
el('div', { style: S.mask, onPointerDown: (e) => { if (e.target === e.currentTarget) onClose && onClose() } },
el('div', {
style: Object.assign({}, S.modal, { left: pos.x + 'px', top: pos.y + 'px', width: size.w + 'px', height: size.h + 'px' }),
onPointerDown: (e) => e.stopPropagation(),
},
el('div', { style: S.head },
el('div', { style: { minWidth: 0 } },
el('div', { style: S.title }, '浏览「' + (vault.name || '库') + '」的画板'),
el('div', { style: S.sub }, '只读浏览 · 不会修改源库 · ' + vault.path),
),
el('div', { style: { display: 'flex', alignItems: 'center', gap: '8px', fontSize: '11px', color: '#5c5c5c' } },
el('span', null, (analysis && analysis.boards.length || 0) + ' 个画板'),
el('button', { style: Object.assign({}, S.btn(false), { borderColor: 'transparent', background: 'transparent' }), onClick: () => { const A = actionsRef.current; setBusy('load'); A.readVault(vault.path).then((a) => { setAnalysis(a); setBusy('') }).catch((e) => { setErr(String((e && e.message) || e)); setBusy('') }) } }, '↻ 刷新'),
el('button', { style: Object.assign({}, S.btn(false), { borderColor: 'transparent', background: 'transparent' }), onClick: () => onClose && onClose() }, '✕'),
),
),
el('div', { style: S.filters },
filterChip('all', '全部', view.counts.all),
filterChip('recent', '最近使用', view.counts.recent),
filterChip('starred', '收藏', view.counts.starred),
filterChip('uncategorized', '未分类', view.counts.uncategorized),
cats.map((c) => filterChip('cat:' + c.name, c.name, c.count)),
),
el('div', { style: S.tools },
el('input', { style: S.search, placeholder: '搜索名称 / 分类…', value: q, onChange: (e) => setQ(e.target.value) }),
el('div', { style: S.sortWrap },
el('div', { style: S.sort, onClick: () => setSortOpen((v) => !v) }, ({ recent: '最近修改', name: '名称 A→Z', nodes: '节点数(多→少)', category: '按分类分组' })[sort] + ' ▾'),
sortOpen ? el('div', { style: S.sortMenu },
[['recent', '最近修改'], ['name', '名称 A→Z'], ['nodes', '节点数(多→少)'], ['category', '按分类分组']].map(([k, label]) =>
el('button', { key: k, style: Object.assign({}, S.sortItem, sort === k ? { background: 'rgba(59,130,246,.12)', color: '#2f5fa8', fontWeight: 600 } : null), onClick: () => { setSort(k); setSortOpen(false) } }, label)),
) : null,
),
),
el('div', { style: S.list }, body()),
sel.size ? el('div', { style: S.batch },
el('span', null, '已选 ' + sel.size + ' 个画板'),
el('span', { style: { display: 'flex', gap: '8px' } },
el('button', { style: S.btn(true), disabled: !!busy, onClick: copySelected }, busy === 'copy' ? '复制中…' : '复制选中到当前库'),
el('button', { style: Object.assign({}, S.btn(false), { borderColor: 'transparent', background: 'transparent' }), onClick: () => setSel(new Set()) }, '取消选择'),
),
) : null,
msg ? el('div', { style: { padding: '0 16px 8px', fontSize: '11px', color: '#2f5fa8', flexShrink: 0 } }, msg) : null,
el('div', { style: S.foot },
el('span', null, '复制会在当前库新建画板;源库保持不变;名称冲突自动加 (2)'),
el('span', null, '右下角可拖拽调整大小 · 双击复位'),
),
// 右下角拖拽握把
el('div', {
title: '拖拽调整窗口大小(双击复位)',
onPointerDown: startResize,
onDoubleClick: resetSize,
style: { position: 'absolute', right: '2px', bottom: '2px', width: '18px', height: '18px', cursor: 'nwse-resize', display: 'flex', alignItems: 'flex-end', justifyContent: 'flex-end', color: '#9a9a9a' },
}, el('svg', { viewBox: '0 0 16 16', width: 14, height: 14, fill: 'none', stroke: 'currentColor', strokeWidth: 1.2, strokeLinecap: 'round' },
el('path', { d: 'M15 9 9 15' }, null), el('path', { d: 'M15 13 13 15' }, null), el('path', { d: 'M15 5 5 15' }, null),
)),
),
),
global.document.body,
)
}
const host = global.IWBStorageFile || (global.IWBStorageFile = {})
host.vaultBrowseUI = { VaultBrowsePanel }
})(typeof window !== 'undefined' ? window : globalThis)