Files
image-workbench-desktop/src/board-transfer-actions.js
T
qiqi 35495df424
Verify (Gitea) / gate (build · unit · facts) (push) Failing after 47s
fix(vault): 分类换行 + 批量复制只成功一个 + 切板缩放跳动(v3.26.268)
- 分类胶囊多行换行、去横向滚动;flex-shrink:0 防止分类栏被列表挤扁(空胶囊)
- 批量复制改为 silent 建板 + 单次 persist:避免同一 rev 连续写导致第二次索引写被丢弃
- 复制期间不再逐个切换画板;单个复制切换用 loadWsInto(noFit) 避免视口重置跳动
- 新增 copyMany 行为单测(silent×N + persist×1);棘轮:app 9,626→9,636、Workbench 2,796→2,806(2026-12-06)
2026-12-06 18:34:38 +08:00

101 lines
4.9 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.
/* board-transfer-actions.js —— 画板导出/导入的动作层(v3.26.266)
*
* 依赖全部由 app.js 注入(模板/图片解析/建画板/保存/弹窗都在宿主)。
* 本模块只负责:命名、校验、转换节奏、提示文案。
*/
;(function (global) {
'use strict'
const T = () => (global.IWBStorageFile && global.IWBStorageFile.boardTransfer) || null
function create(deps) {
const d = deps || {}
const toast = (m, t) => { try { if (d.toast) d.toast(String(m || ''), t) } catch (e) { /* ignore */ } }
const fail = (m) => toast(m, 'err')
async function exportCurrent () {
const T0 = T()
if (!T0) return { ok: false }
let board = null
try { board = d.getBoard && d.getBoard() } catch (e) { board = null }
if (!board) { fail('没有打开的画板'); return { ok: false } }
toast('正在导出画板…', 'ok')
let prepared = null
try { prepared = await d.buildExport(board) } catch (e) { fail('导出失败:' + String((e && e.message) || e)); return { ok: false } }
const doc = Object.assign({
app: 'image-workbench', version: 2, schemaVersion: d.schemaVersion || 1,
exportedAt: new Date().toISOString(), name: board.name || '画板',
}, prepared || {})
const base = T0.boardFileName(board.name || '画板')
let name = base
try { const existing = await d.listExportNames(); name = T0.uniqueFileName(base, existing || []) } catch (e) { /* 用原名 */ }
try {
const blob = new Blob([JSON.stringify(doc, null, 1)], { type: 'application/json' })
await d.saveBlob(blob, name)
} catch (e) { fail('保存失败:' + String((e && e.message) || e)); return { ok: false } }
toast('已导出:' + name, 'ok')
return { ok: true, name: name }
}
async function readAndValidate () {
const T0 = T()
const file = await d.pickFile()
if (!file) return null
const text = await d.readFile(file)
if (!text) throw new Error('读取文件失败')
let obj = null
try { obj = JSON.parse(String(text)) } catch (e) { throw new Error('不是有效的 JSON 文件') }
const v = T0.validateImport(obj)
if (!v.ok) throw new Error(v.error)
if (typeof d.migrate === 'function') {
const m = d.migrate(obj)
if (!m || m.ok === false) throw new Error((m && m.error) || '画板版本不受支持')
obj = m.value || obj
}
return { obj: obj, T0: T0, fileName: String(file.name || '') }
}
async function importToCurrent () {
try {
const got = await readAndValidate()
if (!got) return { ok: false, code: 'cancel' }
toast('正在导入到当前画板…', 'ok')
const conv = await got.T0.convertInlineImages(got.obj, d.writeImage)
if (typeof d.flush === 'function') { try { await d.flush() } catch (e) { /* ignore */ } }
const blob = new Blob([JSON.stringify(got.obj)], { type: 'application/json' })
await d.appendToCurrent(blob, got.obj.name || got.fileName)
toast('已导入到当前画板' + (conv.failed ? '(' + conv.failed + ' 张图片未能转换)' : ''), conv.failed ? 'err' : 'ok')
return { ok: true, converted: conv.converted, failed: conv.failed }
} catch (e) {
fail('导入失败:' + String((e && e.message) || e))
return { ok: false, error: String((e && e.message) || e) }
}
}
async function importAsNew () {
try {
const got = await readAndValidate()
if (!got) return { ok: false, code: 'cancel' }
toast('正在导入为新画板…', 'ok')
const conv = await got.T0.convertInlineImages(got.obj, d.writeImage)
const r = got.T0.remapBoard(got.obj, d.uid)
const base = String(got.obj.name || got.fileName || '导入的画板').replace(/\.iwb\.json$/i, '').trim() || '导入的画板'
const s = got.T0.summarize({ nodes: r.nodes, edges: r.edges })
if (typeof d.flush === 'function') { try { await d.flush() } catch (e) { /* ignore */ } }
const created = await d.createBoard({ name: base, nodes: r.nodes, edges: r.edges, groups: r.groups, category: got.obj.category || '', starred: !!got.obj.starred })
const id = (created && created.id) || created
toast('已导入为新画板「' + base + '」· ' + s.nodes + ' 节点' + (conv.failed ? ' · ' + conv.failed + ' 张图片未能转换' : ''), conv.failed ? 'err' : 'ok')
return { ok: true, id: id, converted: conv.converted, failed: conv.failed }
} catch (e) {
fail('导入失败:' + String((e && e.message) || e))
return { ok: false, error: String((e && e.message) || e) }
}
}
return { exportCurrent, importToCurrent, importAsNew }
}
const host = global.IWBStorageFile || (global.IWBStorageFile = {})
host.boardTransferActions = { create }
})(typeof window !== 'undefined' ? window : globalThis)