Verify (Gitea) / gate (build · unit · facts) (push) Failing after 42s
- src/board-transfer.js:导出命名(.iwb.json 双后缀、-2/-3)、导入校验、id 重映射(含 promptId 等引用)、生成结果剥离、图片引用解析/内联转资产 - src/board-transfer-actions.js:导出/导入到当前/导入为新画板的动作层(依赖注入) - 「更多」菜单三项(仅画布视图);导入为新画板自动切换、名称去重 - 单测 7 条;棘轮上调:app.js 9,564→9,598、Workbench 2,735→2,769(2026-12-06,装配依赖注入,逻辑在 src)
192 lines
8.2 KiB
JavaScript
192 lines
8.2 KiB
JavaScript
/* board-transfer.js —— 画板导出/导入/跨库复制(v3.26.266/267)
|
||
*
|
||
* 纯逻辑 + 注入式依赖:
|
||
* · 命名器(.iwb.json 双后缀、同名 -2/-3)
|
||
* · 导入校验(app 标记 / 节点数 / 边数)
|
||
* · id 重映射(节点/连线/分组 + data 里的 id 引用)
|
||
* · 生成结果剥离
|
||
* · 图片引用收集与解析(__blob / __file / dataURL;__idb/__cache 跨机不可解析)
|
||
* 文件读写与建板由调用方注入,本模块不碰 DOM / Tauri。
|
||
*/
|
||
;(function (global) {
|
||
'use strict'
|
||
const str = (v) => (v == null ? '' : String(v))
|
||
const LIMITS = { nodes: 5000, edges: 10000 }
|
||
|
||
function pad2(n) { return (n < 10 ? '0' : '') + n }
|
||
function stamp(now) {
|
||
const d = now instanceof Date ? now : new Date(now || Date.now())
|
||
return d.getFullYear() + pad2(d.getMonth() + 1) + pad2(d.getDate()) + '-' + pad2(d.getHours()) + pad2(d.getMinutes())
|
||
}
|
||
function sanitizeName(name) {
|
||
const s = str(name).trim().replace(/[\\/:*?"<>|]+/g, '_').replace(/\s+/g, ' ').slice(0, 120)
|
||
return s || '画板'
|
||
}
|
||
/** 画板导出文件名:<名称>-YYYYMMDD-HHMM.iwb.json */
|
||
function boardFileName(name, now) {
|
||
return sanitizeName(name) + '-' + stamp(now) + '.iwb.json'
|
||
}
|
||
/** 同名文件去重:在 .iwb.json 之前追加 -2/-3(不是 uniqueName 的最后一个点切分) */
|
||
function uniqueFileName(desired, existing) {
|
||
const used = new Set((existing || []).map((x) => str(x).toLowerCase()))
|
||
const name = str(desired) || '画板.iwb.json'
|
||
if (!used.has(name.toLowerCase())) return name
|
||
const suffix = '.iwb.json'
|
||
const stem = name.toLowerCase().endsWith(suffix) ? name.slice(0, -suffix.length) : name.replace(/\.[^.]*$/, '')
|
||
const ext = name.toLowerCase().endsWith(suffix) ? name.slice(-suffix.length) : (name.match(/\.[^.]*$/) || [''])[0]
|
||
for (let i = 2; i < 1000; i++) {
|
||
const cand = stem + '-' + i + ext
|
||
if (!used.has(cand.toLowerCase())) return cand
|
||
}
|
||
return stem + '-' + Date.now() + ext
|
||
}
|
||
|
||
function validateImport(obj) {
|
||
if (!obj || typeof obj !== 'object') return { ok: false, error: '文件内容不是有效的 JSON 对象' }
|
||
if (obj.app !== 'image-workbench') return { ok: false, error: '不是 Image Workbench 画板文件(缺少 app 标记)' }
|
||
if (!Array.isArray(obj.nodes)) return { ok: false, error: '文件缺少 nodes 数组' }
|
||
if (obj.nodes.length > LIMITS.nodes) return { ok: false, error: '节点数超过上限(' + LIMITS.nodes + ')' }
|
||
if (Array.isArray(obj.edges) && obj.edges.length > LIMITS.edges) return { ok: false, error: '连线数超过上限(' + LIMITS.edges + ')' }
|
||
if (!obj.nodes.length) return { ok: false, error: '文件里没有可导入的节点' }
|
||
return { ok: true }
|
||
}
|
||
|
||
/** 去掉生成结果(gen/out 的 result 里是源机的引用) */
|
||
function stripResults(board) {
|
||
const out = Object.assign({}, board)
|
||
out.nodes = (board.nodes || []).map((n) => {
|
||
if (!n || (n.type !== 'gen' && n.type !== 'out')) return n
|
||
if (!n.data || n.data.result === undefined) return n
|
||
const data = Object.assign({}, n.data)
|
||
delete data.result
|
||
return Object.assign({}, n, { data: data })
|
||
})
|
||
return out
|
||
}
|
||
|
||
/** id 重映射:节点 / 连线 / 分组 + data 中的 id 引用(如 promptId) */
|
||
function remapBoard(board, uid) {
|
||
const idMap = new Map()
|
||
const srcNodes = (board.nodes || []).filter((n) => n && n.id && n.type)
|
||
const nodes = srcNodes.map((n) => {
|
||
const id = uid('n')
|
||
idMap.set(n.id, id)
|
||
return Object.assign({}, n, { id: id })
|
||
})
|
||
// 只重写“已知的引用字段”(promptId / *Id / *Ids),避免把用户文本里恰好等于 id 的字符串改掉
|
||
const isIdField = (key) => key === 'promptId' || /Ids?$/.test(str(key))
|
||
const rewrite = (v, key) => {
|
||
if (Array.isArray(v)) return v.map((x) => rewrite(x, key))
|
||
if (v && typeof v === 'object') {
|
||
const o = {}
|
||
for (const k of Object.keys(v)) o[k] = rewrite(v[k], k)
|
||
return o
|
||
}
|
||
if (typeof v === 'string' && isIdField(key) && idMap.has(v)) return idMap.get(v)
|
||
return v
|
||
}
|
||
const withData = nodes.map((n) => Object.assign({}, n, { data: rewrite(n.data || {}, '') }))
|
||
const edges = (board.edges || [])
|
||
.filter((e) => e && idMap.has(e.from) && idMap.has(e.to))
|
||
.map((e) => {
|
||
const o = Object.assign({}, e, { id: uid('e'), from: idMap.get(e.from), to: idMap.get(e.to) })
|
||
if (o.batchSrcId) o.batchSrcId = idMap.get(o.batchSrcId) || undefined
|
||
return o
|
||
})
|
||
const groups = (board.groups || [])
|
||
.filter((g) => g && Array.isArray(g.memberIds))
|
||
.map((g) => Object.assign({}, g, { id: uid('g'), memberIds: g.memberIds.map((m) => idMap.get(m)).filter(Boolean) }))
|
||
.filter((g) => g.memberIds.length >= 2)
|
||
return { nodes: withData, edges: edges, groups: groups, idMap: idMap }
|
||
}
|
||
|
||
const isRefObj = (v) => !!(v && typeof v === 'object' && !Array.isArray(v) && (typeof v.__blob === 'string' || typeof v.__file === 'string' || typeof v.__idb === 'string' || typeof v.__cache === 'string'))
|
||
|
||
/** 收集图片引用(含 dataURL) */
|
||
function collectImageRefs(board) {
|
||
const refs = []
|
||
const walk = (obj, owner) => {
|
||
if (!obj || typeof obj !== 'object') return
|
||
if (isRefObj(obj)) { refs.push({ ref: obj, owner: owner }); return }
|
||
for (const k of Object.keys(obj)) {
|
||
const v = obj[k]
|
||
if (typeof v === 'string' && v.indexOf('data:image/') === 0) refs.push({ ref: v, owner: owner })
|
||
else if (v && typeof v === 'object') walk(v, owner)
|
||
}
|
||
}
|
||
walk(board, null)
|
||
return refs
|
||
}
|
||
|
||
/**
|
||
* 解析画板里的图片引用:resolveOne(ref) -> Promise<dataUrl|null>
|
||
* 返回 { board, missing };解析不到的引用保持原样并计数
|
||
*/
|
||
async function resolveBoardImages(board, resolveOne) {
|
||
let missing = 0
|
||
const jobs = []
|
||
const walk = (obj) => {
|
||
if (!obj || typeof obj !== 'object') return
|
||
for (const k of Object.keys(obj)) {
|
||
const v = obj[k]
|
||
if (typeof v === 'string' && v.indexOf('data:image/') === 0) continue
|
||
if (isRefObj(v)) { jobs.push({ obj: obj, key: k, ref: v }); continue }
|
||
if (v && typeof v === 'object') walk(v)
|
||
}
|
||
}
|
||
walk(board)
|
||
for (const j of jobs) {
|
||
let url = null
|
||
try { url = await resolveOne(j.ref) } catch (e) { url = null }
|
||
if (url) j.obj[j.key] = url
|
||
else missing++
|
||
}
|
||
return { board: board, missing: missing }
|
||
}
|
||
|
||
function pluginNodeCount(board) {
|
||
return (board.nodes || []).filter((n) => n && String(n.type).indexOf('plugin:') === 0).length
|
||
}
|
||
|
||
/** 把画板里内联的 data:image 转成资产引用(writeImage 注入;失败保留原值并计数) */
|
||
async function convertInlineImages(board, writeImage, opts) {
|
||
const o = opts || {}
|
||
const limit = Math.max(1, Number(o.concurrency) || 3)
|
||
const jobs = []
|
||
const walk = (obj) => {
|
||
if (!obj || typeof obj !== 'object') return
|
||
for (const k of Object.keys(obj)) {
|
||
const v = obj[k]
|
||
if (typeof v === 'string' && v.indexOf('data:image/') === 0) jobs.push({ obj: obj, key: k, value: v })
|
||
else if (v && typeof v === 'object') walk(v)
|
||
}
|
||
}
|
||
walk(board)
|
||
let converted = 0, failed = 0, i = 0
|
||
const worker = async () => {
|
||
while (i < jobs.length) {
|
||
const j = jobs[i++]
|
||
let ref = null
|
||
try { ref = await writeImage(j.value) } catch (e) { ref = null }
|
||
if (ref) { j.obj[j.key] = ref; converted++ } else failed++
|
||
}
|
||
}
|
||
await Promise.all(new Array(Math.min(limit, jobs.length || 1)).fill(0).map(worker))
|
||
return { board: board, converted: converted, failed: failed }
|
||
}
|
||
|
||
function summarize(board) {
|
||
const nodes = (board.nodes || []).length
|
||
const edges = (board.edges || []).length
|
||
return { nodes: nodes, edges: edges, plugins: pluginNodeCount(board) }
|
||
}
|
||
|
||
const host = global.IWBStorageFile || (global.IWBStorageFile = {})
|
||
host.boardTransfer = {
|
||
LIMITS, stamp, sanitizeName, boardFileName, uniqueFileName,
|
||
validateImport, stripResults, remapBoard, collectImageRefs, resolveBoardImages,
|
||
pluginNodeCount, summarize, convertInlineImages,
|
||
}
|
||
})(typeof window !== 'undefined' ? window : globalThis)
|
||
|