180 lines
11 KiB
JavaScript
180 lines
11 KiB
JavaScript
/* 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, canvasV3On, 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
|
||
// Canvas V3 P1:未选中的 ref 节点只保留透明 hit proxy,视觉由 canvas 画;选中后恢复完整 NodeCard。
|
||
if (canvasV3On && n.type === 'ref' && !sel.includes(n.id)) {
|
||
nodeEls.push(el('div', {
|
||
key: n.id,
|
||
className: 'iwb-node-hit-proxy',
|
||
'data-node-id': n.id,
|
||
style: { transform: 'translate(' + n.x + 'px, ' + n.y + 'px)', width: nodeWidth(n) + 'px', height: hOf(n) + 'px' },
|
||
onPointerDown: (e) => {
|
||
if (e.button !== 0) return
|
||
e.stopPropagation()
|
||
const curSel = selRef.current
|
||
const ids = (e.shiftKey || e.metaKey || e.ctrlKey)
|
||
? (curSel.includes(n.id) ? curSel.filter((x) => x !== n.id) : curSel.concat([n.id]))
|
||
: (curSel.includes(n.id) ? curSel.slice() : [n.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: n.id, ids, start, sx: e.clientX / uiZoom(), sy: e.clientY / uiZoom(), active: false }
|
||
setSel(ids)
|
||
},
|
||
onDoubleClick: (e) => { e.stopPropagation() },
|
||
}, null))
|
||
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)
|