Files
image-workbench-desktop/src/ui-canvas-nodes.js
T
qiqi 2cd52d1d4e
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 43s
refactor(promptlib): 收藏入口归插件(宿主不再自带,随插件启停)
按项目所有人要求:提示词库插件负责「管理与收集」的完整能力,宿主只保留基础接口。

宿主侧移除(全部 4 处入口 + 实现):
- 节点气泡星标(app.js 单选气泡的「添加到提示词库」)
- 「选中操作 ▸ 添加到提示词库」、「该节点 ▸ 添加该提示词」
- 提示词节点标题栏星标(src/ui-node-card.js)
- src/runtime-workbench-logic.js 的 addPromptToLibrary(约 45 行)与 ui-canvas-nodes 的 onFavorite 注入

宿主侧保留:promptlib 能力(list/save/insert/copy/notify/onChanged/file*)、prompts 状态与 api('prompts')(备份/迁移)、插件数据通道。

宿主侧补回(此前缺失):右键菜单重新渲染 ctx.commands 注册的插件命令(「插件命令」分区)。

插件 promptlib 1.13.1 → 1.14.0:
- capabilities 增 nodeActions / commands
- nodeActions「收藏为提示词」:仅对 type=prompt 且 text 非空的节点出现(气泡按钮)
- commands「收藏选中的提示词」:右键「插件命令」入口
- 收藏写入插件数据 plugin-data/promptlib.json(以现有数据为基底,保留 cover:{img} 与分组),
  新条目 group:'' 即未分组,并 notify 回执

测试:
- tests/iwb-prompt-collect.spec.js 改为 3 条新契约:插件提供气泡入口 + 宿主无自带入口;
  右键插件命令可用且宿主旧条目已移除;插件停用后两个入口同时消失
- canvas-meta-notice 的旧「点按钮报错回执」用例改为「宿主不得再渲染该入口」
- tests/_helpers/wait.js openDockPanel 加固(等插件宿主初始化,修并行偶发)
- 棘轮下调:app.js 8324 / Workbench 2178

验证:npm run verify:ci 全绿(含 e2e 冒烟 31 条)。
2026-11-27 10:06:03 +08:00

157 lines
10 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-canvas-nodes.js —— 画布节点列表渲染(2026-11-26 从 app.js 收编)
*
* 纯搬移:原「nodeEls 构建」块逐字一致,外层自由变量由 ctx(渲染期传入)提供。
* ctx 在调用点就地求值,取值与原渲染闭包相同;不涉及 effect/时序,无 TDZ 风险。
*
* 依赖清单由 ESLint no-undef 反推(漏项 lint 直接红)。
*/
;(function (global) {
'use strict'
function buildNodeEls(ctx) {
const { CanvasGeometry, CanvasStore, EMPTY_OBJ, NODE_W, NodeCardMemo, addRefFromUrl, batchPortTop, batchSourcesMap, canConnect, cancelRun, config, connTarget, defaultNodeBoxWidth, deleteNodes, duplicateNodes, edges, el, generationConcurrency, hOf, inPortWorld, isBatchSrc, lodViewport, nodeDerived, nodeOnMeasure, nodeOnPreview, nodeOnSelect, nodeTypeH, nodeWidth, nodes, pushHistory, refs, runLLM, runLayerNode, runNode, runOutSources, saveNow, sel, selRef, setConn, setConnTarget, setNotice, setSel, taskCountOf, thumbRev, typesRev, uiZoom, uid, updateNode, visibleIds, wLodEnabled, zoomToNode } = ctx
const nodeEls = []
const batchSourcesById = batchSourcesMap
for (const n of nodes) {
if (!visibleIds.has(n.id)) continue
// 细粒度:只传该节点相关的 busy/progress/connTarget(其他节点引用稳定,memo 跳过)
let busySelf = nodeDerived.busyById.get(n.id) || EMPTY_OBJ
let progSelf = nodeDerived.progById.get(n.id) || EMPTY_OBJ
const connTargetSelf = (connTarget && connTarget.nodeId === n.id) ? connTarget : null
const sourceBusySelf = nodeDerived.srcBusyById.get(n.id) || EMPTY_OBJ
const sourceProgressSelf = nodeDerived.srcProgById.get(n.id) || EMPTY_OBJ
nodeEls.push(el(NodeCardMemo, {
key: n.id,
node: n,
dragging: !!refs.drag,
sizing: !!(refs.resize && refs.resize.nodeId === n.id), // v3.87.16:正在缩放这个节点 → 冻结它的节点体
resizing: !!refs.resize, // v3.87.16:画布正在缩放(其它节点可跳过重渲染)
zooming: !!refs.zooming, // v3.87.39:画布正在滚轮缩放(同上,免逐帧重建 body)
thumbRev, // v3.87.61:显示缩略图就绪的版本号(必须传进来,否则下面的比较恒等于 undefined===undefined ✗)
typesRev, // 2026-11-14:插件节点体注册版本号(插件异步注册完 ⇒ 强制重建节点体,别停在兜底体)
// LOD:'' / 'lite' / 'plain'(v3.24.78 两档;判据与阈值见 IWBCanvasGeometry.lodModeOf)
// LOD(v3.25.70):15% 阈值加**滞回**(进入 0.15 / 退出 0.18),
// 否则在阈值附近抖一下就整批节点换体,肉眼上是「一到 15% 就卡一下」。
lodLow: (!wLodEnabled() || !CanvasGeometry || !CanvasGeometry.lodModeOf) ? '' : CanvasGeometry.lodModeOf(lodViewport(n), n, nodeWidth),
draggingNode: !!(refs.drag && refs.drag.start && refs.drag.start[n.id]),
selected: sel.includes(n.id),
measuredH: refs.measuredH[n.id],
onMeasure: nodeOnMeasure,
busy: busySelf,
progress: progSelf,
sourceBusy: sourceBusySelf,
sourceProgress: sourceProgressSelf,
edges,
nodes,
batchSources: batchSourcesById.get(n.id) || null,
config,
onSelect: nodeOnSelect,
onDelete: (id) => deleteNodes([id]),
onDuplicate: (id) => duplicateNodes([id]),
onUpdate: updateNode,
// 显式「粘贴图片替换参考图」入口的撤销快照(updateNode 只对参数类键入栈,图片内容不在其中)
onSnapshot: pushHistory,
onNotice: (text) => setNotice({ type: 'err', text }),
connTarget: connTargetSelf,
onRun: runNode,
onRunLayer: runLayerNode,
// 任务条三态按钮的「等待/结束」都走这里(此前 onCancel 从未传入节点卡片 → 旧「结束」按钮点了无反应)
onCancel: cancelRun,
onRunOut: runOutSources,
taskCountOf,
generationConcurrency: generationConcurrency(config),
onRunLlm: runLLM,
onCopyRef: addRefFromUrl,
onPreview: nodeOnPreview,
onInPortDown: (id) => {
if (!refs.connFrom) {
// 反向拖线(2026-10-30):从**输入端口**往左拖,可新建上游节点(或直接连到已有节点的输出口)
const nd = refs.nodes.find((n) => n.id === id)
if (!nd) return
const inP = inPortWorld(nd)
refs.connFrom = { nodeId: id, reverse: true, portPos: inP }
setConnTarget(null)
setConn({ from: id, x: inP.x, y: inP.y, reverse: true })
return
}
{
const fromId = refs.connFrom.nodeId
if (fromId !== id) {
const bsrcId = refs.connFrom.batchSrcId || null
// 与在线版(Web)一致:普通节点同对仅一条边;batch 多输出按来源(batchSrcId)区分,不同来源可连同一目标
// 立即保存最新边(override 避免 setState 未提交读旧值)
const curEdges = refs.edges
if (!curEdges.some((ed) => ed.from === fromId && ed.to === id && (ed.batchSrcId || null) === bsrcId) && canConnect(fromId, id)) {
// 新增入边**不清**旧结果(2026-11-03):多接一条参考图不该让输出节点的图消失;断连才清(见 deleteEdge)
const newEdge = bsrcId ? { id: uid('e'), from: fromId, to: id, batchSrcId: bsrcId } : { id: uid('e'), from: fromId, to: id }
const nextEdges = curEdges.concat([newEdge])
CanvasStore.updateEdges(nextEdges, { persist: false })
refs.edges = nextEdges // 同步镜像:连续连线/未渲染提交期间,后续保存/操作读到最新边
saveNow({ edges: nextEdges }) // 传入最新边,避免保存旧数据(setState 未提交前 refs 旧值)
}
}
refs.connFrom = null
setConn(null)
}
},
onOutPortDown: (id, batchSrcId) => {
// 记录输出端口的世界坐标,保证拖动连接线从端口精确出发(含普通节点,与边渲染同源)
let portPos = null
const node = refs.nodes.find((n) => n.id === id)
if (node) {
if (batchSrcId) {
const bsrcs = refs.edges.filter((e) => e.to === id).map((e) => refs.nodes.find((n) => n.id === e.from)).filter((n) => isBatchSrc(n, refs.edges, refs.nodes)).slice().sort((a, b) => (a.y - b.y) || (a.x - b.x))
const idx = bsrcs.findIndex((s) => s.id === batchSrcId)
if (idx >= 0) portPos = { x: node.x + nodeWidth(node), y: node.y + batchPortTop(idx, Math.max(1, bsrcs.length), hOf(node)) }
} else {
portPos = { x: node.x + nodeWidth(node), y: node.y + hOf(node) / 2 }
}
}
refs.connFrom = { nodeId: id, batchSrcId: batchSrcId || null, portPos }
setConnTarget(null)
},
onHeaderDown: (e, id, noDrag) => {
// Shift / Ctrl(Cmd) + 点击 = 多选切换(编组/批量操作)
// 用 selRef 读最新选择(框选后 setSel 异步,闭包 sel 可能过期导致多选丢失)
const curSel = selRef.current
let ids
if (e.shiftKey || e.metaKey || e.ctrlKey) ids = curSel.includes(id) ? curSel.filter((x) => x !== id) : curSel.concat([id])
else ids = curSel.includes(id) ? curSel.slice() : [id]
const start = {}
for (const x of refs.nodes) if (ids.includes(x.id)) start[x.id] = { x: x.x, y: x.y }
// noDrag(点在文本框/按钮/端口等可交互控件上):只选中,不启动整体拖动,避免抢走控件交互
if (!noDrag) refs.drag = { start, gx: e.clientX / uiZoom(), gy: e.clientY / uiZoom() }
setSel(ids)
if (!(e.shiftKey || e.metaKey || e.ctrlKey)) setSel((current) => current.length === 1 && current[0] === id ? current : [id])
},
// 节点体点按:先选中 + 记录按下起点;真实移动超过阈值才启动拖动(区分点击与拖拽,避免纯点击污染撤销历史)。fromGroup:sel 来自「按下组」的整组全选 → 本次点击降级为单选该节点,否则 keepGroup 会把点击吃成整组拖动,用户得先点画布空白清选才能单选
onBodyDown: (e, id) => {
const curSel = selRef.current, mod = e.shiftKey || e.metaKey || e.ctrlKey, fromGroup = refs.selFromGroup; refs.selFromGroup = false
// 框选后整体移动:点击的节点已在当前选中集合内且无修饰键 → 保持整组拖动(去掉了标题栏后 body 承担组拖动)
const keepGroup = !mod && !fromGroup && curSel.includes(id)
const ids = mod
? (curSel.includes(id) ? curSel.filter((x) => x !== id) : curSel.concat([id]))
: (keepGroup ? curSel.slice() : [id])
const start = {}
for (const x of refs.nodes) if (ids.includes(x.id)) start[x.id] = { x: x.x, y: x.y }
refs.bodyDown = { id, ids, start, sx: e.clientX / uiZoom(), sy: e.clientY / uiZoom(), active: false }
// 修饰键多选必须把算好的集合写回 sel:否则高亮 / 悬浮栏 / 组气泡都不更新,看起来「Shift 点多选没反应」
// (拖动本身读 refs.bodyDown.ids,与新 sel 同源,保持一致)。无修饰键且点在已选集合内 = 保持整组;
// 其余情况由 NodeCard 的 onSelect 负责,这里不重复 setSel。
if (keepGroup || mod || fromGroup) setSel(ids)
},
onResizeStart: (id, e) => {
const n0 = refs.nodes.find((x) => x.id === id)
// 缩放起始宽/高优先用实际值(未手动调整过时),避免从全局默认值开始导致一拖就跳变
const defW = n0 ? defaultNodeBoxWidth(n0) : NODE_W
refs.resize = { nodeId: id, sw: n0 ? (n0.w || defW) : defW, sh: n0 ? (n0.h || refs.measuredH[id] || nodeTypeH(n0.type) || 220) : 220, gx: e.clientX / uiZoom(), gy: e.clientY / uiZoom() , lw: -1, lh: -1 }
},
onZoomTo: zoomToNode,
}))
}
return nodeEls
}
global.IWBCanvasNodes = { buildNodeEls }
})(typeof window !== 'undefined' ? window : globalThis)