669 lines
53 KiB
JavaScript
669 lines
53 KiB
JavaScript
/* 资源中心「左资源栏」侧栏(自 app.js 收编,P2-9 组件抽取)
|
||
*
|
||
* 无状态 UI 组件:数据与回调经 props 传入;React / ReactDOM / el 从 global 解析。
|
||
* 宿主(app.js)注入应用层助手:api / uid / I / useTextDialog / TextDialog / promptGroupsOf。
|
||
*/
|
||
|
||
// 确认统一走页内确认(桌面端),网页端保持原生 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'
|
||
// 存储入口收敛(2026-11-06):统一走 IWBStorageLocal 门面,配额/异常集中处理
|
||
const _lstore = () => (typeof window !== 'undefined' && window.IWBStorageLocal && typeof window.IWBStorageLocal.getStore === 'function') ? window.IWBStorageLocal.getStore() : null
|
||
const React = global.React || {}
|
||
const ReactDOM = global.ReactDOM || {}
|
||
const el = (type, prop, ...children) => React.createElement(type, prop, ...children)
|
||
const useState = React.useState, useEffect = React.useEffect, useRef = React.useRef, useMemo = React.useMemo
|
||
function IWBUILibSidebar (props) {
|
||
const I = props.I
|
||
const api = props.api
|
||
const uid = props.uid
|
||
const useTextDialog = props.useTextDialog
|
||
const TextDialog = props.TextDialog
|
||
const promptGroupsOf = props.promptGroupsOf
|
||
const PluginHost = props.PluginHost
|
||
const wPluginPanels = props.wPluginPanels
|
||
const [query, setQuery] = useState('')
|
||
const [w, setW] = useState(320)
|
||
const [drag] = useState(() => ({ active: false, sx: 0, sw: 320 }))
|
||
const [groupFilter, setGroupFilter] = useState('all')
|
||
const [newGroup, setNewGroup] = useState(false)
|
||
const [newGroupName, setNewGroupName] = useState('')
|
||
const [manage, setManage] = useState(false)
|
||
const [delArm, setDelArm] = useState(null)
|
||
const [groupMgrOpen, setGroupMgrOpen] = useState(false)
|
||
const libTd = useTextDialog()
|
||
const [fullOpen, setFullOpen] = useState(false)
|
||
// 工具栏「提示词全屏」按钮触发:请求号递增 → 打开全屏;已处理确认号用于重挂载时不误触发
|
||
const autoFullRef = useState(() => ({ last: props.autoFullAck || 0 }))[0]
|
||
useEffect(() => {
|
||
if (props.autoFull && props.autoFull > autoFullRef.last) {
|
||
autoFullRef.last = props.autoFull
|
||
setFullOpen(true)
|
||
if (props.onAutoFullConsumed) props.onAutoFullConsumed(props.autoFull)
|
||
}
|
||
}, [props.autoFull])
|
||
const [gmEdit, setGmEdit] = useState(null)
|
||
const [gmNew, setGmNew] = useState('')
|
||
const [fsSel, setFsSel] = useState(null)
|
||
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 [fsSearchEl] = useState(() => ({ el: null }))
|
||
const [expanded, setExpanded] = useState(() => new Set())
|
||
const [fsNew, setFsNew] = useState(false)
|
||
const [fsNewForm, setFsNewForm] = useState({ name: '', group: '', tags: [], text: '' })
|
||
const [fsEditForm, setFsEditForm] = useState(null)
|
||
// 全屏右侧编辑区宽度(可拖分割线调整)+ 折叠状态
|
||
const [fsEditW, setFsEditW] = useState(300)
|
||
// 折叠状态(2026-10-20 起**记忆用户偏好**):
|
||
// 此前是「点卡片永远自动展开编辑区」⇒ 用户手动收起后,下次点卡片又被莫名挤掉 300px。
|
||
// 现在区分三种:null = 从未手动操作过(保持原体验:点卡片自动展开);
|
||
// false = 用户手动收起过(点卡片**不再**自动展开);true = 用户手动展开过(点卡片自动展开)。
|
||
const [fsEditPref, setFsEditPref] = useState(() => {
|
||
try { const st = _lstore(); const v = st ? st.rawGet('iwb_lib_edit_open', null) : null; return v === null || v === undefined ? null : String(v) === '1' } catch (e) { return null }
|
||
})
|
||
const [fsCollapsed, setFsCollapsed] = useState(true)
|
||
// 统一的折叠/展开入口:用户**手动**操作时记住偏好
|
||
const setFsCollapsedManual = (v) => {
|
||
setFsCollapsed(v)
|
||
setFsEditPref(!v)
|
||
try { const st = _lstore(); if (st) st.rawSet('iwb_lib_edit_open', v ? '0' : '1') } catch (e) { /* ignore */ }
|
||
}
|
||
// 点卡片/↑↓ 选中时的展开判定:尊重用户「手动收起过」的偏好
|
||
const openFsEditOnSelect = () => { if (fsEditPref !== false) setFsCollapsed(false) }
|
||
const fsSplit = useState(() => ({ active: false, sx: 0, sw: 300 }))[0]
|
||
// 提示词库活动窗口:位置/大小(localStorage 记忆)+ 拖动/缩放 refs
|
||
const [fsWin, setFsWin] = useState(() => {
|
||
try {
|
||
const st = _lstore(); const v = st ? st.get('iwb_fs_win', null) : null
|
||
if (v && v.w && v.h) return { x: v.x, y: v.y, w: v.w, h: v.h }
|
||
} catch (e) { /* ignore */ }
|
||
return null
|
||
})
|
||
const fsWinRef = useRef(null)
|
||
fsWinRef.current = fsWin
|
||
const fsWinLatest = useRef(null) // setFsWin 同步镜像:pointerup 立即保存时读到最新(不等渲染)
|
||
// 窗口缩放后把资源中心位置/尺寸限制在可视区:浏览器窗口变小不再裁剪、重开按窗口自适应
|
||
const clampFsWin = (win) => {
|
||
if (!win) return win
|
||
const vw = window.innerWidth || 1200, vh = window.innerHeight || 800
|
||
const w = Math.min(Math.max(300, Number(win.w) || 920), vw - 8)
|
||
const h = Math.min(Math.max(220, Number(win.h) || 600), vh - 8)
|
||
const x = Math.max(4, Math.min(Number(win.x) || 0, Math.max(4, vw - w - 4)))
|
||
const y = Math.max(4, Math.min(Number(win.y) || 0, Math.max(4, vh - h - 4)))
|
||
return { x, y, w, h }
|
||
}
|
||
useEffect(() => {
|
||
const onResize = () => setFsWin((p) => (p ? clampFsWin(p) : p))
|
||
window.addEventListener('resize', onResize)
|
||
return () => window.removeEventListener('resize', onResize)
|
||
}, [])
|
||
const fsDrag = useState(() => ({ active: false, sx: 0, sy: 0, ox: 0, oy: 0 }))[0]
|
||
const fsResize = useState(() => ({ active: false, sx: 0, sy: 0, ox: 0, oy: 0, ow: 0, oh: 0, dir: '' }))[0]
|
||
// 拖动标题栏 / 缩放手柄:fullmask 级 React 合成事件 + Pointer Capture
|
||
// (pointerdown 时捕获指针,即使移出窗口也持续收到 move/up)
|
||
const fsWinMove = (e) => {
|
||
if (fsDrag.active) {
|
||
const vw = window.innerWidth || 1200, vh = window.innerHeight || 800
|
||
const nx = Math.max(4 - ((fsWinRef.current && fsWinRef.current.w) || 920) + 200, Math.min(fsDrag.ox + (e.clientX - fsDrag.sx), vw - 200))
|
||
const ny = Math.max(4, Math.min(fsDrag.oy + (e.clientY - fsDrag.sy), vh - 60))
|
||
setFsWin((p) => { const next = { ...(p || { w: 920, h: 600 }), x: nx, y: ny }; fsWinLatest.current = next; return next })
|
||
} else if (fsResize.active) {
|
||
const vw = window.innerWidth || 1600, vh = window.innerHeight || 900
|
||
const d = fsResize
|
||
const dx = e.clientX - d.sx, dy = e.clientY - d.sy
|
||
let nx = d.ox, ny = d.oy, nw = d.ow, nh = d.oh
|
||
if (d.dir === 'l' || d.dir === 'bl' || d.dir === 'tl') nw = Math.max(240, Math.min(d.ow - dx, vw - 8))
|
||
if (d.dir === 'r' || d.dir === 'br' || d.dir === 'tr') nw = Math.max(240, Math.min(d.ow + dx, vw - 8))
|
||
if (d.dir === 't' || d.dir === 'tl' || d.dir === 'tr') nh = Math.max(240, Math.min(d.oh - dy, vh - 8))
|
||
if (d.dir === 'b' || d.dir === 'bl' || d.dir === 'br') nh = Math.max(240, Math.min(d.oh + dy, vh - 8))
|
||
if (d.dir === 'l' || d.dir === 'bl' || d.dir === 'tl') nx = d.ox + (d.ow - nw)
|
||
if (d.dir === 't' || d.dir === 'tl' || d.dir === 'tr') ny = d.oy + (d.oh - nh)
|
||
setFsWin((p) => { const next = { ...(p || { x: d.ox, y: d.oy, w: d.ow, h: d.oh }), x: nx, y: ny, w: nw, h: nh }; fsWinLatest.current = next; return next })
|
||
}
|
||
}
|
||
const fsWinUp = (e) => {
|
||
if (!(fsDrag.active || fsResize.active)) return
|
||
const vw = window.innerWidth || 1200, vh = window.innerHeight || 800
|
||
const base = fsWinLatest.current || fsWinRef.current || { x: 0, y: 0, w: 920, h: 600 }
|
||
const cx = e && typeof e.clientX === 'number' ? e.clientX : null
|
||
const cy = e && typeof e.clientY === 'number' ? e.clientY : null
|
||
let saved = base
|
||
if (fsDrag.active && cx !== null && cy !== null) {
|
||
saved = { ...base, x: Math.max(4 - base.w + 200, Math.min(fsDrag.ox + (cx - fsDrag.sx), vw - 200)), y: Math.max(4, Math.min(fsDrag.oy + (cy - fsDrag.sy), vh - 60)) }
|
||
} else if (fsResize.active && cx !== null && cy !== null) {
|
||
const d = fsResize
|
||
const dx = cx - d.sx, dy = cy - d.sy
|
||
let nx = d.ox, ny = d.oy, nw = d.ow, nh = d.oh
|
||
if (d.dir === 'l' || d.dir === 'bl' || d.dir === 'tl') nw = Math.max(240, Math.min(d.ow - dx, vw - 8))
|
||
if (d.dir === 'r' || d.dir === 'br' || d.dir === 'tr') nw = Math.max(240, Math.min(d.ow + dx, vw - 8))
|
||
if (d.dir === 't' || d.dir === 'tl' || d.dir === 'tr') nh = Math.max(240, Math.min(d.oh - dy, vh - 8))
|
||
if (d.dir === 'b' || d.dir === 'bl' || d.dir === 'br') nh = Math.max(240, Math.min(d.oh + dy, vh - 8))
|
||
if (d.dir === 'l' || d.dir === 'bl' || d.dir === 'tl') nx = d.ox + (d.ow - nw)
|
||
if (d.dir === 't' || d.dir === 'tl' || d.dir === 'tr') ny = d.oy + (d.oh - nh)
|
||
saved = { ...base, x: nx, y: ny, w: nw, h: nh }
|
||
}
|
||
fsDrag.active = false
|
||
fsResize.active = false
|
||
try { const st = _lstore(); if (st) st.set('iwb_fs_win', saved) } catch (e2) { /* ignore */ }
|
||
}
|
||
const prompts = props.prompts || []
|
||
const groups = promptGroupsOf(prompts, props.groups)
|
||
// 空分组兜底缓存:server 异常/未升级时刷新不丢分组(加载时优先服务端,缺失才用缓存)
|
||
const cacheGroups = (gs) => { try { const st = _lstore(); if (st) st.set('iwb_lib_groups_cache', gs) } catch (e) { /* ignore */ } }
|
||
// 保存失败必须可见(2026-11-12):api('prompts'|'groups') 在配额不足/被禁用时**resolve**
|
||
// { ok:false, error:'提示词库保存失败:浏览器本地存储空间不足。' } —— 不会 reject。
|
||
// 历史上这里写 `.catch(() => {})` ⇒ 文案被连同错误一起丢掉,用户以为存好了,刷新后数据消失。
|
||
// 现在两条路(resolve 失败 / 真 reject)都汇到同一句红字提示。放在 saveList 之前定义,避免 TDZ。
|
||
const apiSave = (payload, what) => {
|
||
const fail = (r) => { if (props.onFlash) props.onFlash((r && r.error) || what, 'err') }
|
||
const p = api('prompts', payload)
|
||
if (p && typeof p.then === 'function') p.then((r) => { if (r && r.ok === false) fail(r) }).catch(() => fail(null))
|
||
return p
|
||
}
|
||
const saveList = (list) => {
|
||
props.onPromptsChange(list)
|
||
// 勿在此 cacheGroups(groups):groups 是渲染时的旧闭包,会覆盖 saveGroups 刚写入的新分组
|
||
// (renameGroup/removeGroup/saveFsNew 都是「先 saveGroups 再 saveList」)⇒ 刷新后分组回退。分组只由 saveGroups 落盘。
|
||
apiSave({ prompts: list, groups }, '提示词库保存失败,请重试')
|
||
}
|
||
// 「复制全文」的失败必须可见(2026-11-12):写剪贴板要 secure context + 用户激活,
|
||
// 被拒时原来走 `.catch(() => {})` ⇒ 点了按钮屏幕上毫无变化,用户只能反复点。
|
||
const copyFull = (text) => {
|
||
const ok = () => { if (props.onFlash) props.onFlash('已复制全文') }
|
||
const bad = () => { if (props.onFlash) props.onFlash('复制失败:浏览器拒绝了剪贴板写入(需 https 页面且窗口有焦点)', 'err') }
|
||
try {
|
||
const p = navigator.clipboard && navigator.clipboard.writeText(text || '')
|
||
if (p && typeof p.then === 'function') p.then(ok).catch(bad)
|
||
else bad()
|
||
} catch (e) { bad() }
|
||
}
|
||
const saveGroups = (gs) => {
|
||
props.onGroupsChange(gs)
|
||
cacheGroups(gs)
|
||
apiSave({ prompts: props.prompts || [], groups: gs }, '提示词分组保存失败,请重试')
|
||
}
|
||
// 多关键词搜索:空格分隔,全部命中才显示(名称/内容/标签任一含该词)
|
||
const qs = query.trim().toLowerCase().split(/\s+/).filter(Boolean)
|
||
const groupCount = (gname) => {
|
||
if (gname === 'all') return prompts.length
|
||
if (gname === '__none__') return prompts.filter((p) => !String((p.group || '')).trim()).length
|
||
return prompts.filter((p) => (p.group || '') === gname).length
|
||
}
|
||
const filtered = []
|
||
for (const p of prompts) {
|
||
// 2026-10-20:搜索是**全局**的 —— 输入搜索词时不再受当前分组限制。
|
||
// 此前是先按 groupFilter 过滤、再按搜索词过滤 ⇒ 在「小红书」里搜「收藏」
|
||
// 搜不到「收藏」分组里的内容(实测结果数 = 0,界面却没说清范围)。
|
||
// 修法:**有搜索词时忽略分组过滤**(跨分组搜);无搜索词时按分组过滤。
|
||
// 同时 onSearchChange 会把 groupFilter 自动切到「全部」⇒ chips 的高亮与
|
||
// 实际结果范围**始终一致**(不会出现「小红书高亮但列出收藏内容」这种界面说谎)。
|
||
if (!qs.length) {
|
||
if (groupFilter === '__none__') { if (String((p.group || '')).trim()) continue }
|
||
else if (groupFilter !== 'all' && (p.group || '') !== groupFilter) continue
|
||
}
|
||
if (qs.length) {
|
||
const hay = ((p.name || '') + ' ' + (p.text || '') + ' ' + (p.tags || []).join(' ')).toLowerCase()
|
||
if (!qs.every((w) => hay.indexOf(w) >= 0)) continue
|
||
}
|
||
filtered.push(p)
|
||
}
|
||
// 搜索时自动切「全部」+ 清空搜索时回到原分组(2026-10-20)
|
||
// 用户在「小红书」里点搜索 → 自动切到「全部」开始跨组搜(chips 高亮跟着变,范围可见);
|
||
// 清空搜索 → 回到「小红书」(不打断用户原本的浏览上下文)。
|
||
const groupBeforeSearchRef = useRef(null)
|
||
const onSearchChange = (next) => {
|
||
const had = qs.length > 0
|
||
const has = String(next || '').trim().length > 0
|
||
if (!had && has && groupFilter !== 'all') {
|
||
groupBeforeSearchRef.current = groupFilter
|
||
setGroupFilter('all')
|
||
} else if (had && !has && groupBeforeSearchRef.current) {
|
||
setGroupFilter(groupBeforeSearchRef.current)
|
||
groupBeforeSearchRef.current = null
|
||
}
|
||
setQuery(next)
|
||
}
|
||
const doDelete = (id) => {
|
||
if (delArm === id) { saveList(prompts.filter((x) => x.id !== id)); setDelArm(null); props.onFlash('已删除') }
|
||
else { setDelArm(id); setTimeout(() => setDelArm(null), 4000) }
|
||
}
|
||
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('')
|
||
props.onFlash('已选择分组「' + name + '」')
|
||
}
|
||
// ---- 分组管理操作 ----
|
||
const renameGroup = (oldName, newName) => {
|
||
const n = String(newName || '').trim()
|
||
if (!n || n === oldName) return
|
||
if (groups.some((g) => g.name === n && g.name !== oldName)) { props.onFlash('分组「' + n + '」已存在'); return }
|
||
const gs = groups.map((g) => (g.name === oldName ? { id: n, name: n } : g))
|
||
saveGroups(gs)
|
||
saveList(prompts.map((p) => (p.group === oldName ? { ...p, group: n } : p)))
|
||
if (groupFilter === oldName) setGroupFilter(n)
|
||
props.onFlash('已重命名分组「' + oldName + '」→「' + n + '」')
|
||
}
|
||
const removeGroup = async (name) => {
|
||
if (!(await askConfirm('删除分组「' + name + '」?组内 ' + groupCount(name) + ' 条提示词将移至「未分组」(不删除内容)。', { title: '删除分组', okText: '删除' }))) return
|
||
saveGroups(groups.filter((g) => g.name !== name))
|
||
saveList(prompts.map((p) => (p.group === name ? { ...p, group: '' } : p)))
|
||
if (groupFilter === name) setGroupFilter('all')
|
||
props.onFlash('已删除分组「' + name + '」')
|
||
}
|
||
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)
|
||
}
|
||
// ---- 全屏视图:选中/编辑/批量 ----
|
||
// 卡片收藏:group 切换为「收藏」(收藏栏)/ 取消回未分组
|
||
const toggleStar = (p) => {
|
||
const isStar = (p.group || '') === '收藏'
|
||
saveList(prompts.map((x) => (x.id === p.id ? { ...x, group: isStar ? '' : '收藏' } : x)))
|
||
props.onFlash(isStar ? '已取消收藏' : '已收藏到收藏栏')
|
||
}
|
||
const patchFs = (id, patch) => { saveList(prompts.map((p) => (p.id === id ? { ...p, ...patch } : p))) }
|
||
// 全屏右侧面板新建模式:表单 → 保存(自动归入当前选中分组)
|
||
const startFsNew = () => {
|
||
setFsNew(true)
|
||
setFsSel(null)
|
||
setSelSet(() => new Set())
|
||
setFsNewForm({ name: '', group: (groupFilter === 'all' || groupFilter === '__none__') ? '' : groupFilter, tags: [], text: '' })
|
||
}
|
||
const saveFsNew = () => {
|
||
const t = (fsNewForm.text || '').trim()
|
||
if (!t) { props.onFlash('提示词内容不能为空'); return }
|
||
const np = { id: uid('q'), name: (fsNewForm.name || '').trim() || ('提示词 ' + t.slice(0, 8) + '…'), text: t, tags: (fsNewForm.tags || []).slice(), group: (fsNewForm.group || '').trim() }
|
||
const list = prompts.concat([np])
|
||
// 新建分组时确保分组存在
|
||
let gs = groups.slice()
|
||
if (np.group && !gs.some((g) => g.name === np.group)) gs.push({ id: np.group, name: np.group })
|
||
saveGroups(gs)
|
||
saveList(list)
|
||
setFsNew(false)
|
||
setFsSel(np.id)
|
||
setFsEditForm({ ...np })
|
||
props.onFlash('已新建提示词')
|
||
}
|
||
// 全屏草稿编辑:显式保存 / 建立副本 / 删除
|
||
const saveFsEdit = () => {
|
||
if (!fsEditForm) return
|
||
patchFs(fsEditForm.id, { name: fsEditForm.name || '', group: fsEditForm.group || '', tags: (fsEditForm.tags || []).slice(), text: fsEditForm.text || '' })
|
||
props.onFlash('已保存')
|
||
}
|
||
const dupFsEdit = () => {
|
||
if (!fsEditForm) return
|
||
const np = { id: uid('q'), name: ((fsEditForm.name || '').trim() || '提示词') + ' 副本', text: fsEditForm.text || '', tags: (fsEditForm.tags || []).slice(), group: fsEditForm.group || '' }
|
||
saveList(prompts.concat([np]))
|
||
setFsSel(np.id)
|
||
setFsEditForm({ ...np })
|
||
props.onFlash('已建立副本')
|
||
}
|
||
const delFsEdit = async () => {
|
||
if (!fsEditForm) return
|
||
if (!(await askConfirm('删除该提示词?', { title: '删除提示词', okText: '删除' }))) return
|
||
saveList(prompts.filter((p) => p.id !== fsEditForm.id))
|
||
setFsSel(null)
|
||
setFsEditForm(null)
|
||
props.onFlash('已删除')
|
||
}
|
||
const toggleSel = (id, checked) => {
|
||
setSelSet((s) => { const n = new Set(s); if (checked) n.add(id); else n.delete(id); return n })
|
||
}
|
||
const clearSel = () => setSelSet(() => new Set())
|
||
const moveSel = (group) => {
|
||
if (!selSet.size) return
|
||
saveList(prompts.map((p) => (selSet.has(p.id) ? { ...p, group: group || '' } : p)))
|
||
props.onFlash('已移动 ' + selSet.size + ' 条提示词' + (group ? '到「' + group + '」' : '至未分组'))
|
||
clearSel()
|
||
}
|
||
const delSel = async () => {
|
||
if (!selSet.size) return
|
||
if (!(await askConfirm('删除选中的 ' + selSet.size + ' 条提示词?', { title: '删除提示词', okText: '删除' }))) return
|
||
saveList(prompts.filter((p) => !selSet.has(p.id)))
|
||
if (fsSel && selSet.has(fsSel)) setFsSel(null)
|
||
clearSel()
|
||
props.onFlash('已删除 ' + selSet.size + ' 条提示词')
|
||
}
|
||
// 面板键盘:↑↓ 移动选中、Esc 关闭、Ctrl+F 聚焦搜索
|
||
// 2026-10-20 修:此前守卫是 `if (!fullOpen) return`,而**资源中心模式走的是
|
||
// `props.resOpen`**(fullOpen 恒为 false)⇒ 资源中心里这三个快捷键**全部失效**,
|
||
// 但搜索框 placeholder 明写着「Ctrl+F 聚焦」⇒ **界面在说谎**。
|
||
// 现改为「面板开着就生效」。
|
||
// 注:用 `props.resOpen` 而非 `isResourceCenter`(后者定义在下方,会触发 TDZ)。
|
||
//
|
||
// ⚠️ **Esc 不由本处理器关闭面板**(2026-10-20 实测踩坑后改):
|
||
// app.js 的画布键盘处理(app.js:10349 `hit('cancel')`)**已经**负责关资源中心,
|
||
// 而且它有正确的分层顺序:右键菜单 → 更多菜单 → 设置 → 备份 → **资源中心**。
|
||
// 若本处理器也关面板(捕获阶段先执行)⇒ 会**抢在**那些临时层之前把面板关掉,
|
||
// 导致「Esc 关闭右键菜单」「Esc 关闭选择气泡」等行为被破坏(实测 iwb-v32 两个用例失败)。
|
||
// ⇒ 本处理器只负责 ↑↓ 与 Ctrl+F;Esc 交回 app.js(有分层),
|
||
// 且 app.js 已有「target 是 INPUT/TEXTAREA/SELECT 就 return」的守卫
|
||
// ⇒ 输入框里的 Esc 天然归输入框(取消编辑),不会被抢。
|
||
const isEditingText = (el) => {
|
||
if (!el || !el.tagName) return false
|
||
const tag = String(el.tagName).toLowerCase()
|
||
if (tag === 'textarea') return true
|
||
if (tag !== 'input') return false
|
||
const t = String(el.type || 'text').toLowerCase()
|
||
return !['checkbox', 'radio', 'button', 'submit', 'reset', 'file', 'range', 'color'].includes(t)
|
||
}
|
||
useEffect(() => {
|
||
const panelOpen = fullOpen || !!props.resOpen
|
||
if (!panelOpen) return
|
||
const onKey = (e) => {
|
||
// 正在输入框打字时一律不抢(方向键 = 移动光标;Esc = 取消编辑)
|
||
if (isEditingText(e.target)) return
|
||
if (e.key === 'ArrowDown' || e.key === 'ArrowUp') {
|
||
const arr = filtered
|
||
if (!arr.length) return
|
||
const ci = arr.findIndex((p) => p.id === fsSel)
|
||
const ni = e.key === 'ArrowDown' ? ci + 1 : ci - 1
|
||
const next = arr[ni < 0 ? arr.length - 1 : (ni >= arr.length ? 0 : ni)]
|
||
if (next) { e.preventDefault(); setFsSel(next.id); setFsEditForm({ ...next }); openFsEditOnSelect() }
|
||
} else if ((e.ctrlKey || e.metaKey) && e.key === 'f') {
|
||
e.preventDefault()
|
||
if (fsSearchEl.el) fsSearchEl.el.focus()
|
||
}
|
||
}
|
||
window.addEventListener('keydown', onKey, true)
|
||
return () => window.removeEventListener('keydown', onKey, true)
|
||
}, [fullOpen, props.resOpen, filtered, fsSel, fsSearchEl])
|
||
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 }
|
||
// 卡片元素(面板 / 全屏共用;opts: full/selected/checked/onCheck/onClick)
|
||
// 搜索关键词高亮(返回字符串或 React 片段)
|
||
const hl = (text) => {
|
||
const t = String(text || '')
|
||
if (!qs.length) return t
|
||
let nodes = [t]
|
||
for (const w of qs) {
|
||
const next = []
|
||
for (const n of nodes) {
|
||
if (typeof n !== 'string') { next.push(n); continue }
|
||
const i = n.toLowerCase().indexOf(w)
|
||
if (i < 0) { next.push(n); continue }
|
||
next.push(n.slice(0, i), el('mark', { key: 'hl', className: 'iwb-hl' }, n.slice(i, i + w.length)), n.slice(i + w.length))
|
||
}
|
||
nodes = next
|
||
}
|
||
return nodes
|
||
}
|
||
const cardEl = (p, opts) => {
|
||
const o = opts || {}
|
||
const tagEls = []
|
||
for (const t of (p.tags || [])) tagEls.push(el('span', { key: t, className: 'iwb-tag' }, t))
|
||
if ((p.group || '').trim()) tagEls.push(el('span', { key: 'g', className: 'iwb-tag iwb-tag-group' }, p.group))
|
||
// 卡片按钮:全屏=插入到画布+收藏;侧边=收藏(插入靠拖拽)
|
||
// 2026-10-20 图标语义修正:「插入到画布」此前用的是**下载图标(⬇)**,
|
||
// 而它实际是「添加到画布」⇒ 图标与行为相反,会误导。改为 +(plus)。
|
||
// ⓘ title 措辞**必须保留「插入到画布」**——tests/iwb-v32-web-test.js 按它定位按钮。
|
||
const btns = []
|
||
if (o.full) btns.push(el('button', { key: 'ins', className: 'iwb-btn iwb-btn-sm', title: '插入到画布(可多次,不关闭面板)', onClick: (e) => { e.stopPropagation(); props.onInsertPrompt(p.text, p.name); props.onFlash('已添加「' + (p.name || '提示词') + '」到画布') } }, I('plus')))
|
||
btns.push(el('button', { key: 'star', className: 'iwb-btn iwb-btn-sm' + ((p.group || '') === '收藏' ? ' iwb-btn-starred' : ''), title: (p.group || '') === '收藏' ? '取消收藏(移出「收藏」分组)' : '收藏到「收藏」分组', onClick: (e) => { e.stopPropagation(); toggleStar(p) } }, I('star')))
|
||
const cls = 'iwb-pcard' + (o.full ? ' iwb-pcard-full' : '') + (o.selected ? ' iwb-pcard-selected' : '')
|
||
const headExtra = (o.full && manage) ? el('input', { key: 'ck', type: 'checkbox', className: 'iwb-pcard-check', checked: !!o.checked, onChange: (e) => { e.stopPropagation(); o.onCheck && o.onCheck(p.id, e.target.checked) }, onClick: (e) => e.stopPropagation() }) : null
|
||
return el('div', { key: p.id, className: cls, draggable: !manage, onContextMenu: (e) => openPromptGroupMenu(e, p), onClick: o.onClick,
|
||
onDragStart: (e) => {
|
||
if (manage) return
|
||
try {
|
||
e.stopPropagation(); e.dataTransfer.setData('application/x-iwb-prompt', JSON.stringify({ text: p.text || '', name: p.name || '' }))
|
||
e.dataTransfer.setData('application/x-iwb-promptid', p.id)
|
||
e.dataTransfer.setData('text/plain', (p.text || '').slice(0, 200))
|
||
e.dataTransfer.effectAllowed = 'copy'
|
||
} catch (err) { /* ignore */ }
|
||
},
|
||
},
|
||
el('div', { className: 'iwb-row' },
|
||
headExtra,
|
||
el('span', { className: 'iwb-pname' }, hl(p.name || '未命名')),
|
||
btns ? el('span', { className: 'iwb-pbtns' }, btns) : null,
|
||
),
|
||
tagEls,
|
||
// 默认 2 行截断(CSS line-clamp),点击展开全文
|
||
el('div', { className: 'iwb-ptext' + (o.expanded ? ' iwb-ptext-open' : '') + (!o.full ? ' iwb-ptext-clickable' : ''), title: (!o.full && o.expanded) ? '点击收起' : (!o.full ? '点击展开全文' : ''), onClick: !o.full ? (e) => { e.stopPropagation(); o.onToggleText && o.onToggleText() } : undefined }, hl(p.text || '')),
|
||
o.expanded ? el('div', { className: 'iwb-row iwb-pcard-foot' },
|
||
el('span', { className: 'iwb-hint' }, (p.text || '').length + ' 字'),
|
||
el('span', { className: 'iwb-drawer-spacer' }, null),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm', title: '复制全文', onClick: (e) => { e.stopPropagation(); copyFull(p.text) } }, '复制全文'),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm', title: '收起', onClick: (e) => { e.stopPropagation(); o.onToggleText && o.onToggleText() } }, '收起'),
|
||
) : null,
|
||
)
|
||
}
|
||
const assignPromptGroup = (ids, group) => { saveList(prompts.map((x) => ids.includes(x.id) ? { ...x, group: group || '' } : x)); setGroupMenu(null); props.onFlash('已移动到' + (group || '未分组')) }
|
||
const openPromptGroupMenu = (e, p) => { e.preventDefault(); e.stopPropagation(); setGroupMenu({ x: e.clientX, y: e.clientY, ids: [p.id] }) }
|
||
const toggleExpand = (id) => {
|
||
setExpanded((s) => { const n = new Set(s); if (n.has(id)) n.delete(id); else n.add(id); return n })
|
||
}
|
||
const cards = filtered.map((p) => cardEl(p, { expanded: expanded.has(p.id), onToggleText: () => toggleExpand(p.id) }))
|
||
// 分类胶囊行(面板与全屏共用):全部 / 未分组 / 各分组(带计数),点按即切;尾部「+」新建(侧边)、「⚙」管理
|
||
// 2026-10-20:分组多了会换行成两三排,把卡片区挤矮 ⇒ 改为**单行横向滚动**,
|
||
// 「+」「⚙」**固定在右侧**不随滚动(始终可点)。故把可滚动的分类包一层 `.iwb-lib-chips-scroll`。
|
||
// ⓘ 测试依赖:iwb-v32-web-test.js 用 `.iwb-fullpanel .iwb-chip` 选元素 ⇒ chip 自身的 class 不变。
|
||
const chipsRow = (noAdd) => el('div', { className: 'iwb-lib-chips' },
|
||
el('div', { className: 'iwb-lib-chips-scroll' },
|
||
el('button', { className: 'iwb-chip' + (groupFilter === 'all' ? ' iwb-chip-active' : ''), onClick: () => { setNewGroup(false); setGroupFilter('all') } }, '全部', el('span', { className: 'iwb-chip-count' }, prompts.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 pid = e.dataTransfer && e.dataTransfer.getData('application/x-iwb-promptid'); if (pid) { saveList(prompts.map((x) => (x.id === pid ? { ...x, group: '' } : x))); 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 pid = e.dataTransfer && e.dataTransfer.getData('application/x-iwb-promptid'); if (pid) { saveList(prompts.map((x) => (x.id === pid ? { ...x, group: g.name } : x))); props.onFlash('已移动到分组「' + g.name + '」') } } }, g.name, el('span', { className: 'iwb-chip-count' }, groupCount(g.name)))),
|
||
),
|
||
noAdd ? null : 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: () => setGroupMgrOpen(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
|
||
const promptGroupMenuEl = 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: () => assignPromptGroup(groupMenu.ids, '') }, '未分组'), groups.map((g) => el('button', { key: g.id || g.name, className: 'iwb-ctx-item', onClick: () => assignPromptGroup(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('input', { className: 'iwb-input iwb-grow', placeholder: '🔍 搜索名称 / 内容 / 标签…', value: query, onChange: (e) => onSearchChange(e.target.value) }),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm', title: '全屏管理(↑↓ 选择 · Esc 关闭 · Ctrl+F 搜索)', onClick: () => setFullOpen(true) }, '⛶ 全屏'),
|
||
),
|
||
chipsRow(false),
|
||
newGroupRow,
|
||
),
|
||
// 滚动区:卡片列表
|
||
el('div', { className: 'iwb-lib-scroll' },
|
||
el('div', { className: 'iwb-lib-cards' }, cards),
|
||
(!filtered.length) ? el('div', { className: 'iwb-hint' }, '暂无提示词。全屏模式可新建。') : null,
|
||
),
|
||
)
|
||
// 分组管理弹窗:重命名 / 排序 / 删除 / 新建(空分组保留)
|
||
// ⚠️ 用 Portal 挂到 body:脱离 overlay/drawer 层叠上下文,避免被左侧栏(wsrail z-25)遮挡
|
||
const groupMgrModal = groupMgrOpen ? ReactDOM.createPortal(el('div', { className: 'iwb-modal-mask', onWheel: (e) => e.stopPropagation(), onPointerDown: (e) => { if (e.target === e.currentTarget) setGroupMgrOpen(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: () => setGroupMgrOpen(false) }, I('x')),
|
||
),
|
||
el('div', { className: 'iwb-modal-body' },
|
||
!groups.length ? el('div', { className: 'iwb-hint' }, '暂无分组。可在下方新建,或给提示词设置 group 字段。') : 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('') } } }, '+ 添加'),
|
||
),
|
||
),
|
||
),
|
||
), document.body) : null
|
||
// 全屏管理视图:顶部条 + 左区(库)+ 可折叠右区(编辑/新建)
|
||
// ⚠️ 用 Portal 挂到 body:脱离 overlay/drawer 层叠上下文,避免被左侧栏(wsrail z-25)遮挡
|
||
// 提示词库活动窗口:可拖动标题栏移动、右下角缩放;默认居中,位置/大小记忆在 localStorage
|
||
const fsw = clampFsWin(fsWin || { x: Math.max(8, Math.round(((window.innerWidth || 1600) - 920) / 2)), y: Math.max(8, Math.round(((window.innerHeight || 900) - 600) / 3)), w: 920, h: 600 })
|
||
// 资源中心模式:resOpen 时 asResourceCenter,resTab 决定当前 tab(提示词库/节点组/图库)
|
||
const resourceTab = props.resTab || 'prompts'
|
||
const setResourceTab = props.onResTab || (() => {})
|
||
const isResourceCenter = !!props.resOpen
|
||
const tabIcon = (k) => k === 'prompts' ? I('book') : k === 'groups' ? I('group') : I('ref')
|
||
const tabLabel = (k) => k === 'prompts' ? '提示词库' : k === 'groups' ? '节点组' : '图库'
|
||
const [panelRev, setPanelRev] = React.useState(0)
|
||
React.useEffect(() => {
|
||
if (!PluginHost || typeof PluginHost.on !== 'function') return undefined
|
||
return PluginHost.on(() => setPanelRev((v) => v + 1))
|
||
}, [])
|
||
const pluginPanelTabs = React.useMemo(() => wPluginPanels.map((p) => ({ id: p.id, title: p.title, icon: p.icon, mount: p.mount })), [panelRev])
|
||
const activePluginPanel = typeof resourceTab === 'string' && resourceTab.indexOf('plugin:') === 0 ? (pluginPanelTabs.find((p) => p.id === resourceTab.slice(7)) || null) : null
|
||
const resTabs = el('div', { className: 'iwb-res-tabs' },
|
||
['prompts', 'groups', 'history'].map((k) => el('button', { key: k, className: 'iwb-res-tab' + (resourceTab === k ? ' iwb-res-tab-active' : ''), onClick: () => setResourceTab(k) }, tabIcon(k), el('span', null, tabLabel(k)))),
|
||
pluginPanelTabs.map((p) => el('button', { key: 'plugin:' + p.id, className: 'iwb-res-tab' + (resourceTab === 'plugin:' + p.id ? ' iwb-res-tab-active' : ''), onClick: () => setResourceTab('plugin:' + p.id) }, I(p.icon || 'panel'), el('span', null, String(p.title)))),
|
||
)
|
||
const fullModal = (fullOpen || isResourceCenter) ? ReactDOM.createPortal(el('div', { className: 'iwb-fullmask', style: { left: fsw.x, top: fsw.y, width: fsw.w, height: fsw.h }, onWheel: (e) => e.stopPropagation(), onPointerDown: (e) => e.stopPropagation(), onPointerMove: (e) => { e.stopPropagation(); fsWinMove(e) }, onPointerUp: (e) => { e.stopPropagation(); fsWinUp(e) }, onPointerCancel: (e) => { e.stopPropagation(); fsWinUp(e) }, onClick: (e) => e.stopPropagation() },
|
||
el('div', { className: 'iwb-fullpanel', onPointerMove: (e) => { if (fsSplit.active) { setFsEditW(Math.max(220, Math.min(760, fsSplit.sw - (e.clientX - fsSplit.sx)))) } }, onPointerUp: () => { fsSplit.active = false }, onPointerLeave: () => { fsSplit.active = false } },
|
||
// 顶部条:标题 + Tab + 关闭(按住拖动窗口)
|
||
el('div', { className: 'iwb-full-head', title: '按住拖动窗口', onPointerDown: (e) => { if (e.button !== 0) return; if (e.target.closest && e.target.closest('button, input, select, textarea')) return; try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsDrag.active = true; fsDrag.sx = e.clientX; fsDrag.sy = e.clientY; fsDrag.ox = fsw.x; fsDrag.oy = fsw.y; e.preventDefault() } },
|
||
resTabs,
|
||
el('span', { className: 'iwb-drawer-spacer' }, null),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm', title: '关闭(Esc)', onClick: () => { setFullOpen(false); setManage(false); if (props.onClose) props.onClose() } }, I('x')),
|
||
),
|
||
el('div', { className: 'iwb-full-main' }, ...(resourceTab === 'prompts' ? [
|
||
// 左区:搜索 + 批量 + 新建 + 分类 + 卡片
|
||
el('div', { className: 'iwb-full-left' },
|
||
el('div', { className: 'iwb-row' },
|
||
el('input', { className: 'iwb-input iwb-grow', placeholder: '🔍 搜索名称 / 内容 / 标签(Ctrl+F 聚焦)', value: query, onChange: (e) => onSearchChange(e.target.value), ref: (e) => { fsSearchEl.el = e } }),
|
||
// 搜索范围提示(2026-10-20):搜索是**全局**的(跨全部分组),
|
||
// 但用户可能停在某个分组上 ⇒ 明确告诉他「在全部中搜索」,并可一键清空回到分组筛选。
|
||
qs.length ? el('span', { className: 'iwb-lib-searchscope', title: '搜索会跨全部分组进行' },
|
||
'在全部中搜索',
|
||
el('button', { className: 'iwb-lib-searchscope-back', title: '清空搜索,回到分组筛选', onClick: () => onSearchChange('') }, '×'),
|
||
) : null,
|
||
el('button', { className: 'iwb-btn iwb-btn-sm' + (manage ? ' iwb-btn-primary' : ''), title: '批量管理模式(勾选多条后批量操作)', onClick: () => setManage(!manage) }, manage ? '退出批量' : '批量'),
|
||
el('button', { className: 'iwb-btn', title: '新建提示词(右侧面板填写)', onClick: () => { startFsNew(); setFsCollapsed(false) } }, '+ 新建'),
|
||
),
|
||
chipsRow(true),
|
||
newGroupRow,
|
||
selSet.size ? el('div', { className: 'iwb-full-batch' },
|
||
el('span', { className: 'iwb-full-batch-info' }, '已选 ' + selSet.size + ' 条'),
|
||
el('select', { className: 'iwb-input iwb-full-batch-move', value: '__move__', onChange: (e) => { const v = e.target.value; if (v !== '__move__') { moveSel(v); e.target.value = '__move__' } } },
|
||
el('option', { value: '__move__' }, '移动到分组…'),
|
||
el('option', { value: '' }, '未分组'),
|
||
groups.map((g) => el('option', { key: g.id || g.name, value: g.name }, g.name)),
|
||
),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm iwb-btn-danger', title: '删除选中(确认)', onClick: delSel }, '删除选中'),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm', onClick: clearSel }, '清除选择'),
|
||
) : null,
|
||
el('div', { className: 'iwb-full-grid' },
|
||
filtered.map((p) => cardEl(p, { full: true, selected: fsSel === p.id, checked: selSet.has(p.id), onCheck: toggleSel,
|
||
onClick: () => { if (manage) toggleSel(p.id, !selSet.has(p.id)); else { setFsSel(p.id); setSelSet(new Set()); setFsNew(false); setFsEditForm({ ...p }); openFsEditOnSelect() } } })),
|
||
!filtered.length ? el('div', { className: 'iwb-hint' }, '没有匹配的提示词') : null,
|
||
),
|
||
),
|
||
// 分割线(右区收起时不显示)
|
||
fsCollapsed ? null : el('div', { className: 'iwb-full-split', title: '拖动调整编辑区宽度', onPointerDown: (e) => { e.stopPropagation(); e.preventDefault(); try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsSplit.active = true; fsSplit.sx = e.clientX; fsSplit.sw = fsEditW } }, null),
|
||
// 右区:编辑/新建(可折叠)
|
||
fsCollapsed
|
||
? el('div', { className: 'iwb-full-collapsed', title: '展开编辑面板(点卡片也会展开)', onClick: () => setFsCollapsedManual(false) }, '编辑面板 ▶')
|
||
: el('div', { className: 'iwb-full-edit', style: { width: fsEditW } },
|
||
el('div', { className: 'iwb-full-edit-head' },
|
||
el('span', { className: 'iwb-full-editor-title' }, fsNew ? '新建提示词' : (fsEditForm ? '编辑提示词' : '内容预览')),
|
||
el('span', { className: 'iwb-drawer-spacer' }, null),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm', title: '收起编辑面板', onClick: () => setFsCollapsedManual(true) }, '◀'),
|
||
),
|
||
fsNew ? el('div', { className: 'iwb-full-editor' },
|
||
el('input', { className: 'iwb-input', placeholder: '名称(留空自动生成)', value: fsNewForm.name || '', onChange: (e) => setFsNewForm((f) => ({ ...f, name: e.target.value })) }),
|
||
el('select', { className: 'iwb-input', value: fsNewForm.group || '', onChange: (e) => { const v = e.target.value; if (v === '__new__') { libTd.open({ title: '新建分组', placeholder: '新分组名称', onSubmit: (nm) => { const gs2 = groups.concat([{ id: nm, name: nm }]); saveGroups(gs2); setFsNewForm((f) => ({ ...f, group: nm })) } }) } else setFsNewForm((f) => ({ ...f, group: v })) } },
|
||
el('option', { value: '' }, '未分组'),
|
||
groups.map((g) => el('option', { key: g.id || g.name, value: g.name }, g.name)),
|
||
el('option', { value: '__new__' }, '+ 新建分组…'),
|
||
),
|
||
el('input', { className: 'iwb-input', placeholder: '标签,逗号分隔', value: (fsNewForm.tags || []).join(', '), onChange: (e) => setFsNewForm((f) => ({ ...f, tags: e.target.value.split(',').map((s) => s.trim()).filter(Boolean) })) }),
|
||
el('textarea', { className: 'iwb-textarea iwb-full-editor-text', placeholder: '提示词内容(必填)', value: fsNewForm.text || '', onChange: (e) => setFsNewForm((f) => ({ ...f, text: e.target.value })) }),
|
||
el('div', { className: 'iwb-row' },
|
||
el('button', { className: 'iwb-btn iwb-btn-primary', onClick: saveFsNew }, '保存'),
|
||
el('button', { className: 'iwb-btn', onClick: () => setFsNew(false) }, '取消'),
|
||
),
|
||
el('div', { className: 'iwb-hint' }, '保存后自动加入「' + (fsNewForm.group || '未分组') + '」;分组下拉选「+ 新建分组…」可直接命名并创建。'),
|
||
) : fsEditForm ? el('div', { className: 'iwb-full-editor' },
|
||
el('input', { className: 'iwb-input', placeholder: '名称', value: fsEditForm.name || '', onChange: (e) => setFsEditForm((f) => ({ ...f, name: e.target.value })) }),
|
||
el('select', { className: 'iwb-input', value: fsEditForm.group || '', onChange: (e) => setFsEditForm((f) => ({ ...f, group: e.target.value })) },
|
||
el('option', { value: '' }, '未分组'),
|
||
groups.map((g) => el('option', { key: g.id || g.name, value: g.name }, g.name)),
|
||
),
|
||
el('input', { className: 'iwb-input', placeholder: '标签,逗号分隔', value: (fsEditForm.tags || []).join(', '), onChange: (e) => setFsEditForm((f) => ({ ...f, tags: e.target.value.split(',').map((s) => s.trim()).filter(Boolean) })) }),
|
||
el('textarea', { className: 'iwb-textarea iwb-full-editor-text', placeholder: '提示词内容', value: fsEditForm.text || '', onChange: (e) => setFsEditForm((f) => ({ ...f, text: e.target.value })) }),
|
||
el('div', { className: 'iwb-row' },
|
||
el('button', { className: 'iwb-btn iwb-btn-primary', title: '保存草稿修改', onClick: saveFsEdit }, '保存'),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm', title: '复制为一条新提示词(名称加“副本”)', onClick: dupFsEdit }, '建立副本'),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm', title: '复制全文', onClick: () => copyFull(fsEditForm.text) }, '复制全文'),
|
||
el('button', { className: 'iwb-btn iwb-btn-sm iwb-btn-danger', title: '删除该条', onClick: delFsEdit }, '删除'),
|
||
),
|
||
el('div', { className: 'iwb-hint' }, '改动为草稿:点「保存」落盘;「建立副本」复制为新条目;↑↓ 选择相邻;Esc 关闭全屏。'),
|
||
) : el('div', { className: 'iwb-full-editor iwb-full-editor-empty' }, '← 点击左侧卡片查看 / 编辑(批量模式下点击勾选)'),
|
||
),
|
||
] : [ resourceTab === 'groups' ? props.groupsContent : resourceTab === 'history' ? props.historyContent : activePluginPanel ? el('div', { className: 'iwb-full-panel-plugin',
|
||
// ⚠️ key 不能省:图库分支(props.historyContent)的根节点同样是 div,同位置同标签,
|
||
// React 会把同一个 DOM 节点复用给另一个 tab(只清理自己记账的子节点)
|
||
// ⇒ 插件手动 mount 进去的 DOM 不会被清掉,面板「串页」到图库上方
|
||
key: 'plugin:' + activePluginPanel.id,
|
||
ref: (node) => { if (!activePluginPanel) return; try { if (node && !node.firstChild) activePluginPanel.mount(node) } catch (e) { /* 插件面板挂载异常不得冒泡 */ } } }) : null ]),
|
||
),
|
||
),
|
||
// 缩放手柄:按住右下角调整窗口大小
|
||
el('div', { className: 'iwb-full-resize iwb-full-resize-r', title: '拖动调整宽度', onPointerDown: (e) => { e.stopPropagation(); try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsResize.active = true; fsResize.sx = e.clientX; fsResize.sy = e.clientY; fsResize.ox = fsw.x; fsResize.oy = fsw.y; fsResize.ow = fsw.w; fsResize.oh = fsw.h; fsResize.dir = 'r' } }, null),
|
||
el('div', { className: 'iwb-full-resize iwb-full-resize-l', title: '拖动调整宽度', onPointerDown: (e) => { e.stopPropagation(); try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsResize.active = true; fsResize.sx = e.clientX; fsResize.sy = e.clientY; fsResize.ox = fsw.x; fsResize.oy = fsw.y; fsResize.ow = fsw.w; fsResize.oh = fsw.h; fsResize.dir = 'l' } }, null),
|
||
el('div', { className: 'iwb-full-resize iwb-full-resize-t', title: '拖动调整高度', onPointerDown: (e) => { e.stopPropagation(); try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsResize.active = true; fsResize.sx = e.clientX; fsResize.sy = e.clientY; fsResize.ox = fsw.x; fsResize.oy = fsw.y; fsResize.ow = fsw.w; fsResize.oh = fsw.h; fsResize.dir = 't' } }, null),
|
||
el('div', { className: 'iwb-full-resize iwb-full-resize-b', title: '拖动调整高度', onPointerDown: (e) => { e.stopPropagation(); try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsResize.active = true; fsResize.sx = e.clientX; fsResize.sy = e.clientY; fsResize.ox = fsw.x; fsResize.oy = fsw.y; fsResize.ow = fsw.w; fsResize.oh = fsw.h; fsResize.dir = 'b' } }, null),
|
||
el('div', { className: 'iwb-full-resize iwb-full-resize-br', title: '拖动调整大小', onPointerDown: (e) => { e.stopPropagation(); try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsResize.active = true; fsResize.sx = e.clientX; fsResize.sy = e.clientY; fsResize.ox = fsw.x; fsResize.oy = fsw.y; fsResize.ow = fsw.w; fsResize.oh = fsw.h; fsResize.dir = 'br' } }, null),
|
||
el('div', { className: 'iwb-full-resize iwb-full-resize-tl', title: '拖动调整大小', onPointerDown: (e) => { e.stopPropagation(); try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsResize.active = true; fsResize.sx = e.clientX; fsResize.sy = e.clientY; fsResize.ox = fsw.x; fsResize.oy = fsw.y; fsResize.ow = fsw.w; fsResize.oh = fsw.h; fsResize.dir = 'tl' } }, null),
|
||
el('div', { className: 'iwb-full-resize iwb-full-resize-tr', title: '拖动调整大小', onPointerDown: (e) => { e.stopPropagation(); try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsResize.active = true; fsResize.sx = e.clientX; fsResize.sy = e.clientY; fsResize.ox = fsw.x; fsResize.oy = fsw.y; fsResize.ow = fsw.w; fsResize.oh = fsw.h; fsResize.dir = 'tr' } }, null),
|
||
el('div', { className: 'iwb-full-resize iwb-full-resize-bl', title: '拖动调整大小', onPointerDown: (e) => { e.stopPropagation(); try { e.currentTarget.setPointerCapture(e.pointerId) } catch (err) { /* ignore */ } fsResize.active = true; fsResize.sx = e.clientX; fsResize.sy = e.clientY; fsResize.ox = fsw.x; fsResize.oy = fsw.y; fsResize.ow = fsw.w; fsResize.oh = fsw.h; fsResize.dir = 'bl' } }, null),
|
||
), document.body) : null
|
||
if (isResourceCenter) return el(React.Fragment, null, fullModal, groupMgrModal, promptGroupMenuEl, el(TextDialog, { modal: libTd.modal, onChange: libTd.setValue, onClose: libTd.close }))
|
||
if (props.embedded) {
|
||
return el('div', { className: 'iwb-lib-embedded' }, bodyEl, groupMgrModal, fullModal, promptGroupMenuEl, el(TextDialog, { modal: libTd.modal, onChange: libTd.setValue, onClose: libTd.close }))
|
||
}
|
||
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', onClick: props.onClose }, '✕'),
|
||
),
|
||
),
|
||
bodyEl,
|
||
groupMgrModal,
|
||
fullModal,
|
||
el(TextDialog, { modal: libTd.modal, onChange: libTd.setValue, onClose: libTd.close }),
|
||
)
|
||
}
|
||
global.IWBUILibSidebar = IWBUILibSidebar
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|