83 lines
5.0 KiB
JavaScript
83 lines
5.0 KiB
JavaScript
/* 批量(batch)/ 组合文案的纯文本助手(自 app.js 收编,P2-9)
|
||
*
|
||
* 全部为纯函数(入参为 node / edges / nodes 数组),无外部依赖;宿主只经 window.IWBCanvasText 使用:
|
||
* - compareCanvasTextOrder:文本源排序(Y 优先 → X → id)
|
||
* - batchSrcText / isBatchSrc / batchPortTop / batchJoinedText
|
||
* - combineEdgeTexts / combineInputTexts:按入边展开文案列表
|
||
* 注意:batchSrcText 不含停用检查 —— isBatchSrc 依赖它判定来源,停用来源需保留端口/列表可见。
|
||
*/
|
||
;(function (global) {
|
||
'use strict'
|
||
|
||
function compareCanvasTextOrder(a, b) { return (Number(a.y) - Number(b.y)) || (Number(a.x) - Number(b.x)) || String(a.id).localeCompare(String(b.id)) }
|
||
|
||
function batchSrcText(n) {
|
||
if (!n || !n.data) return ''
|
||
if (n.type === 'combine') return ((n.data.combined || n.data.extra) || '').trim()
|
||
if (n.type === 'relay') return ((n.data.text || '')).trim()
|
||
if (n.type === 'llm') return ((n.data.result && n.data.result.text) || '').trim()
|
||
return (n.data.text || '').trim()
|
||
}
|
||
function isBatchSrc(n) {
|
||
// outKind === 'prompt'(2026-11-14):插件节点声明「我输出的是提示词」后也算批量来源。
|
||
// 加在这一处而不是三处:批量节点的候选来源、端口分布、组合来源行都走 isBatchSrc,
|
||
// 认了它,「一条样式 × N 个商品图」的批量出图才成立。
|
||
return !!n && (n.type === 'prompt' || n.type === 'textout' || n.type === 'combine' || n.type === 'llm' || n.type === 'relay' || (n.data && n.data.outKind === 'prompt')) && !!batchSrcText(n)
|
||
}
|
||
function batchPortTop(idx, total, h) {
|
||
const gap = Math.min(30, Math.max(20, (h - 90) / Math.max(1, total - 1)))
|
||
const first = (h - (total - 1) * gap) / 2
|
||
return Math.round(first + idx * gap)
|
||
}
|
||
function batchJoinedText(bd, sbase) {
|
||
const bextra = String((bd && bd.extra) || '').trim()
|
||
if (!bextra) return sbase
|
||
if (!sbase) return bextra
|
||
const bsep = (bd.sep && bd.sep.length) ? bd.sep : ([',', ',', '。', '!', '?', ';', '、'].includes(bextra.charAt(0)) ? '' : ' ')
|
||
const before = (bd && bd.extraPos) === 'before'
|
||
return before ? bextra + bsep + sbase : sbase + bsep + bextra
|
||
}
|
||
function combineEdgeTexts(inEdge, edgesArr, nodesArr) {
|
||
const n = nodesArr.find((x) => x.id === inEdge.from)
|
||
if (!n || n.type === 'ref') return []
|
||
if (n.type === 'relay') {
|
||
const texts = []
|
||
const relaySrcs = edgesArr.filter((x) => x.to === n.id).map((e) => nodesArr.find((x) => x.id === e.from)).filter(Boolean).slice().sort(compareCanvasTextOrder)
|
||
for (const src of relaySrcs) for (const t of combineInputTexts(src, edgesArr, nodesArr)) if (t && !texts.includes(t)) texts.push(t)
|
||
return texts
|
||
}
|
||
if (n.data && n.data.enabled === false) return []
|
||
if (n.type === 'batch') {
|
||
const bsrcs = edgesArr.filter((e) => e.to === n.id).map((e) => nodesArr.find((x) => x.id === e.from)).filter((x) => isBatchSrc(x)).slice().sort((a, b) => (a.y - b.y) || (a.x - b.x))
|
||
const s = inEdge.batchSrcId ? bsrcs.find((x) => x.id === inEdge.batchSrcId) : bsrcs[0]
|
||
if (!s || (s.data && s.data.enabled === false)) return []
|
||
const st = s ? batchJoinedText(n.data, batchSrcText(s)) : ''
|
||
return st ? [st] : []
|
||
}
|
||
return combineInputTexts(n, edgesArr, nodesArr)
|
||
}
|
||
function combineInputTexts(n, edgesArr, nodesArr) {
|
||
if (!n || (n.data && n.data.enabled === false)) return []
|
||
if (n.type === 'batch') {
|
||
const bd = n.data || {}
|
||
const bsrcs = edgesArr.filter((e) => e.to === n.id).map((e) => nodesArr.find((x) => x.id === e.from)).filter((x) => isBatchSrc(x) && !(x.data && x.data.enabled === false)).slice().sort((a, b) => (a.y - b.y) || (a.x - b.x))
|
||
return bsrcs.map((src) => batchJoinedText(bd, batchSrcText(src))).filter(Boolean)
|
||
}
|
||
if (n.type === 'relay') { const out = []; const relaySrcs = edgesArr.filter((x) => x.to === n.id).map((e) => nodesArr.find((x) => x.id === e.from)).filter(Boolean).slice().sort(compareCanvasTextOrder); for (const src of relaySrcs) for (const t0 of combineInputTexts(src, edgesArr, nodesArr)) if (t0 && !out.includes(t0)) out.push(t0); return out }
|
||
const t = batchSrcText(n)
|
||
if (n.type === 'textout') {
|
||
if (t) return [t]
|
||
const bEdge = edgesArr.filter((e) => e.to === n.id).map((e) => ({ e, b: nodesArr.find((x) => x.id === e.from) })).find((o) => o.b && o.b.type === 'batch')
|
||
if (bEdge) {
|
||
const bsrcs = edgesArr.filter((e) => e.to === bEdge.b.id).map((e) => nodesArr.find((x) => x.id === e.from)).filter((x) => isBatchSrc(x)).slice().sort((a, b2) => (a.y - b2.y) || (a.x - b2.x))
|
||
const target = bEdge.e.batchSrcId ? bsrcs.find((s) => s.id === bEdge.e.batchSrcId) : bsrcs[0]
|
||
const bt = target ? batchSrcText(target) : ''
|
||
return bt ? [bt] : []
|
||
}
|
||
}
|
||
return t ? [t] : []
|
||
}
|
||
|
||
global.IWBCanvasText = Object.freeze({ compareCanvasTextOrder, batchSrcText, isBatchSrc, batchPortTop, batchJoinedText, combineEdgeTexts, combineInputTexts })
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|