Files
image-workbench-desktop/tests/clipboard-copy-test.js
T

67 lines
4.2 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.
'use strict'
// src/clipboard-copy.js:hasTextSelection / fieldCopyIntent
//
// 背景(用户 2026-11-11 反馈「复制本地图片到画布后,复制粘贴就不起作用了」):
// 根因是焦点落在节点内的输入框(参考图节点的图片链接框 / 提示词 textarea)时,
// onKey 第一行无条件 return —— 原生复制在"无选区"时本来就拿不到任何东西,
// 于是 ⌘C、⌘V 双双静默失效。这里守的是"什么情况下该由画布接管 ⌘C/⌘X"。
const fs = require('fs')
const path = require('path')
const root = path.join(__dirname, '..')
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)
// 可切换的 getSelection:fieldCopyIntent 在 contenteditable 分支要看 window.getSelection()
let selection = null
const win = { getSelection: () => selection, addEventListener() {}, removeEventListener() {} }
new Function('window', 'globalThis', fs.readFileSync(path.join(root, 'src/clipboard-copy.js'), 'utf8'))(win, win)
const C = win.IWBClipboardCopy
ok('模块加载并导出 fieldCopyIntent', !!C && typeof C.fieldCopyIntent === 'function')
const key = (k, mod) => Object.assign({ key: k, ctrlKey: false, metaKey: false, altKey: false, shiftKey: false }, mod || {})
const cmd = (k, extra) => key(k, Object.assign({ metaKey: true }, extra || {}))
const ctrl = (k, extra) => key(k, Object.assign({ ctrlKey: true }, extra || {}))
// 假输入框:selectionStart/End 决定"有没有选中文字"
const field = (a, b, extra) => Object.assign({ tagName: 'INPUT', selectionStart: a, selectionEnd: b, closest: () => null }, extra || {})
// ---- hasTextSelection ----
{
selection = { isCollapsed: false, toString: () => '选中的文字' }
ok('画布选中文字 → hasTextSelection 为真', C.hasTextSelection() === true)
selection = { isCollapsed: true, toString: () => '' }
eq('画布没有选区 → false', C.hasTextSelection(), false)
selection = { isCollapsed: false, toString: () => ' ' }
eq('选区只有空白 → false(不抢复制)', C.hasTextSelection(), false)
const f = field(2, 7)
ok('输入框 2~7 → 有选区', C.hasTextSelection(f) === true)
eq('输入框 5~5(光标无选区)→ false', C.hasTextSelection(field(5, 5)), false)
}
// ---- fieldCopyIntent:无选区时 ⌘C/⌘X 应由画布接管 ----
{
eq('⌘C + 空输入框 → 接管(复制节点)', C.fieldCopyIntent(cmd('c'), field(0, 0)), true)
eq('Ctrl+C + 空输入框 → 接管', C.fieldCopyIntent(ctrl('c'), field(3, 3)), true)
eq('⌘X + 空输入框 → 接管(剪切节点)', C.fieldCopyIntent(cmd('x'), field(0, 0)), true)
eq('⌘C + 输入框里有选区 → 不接管(用户要复制那段文字)', C.fieldCopyIntent(cmd('c'), field(0, 4)), false)
eq('⌘V + 空输入框 → 不接管(粘贴永远交给原生/粘贴模块)', C.fieldCopyIntent(cmd('v'), field(0, 0)), false)
eq('⌘A + 空输入框 → 不接管', C.fieldCopyIntent(cmd('a'), field(0, 0)), false)
eq('⌘⇧C + 空输入框 → 不接管(避开 DevTools 等组合)', C.fieldCopyIntent(cmd('c', { shiftKey: true }), field(0, 0)), false)
eq('⌘⌥C + 空输入框 → 不接管', C.fieldCopyIntent(cmd('c', { altKey: true }), field(0, 0)), false)
eq('无修饰键的 c → 不接管', C.fieldCopyIntent(key('c'), field(0, 0)), false)
eq('F2 之类别的键 → 不接管', C.fieldCopyIntent(key('F2'), field(0, 0)), false)
eq('没有 event → 不接管', C.fieldCopyIntent(null, field(0, 0)), false)
eq('没有 target → 不接管', C.fieldCopyIntent(cmd('c'), null), false)
}
// ---- 放大编辑浮层 / 全屏遮罩里永远不抢(那是在浮层里编辑文本)----
{
const inOverlay = field(0, 0, { closest: (sel) => (/iwb-md-expand|iwb-fullmask/.test(sel) ? { className: 'iwb-md-expand' } : null) })
eq('放大编辑浮层里 ⌘C → 不接管', C.fieldCopyIntent(cmd('c'), inOverlay), false)
eq('放大编辑浮层里 ⌘X → 不接管', C.fieldCopyIntent(cmd('x'), inOverlay), false)
}
console.log('\n clipboard-copy: ' + pass + ' passed, ' + fail + ' failed')
if (fail) process.exit(1)