'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('/, '') // 模拟 WebKit 把注释剥掉 ok('注释被剥掉后属性仍能解回', !!G.decode(attrOnly)) eq('属性解回的节点数一致', (G.decode(attrOnly) || {}).nodes ? G.decode(attrOnly).nodes.length : -1, 2) eq('属性用单引号包裹也能解', !!G.decode(html.replace(/data-iwb-graph="[^"]*"/, (m) => m.replace(/"/g, "'"))), true) eq('没有标记 → null', G.decode('
普通网页
'), null) } { const c = mkClip() const dt = { data: {}, setData(k, v) { this.data[k] = v } } const ev = { clipboardData: dt, defaultPrevented: false, preventDefault() { ev.defaultPrevented = true } } eq('writeToEvent 返回 true', G.writeToEvent(ev, c), true) ok('写了 text/html(带标记)', String(dt.data['text/html']).indexOf('iwb:graph:v1:') >= 0) eq('写了 text/plain(人话摘要,不是裸 JSON)', dt.data['text/plain'], G.plainFor(c)) eq('writeToEvent 不碰 preventDefault(交给调用方)', ev.defaultPrevented, false) eq('没有 clipboardData → false(不抛)', G.writeToEvent({}, c), false) } { // bindCopy:注册 copy 监听,事件里同步写剪贴板 + 落内部剪贴板 const b = { h: {}, addEventListener(t, fn) { (b.h[t] = b.h[t] || []).push(fn) }, removeEventListener(t, fn) { const a = b.h[t] || []; const i = a.indexOf(fn); if (i >= 0) a.splice(i, 1) } } new Function('window', 'globalThis', SRC)(b, b) let staged = null let notice = null let sysOk = null const ctx = { ids: () => ['n1', 'n2'], clipboardOf: () => mkClip(), stageClip: (c) => { staged = c; notice = { type: 'ok', text: '已复制 ' + c.nodes.length + ' 个节点' }; return true }, setNotice: (n) => { notice = n }, markSys: (ok) => { sysOk = ok }, hasTextSelection: () => false, } let current = ctx b.IWBClipboardGraph.bindCopy(() => current) const fire = (ev) => (b.h.copy || []).forEach((fn) => fn(ev)) const dt = { data: {}, setData(k, v) { this.data[k] = v } } const ev = { target: {}, clipboardData: dt, defaultPrevented: false, preventDefault() { ev.defaultPrevented = true } } fire(ev) ok('copy 事件:同步写进 clipboardData', String(dt.data['text/html']).indexOf('iwb:graph:v1:') >= 0) eq('copy 事件:preventDefault(不覆盖用户其它剪贴板内容)', ev.defaultPrevented, true) eq('copy 事件:内部剪贴板也落了一份', !!staged, true) eq('copy 事件:标记系统剪贴板可写', sysOk, true) ok('copy 事件:提示已复制', !!notice && String(notice.text).indexOf('已复制') >= 0) // 有文本选区 → 让原生复制文字(不许抢) staged = null current = Object.assign({}, ctx, { hasTextSelection: () => true }) const ev2 = { target: {}, clipboardData: { setData() { throw new Error('不该写剪贴板') } }, defaultPrevented: false, preventDefault() { ev2.defaultPrevented = true } } fire(ev2) eq('有文本选区 → 不写剪贴板(原生复制文字)', staged, null) eq('有文本选区 → 不 preventDefault', ev2.defaultPrevented, false) // 没选中节点 → 说一句,别静默 notice = null current = Object.assign({}, ctx, { ids: () => [] }) fire({ target: {}, clipboardData: { setData() {} }, defaultPrevented: false, preventDefault() {} }) ok('没选中节点 → 提示没有可复制的内容', !!notice && String(notice.text).indexOf('没有可复制') >= 0) // 解绑后必须真的摘掉监听(否则回到首页按 ⌘C 会去复制已卸载那份 refs) b.IWBClipboardGraph.unbindCopy() staged = null current = ctx fire({ target: {}, clipboardData: { setData() { staged = 'wrote' } }, defaultPrevented: false, preventDefault() {} }) eq('unbindCopy 之后 copy 事件不再被处理', staged, null) // 重新绑定可以恢复(Workbench 重挂载) b.IWBClipboardGraph.bindCopy(() => current) fire({ target: {}, clipboardData: { setData() {} }, defaultPrevented: false, preventDefault() {} }) eq('重新 bindCopy 后恢复处理', !!staged, true) } console.log('\n' + (fail ? '❌' : '✅') + ' clipboard-graph: ' + pass + ' passed, ' + fail + ' failed') if (fail) process.exit(1) })()