feat(image-annotate): 编辑器内 ⌘/Ctrl+V 粘贴剪贴板图片为新图层(1.0.4)
- 粘贴优先级:剪贴板图片文件 > text/html 的 <img src> > text/plain 图片 URL/data URL - 命中后进入既有「图片放置」模式(拖动/缩放,点外部或回车确认,Esc 取消) - URL 一律 fetch→blob→dataURL(跨域图直接挂 <img> 会污染画布,导出时 toBlob 抛 SecurityError); data: URL 反而不能 fetch(CSP connect-src 无 data:),直接走 loadImage - 命中即 stopImmediatePropagation:宿主在 window 气泡阶段也监听 paste(建 ref 节点+下载), 不掐断会一次粘贴双响应,且宿主抢先建节点会破坏后续「覆盖」写回(e2e 实测抓到) - 正在文字输入框打字时不抢 text/html 与 text/plain 的粘贴 - e2e 新增合成 ClipboardEvent 粘贴用例;修过期 SRI 断言(内容寻址名带哈希)
This commit is contained in:
1 parent
0cdadc3a89
commit
5f9303986a
6 files changed
+179
-11
No files matched your search
@@ -69,7 +69,7 @@ import { bindEvents } from './events.js'
|
||||
<div class="iwb-editor-layer-panel-header">
|
||||
<span class="iwb-editor-layer-panel-title">图层</span>
|
||||
<div class="iwb-editor-layer-add-group">
|
||||
<button class="iwb-editor-layer-add-btn" data-action="addBitmapLayer" title="导入图片为新图层"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="9" cy="9" r="2"/><path d="m21 15-3.1-3.1a2 2 0 0 0-2.8 0L6 21"/><line x1="19" y1="8" x2="19" y2="14"/><line x1="22" y1="11" x2="16" y2="11"/></svg></button>
|
||||
<button class="iwb-editor-layer-add-btn" data-action="addBitmapLayer" title="导入图片为新图层(也可直接 ⌘/Ctrl+V 粘贴剪贴板图片)"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><circle cx="9" cy="9" r="2"/><path d="m21 15-3.1-3.1a2 2 0 0 0-2.8 0L6 21"/><line x1="19" y1="8" x2="19" y2="14"/><line x1="22" y1="11" x2="16" y2="11"/></svg></button>
|
||||
<button class="iwb-editor-layer-add-btn" data-action="addBlankLayer" title="新建空白图层"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="3" width="18" height="18" rx="2"/><line x1="12" y1="8" x2="12" y2="16"/><line x1="8" y1="12" x2="16" y2="12"/></svg></button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1108,11 +1108,13 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
})
|
||||
}
|
||||
|
||||
/** 全局监听:键盘快捷键、窗口失焦、modal 内阻止冒泡 */
|
||||
/** 全局监听:键盘快捷键、窗口失焦、剪贴板粘贴、modal 内阻止冒泡 */
|
||||
function bindGlobalListeners() {
|
||||
// 键盘快捷键
|
||||
document.addEventListener('keydown', onKeydown, true)
|
||||
document.addEventListener('keyup', onKeyup, true)
|
||||
// 剪贴板粘贴(⌘/Ctrl+V):编辑器打开时把剪贴板里的图片贴为新图层
|
||||
document.addEventListener('paste', onPaste, true)
|
||||
|
||||
// 窗口失焦
|
||||
window.addEventListener('blur', () => {
|
||||
@@ -1355,3 +1357,72 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
updateCursor()
|
||||
}
|
||||
}
|
||||
|
||||
// ============ 剪贴板粘贴(⌘/Ctrl+V 贴为新图层)============
|
||||
/**
|
||||
* 优先级:剪贴板里的图片文件(截图 / 复制图片)> text/html 里的 <img src>(网页里复制图片的形态)
|
||||
* > text/plain 的图片 URL。命中即 preventDefault,进入「图片放置」模式 ——
|
||||
* 与「导入图片为新图层」完全同一流程:拖动 / 缩放调好后点外部或回车确认,Esc 取消。
|
||||
* 两个坑(都实测过):
|
||||
* 1) URL 不能直接 new Image(url):跨域图会污染所有画布,导出时 toBlob 抛 SecurityError
|
||||
* (表现是「图片跨域无法导出」)⇒ 必须 fetch→blob→dataURL(CSP connect-src 放行 https:);
|
||||
* 反过来 data: URL 不能 fetch(connect-src 没有 data:,会被 CSP 拦),要直接走 loadImage。
|
||||
* 2) 正在输入文字(isTyping)时不抢 text/html 与 text/plain 的粘贴 —— 用户可能就是想往文字输入框里贴 URL。
|
||||
*/
|
||||
function onPaste(e) {
|
||||
if (!S.open) return
|
||||
const cd = e.clipboardData
|
||||
if (!cd) return
|
||||
// ① 图片文件(截图 / 右键复制图片)
|
||||
const items = cd.items || []
|
||||
for (const item of items) {
|
||||
if (item.kind === 'file' && /^image\//.test(item.type)) {
|
||||
const file = item.getAsFile()
|
||||
if (!file) continue
|
||||
// 宿主在 window 气泡阶段也监听 paste(会把图片建成 ref 节点 + 触发下载)——
|
||||
// 编辑器打开时这次粘贴归编辑器,必须掐断后续传播,否则一次粘贴出两层表现
|
||||
e.preventDefault()
|
||||
e.stopImmediatePropagation()
|
||||
const reader = new FileReader()
|
||||
reader.onload = async () => {
|
||||
try { addBitmapLayer(await loadImage(reader.result)) }
|
||||
catch (err) { console.error('[IWB编辑器] 粘贴图层失败:', err) }
|
||||
}
|
||||
reader.readAsDataURL(file)
|
||||
return
|
||||
}
|
||||
}
|
||||
if (isTyping(e)) return
|
||||
// ② 网页复制的图片:text/html 里带 <img src="...">
|
||||
const html = typeof cd.getData === 'function' ? (cd.getData('text/html') || '') : ''
|
||||
if (html) {
|
||||
const m = /<img[^>]*\ssrc\s*=\s*["']([^"']+)["']/i.exec(html)
|
||||
if (m) { e.preventDefault(); e.stopImmediatePropagation(); pasteImageFromUrl(m[1]); return }
|
||||
}
|
||||
// ③ 纯文本:图片链接 / data URL
|
||||
const text = (typeof cd.getData === 'function' ? (cd.getData('text/plain') || '') : '').trim()
|
||||
if (/^(https?:\/\/\S+|data:image\/\S+)$/i.test(text)) { e.preventDefault(); e.stopImmediatePropagation(); pasteImageFromUrl(text); return }
|
||||
}
|
||||
|
||||
/** 把 URL / data URL 的图片取回并贴为新图层;失败时给出可见提示(console + alert,编辑器没有 toast) */
|
||||
async function pasteImageFromUrl(url) {
|
||||
try {
|
||||
let dataUrl = url
|
||||
if (!/^data:image\//i.test(url)) {
|
||||
const resp = await fetch(url)
|
||||
if (!resp.ok) throw new Error('HTTP ' + resp.status)
|
||||
const blob = await resp.blob()
|
||||
if (!/^image\//.test(blob.type)) throw new Error('链接不是图片(' + (blob.type || '未知类型') + ')')
|
||||
dataUrl = await new Promise((resolve, reject) => {
|
||||
const reader = new FileReader()
|
||||
reader.onload = () => resolve(reader.result)
|
||||
reader.onerror = () => reject(new Error('图片读取失败'))
|
||||
reader.readAsDataURL(blob)
|
||||
})
|
||||
}
|
||||
addBitmapLayer(await loadImage(dataUrl))
|
||||
} catch (err) {
|
||||
console.error('[IWB编辑器] 粘贴图片链接失败:', err)
|
||||
try { window.alert('粘贴失败:' + ((err && err.message) || '无法读取剪贴板里的图片链接')) } catch (e) { /* 忽略 */ }
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user