Files
image-workbench-desktop/src/canvas-normalize.js
T

68 lines
3.7 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 Workbench() 收编,P2-9 第一片)
*
* 纯逻辑:节点列表规范化(含 ref 旧版 images[] 迁移、按类型补默认宽度)、边/组过滤、旧任务 id 迁移。
* 依赖 NODE_H / DEFAULTS(节点的存在判定与默认数据)经 create({ NODE_H, DEFAULTS }) 注入,
* 不引用 app.js 的任何全局;宿主只经 window.IWBCanvasNormalize 使用。
*/
;(function (global) {
'use strict'
const create = (deps) => {
const D = deps || {}
const NODE_H = D.NODE_H || {}
const DEFAULTS = D.DEFAULTS || {}
const hasTypeRaw = typeof D.hasType === 'function' ? D.hasType : ((t) => !!(NODE_H[t] || DEFAULTS[t]))
// 2026-11-14:插件类型(`plugin:<插件id>:<变体id>`)在**冷启动这一刻**几乎一定还没注册 —— 插件脚本是异步下载的。
// 旧逻辑 hasTypeRaw 为 false ⇒ 归成 `plugin`,把变体身份抹掉并随存档写回(插件随后注册也认不回来:
// 样式节点会永远退化成「标题 + 一个 textarea」)。这里按**命名规则**认它,注册晚到的由渲染层兜底。
const PLUGIN_TYPE_RE = /^plugin:[a-z0-9-]+:[a-z0-9-]+$/
const looksLikePluginType = (t) => PLUGIN_TYPE_RE.test(String(t || ''))
// F3(P2-11b):未知类型**永不丢弃**(旧实现 `.filter(NODE_H[n.type])` 会静默丢节点 = 数据丢失)。
// 未知类型但带 data.variant ⇒ 归为插件节点(保留变体);否则保留为 `_unknown`(带 __raw 兜底)。
const normalizeNodes = (arr) => (arr || []).filter((n) => n && n.type).map((n) => {
const isPluginType = looksLikePluginType(n.type)
const known = isPluginType || hasTypeRaw(n.type)
const type = known ? n.type : ((n.data && n.data.variant) ? 'plugin' : '_unknown')
const data = { ...(DEFAULTS[type] || {}), ...(n.data || {}) }
if (type === '_unknown') data.__raw = true
if (type === 'ref') {
if (Array.isArray(data.images) && data.images.length) {
const im = data.images[0]
data.url = im.url || ''; data.data = im.data || null; data.name = im.name || ''
}
if (!data.url && !data.data && !data.localName) data.localName = null
delete data.images
delete data.urlInput
}
const size = n.w ? {} : (type === 'layer' ? { w: 520 } : (type === 'gen' ? { w: 480 } : (type === 'out' ? { w: 420 } : {})))
return { ...n, type, ...size, data }
})
const normalizeEdges = (arr, nodeIds) => (arr || []).filter((e) => e && e.from && e.to && (!nodeIds || (nodeIds.has(e.from) && nodeIds.has(e.to))))
const normalizeGroups = (arr) => (arr || []).filter((gr) => gr && Array.isArray(gr.memberIds))
// 旧任务状态迁移:批量边任务 id 唯一化后,把 gen 的 taskActive/taskVariants 中旧「来源 id」状态复制到「来源@批量节点」
const migrateGenTaskIds = (ns, es) => {
let migrated = false
for (const g of ns) {
if (g.type !== 'gen') continue
const ta = g.data.taskActive, tv = g.data.taskVariants
if ((!ta || !Object.keys(ta).length) && (!tv || !Object.keys(tv).length)) continue
for (const be of es) {
if (be.to !== g.id || !be.batchSrcId) continue
const sid = be.batchSrcId, tid = sid + '@' + be.from
if (ta && ta[sid] !== undefined && ta[tid] === undefined) { ta[tid] = ta[sid]; migrated = true }
if (tv && tv[sid] !== undefined && tv[tid] === undefined) { tv[tid] = tv[sid]; migrated = true }
}
}
return migrated
}
return { normalizeNodes, normalizeEdges, normalizeGroups, migrateGenTaskIds }
}
global.IWBCanvasNormalize = Object.freeze({ create })
})(typeof window !== 'undefined' ? window : globalThis)