Files
image-workbench-desktop/src/ui-groups-sidebar.js
T

204 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.
/* 资源中心「节点组」侧栏(自 app.js 收编,P2-9 组件抽取)
*
* 无状态 UI 组件:数据与回调经 props 传入;React / el 从 global 解析。
* 宿主(app.js)注入两个应用层助手:props.api(数据层)、props.I(图标)。
*/
// 确认统一走页内确认(桌面端),网页端保持原生 confirm
function askConfirm (text, opts) {
const C = (typeof window !== 'undefined') ? window.IWBUIConfirm : null
if (C && typeof C.askOrNative === 'function') return C.askOrNative(text, opts)
return Promise.resolve(window.confirm(String(text)))
}
;(function (global) {
'use strict'
const R = global.React || {}
const ReactDOM = global.ReactDOM || {}
const el = (type, prop, ...children) => R.createElement(type, prop, ...children)
const useState = R.useState, useEffect = R.useEffect, useRef = R.useRef
function IWBUIGroupsSidebar (props) {
const api = props.api
const I = props.I
const [w, setW] = useState(300)
const [drag] = useState(() => ({ active: false, sx: 0, sw: 300 }))
const [gEdit, setGEdit] = useState(null)
const [gEditName, setGEditName] = useState('')
const [groupFilter, setGroupFilter] = useState('all')
const [newGroup, setNewGroup] = useState(false)
const [newGroupName, setNewGroupName] = useState('')
const [gMgrOpen, setGMgrOpen] = useState(false)
const [manage, setManage] = useState(false)
const [selSet, setSelSet] = useState(() => new Set())
const [groupMenu, setGroupMenu] = useState(null)
useEffect(() => {
if (!groupMenu) return
const closeOutside = (e) => { if (!e.target || !e.target.closest || !e.target.closest('.iwb-lib-group-menu')) setGroupMenu(null) }
document.addEventListener('pointerdown', closeOutside, true)
return () => document.removeEventListener('pointerdown', closeOutside, true)
}, [groupMenu])
const [gmEdit, setGmEdit] = useState(null)
const [gmNew, setGmNew] = useState('')
const gEditCancel = useRef(false) // Esc 取消后 blur 不误提交
const handleDrag = (e) => {
if (!drag.active) return
e.preventDefault()
setW(Math.max(240, Math.min(560, drag.sw + (drag.sx - e.clientX))))
}
const stopDrag = () => { drag.active = false }
const list = props.savedGroups || []
const groups = (props.nodeGroupGroups && props.nodeGroupGroups.length)
? props.nodeGroupGroups
: (() => { const seen = []; for (const g of list) { const gr = String((g && g.group) || '').trim(); if (gr && !seen.some((x) => x.name === gr)) seen.push({ id: gr, name: gr }) } return seen })()
const saveList = (newList) => {
// 失败提示走红色(2026-11-12):这里原来只传一句文案,onFlash 固定按「成功」绿色渲染 ⇒ 失败看着像成功。
props.onSavedGroupsChange((newList || []).map((g) => ({ ...g, group: String((g && g.group) || '').trim() })), () => props.onFlash && props.onFlash('节点组保存失败,请重试', 'err'))
}
const saveGroups = (gs) => {
props.onNodeGroupGroupsChange(gs)
// 同 ui-lib-sidebar:api() 失败是 resolve({ok:false}),不是 reject ⇒ .catch 抓不到,必须看返回值。
api('groups', { groups: list, groupGroups: gs })
.then((r) => { if (r && r.ok === false && props.onFlash) props.onFlash(r.error || '节点分组保存失败,请重试', 'err') })
.catch(() => { if (props.onFlash) props.onFlash('节点分组保存失败,请重试', 'err') })
}
const assignNodeGroup = (ids, group) => { saveList(list.map((x) => ids.includes(x.id) ? { ...x, group: group || '' } : x)); setGroupMenu(null); props.onFlash && props.onFlash('已移动到' + (group || '未分组')) }
const openNodeGroupMenu = (e, g) => { e.preventDefault(); e.stopPropagation(); setGroupMenu({ x: e.clientX, y: e.clientY, ids: [g.id] }) }
const moveSelected = (group) => { if (!selSet.size) return; assignNodeGroup(Array.from(selSet), group); setSelSet(new Set()) }
const groupCount = (gname) => {
if (gname === 'all') return list.length
if (gname === '__none__') return list.filter((g) => !String((g && g.group) || '').trim()).length
return list.filter((g) => (g && g.group) === gname).length
}
const filtered = list.filter((g) => {
if (groupFilter === '__none__') return !String((g && g.group) || '').trim()
if (groupFilter !== 'all') return (g && g.group) === groupFilter
return true
})
// 分组管理操作(与提示词库一致:重命名/删除/排序/新建,空分组保留)
const confirmNewGroup = () => {
const name = newGroupName.trim()
if (!name) { setNewGroup(false); return }
if (!groups.some((g) => g.name === name)) saveGroups(groups.concat([{ id: name, name }]))
setGroupFilter(name)
setNewGroup(false)
setNewGroupName('')
}
const renameGroup = (oldName, newName) => {
const n = String(newName || '').trim()
if (!n || n === oldName) return
if (groups.some((g) => g.name === n && g.name !== oldName)) return
saveGroups(groups.map((g) => (g.name === oldName ? { id: n, name: n } : g)))
saveList(list.map((g) => (g.group === oldName ? { ...g, group: n } : g)))
if (groupFilter === oldName) setGroupFilter(n)
}
const removeGroup = async (name) => {
if (!(await askConfirm('删除节点组分组「' + name + '」?组内节点组将移至「未分组」(不删除节点组)。', { title: '删除分组', okText: '删除' }))) return
saveGroups(groups.filter((g) => g.name !== name))
saveList(list.map((g) => (g.group === name ? { ...g, group: '' } : g)))
if (groupFilter === name) setGroupFilter('all')
}
const moveGroup = (i, dir) => {
const ni = i + dir
if (ni < 0 || ni >= groups.length) return
const gs = groups.slice()
const t = gs[i]; gs[i] = gs[ni]; gs[ni] = t
saveGroups(gs)
}
const mgrModal = gMgrOpen ? el('div', { className: 'iwb-modal-mask', onWheel: (e) => e.stopPropagation(), onPointerDown: (e) => { if (e.target === e.currentTarget) setGMgrOpen(false) } },
el('div', { className: 'iwb-modal iwb-modal-sm' },
el('div', { className: 'iwb-modal-head' },
el('span', { className: 'iwb-toolbar-title' }, '管理节点组分组(' + groups.length + ')'),
el('button', { className: 'iwb-btn iwb-btn-sm', title: '关闭', onClick: () => setGMgrOpen(false) }, I('x')),
),
el('div', { className: 'iwb-modal-body' },
!groups.length ? el('div', { className: 'iwb-hint' }, '暂无分组。可在下方新建。') : null,
groups.map((g, i) => {
const editingThis = gmEdit && gmEdit.name === g.name
return el('div', { key: g.id || g.name, className: 'iwb-gmgr-row' },
editingThis
? el('input', { className: 'iwb-input iwb-grow', value: gmEdit.value, autoFocus: true, onChange: (e) => setGmEdit({ name: g.name, value: e.target.value }), onKeyDown: (e) => { if (e.key === 'Enter') { renameGroup(g.name, gmEdit.value); setGmEdit(null) } if (e.key === 'Escape') setGmEdit(null) } })
: el('span', { className: 'iwb-gmgr-name' }, g.name),
el('span', { className: 'iwb-gmgr-count' }, groupCount(g.name) + ' 组'),
editingThis
? el('button', { className: 'iwb-btn iwb-btn-sm iwb-btn-primary', onClick: () => { renameGroup(g.name, gmEdit.value); setGmEdit(null) } }, '✓')
: el('button', { className: 'iwb-btn iwb-btn-sm', title: '重命名', onClick: () => setGmEdit({ name: g.name, value: g.name }) }, '重命名'),
el('button', { className: 'iwb-btn iwb-btn-sm', title: '上移', disabled: i === 0, onClick: () => moveGroup(i, -1) }, '↑'),
el('button', { className: 'iwb-btn iwb-btn-sm', title: '下移', disabled: i === groups.length - 1, onClick: () => moveGroup(i, 1) }, '↓'),
el('button', { className: 'iwb-btn iwb-btn-sm iwb-btn-danger', title: '删除分组(组内节点组移至未分组)', onClick: () => removeGroup(g.name) }, '删除'),
)
}),
el('div', { className: 'iwb-row' },
el('input', { className: 'iwb-input iwb-grow', placeholder: '新分组名称', value: gmNew, onChange: (e) => setGmNew(e.target.value), onKeyDown: (e) => { if (e.key === 'Enter') { const n = gmNew.trim(); if (n) { if (!groups.some((x) => x.name === n)) saveGroups(groups.concat([{ id: n, name: n }])); setGmNew('') } } } }),
el('button', { className: 'iwb-btn', onClick: () => { const n = gmNew.trim(); if (n) { if (!groups.some((x) => x.name === n)) saveGroups(groups.concat([{ id: n, name: n }])); setGmNew('') } } }, '+ 添加'),
),
),
),
) : null
// 分类胶囊行(同提示词库;拖拽卡片到分组 = 移动分组)
const chipsRow = el('div', { className: 'iwb-lib-chips' },
el('button', { className: 'iwb-chip' + (groupFilter === 'all' ? ' iwb-chip-active' : ''), onClick: () => { setNewGroup(false); setGroupFilter('all') } }, '全部', el('span', { className: 'iwb-chip-count' }, list.length)),
el('button', { className: 'iwb-chip' + (groupFilter === '__none__' ? ' iwb-chip-active' : '') + ' iwb-chip-drop', title: '未分组;拖拽节点组卡片到此 = 移出分组', onClick: () => { setNewGroup(false); setGroupFilter('__none__') }, onDragOver: (e) => { e.preventDefault(); e.stopPropagation(); e.dataTransfer.dropEffect = 'move' }, onDrop: (e) => { e.preventDefault(); e.stopPropagation(); const gid = e.dataTransfer && e.dataTransfer.getData('application/x-iwb-groupid'); if (gid) { saveList(list.map((x) => (x.id === gid ? { ...x, group: '' } : x))); props.onFlash && props.onFlash('已移到未分组') } } }, '未分组', el('span', { className: 'iwb-chip-count' }, groupCount('__none__'))),
groups.map((g) => el('button', { key: g.id || g.name, className: 'iwb-chip' + (groupFilter === g.name ? ' iwb-chip-active' : '') + ' iwb-chip-drop', title: '拖拽节点组卡片到此 = 移动到该分组', onClick: () => { setNewGroup(false); setGroupFilter(g.name) }, onDragOver: (e) => { e.preventDefault(); e.stopPropagation(); e.dataTransfer.dropEffect = 'copy' }, onDropCapture: (e) => { e.stopPropagation() }, onDrop: (e) => { e.preventDefault(); e.stopPropagation(); const gid = e.dataTransfer && e.dataTransfer.getData('application/x-iwb-groupid'); if (gid) { saveList(list.map((x) => (x.id === gid ? { ...x, group: g.name } : x))); props.onFlash && props.onFlash('已移动到分组「' + g.name + '」') } } }, g.name, el('span', { className: 'iwb-chip-count' }, groupCount(g.name)))),
el('button', { key: 'add', className: 'iwb-chip iwb-chip-add', title: '新建分组', onClick: () => { setNewGroup(!newGroup); if (!newGroup) setNewGroupName('') } }, '+'),
el('button', { key: 'mgmt', className: 'iwb-chip iwb-chip-add', title: '管理分组(重命名 / 排序 / 删除)', onClick: () => setGMgrOpen(true) }, '⚙'),
)
const newGroupRow = newGroup ? el('div', { className: 'iwb-row' },
el('input', { className: 'iwb-input iwb-grow', placeholder: '新分组名称', value: newGroupName, onChange: (e) => setNewGroupName(e.target.value) }),
el('button', { className: 'iwb-btn iwb-btn-primary', onClick: confirmNewGroup }, '确定'),
el('button', { className: 'iwb-btn', onClick: () => setNewGroup(false) }, '取消'),
) : null
// 卡片:只展示标题;双击标题原位置改名(Enter/blur 提交、Esc 取消);管理按钮删除(确认提示)
const cards = filtered.map((g) => {
const editing = gEdit === g.id
const commitRename = () => {
if (gEditCancel.current) { gEditCancel.current = false; return }
saveList(list.map((x) => (x.id === g.id ? { ...x, name: gEditName.trim() || x.name } : x)))
setGEdit(null)
}
const cancelEdit = () => { gEditCancel.current = true; setGEdit(null) }
return el('div', { key: g.id, className: 'iwb-pcard' + (editing ? ' iwb-pcard-editing' : '') + (manage && selSet.has(g.id) ? ' iwb-pcard-selected' : ''), draggable: !editing, onContextMenu: (e) => openNodeGroupMenu(e, g),
onDragStart: (e) => { try { e.stopPropagation(); e.dataTransfer.setData('application/x-iwb-group', JSON.stringify(g.id)); e.dataTransfer.setData('application/x-iwb-groupid', g.id); e.dataTransfer.effectAllowed = 'copy' } catch (err) { /* ignore */ } },
onDragEnd: (e) => { try { e.stopPropagation() } catch (err) { /* ignore */ } },
onDoubleClick: (e) => { e.stopPropagation(); gEditCancel.current = false; setGEdit(g.id); setGEditName(g.name || '') },
},
el('div', { className: 'iwb-row' },
editing
? el('input', { className: 'iwb-input iwb-grow iwb-pname-input', value: gEditName, autoFocus: true, placeholder: '组名', spellCheck: false, onChange: (e) => setGEditName(e.target.value), onKeyDown: (e) => { if (e.key === 'Enter') { e.stopPropagation(); commitRename() } if (e.key === 'Escape') { e.stopPropagation(); cancelEdit() } }, onBlur: commitRename, onPointerDown: (e) => e.stopPropagation() })
: el('span', { className: 'iwb-pname', title: '双击改名' }, g.name || '分组'),
el('span', { className: 'iwb-pbtns' },
el('button', { className: 'iwb-btn iwb-btn-sm iwb-btn-danger', title: '删除节点组', onClick: async (e) => { e.stopPropagation(); if (await askConfirm('删除节点组「' + (g.name || '') + '」?', { title: '删除节点组', okText: '删除' })) saveList(list.filter((x) => x.id !== g.id)) } }, I('x')),
),
),
)
})
const nodeGroupMenuEl = groupMenu ? ReactDOM.createPortal(el('div', { className: 'iwb-ctxmenu iwb-lib-group-menu', style: { left: groupMenu.x, top: groupMenu.y }, onPointerDown: (e) => e.stopPropagation(), onContextMenu: (e) => e.preventDefault() }, el('div', { className: 'iwb-ctx-title' }, '移动到分组'), el('button', { className: 'iwb-ctx-item', onClick: () => assignNodeGroup(groupMenu.ids, '') }, '未分组'), groups.map((g) => el('button', { key: g.id || g.name, className: 'iwb-ctx-item', onClick: () => assignNodeGroup(groupMenu.ids, g.name) }, g.name))), document.body) : null
const bodyEl = el('div', { className: 'iwb-lib-body' },
el('div', { className: 'iwb-lib-fixed' },
el('div', { className: 'iwb-row' }, el('button', { className: 'iwb-btn iwb-btn-sm' + (manage ? ' iwb-btn-primary' : ''), onClick: () => { setManage(!manage); setSelSet(new Set()) } }, manage ? '退出管理' : '管理'), manage && selSet.size ? el('select', { className: 'iwb-input', value: '__move__', onChange: (e) => { if (e.target.value !== '__move__') moveSelected(e.target.value) } }, el('option', { value: '__move__' }, '移动到分组…'), el('option', { value: '' }, '未分组'), groups.map((g) => el('option', { key: g.id || g.name, value: g.name }, g.name))) : null),
chipsRow,
newGroupRow,
),
el('div', { className: 'iwb-lib-scroll' },
el('div', { className: 'iwb-lib-cards' }, cards),
!filtered.length ? el('div', { className: 'iwb-hint' }, '暂无收藏节点组。画布编组后点组右上角收藏,拖卡片到画布实例化。') : null,
),
)
if (props.embedded) {
return el('div', { className: 'iwb-groups-embedded' }, bodyEl, mgrModal, nodeGroupMenuEl)
}
return el('div', { className: 'iwb-lib', style: { width: w }, onPointerDown: (e) => e.stopPropagation(), onPointerMove: (e) => { e.stopPropagation(); handleDrag(e) }, onPointerUp: (e) => { e.stopPropagation(); stopDrag() }, onPointerLeave: (e) => { e.stopPropagation(); stopDrag() }, onClick: (e) => e.stopPropagation(), onDoubleClick: (e) => e.stopPropagation(), onWheel: (e) => e.stopPropagation() },
el('div', { className: 'iwb-lib-handle', onPointerDown: (e) => { e.stopPropagation(); drag.active = true; drag.sx = e.clientX; drag.sw = w } }, null),
el('div', { className: 'iwb-lib-head' },
el('span', { className: 'iwb-toolbar-title' }, '节点组'),
el('span', { className: 'iwb-pbtns' },
el('button', { className: 'iwb-btn iwb-btn-sm', title: '刷新', onClick: props.onRefresh }, I('refresh')),
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: props.onClose }, '✕'),
),
),
bodyEl,
mgrModal,
)
}
global.IWBUIGroupsSidebar = IWBUIGroupsSidebar
})(typeof window !== 'undefined' ? window : globalThis)