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
@@ -9,7 +9,7 @@
|
||||
var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
id: 'image-annotate',
|
||||
name: '标注 / 修图',
|
||||
version: '1.0.3',
|
||||
version: '1.0.4',
|
||||
apiVersion: 1,
|
||||
capabilities: ['commands', 'nodeActions', 'canvas', 'canvasWrite'],
|
||||
}
|
||||
@@ -1105,7 +1105,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
<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>
|
||||
@@ -2344,6 +2344,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
function bindGlobalListeners() {
|
||||
document.addEventListener("keydown", onKeydown, true);
|
||||
document.addEventListener("keyup", onKeyup, true);
|
||||
document.addEventListener("paste", onPaste, true);
|
||||
window.addEventListener("blur", () => {
|
||||
if (S.spaceDown) {
|
||||
S.spaceDown = false;
|
||||
@@ -2626,6 +2627,72 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
updateCursor();
|
||||
}
|
||||
}
|
||||
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;
|
||||
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;
|
||||
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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// image-annotate/plugin/editor/viewport.js
|
||||
function syncOverlayCanvas() {
|
||||
|
||||
Reference in new issue
Block a user