'use strict' // 统一剪贴板的序列化(src/clipboard-graph.js)—— 独立文件 // 背景(用户 2026-11-11 二次裁定):复制节点要能直接用 Ctrl+V 贴回来,不再要求 Ctrl+Shift+V。 // 做法是把节点组写进系统剪贴板的 text/html(带 iwb:graph 标记)+ text/plain(人话摘要)。 // 这个文件守的是「标记能不能原样往返」—— 一旦丢字/丢节点,Ctrl+V 就会贴出错节点。 const fs = require('fs') const path = require('path') const root = path.join(__dirname, '..') const SRC = fs.readFileSync(path.join(root, 'src/clipboard-graph.js'), 'utf8') const box = {} new Function('window', 'globalThis', SRC)(box, box) const G = box.IWBClipboardGraph let pass = 0, fail = 0 const ok = (name, cond) => { if (cond) { pass++; console.log(' ✅ ' + name) } else { fail++; console.log(' ❌ ' + name) } } const eq = (name, got, want) => ok(name + '(' + JSON.stringify(got) + ' === ' + JSON.stringify(want) + ')', got === want) const mkClip = (over) => Object.assign({ app: 'image-workbench', type: 'iwb-clipboard', at: 1760000000000, nodes: [ { id: 'n1', type: 'prompt', x: 0, y: 0, data: { text: '一只赛博朋克风格的猫', title: '' } }, { id: 'n2', type: 'ref', x: 300, y: 0, data: { url: '', data: null, name: 'ref.png', localName: null } }, ], edges: [{ id: 'e1', from: 'n1', to: 'n2' }], groups: [{ id: 'g1', name: '分组 1', memberIds: ['n1', 'n2'] }], }, over || {}) // ---- ① 导出 ---- ok('模块导出完整', !!G && typeof G.htmlFor === 'function' && typeof G.decode === 'function' && typeof G.write === 'function') eq('标记常量', G.MARK, 'iwb:graph:v1:') // ---- ② 往返 ---- { const c = mkClip() const html = G.htmlFor(c) ok('htmlFor 含标记', html.indexOf('iwb:graph:v1:') >= 0) const back = G.decode(html) ok('decode 能解回', !!back) eq('节点数一致', back && back.nodes.length, 2) eq('连线数一致', back && back.edges.length, 1) eq('分组数一致', back && back.groups.length, 1) eq('中文提示词不丢字', back && back.nodes[0].data.text, '一只赛博朋克风格的猫') eq('分组 id 保留', back && back.groups[0].memberIds.join(','), 'n1,n2') eq('at 时间戳保留', back && back.at, 1760000000000) } // ---- ③ 中文必须能编码:btoa 只吃 latin1,直接 btoa(JSON) 会抛 InvalidCharacterError ---- { let threw = null let html = '' try { html = G.htmlFor(mkClip()) } catch (e) { threw = e } ok('中文不会让编码抛异常(btoa latin1 坑)', threw === null) ok('中文往返后仍是中文', (G.decode(html) || { nodes: [{ data: {} }] }).nodes[0].data.text === '一只赛博朋克风格的猫') } // ---- ④ Chrome 读回来会把 fragment 包成完整文档:decode 不能假设以注释开头 ---- { const html = G.htmlFor(mkClip()) const wrapped = '' + html + '' eq('被 包裹后仍能解', (G.decode(wrapped) || {}).nodes.length, 2) eq('前面有别的节点也能解', (G.decode('
无关内容
' + html) || {}).nodes.length, 2) } // ---- ⑤ 坏输入一律返回 null,绝不抛 ---- eq('没有标记 → null', G.decode('
普通网页内容
'), null) eq('空字符串 → null', G.decode(''), null) eq('null → null', G.decode(null), null) eq('标记后跟垃圾 → null', G.decode(''), null) eq('base64 但 JSON 坏 → null', G.decode(''), null) eq('nodes 为空数组 → null', G.decode(''), null) eq('nodes 不是数组 → null', G.decode(''), null) eq('标记没闭合(被截断)仍尽力解出', (G.decode('