Files
iwb-plugins-desktop/image-annotate/plugin/editor/dom.js
T

401 lines
33 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.
/* dom.js - 编辑器 DOM 构建与图片加载
* 节:DOM 创建、图片加载。
* createEl 负责创建遮罩根节点并写入 runtime.el,随后把各 canvas/面板
* 引用挂到 S 上;open() 依赖它保证 el 只创建一次。
*/
'use strict'
import { S, runtime } from './state.js'
import { bindEvents } from './events.js'
// ============ 调色面板(两处共用) ============
/**
* v1.16:调色面板从「只能弹层」变成「右栏常驻 + 弹层保留」,两处用同一份 DOM。
*
* 为什么抽成函数而不是复制两段 HTML:这套控件有 4 组联动(tab 高亮、SV/色相指示点、
* HEX 回填、常用色/最近色的 active 圈),复制两段 = 以后每加一种颜色语义都要改两遍,
* 漏一处就是「一边改了另一边不变」。同一份结构 + 按 `[data-color-surface]` 遍历,
* 才可能保证一致(events.js#updateColorUI 就是按 surface 逐个刷的)。
*
* 布局差异全部交给 CSS:弹层是横排一行,右栏窄,改成竖排(picker 全宽、色块自动换行)。
*/
const COMMON_COLORS = [
'#ff4444', '#ff8a00', '#ffd400', '#4cd964', '#00bfa5', '#4488ff',
'#aa44ff', '#ff66aa', '#ffffff', '#d0d0d0', '#808080', '#000000'
]
function colorSurfaceHtml() {
return `
<div class="iwb-color-pop-head">
<button type="button" class="iwb-color-pop-tab active" data-color-slot="stroke" title="正在调描边色(形状描边 / 画笔 / 文字色)"><i></i><span class="iwb-color-pop-tab-label">描边</span></button>
<button type="button" class="iwb-color-pop-tab" data-color-slot="fill" title="正在调填充色(形状内部颜色 / 文字描边色)"><i></i><span class="iwb-color-pop-tab-label">填充</span></button>
<button type="button" class="iwb-color-pop-tab" data-color-slot="bg" hidden title="正在调文本框底色(垫在文字下面)"><i></i><span class="iwb-color-pop-tab-label">底色</span></button>
<button type="button" class="iwb-color-pop-none" data-color-none="1" title="无填充:形状内部透明,只留描边"><span></span>无</button>
<!-- 钉住(v1.18,用户 2026-11-10):调色常常要连着改好几处,每次点开色板太碎。
钉住后点画布 / 点别处都不收起,只有再点一次图钉、按 Esc、或点吸管才收。
图标是竖向图钉(头朝上),激活态整颗变琥珀色。 -->
<button type="button" class="iwb-color-pop-pin" data-action="pinColorPop" aria-pressed="false" title="钉住色板:一直开着,点画布也不收起(再点一次取消)"><svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 17v5"/><path d="M9 10.76a2 2 0 0 1-1.11 1.79l-1.78.9A2 2 0 0 0 5 15.24V16a1 1 0 0 0 1 1h12a1 1 0 0 0 1-1v-.76a2 2 0 0 0-1.11-1.79l-1.78-.9A2 2 0 0 1 15 10.76V7a1 1 0 0 1 1-1 2 2 0 0 0 0-4H8a2 2 0 0 0 0 4 1 1 0 0 1 1 1z"/></svg></button>
</div>
<div class="iwb-color-pop-body">
<div class="iwb-color-pop-picker">
<div class="iwb-color-sv"><span></span></div>
<div class="iwb-color-hue"><span></span></div>
</div>
<span class="iwb-color-pop-sep"></span>
<!-- 右侧竖列(v1.18):设置 → 常用色 → 最近,三块上下叠,块间用 1px 细线分隔,
不再写「常用色 / 最近」小标题 —— 标题各占一行,是这块面板最浪费的高度。
右栏常驻面板复用同一份 DOM,那边用 display:contents 拆掉这一层,走自己的竖排。 -->
<div class="iwb-color-pop-side">
<div class="iwb-color-pop-fields">
<button type="button" class="iwb-editor-color-eye" data-action="palettePick" title="吸色:切到吸管工具,在画布上点一下取色"><svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m2 22 1-1h3l9-9"/><path d="M3 21v-3l9-9"/><path d="m15 6 3.4-3.4a2.1 2.1 0 1 1 3 3L18 9l.4.4a2.1 2.1 0 1 1-3 3l-3.8-3.8a2.1 2.1 0 1 1 3-3Z"/></svg></button>
<input class="iwb-editor-color-hex-input" value="#ff4444" maxlength="7" spellcheck="false" autocomplete="off" title="十六进制颜色,回车生效">
</div>
<div class="iwb-color-group">
<span class="iwb-color-group-label">常用色</span>
<div class="iwb-editor-color-swatches">
${COMMON_COLORS.map(c => `<button type="button" class="iwb-editor-color-panel-swatch" data-panel-color="${c}" style="background:${c}" title="${c}"></button>`).join('')}
</div>
</div>
<div class="iwb-color-group">
<span class="iwb-color-group-label">最近</span>
<div class="iwb-editor-recent-colors" hidden title="最近使用"></div>
</div>
</div>
</div>`
}
// ============ DOM 创建 ============
export function createEl() {
if (runtime.el) return
runtime.el = document.createElement('div')
runtime.el.id = 'iwb-editor-overlay'
runtime.el.className = 'iwb-editor-overlay'
runtime.el.innerHTML = `
<div class="iwb-editor-modal">
<div class="iwb-editor-header">
<span class="iwb-editor-title">图片编辑 <em class="iwb-editor-doc-size">—</em></span>
<!-- 会话组(v1.2.9):标题是标题、按钮是按钮 —— 新建/覆盖/退出跟在标题后(用户裁定 2026-11-04 晚) -->
<div class="iwb-editor-session">
<button class="iwb-editor-btn iwb-editor-btn-new" data-action="newNode" title="把当前编辑结果新建为节点(不关闭编辑器)">新建</button>
<button class="iwb-editor-btn iwb-editor-btn-replace" data-action="replace" title="用当前编辑结果覆盖原图">覆盖</button>
<button class="iwb-editor-btn iwb-editor-btn-cancel" data-action="cancel" title="放弃修改并退出">退出</button>
</div>
<!-- 撤销/重做/还原(v1.18,用户 2026-11-10):从顶栏搬到画布底部工具栏最右侧。
理由:这三个是「画画时会连着按」的操作,手在画布上,不该抬头去顶栏找。
顶栏只留会话组(新建/覆盖/退出)与输出。 -->
<!-- 顶栏命令带已清空(v1.13):
四个「置顶/上移/下移/置底」按钮删除(图层面板可拖动 + Ctrl+]/[ 已覆盖);
「适应窗口 / 收起侧栏」搬到画布底部工具栏最右侧,离画布更近。
整个 .iwb-editor-commands 容器一并移除,不留一条空分隔线。 -->
<div class="iwb-editor-export-actions">
<div class="iwb-editor-export-settings-wrap">
<button class="iwb-editor-export-settings-btn" data-action="toggleExportSettings" aria-expanded="false">输出 <span class="iwb-editor-export-summary">原尺寸 PNG</span> ▾</button>
<div class="iwb-editor-export-settings">
<div class="iwb-editor-export-row"><span>预设</span>
<button data-export-key="preset" data-export-value="original">原尺寸</button>
<button data-export-key="preset" data-export-value="ecom800">电商方图</button>
</div>
<div class="iwb-editor-export-row"><span>格式</span>
<button data-export-key="format" data-export-value="png">PNG</button>
<button data-export-key="format" data-export-value="jpeg">JPG</button>
<button data-export-key="format" data-export-value="webp">WebP</button>
</div>
<div class="iwb-editor-export-row"><span>背景</span>
<button data-export-key="bg" data-export-value="transparent">透明</button>
<button data-export-key="bg" data-export-value="white">白底</button>
</div>
<div class="iwb-editor-export-row" data-export-when="ecom800"><span>边长</span>
<button data-export-key="size" data-export-value="750">750</button>
<button data-export-key="size" data-export-value="800">800</button>
<button data-export-key="size" data-export-value="1000">1000</button>
<button data-export-key="size" data-export-value="1200">1200</button>
</div>
<div class="iwb-editor-export-row" data-export-when="ecom800" title="商品图体积上限(仅对 JPG / WebP 生效,PNG 无法降质)"><span>≤KB</span>
<button data-export-key="maxKB" data-export-value="0">不限</button>
<button data-export-key="maxKB" data-export-value="300">300</button>
<button data-export-key="maxKB" data-export-value="500">500</button>
<button data-export-key="maxKB" data-export-value="800">800</button>
</div>
<div class="iwb-editor-export-note">一次只输出一个结果;设置会记住</div>
</div>
</div>
<button class="iwb-editor-btn iwb-editor-btn-copy" data-action="copyClipboard" title="复制当前结果到系统剪贴板(可直接粘到聊天窗口/后台),不关闭编辑器">复制</button>
<button class="iwb-editor-btn iwb-editor-btn-download" data-action="download" title="导出当前这一张(不关闭编辑器)">导出</button>
</div>
</div>
<div class="iwb-editor-optionbar">
<span class="iwb-editor-optionbar-label">工具选项</span>
<div class="iwb-editor-optionbar-body"></div>
<span class="iwb-editor-optionbar-tip" title="选中后还可右键;Ctrl+J 复制 · Delete 清除 · Ctrl+D 取消选区">Ctrl+J 复制 · Delete 清除</span>
</div>
<div class="iwb-editor-main">
<div class="iwb-editor-layer-panel">
<div class="iwb-editor-layer-panel-header">
<span class="iwb-editor-layer-panel-title">图层</span>
<!-- 图层的「新建 / 导入图片」回到标题栏右侧(用户裁定 2026-11-04:顶栏只留顺序与视图命令) -->
<div class="iwb-editor-layer-add-group">
<button class="iwb-editor-layer-add-btn" data-action="mergeLayers" title="合并图层 (Ctrl/⌘+E):⌘/Ctrl 点击可多选图层后合并;未多选时向下合并"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m12 2 9 4.5-9 4.5-9-4.5L12 2Z"/><path d="m3 12 9 4.5 9-4.5"/><path d="M12 17v4"/><path d="m9.5 18.5 2.5 2.5 2.5-2.5"/></svg></button>
<button class="iwb-editor-layer-add-btn" data-action="mergeVisible" title="合并可见图层 (Ctrl/⌘+Shift+E):隐藏的图层保留不动"><svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m12 2 9 4.5-9 4.5-9-4.5L12 2Z"/><path d="m3 11 9 4.5 9-4.5"/><path d="m3 15.5 9 4.5 9-4.5"/></svg></button>
<button class="iwb-editor-layer-add-btn" data-action="addBlankLayer" title="新建空白图层 (Ctrl+Shift+N)"><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>
<button class="iwb-editor-layer-add-btn" data-action="addBitmapLayer" title="导入图片为新图层 (Ctrl+O),也可直接 ⌘/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>
</div>
</div>
<!-- 图层顺序(置顶/上移/下移/置底)已移到顶栏命令带(v1.2.4) -->
<div class="iwb-editor-layer-list"></div>
</div>
<div class="iwb-editor-canvas-wrap">
<div class="iwb-editor-canvas-stack">
<div class="iwb-editor-transparency-grid"></div>
<!-- 位图图层 canvas 动态插入此处 -->
<canvas class="iwb-editor-stroke"></canvas>
<canvas class="iwb-editor-text"></canvas>
<canvas class="iwb-editor-crop-overlay"></canvas>
<canvas class="iwb-editor-selection-overlay"></canvas>
<canvas class="iwb-editor-cursor-overlay"></canvas>
</div>
<!-- v1.9:input → textarea,支持 Shift+Enter 换行(Enter 仍是确认) -->
<textarea class="iwb-editor-text-input" rows="1" wrap="off" placeholder="输入文字,Enter 确认,Shift+Enter 换行" spellcheck="false"></textarea>
<div class="iwb-editor-picker-tip"><span class="iwb-editor-picker-chip"></span><span class="iwb-editor-picker-hex">#ffffff</span></div>
<!-- 裁剪参数已迁入选项栏(v1.2.2),画布下方浮窗删除 -->
<div class="iwb-editor-sel-bar">
<span class="iwb-editor-sel-info">未框选</span>
<button data-sel-cmd="sel.copy" data-needs-sel="1" title="把当前图层选区复制为新图层 (Ctrl+J)">复制</button>
<button data-sel-cmd="sel.cut" data-needs-sel="1" title="剪切到新图层 (Ctrl+X)">剪切</button>
<button data-sel-cmd="sel.delete" data-needs-sel="1" title="只清除当前图层选区内容 (Delete)">删除</button>
<button data-sel-cmd="sel.fill" data-needs-sel="1" title="用当前颜色填充选区">填充</button>
<button data-sel-cmd="sel.invert" data-needs-sel="1" title="反选 (Ctrl+Shift+I)">反选</button>
<button data-sel-cmd="sel.all" title="全选 (Ctrl+A)">全选</button>
<button data-sel-cmd="sel.none" title="取消选区 (Esc / Ctrl+D)">取消</button>
</div>
<div class="iwb-editor-tools">
<!-- 颜色(v1.11 两格 → v1.12 文字三格):
形状下是「描边 + 填充」两格叠放在一个按钮位里(PS 式前/背景色);
文字下是「文字 / 描边 / 底色」三格横排 —— 文字对象天生就有三个颜色,
只给两格必然有一格无处安放(用户反馈「文本不支持描边与字体色切换」)。
点哪格就调哪格:色板只有一套,写谁由 S.colorTarget 决定。
关掉的那格画斜杠,跟随描边色的填充块带一个小三角角标。 -->
<div class="iwb-editor-color-pair" data-color-mode="single">
<button type="button" class="iwb-editor-color-btn" data-color-slot="stroke" data-action="toggleColorPop" aria-expanded="false" title="描边色:形状描边 / 画笔 / 文字都用它。点开色板调色"><i></i></button>
<button type="button" class="iwb-editor-color-btn" data-color-slot="fill" data-action="toggleColorPop" aria-expanded="false" title="填充色:形状内部颜色。点开色板调色,色板里可切换「无填充」"><i></i></button>
<button type="button" class="iwb-editor-color-btn" data-color-slot="bg" data-action="toggleColorPop" aria-expanded="false" title="文本框底色:垫在文字下面的一块半透明色板。点开调色,可选「无」"><i></i></button>
</div>
<span class="iwb-editor-divider"></span>
<button class="iwb-editor-tool" data-tool="select" title="选择/移动 (V):点击元素选中,拖拽移动,手柄调整大小,双击文字改内容"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M3 3l7.07 16.97 2.51-7.39 7.39-2.51L3 3z"/></svg></button>
<span class="iwb-editor-divider"></span>
<button class="iwb-editor-tool" data-tool="marquee" title="选框 (M):拖出矩形/椭圆选区,松手后旁边出现操作条;Shift+M 切换矩形/椭圆"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" stroke-dasharray="4 3"><rect x="3" y="4" width="18" height="16" rx="1"/></svg></button>
<button class="iwb-editor-tool" data-tool="lasso" title="套索 (A):按住拖动自由圈选,松手闭合"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M4 12c0-4 3.6-7 8-7s8 3 8 7-3.6 7-8 7c-2 0-3.8-.6-5.2-1.6"/><path d="M6.8 17.4 4 20l1.2-3.2"/><circle cx="20" cy="19" r="2"/></svg></button>
<button class="iwb-editor-tool active" data-tool="brush" title="画笔 (B):] 增大 / [ 减小笔刷;按住 Alt 临时吸色"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M17 3a2.85 2.83 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5Z"/><path d="m15 5 4 4"/></svg></button>
<button class="iwb-editor-tool" data-tool="eraser" title="橡皮 (E):] 增大 / [ 减小笔刷;擦除当前活跃图层,与画笔共用大小/硬度/透明度;按住 Alt 临时吸色"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m7 21-4.3-4.3c-1-1-1-2.5 0-3.4l9.6-9.6c1-1 2.5-1 3.4 0l5.6 5.6c1 1 1 2.5 0 3.4L13 21"/><path d="M22 21H7"/><path d="m5 11 9 9"/></svg></button>
<button class="iwb-editor-tool" data-tool="pen" title="钢笔 (P):点击添加节点,双击或 Enter 完成自定义形状"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m12 19 7-7 3 3-7 7-3-3z"/><path d="m18 13-1.5-7.5L2 2l3.5 14.5L13 18l5-5z"/><path d="m2 2 7.586 7.586"/><circle cx="11" cy="11" r="2"/></svg></button>
<button class="iwb-editor-tool" data-tool="text" title="文字 (T)"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="4 7 4 3 20 3 20 7"/><line x1="9" y1="20" x2="15" y2="20"/><line x1="12" y1="3" x2="12" y2="20"/></svg></button>
<button class="iwb-editor-tool" data-tool="rect" title="矩形 (R):Shift+U 在矩形/圆形/箭头间循环"><svg width="15" height="15" 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"/></svg></button>
<button class="iwb-editor-tool" data-tool="circle" title="圆形 (O):Shift+U 在矩形/圆形/箭头间循环"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><circle cx="12" cy="12" r="8"/></svg></button>
<button class="iwb-editor-tool" data-tool="arrow" title="箭头 (L):Shift+U 在矩形/圆形/箭头间循环"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><line x1="5" y1="12" x2="19" y2="12"/><polyline points="12 5 19 12 12 19"/></svg></button>
<button class="iwb-editor-tool" data-tool="picker" title="吸管 (I):点击画布取色(悬停预览);选中形状/文字时取色直接改它的颜色,取完切回上一工具"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="m2 22 1-1h3l9-9"/><path d="M3 21v-3l9-9"/><path d="m15 6 3.4-3.4a2.1 2.1 0 1 1 3 3L18 9l.4.4a2.1 2.1 0 1 1-3 3l-3.8-3.8a2.1 2.1 0 1 1 3-3Z"/></svg></button>
<button class="iwb-editor-tool" data-tool="hand" title="抓手 (H):拖拽自由移动画布(任意工具下按住空格或鼠标中键也可平移)"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 11V6a2 2 0 0 0-2-2v0a2 2 0 0 0-2 2v0"/><path d="M14 10V4a2 2 0 0 0-2-2v0a2 2 0 0 0-2 2v2"/><path d="M10 10.5V6a2 2 0 0 0-2-2v0a2 2 0 0 0-2 2v8"/><path d="M18 10a2 2 0 1 1 4 0v3a8 8 0 0 1-8 8h-2c-2.8 0-4.5-.86-5.99-2.34l-3.6-3.6a2 2 0 0 1 2.83-2.82L7 15"/></svg></button>
<span class="iwb-editor-divider"></span>
<button class="iwb-editor-tool" data-tool="crop" title="裁剪 (C):参数在选项栏;Enter 确认 / Esc 取消"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6.13 1L6 16a2 2 0 0 0 2 2h15"/><path d="M1 6.13L16 6a2 2 0 0 1 2 2v15"/></svg></button>
<!-- 视图快捷(v1.13):从顶栏搬过来,贴在工具条最右侧的「|」之后。
工具条本身是 shrink-to-fit 且居中,所以这一组天然就是「最右边」。 -->
<span class="iwb-editor-divider"></span>
<button class="iwb-editor-tool iwb-editor-view-btn" data-action="zoomFit" title="适应窗口 (Ctrl+0):整张图缩放到刚好放得下"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="9 3 3 3 3 9"/><polyline points="15 3 21 3 21 9"/><polyline points="3 15 3 21 9 21"/><polyline points="21 15 21 21 15 21"/></svg></button>
<button class="iwb-editor-tool iwb-editor-view-btn" data-action="togglePanels" title="收起 / 展开侧栏 (Tab)"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><rect x="3" y="4" width="18" height="16" rx="2"/><line x1="9" y1="4" x2="9" y2="20"/></svg></button>
<!-- 撤销 / 重做 / 还原(v1.18):从顶栏移到工具条最右。
放在视图快捷之后,是整条工具栏真正的末端 —— 手在画布区时离得最近。 -->
<span class="iwb-editor-divider"></span>
<div class="iwb-editor-history">
<button class="iwb-editor-action" data-action="undo" title="撤销 (Ctrl+Z)"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="1 4 1 10 7 10"/><path d="M3.51 15a9 9 0 1 0 2.13-9.36L1 10"/></svg></button>
<button class="iwb-editor-action" data-action="redo" title="重做 (Ctrl+Shift+Z)"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="23 4 23 10 17 10"/><path d="M20.49 15a9 9 0 1 1-2.12-9.36L23 10"/></svg></button>
<button class="iwb-editor-action iwb-editor-action-reset" data-action="reset" title="还原原图:删掉所有标注/图层,回到打开时的样子(Ctrl+Z 可撤销)"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 2v6h-6"/><path d="M3 12a9 9 0 0 1 15-6.7L21 8"/><path d="M3 22v-6h6"/><path d="M21 12a9 9 0 0 1-15 6.7L3 16"/></svg></button>
</div>
</div>
</div>
<div class="iwb-editor-zoom-hint">滚轮缩放 · 抓手/空格/中键拖拽自由移动画布</div>
<aside class="iwb-editor-left">
<!-- v1.16:侧栏宽度拖拽手柄。贴在侧栏左边缘(它在画布右侧),
双击复位。宽度写进 CSS 变量 --iwb-side-w,由 --iwb-side-w 统一裁各条 width 规则。 -->
<div class="iwb-editor-side-resizer" data-side-resizer="1" title="拖动调整侧栏宽度,双击复位"></div>
<!-- 调色面板(v1.16):从弹层搬进来常驻一份,弹层保留。
两处是同一份 DOM(dom.js#colorSurfaceHtml),靠 data-color-surface 区分与遍历,
不存在「一边改了另一边不变」。顶部色格 = 当前元素可调的颜色项,
跟随选中对象变化:形状=描边/填充,文字=文字/描边/底色,箭头=只要描边。 -->
<section class="iwb-editor-colors" data-color-surface="side">
<div class="iwb-editor-colors-head">
<span class="iwb-editor-colors-title">颜色</span>
<span class="iwb-editor-colors-hint" title="点上面的色格选择要调哪个颜色">点色格切换</span>
</div>
${colorSurfaceHtml()}
</section>
<!-- 调色板已迁到工具栏弹层(v1.3,用户裁定 2026-11-05):
侧栏只留快速效果 + 缩放;色板 DOM 在 modal 末尾的 .iwb-editor-color-pop -->
<!-- 图片调节(v1.15):连续调色,作用于当前位图图层(默认原图)。
行由 adjust.js#mountAdjustPanel() 按 ADJUST_DEFS 生成 —— 参数表只维护一份,
不在 DOM 里再抄一遍(这个项目已经因为「同一个概念两处各写一份」栽过几次)。
和下方「快速效果」的分工:这里是精细连续调,那边是一键换风格。 -->
<section class="iwb-editor-adj">
<div class="iwb-editor-adj-head">
<span class="iwb-editor-adj-title">图片调节</span>
<span class="iwb-editor-adj-target" title="调节作用于当前位图图层">原图</span>
<button type="button" class="iwb-editor-adj-reset" data-adj-reset="all" disabled title="把所有调节项归零(Ctrl+Z 可撤销)">复位</button>
</div>
<div class="iwb-editor-adj-list"></div>
<p class="iwb-editor-adj-tip">拖动实时预览,松手出精细结果 · 双击滑杆归零</p>
</section>
<!-- 快速效果(v1.4):作用于当前位图图层,点一下就应用,Ctrl+Z 撤销。
目标层名由 effects.js 的 updateFxTargetLabel() 实时刷新。 -->
<section class="iwb-editor-fx">
<div class="iwb-editor-fx-head">
<span class="iwb-editor-fx-title">快速效果</span>
<span class="iwb-editor-fx-target" title="效果作用于当前位图图层">原图</span>
</div>
<div class="iwb-editor-fx-grid">
<button type="button" class="iwb-editor-fx-btn" data-fx="invert" title="反白(反相 Invert):明暗翻转,白底变黑底、黑字变白字">
<span class="iwb-editor-fx-ico ico-invert"></span><span>反白</span>
</button>
<button type="button" class="iwb-editor-fx-btn" data-fx="gray" title="灰度:去色,保留明暗层次">
<span class="iwb-editor-fx-ico ico-gray"></span><span>灰度</span>
</button>
<button type="button" class="iwb-editor-fx-btn" data-fx="boost" title="增强:提亮并略加对比,适合发灰的截图">
<span class="iwb-editor-fx-ico ico-boost"></span><span>增强</span>
</button>
<button type="button" class="iwb-editor-fx-btn" data-fx="contrast" title="高对比:压成黑白硬调,突出主体">
<span class="iwb-editor-fx-ico ico-contrast"></span><span>高对比</span>
</button>
<button type="button" class="iwb-editor-fx-btn" data-fx="soft" title="柔化:轻微模糊,弱化背景">
<span class="iwb-editor-fx-ico ico-soft"></span><span>柔化</span>
</button>
<button type="button" class="iwb-editor-fx-btn" data-fx="mosaic" title="马赛克:像素化,用于打码手机号/地址等隐私信息">
<span class="iwb-editor-fx-ico ico-mosaic"></span><span>马赛克</span>
</button>
</div>
<!-- v1.8:效果参数。点效果后启用,范围/单位随效果切换(马赛克=块大小,柔化=半径,其余=强度) -->
<div class="iwb-editor-fx-adj iwb-editor-fx-adj-off">
<div class="iwb-editor-fx-adj-head">
<span class="iwb-editor-fx-adj-name">强度</span>
<span class="iwb-editor-fx-adj-val">—</span>
</div>
<input type="range" class="iwb-editor-fx-range" min="0" max="100" step="1" value="0" disabled aria-label="效果强度">
</div>
<p class="iwb-editor-fx-tip">先点效果,再拖强度微调 · Ctrl+Z 撤销</p>
</section>
<div class="iwb-editor-zoom">
<button class="iwb-editor-zoom-btn" data-action="zoomOut" title="缩小 (Ctrl+- / 滚轮向下)">−</button>
<span class="iwb-editor-zoom-val" title="滚轮缩放">100%</span>
<button class="iwb-editor-zoom-btn" data-action="zoomIn" title="放大 (Ctrl++ / 滚轮向上)">+</button>
<button class="iwb-editor-zoom-btn iwb-editor-zoom-fit" data-action="zoomFit" title="适应窗口">适应</button>
</div>
</aside>
</div>
<div class="iwb-editor-footer">
</div>
<div class="iwb-editor-statusbar">
<span class="iwb-editor-status-zoom">100%</span>
<span class="iwb-editor-status-size">—</span>
<span class="iwb-editor-status-pos">x — y —</span>
<span class="iwb-editor-status-sel">无选区</span>
<span class="iwb-editor-status-layer">当前层:原图</span>
<span class="iwb-editor-status-tool">画笔</span>
</div>
<!-- 色板弹层(v1.3;v1.16 起与右栏常驻面板共用同一份 DOM):
工具栏色块点击展开。必须挂在 modal 下、不能塞进 .iwb-editor-tools ——
工具栏是 overflow-x:auto,canvas-wrap 是 overflow:clip,塞进去弹层会被直接裁掉;
定位由 events.js 计算。 -->
<div class="iwb-editor-color-pop" data-color-surface="pop" hidden>
${colorSurfaceHtml()}
</div>
<div class="iwb-editor-toast" role="status" aria-live="polite"></div>
</div>
`
document.body.appendChild(runtime.el)
S.canvasWrap = runtime.el.querySelector('.iwb-editor-canvas-wrap')
S.canvasStack = runtime.el.querySelector('.iwb-editor-canvas-stack')
S.transparencyGrid = runtime.el.querySelector('.iwb-editor-transparency-grid')
S.textCanvas = runtime.el.querySelector('.iwb-editor-text')
S.strokeCanvas = runtime.el.querySelector('.iwb-editor-stroke')
S.overlayCanvas = runtime.el.querySelector('.iwb-editor-crop-overlay')
S.selectionCanvas = runtime.el.querySelector('.iwb-editor-selection-overlay')
S.cursorCanvas = runtime.el.querySelector('.iwb-editor-cursor-overlay')
S.textCtx = S.textCanvas.getContext('2d')
S.strokeCtx = S.strokeCanvas.getContext('2d')
S.overlayCtx = S.overlayCanvas.getContext('2d')
S.selectionCtx = S.selectionCanvas.getContext('2d')
S.cursorCtx = S.cursorCanvas.getContext('2d')
bindEvents()
}
// ============ 图片加载 ============
/**
* 把 base64 dataURL 本地解码成 Blob(不经过 fetch)。
*
* [插件移植改动] 上游用 `fetch(dataURL)` 转 blob URL,但宿主 CSP 是
* `connect-src 'self' https: http://127.0.0.1:30092 http://localhost:30092` —— 不含 `data:`,
* 于是每次 loadImage 都会触发一次 CSP 违规(上游的 catch 回退让它「还能用」,但控制台每次都报错)。
* 这里改为纯本地解码,行为与上游一致(同样拿到 blob URL),且不依赖网络/CSP。
*/
function dataUrlToBlob(dataUrl) {
const comma = dataUrl.indexOf(',')
if (comma < 0) return null
const meta = dataUrl.slice(0, comma)
if (!/;base64/i.test(meta)) return null
const mime = (/^data:([^;,]+)/.exec(meta) || [])[1] || 'image/png'
const bin = atob(dataUrl.slice(comma + 1))
const bytes = new Uint8Array(bin.length)
for (let i = 0; i < bin.length; i++) bytes[i] = bin.charCodeAt(i)
return new Blob([bytes], { type: mime })
}
/**
* dataURL 一律先转 blob URL 再喂给 Image:img.src 直接挂 dataURL 会让
* 元素在整个编辑会话期间持有一份几十 MB 的字符串(4K 源图 ≈44MB),而
* 解码只需一次 —— 载入完成立即 revoke,img 保留的是自己的解码资源,
* 之后 drawImage/复原都不再依赖那串字符。
*/
export async function loadImage(src) {
let useSrc = src
let revokeUrl = null
if (typeof src === 'string' && src.slice(0, 5) === 'data:') {
try {
const blob = dataUrlToBlob(src)
if (!blob) throw new Error('dataURL 不是 base64')
useSrc = URL.createObjectURL(blob)
revokeUrl = useSrc
} catch (e) { useSrc = src } // 解码不可用/失败:回落原 dataURL
}
try {
return await new Promise((resolve, reject) => {
const img = new Image()
img.onload = () => resolve(img)
img.onerror = () => reject(new Error('图片加载失败'))
img.src = useSrc
})
} finally {
if (revokeUrl) { try { URL.revokeObjectURL(revokeUrl) } catch (e) { /* ignore */ } }
}
}