fix(image-annotate): 图层 z 序统一 + 排序按钮/选中修复(1.0.5)
- 根因:粘贴图片用 S.layers.length 当 z,矢量用 ++S.zCounter ⇒ z 撞车; 面板/DOM/合成/命中四处各自排序,导致排序按钮交换 z 无效、截图选不中、双高亮 - objects.js 新增 layerUnits()/layerUnitsDesc()/assignLayerZ()/selectUnit()/hitTopmost(): 图层顺序唯一真源、连续 z、选中态唯一、统一 z 降序命中 - moveLayer 改为统一列表重排(置顶/上移/下移/置底全部生效,base 固定在最底不吞元素) - placement 改 ++S.zCounter;deleteLayer 不再重置位图 z;面板高亮与画笔目标(✎)分离 - 形状/文字/钢笔创建、裁剪、清空标注、重开编辑器均清旧位图选中 id - e2e 补 9 条回归断言(真站 22/22 通过);README 记录与上游的偏离,防回灌覆盖
This commit is contained in:
1 parent
5f9303986a
commit
b9ace636ef
13 files changed
+432
-398
No files matched your search
@@ -5,10 +5,12 @@
|
||||
|
||||
编辑器**移植自** `iwb-plaus` 仓库的 `iwb-cutout-extension`(纯 Canvas 2D、零第三方库):
|
||||
- 源:`/Users/qiqi/open/github/iwb-plaus/iwb-cutout-extension/src/editor/`(11 个模块)+ `src/shared/{image-math,layer-logic}.js`
|
||||
- 差异:只有「宿主胶水」(`plugin/glue.js`、`plugin/entry.js`)是新写的;编辑器模块与样式**逐字移植**,便于日后回灌上游 —— **唯一例外见下**。
|
||||
- 差异:只有「宿主胶水」(`plugin/glue.js`、`plugin/entry.js`)是新写的;编辑器模块与样式**逐字移植**,便于日后回灌上游 —— **例外见下两节**(`loadImage` 的 CSP 适配、v1.0.5 的图层顺序修复)。
|
||||
- 原扩展的 CSS 由 `manifest.json` 的 `content_scripts.css` 注入;插件没有 manifest,改为构建时以字符串内联、运行时注入 `<style>`。
|
||||
|
||||
### 移植改动(唯一一处)
|
||||
> ⚠️ **回灌上游前先看这里**:v1.0.5 改了多个编辑器模块(`objects.js` / `layers.js` / `events.js` / `tools.js` / `placement.js` / `crop.js` / `history.js` / `index.js` / `viewport.js` 的相邻逻辑、`editor.css`),修的是上游的图层 z 序缺陷。整目录覆盖会把修复冲掉。
|
||||
|
||||
### 移植改动之一:CSP 适配
|
||||
`plugin/editor/dom.js` 的 `loadImage()`:上游用 `fetch(dataURL)` 把 dataURL 转成 blob URL(避免 `img.src` 在整个编辑会话里
|
||||
持有几十 MB 的 base64 字符串)。但宿主 CSP 是 `connect-src 'self' https: …`,**不含 `data:`** ⇒ `fetch(data:)` 必被浏览器拦,
|
||||
每次打开编辑器都产生一次 CSP 违规(上游的 `catch` 回退让它「还能用」,所以功能看起来正常、只在控制台报错)。
|
||||
@@ -35,6 +37,23 @@
|
||||
而 `data:` URL 不能 fetch(CSP `connect-src` 不含 `data:`,会被拦)要直接走 `loadImage`。
|
||||
正在文字输入框打字时不抢文本粘贴。
|
||||
|
||||
### 移植改动之二:图层顺序与选中(v1.0.5 修复)
|
||||
|
||||
上游存在**两套 z 计数器**:位图初始化 `base=0 / draw=1 / zCounter=1`,矢量与空白层用 `++zCounter`,
|
||||
而「粘贴/导入图片」落层却用 `z = S.layers.length` ⇒ 画一个图形(z=2)后粘贴截图(z=2)**z 撞车**。后果:
|
||||
- 排序按钮靠「交换相邻两项 z」实现上移/下移,2↔2 交换后没变化;同 z 时矢量被判成最顶层,`置顶/上移` 直接 return;
|
||||
`置底` 算出 `-1` 被不透明原图盖住,看起来像图层消失了;
|
||||
- 命中测试先查矢量、再查位图,**两段各自排序、从不比较彼此 z** ⇒ 截图下面有图形时永远选不中截图,
|
||||
于是排序按钮作用在了错误对象上;面板高亮还用 `selectedLayerId || activeLayerId`,出现两个图层同时亮。
|
||||
|
||||
本插件的修法(**`objects.js` 的 `layerUnits()` 是图层顺序唯一真源**):
|
||||
- 所有新建对象统一 `++S.zCounter`;重排后按索引重新分配连续 z(`assignLayerZ`),z 永不重复;
|
||||
- 面板 / DOM 叠放 / 导出合成 / 命中测试**共用同一份 z 序**;置顶/上移/下移/置底改为「统一列表重排」;
|
||||
- 命中测试合并为 `hitTopmost()`,按统一 z 降序逐项判定(位图按矩形/像素、矢量按包围盒/形状);
|
||||
- 选中态统一走 `selectUnit()`,四个 id 一起写;**高亮只代表选中**,画笔/橡皮作用目标改用图层项上的 `✎` 标记;
|
||||
- `base` 是锁定且不透明的底图,固定在最下层,「置底」到它上面为止(不会再让元素消失);
|
||||
- 删除图层不再把位图 z 重置成 `0..n-1`;裁剪 / 清空标注 / 重开编辑器时清掉旧的位图选中 id。
|
||||
|
||||
## 已知限制(v1)
|
||||
- **远程 http(s) 图片不支持**:`ctx.canvas.getNodeImage` 对非 data: 字符串只返回 `{ url }`(画布读像素会污染),
|
||||
插件会给出「请先转存为本地位图」的提示。对象引用(`{__blob}` 等)会被宿主解成 dataUrl,正常可用。
|
||||
@@ -62,14 +81,15 @@ npm run validate # 契约校验
|
||||
```bash
|
||||
cd /Users/qiqi/open/github/image-workbench-web
|
||||
cp /Users/qiqi/open/github/iwb-plugins/image-annotate/src/index.js /tmp/plugin-image-annotate.js
|
||||
node scripts/plugins-publish.mjs /tmp/plugin-image-annotate.js --version image-annotate=1.0.4 # 先 dry-run,检查后加 --upload
|
||||
node scripts/plugins-publish.mjs /tmp/plugin-image-annotate.js --version image-annotate=1.0.5 # 先 dry-run,检查后加 --upload
|
||||
```
|
||||
发布脚本**默认与线上清单合并**(本次没传的插件原样保留),只传自己的文件即可;`--prune` 才会缩减清单(别用)。
|
||||
版本两处一起改:`package.json`(构建时注入 manifest)+ `--version`。上传后回读校验清单与本地一致。
|
||||
发布后刷新即生效(KV 为测试站/正式站共享;新插件在插件管理里**默认未启用**,需手动开启)。
|
||||
|
||||
## 验证(可选,开发用)
|
||||
`tools/e2e-site.mjs`:**带 CSP 的真实站点**端到端(真 KV 加载 + 浏览器 SRI + 漂浮菜单 → 编辑器 → 覆盖/新建)。
|
||||
`tools/e2e-site.mjs`:**带 CSP 的真实站点**端到端(真 KV 加载 + 浏览器 SRI + 漂浮菜单 → 编辑器 → 覆盖/新建),
|
||||
另含 v1.0.5 的图层顺序回归:画图形 + 粘贴截图 → 唯一高亮、点重叠区选中最上层截图、四个排序按钮逐一生效、原图始终在最底。
|
||||
需要 Playwright(可从宿主仓 `node_modules` 或全局解析),**不进 CI**(本仓 CI 只跑 `npm run validate`,不联网、无依赖)。
|
||||
|
||||
```bash
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@iwb-plugin/image-annotate",
|
||||
"private": true,
|
||||
"version": "1.0.4",
|
||||
"version": "1.0.5",
|
||||
"description": "IWB 插件:图片标注 / 修图(编辑器移植自 iwb-plaus 扩展,纯 Canvas 2D)",
|
||||
"ibwPlugin": {
|
||||
"id": "image-annotate",
|
||||
|
||||
@@ -772,6 +772,8 @@
|
||||
.iwb-layer-item[draggable="true"] { cursor: grab; }
|
||||
.iwb-layer-item.dragging { opacity: 0.45; }
|
||||
.iwb-layer-item.drop-target { outline: 1px dashed #e8a735; background: rgba(232, 167, 53, 0.16); }
|
||||
/* 画笔/橡皮作用目标:与「选中」分离,避免出现两个图层同时高亮 */
|
||||
.iwb-layer-paint { flex: 0 0 auto; color: #a6adc8; font-size: 11px; opacity: 0.85; }
|
||||
.iwb-layer-color { width: 22px; height: 22px; padding: 0; border: 1px solid #45475a; border-radius: 50%; background: transparent; cursor: pointer; flex: 0 0 auto; }
|
||||
.iwb-layer-width { width: 42px; height: 22px; padding: 2px 3px; border: 1px solid #45475a; border-radius: 4px; background: #1e1e2e; color: #e8a735; font-size: 11px; text-align: center; flex: 0 0 auto; }
|
||||
.iwb-layer-width:focus { outline: none; border-color: #e8a735; }
|
||||
|
||||
@@ -301,6 +301,9 @@ import { setupCanvas } from './viewport.js'
|
||||
S.textDrag = null
|
||||
S.selectedShapeId = null
|
||||
S.shapeDrag = null
|
||||
// 裁剪后 setupCanvas 会重建全部位图图层,旧 selectedLayerId 会指向已删除的 id
|
||||
S.selectedLayerId = null
|
||||
S.bitmapDrag = null
|
||||
S.zoom = 1
|
||||
setupCanvas(cropC)
|
||||
S.history = []
|
||||
|
||||
@@ -8,10 +8,10 @@
|
||||
|
||||
import { S, ZOOM_STEP, runtime } from './state.js'
|
||||
import { ROT_CURSOR, clamp, getPos, isOverRotationHandle, setView, setZoomFit, zoomStep } from './viewport.js'
|
||||
import { addBitmapLayer, addBlankLayer, applyBitmapDrag, beginBitmapDrag, clearSelection, getActiveCtx, hitBitmapHandle, hitBitmapLayer, removeVecCanvas, renderLayerPanel } from './layers.js'
|
||||
import { addBitmapLayer, addBlankLayer, applyBitmapDrag, beginBitmapDrag, clearSelection, getActiveCtx, hitBitmapHandle, removeVecCanvas, renderLayerPanel } from './layers.js'
|
||||
import { applyColorChange, beginStroke, drawToolRing, endStroke, eraseSegment, exitPicker, hidePickerTip, sampleColor, setTool, showPickerTip, strokeSegment } from './tools.js'
|
||||
import { applyConstraintDrag, cancelCrop, clearOverlay, confirmCrop, drawCropPreview, hideCropBar, hitCropHandle, insideRect, resizeCropRect, setConstraint, showCropBar, updateCropInfo } from './crop.js'
|
||||
import { applyShapeHandle, beginPenEdit, beginShapeHandleDrag, beginTextRotate, beginTextScaleDrag, commitTextInput, deleteSelectedShape, deleteSelectedText, exitPenEdit, getPenEditingShape, getSelectedShape, getSelectedText, hideTextInput, hitAnyObject, hitPenEdgeInsert, hitPenPoint, hitShapeHandle, hitShapeLayer, hitTextHandle, hitTextLayer, moveLayer, renderObjects, shapeBounds, showTextInput } from './objects.js'
|
||||
import { applyShapeHandle, beginPenEdit, beginShapeHandleDrag, beginTextRotate, beginTextScaleDrag, commitTextInput, deleteSelectedShape, deleteSelectedText, exitPenEdit, getPenEditingShape, getSelectedShape, getSelectedText, hideTextInput, hitAnyObject, hitPenEdgeInsert, hitPenPoint, hitShapeHandle, hitShapeLayer, hitTextHandle, hitTextLayer, hitTopmost, moveLayer, renderObjects, selectUnit, shapeBounds, showTextInput } from './objects.js'
|
||||
import { cancelImagePlacement, confirmImagePlacement, drawPlacementPreview, hitPlacementHandle, insidePlacementRect, resizePlacement } from './placement.js'
|
||||
import { close, exportResult } from './index.js'
|
||||
import { loadImage } from './dom.js'
|
||||
@@ -120,7 +120,9 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
return false
|
||||
}
|
||||
|
||||
/** 选择工具的按下:手柄 > 对象 > 位图 > 清空选择,逐级命中 */
|
||||
/** 选择工具的按下:手柄 > 统一 z 序命中最上层 > 清空选择。
|
||||
* 旧实现「先 hitAnyObject(只看矢量) 再 hitBitmapLayer(只看位图)」两段各自排序,
|
||||
* 从不比较位图与矢量的 z ⇒ 截图下面有图形时永远选不中截图,排序按钮也跟着作用错对象。 */
|
||||
function onSelectMouseDown(pos) {
|
||||
if (commitTextInput()) return
|
||||
const bitmapHandle = hitBitmapHandle(pos)
|
||||
@@ -129,39 +131,25 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
if (th) { if (th === 'rotate') beginTextRotate(pos); else beginTextScaleDrag(th, pos); return }
|
||||
const shH = hitShapeHandle(pos)
|
||||
if (shH) { beginShapeHandleDrag(shH, pos); return }
|
||||
const hit = hitAnyObject(pos)
|
||||
if (hit) {
|
||||
S.selectedLayerId = null
|
||||
if (hit.kind === 'text') {
|
||||
S.selectedShapeId = null
|
||||
S.selectedTextId = hit.obj.id
|
||||
if (hit.obj.locked) { renderObjects(); renderLayerPanel(); return }
|
||||
S.textDrag = { mode: 'move', start: pos, layer: { x: hit.obj.x, y: hit.obj.y }, pending: snapshotState() }
|
||||
} else {
|
||||
S.selectedTextId = null
|
||||
S.selectedShapeId = hit.obj.id
|
||||
if (hit.obj.locked) { renderObjects(); renderLayerPanel(); return }
|
||||
S.shapeDrag = { mode: 'move', start: pos, shape: { ...hit.obj }, pending: snapshotState() }
|
||||
}
|
||||
const hit = hitTopmost(pos)
|
||||
if (!hit) {
|
||||
selectUnit(null, null)
|
||||
renderObjects()
|
||||
renderLayerPanel()
|
||||
} else {
|
||||
const bitmap = hitBitmapLayer(pos)
|
||||
if (bitmap) {
|
||||
S.selectedLayerId = bitmap.id
|
||||
S.activeLayerId = bitmap.id
|
||||
S.selectedTextId = null
|
||||
S.selectedShapeId = null
|
||||
beginBitmapDrag(null, pos)
|
||||
renderObjects()
|
||||
renderLayerPanel()
|
||||
} else if (S.selectedLayerId || S.selectedTextId || S.selectedShapeId) {
|
||||
clearSelection()
|
||||
renderObjects()
|
||||
renderLayerPanel()
|
||||
}
|
||||
return
|
||||
}
|
||||
return
|
||||
selectUnit(hit.kind, hit.obj.id)
|
||||
if (hit.obj.locked) { renderObjects(); renderLayerPanel(); return }
|
||||
if (hit.kind === 'bitmap') {
|
||||
S.activeLayerId = hit.obj.id
|
||||
beginBitmapDrag(null, pos)
|
||||
} else if (hit.kind === 'text') {
|
||||
S.textDrag = { mode: 'move', start: pos, layer: { x: hit.obj.x, y: hit.obj.y }, pending: snapshotState() }
|
||||
} else {
|
||||
S.shapeDrag = { mode: 'move', start: pos, shape: { ...hit.obj }, pending: snapshotState() }
|
||||
}
|
||||
renderObjects()
|
||||
renderLayerPanel()
|
||||
}
|
||||
|
||||
/** 裁剪工具的按下:已有选区则拖手柄/整框移动,否则开始拉新选框 */
|
||||
@@ -186,7 +174,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
if (handle) { if (handle === 'rotate') beginTextRotate(pos); else beginTextScaleDrag(handle, pos); return }
|
||||
const hit = hitTextLayer(pos)
|
||||
if (hit) {
|
||||
S.selectedTextId = hit.id
|
||||
selectUnit('text', hit.id)
|
||||
if (hit.locked) { renderObjects(); renderLayerPanel(); return }
|
||||
S.textDrag = {
|
||||
mode: 'move',
|
||||
@@ -643,7 +631,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
z: ++S.zCounter
|
||||
}
|
||||
S.shapeLayers.push(shape)
|
||||
S.selectedShapeId = shape.id
|
||||
selectUnit('shape', shape.id)
|
||||
}
|
||||
renderObjects()
|
||||
renderLayerPanel()
|
||||
@@ -1131,7 +1119,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
if (!S.penPath || S.penPath.pts.length < 2) { S.penPath = null; renderObjects(); return }
|
||||
saveSnapshot()
|
||||
const shape = { id: 's' + Date.now() + Math.floor(Math.random() * 1000), type: 'pen', pts: S.penPath.pts.slice(), closed: !!S.penPath.closed, color: S.color, lw: Math.max(2, S.brushSize), fill: !!S.penPath.closed, opacity: 1, visible: true, locked: false, z: ++S.zCounter }
|
||||
S.shapeLayers.push(shape); S.selectedShapeId = shape.id; S.penPath = null; renderObjects(); renderLayerPanel()
|
||||
S.shapeLayers.push(shape); selectUnit('shape', shape.id); S.penPath = null; renderObjects(); renderLayerPanel()
|
||||
}
|
||||
|
||||
/** 将 canvas 鼠标事件绑定到活跃图层 canvas(在 setupCanvas 后调用,仅绑定一次) */
|
||||
@@ -1188,7 +1176,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
if (S.tool !== 'text' && S.tool !== 'select') return
|
||||
const hit = hitTextLayer(pos)
|
||||
if (hit) {
|
||||
S.selectedTextId = hit.id
|
||||
selectUnit('text', hit.id)
|
||||
if (hit.locked) return
|
||||
renderObjects()
|
||||
showTextInput(hit.x, hit.y, hit)
|
||||
|
||||
@@ -204,6 +204,8 @@ import { isDrawableLayer, needsPixelSnapshot, restorePath } from '../vendor/laye
|
||||
S.shapeLayers = []
|
||||
S.selectedShapeId = null
|
||||
S.shapeDrag = null
|
||||
S.selectedLayerId = null
|
||||
S.bitmapDrag = null
|
||||
renderObjects()
|
||||
renderLayerPanel()
|
||||
clearOverlay()
|
||||
|
||||
@@ -95,6 +95,9 @@ import { updateAllUI } from './events.js'
|
||||
S.penDragPt = -1
|
||||
S.penDragBase = null
|
||||
S.penDragPending = null
|
||||
// 编辑器 DOM 复用,重开时旧的位图选中 id 指向已删除的图层,必须清掉
|
||||
S.selectedLayerId = null
|
||||
S.bitmapDrag = null
|
||||
S.zCounter = Math.max(S.layers.length - 1, 0)
|
||||
S.placingImage = null
|
||||
S.placingHandle = null
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
'use strict'
|
||||
|
||||
import { S, runtime } from './state.js'
|
||||
import { TEXT_FONT, allObjects, deleteSelectedShape, deleteSelectedText, renderObjects, sortLayersByZ, textBounds } from './objects.js'
|
||||
import { TEXT_FONT, allObjects, assignLayerZ, deleteSelectedShape, deleteSelectedText, layerUnits, layerUnitsDesc, renderObjects, selectUnit, sortLayersByZ, textBounds } from './objects.js'
|
||||
import { boxCorners, clamp, drawRotationHandles, hitRotationHandle, rotPt, unrotPt } from './viewport.js'
|
||||
import { drawShape } from './tools.js'
|
||||
import { saveSnapshot, snapshotState } from './history.js'
|
||||
@@ -38,14 +38,12 @@ import { isDrawableLayer } from '../vendor/layer-logic.js'
|
||||
* 所有 canvas 必须共享 canvas-stack 的坐标系;隐藏图层只用 CSS 隐藏,
|
||||
* 不从 DOM 移除,避免 canvas-stack 在 flex/inline 布局下重新计算位置。 */
|
||||
export function reorderLayerCanvas() {
|
||||
const units = []
|
||||
for (const l of S.layers) units.push({ z: l.z, el: l.canvas, vis: l.visible })
|
||||
for (const { obj } of allObjects()) if (obj._canvas) units.push({ z: obj.z || 0, el: obj._canvas, vis: obj.visible !== false })
|
||||
units.sort((a, b) => a.z - b.z)
|
||||
for (const u of units) {
|
||||
if (!u.el) continue
|
||||
S.canvasStack.insertBefore(u.el, S.strokeCanvas)
|
||||
u.el.style.display = u.vis ? 'block' : 'none'
|
||||
// 与合成/导出/面板/命中测试共用同一份 z 序,杜绝「面板顺序 ≠ 实际叠放」
|
||||
for (const u of layerUnits()) {
|
||||
const el = u.kind === 'bitmap' ? u.obj.canvas : u.obj._canvas
|
||||
if (!el) continue
|
||||
S.canvasStack.insertBefore(el, S.strokeCanvas)
|
||||
el.style.display = u.obj.visible === false ? 'none' : 'block'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -168,35 +166,26 @@ import { isDrawableLayer } from '../vendor/layer-logic.js'
|
||||
return { x: layer.x, y: layer.y, w: layer.w, h: layer.h }
|
||||
}
|
||||
|
||||
/** 单个位图图层是否命中该点:绘制类按像素(透明处不算),图片类按矩形 */
|
||||
export function hitBitmapAt(layer, pos) {
|
||||
if (!layer || layer.visible === false || layer.locked) return false
|
||||
const drawable = isDrawableLayer(layer)
|
||||
if (!drawable && !layer.transformable) return false
|
||||
if (!(layer.w > 0 && layer.h > 0)) return false
|
||||
const rot = layer.rotation || 0
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, layer.x + layer.w / 2, layer.y + layer.h / 2, rot) : [pos.x, pos.y]
|
||||
if (lp[0] < layer.x || lp[0] > layer.x + layer.w || lp[1] < layer.y || lp[1] > layer.y + layer.h) return false
|
||||
if (!drawable) return true
|
||||
const px = Math.round(lp[0]), py = Math.round(lp[1])
|
||||
if (px < 0 || py < 0 || px >= layer.canvas.width || py >= layer.canvas.height) return false
|
||||
try { return layer.ctx.getImageData(px, py, 1, 1).data[3] > 8 } catch (e) { return true }
|
||||
}
|
||||
|
||||
/** 兼容入口:按统一 z 降序找最上面的位图图层 */
|
||||
export function hitBitmapLayer(pos) {
|
||||
const layers = [...S.layers].sort((a, b) => (a.z || 0) - (b.z || 0))
|
||||
const hitRect = (layer) => {
|
||||
if (!(layer.w > 0 && layer.h > 0)) return false
|
||||
const rot = layer.rotation || 0
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, layer.x + layer.w / 2, layer.y + layer.h / 2, rot) : [pos.x, pos.y]
|
||||
return lp[0] >= layer.x && lp[0] <= layer.x + layer.w && lp[1] >= layer.y && lp[1] <= layer.y + layer.h
|
||||
}
|
||||
const usable = (layer) => layer && layer.visible !== false && !layer.locked
|
||||
// 第一轮:绘制类图层按像素命中(点内容才算,避免被上层图片层拦截)
|
||||
for (let i = layers.length - 1; i >= 0; i--) {
|
||||
const layer = layers[i]
|
||||
if (!usable(layer)) continue
|
||||
if (!isDrawableLayer(layer)) continue
|
||||
if (!hitRect(layer)) continue
|
||||
const rot = layer.rotation || 0
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, layer.x + layer.w / 2, layer.y + layer.h / 2, rot) : [pos.x, pos.y]
|
||||
const px = Math.round(lp[0]), py = Math.round(lp[1])
|
||||
if (px < 0 || py < 0 || px >= layer.canvas.width || py >= layer.canvas.height) continue
|
||||
try {
|
||||
const d = layer.ctx.getImageData(px, py, 1, 1).data
|
||||
if (d[3] > 8) return layer
|
||||
} catch (e) { return layer }
|
||||
}
|
||||
// 第二轮:图片类图层按矩形命中
|
||||
for (let i = layers.length - 1; i >= 0; i--) {
|
||||
const layer = layers[i]
|
||||
if (!usable(layer) || !layer.transformable) continue
|
||||
if (hitRect(layer)) return layer
|
||||
for (const u of layerUnitsDesc()) {
|
||||
if (u.kind !== 'bitmap') continue
|
||||
if (hitBitmapAt(u.obj, pos)) return u.obj
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -318,11 +307,7 @@ import { isDrawableLayer } from '../vendor/layer-logic.js'
|
||||
/** 切换画笔/橡皮作用的活动图层 */
|
||||
export function setActiveLayer(id) {
|
||||
S.activeLayerId = id
|
||||
S.selectedLayerId = id
|
||||
S.selectedTextId = null
|
||||
S.selectedShapeId = null
|
||||
S.textDrag = null
|
||||
S.shapeDrag = null
|
||||
selectUnit('bitmap', id)
|
||||
renderLayerPanel()
|
||||
renderObjects()
|
||||
}
|
||||
@@ -370,12 +355,11 @@ export function setActiveLayer(id) {
|
||||
saveSnapshot()
|
||||
if (layer.canvas.parentNode) layer.canvas.parentNode.removeChild(layer.canvas)
|
||||
S.layers.splice(idx, 1)
|
||||
// 重排 z 序
|
||||
S.layers.forEach((l, i) => { l.z = i })
|
||||
// 如果删除的是活跃图层,切到最后一个
|
||||
if (S.activeLayerId === id) {
|
||||
S.activeLayerId = S.layers[S.layers.length - 1].id
|
||||
}
|
||||
// 旧实现把位图 z 重设为 0..n-1,会与矢量对象的 z 再次撞车;统一重新分配
|
||||
assignLayerZ(layerUnits())
|
||||
// 如果删除的是活跃/选中图层,切到最后一个
|
||||
if (S.activeLayerId === id) S.activeLayerId = S.layers[S.layers.length - 1].id
|
||||
if (S.selectedLayerId === id) S.selectedLayerId = null
|
||||
reorderLayerCanvas()
|
||||
renderLayerPanel()
|
||||
}
|
||||
@@ -482,47 +466,33 @@ export function setActiveLayer(id) {
|
||||
const list = runtime.el.querySelector('.iwb-editor-layer-list')
|
||||
if (!list) return
|
||||
|
||||
// 收集所有图层项(位图 + 矢量对象),按 z 序倒序(最上层在最前)
|
||||
// 面板顺序直接取统一 z 序的倒序(与画布叠放完全一致)。
|
||||
// 旧实现位图用 S.layers 数组序、矢量用 allObjects() 再 sort,z 相同时两者次序相反。
|
||||
const items = []
|
||||
|
||||
// 位图图层
|
||||
for (const layer of S.layers) {
|
||||
items.push({
|
||||
id: layer.id,
|
||||
type: 'bitmap',
|
||||
name: layer.name,
|
||||
opacity: layer.opacity,
|
||||
visible: layer.visible,
|
||||
z: layer.z,
|
||||
color: layer.color || S.color,
|
||||
lineWidth: layer.lineWidth || S.brushSize,
|
||||
active: layer.id === (S.selectedLayerId || S.activeLayerId),
|
||||
locked: !!layer.locked,
|
||||
canDelete: S.layers.length > 1
|
||||
})
|
||||
for (const { kind, obj } of layerUnitsDesc()) {
|
||||
if (kind === 'bitmap') {
|
||||
items.push({
|
||||
id: obj.id, type: 'bitmap', name: obj.name,
|
||||
opacity: obj.opacity, visible: obj.visible, z: obj.z,
|
||||
color: obj.color || S.color, lineWidth: obj.lineWidth || S.brushSize,
|
||||
// 高亮只代表「选中」;画笔作用目标独立用 ✎ 标记,避免两个图层同时亮
|
||||
active: obj.id === S.selectedLayerId,
|
||||
paintTarget: obj.id === S.activeLayerId,
|
||||
locked: !!obj.locked, canDelete: S.layers.length > 1
|
||||
})
|
||||
} else {
|
||||
items.push({
|
||||
id: obj.id, type: kind,
|
||||
name: kind === 'text' ? '"' + (obj.text || '').slice(0, 12) + '"' : (obj.type === 'rect' ? '矩形' : obj.type === 'circle' ? '圆形' : obj.type === 'pen' ? '钢笔形状' : '箭头'),
|
||||
opacity: obj.opacity != null ? obj.opacity : 1,
|
||||
visible: obj.visible !== false, z: obj.z || 0,
|
||||
color: obj.color || S.color, lineWidth: obj.lw || S.brushSize,
|
||||
active: (kind === 'text' && obj.id === S.selectedTextId) || (kind === 'shape' && obj.id === S.selectedShapeId),
|
||||
paintTarget: false, locked: !!obj.locked, canDelete: true
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// 矢量对象(文字 + 形状),按 z 序排序
|
||||
const vectorObjs = allObjects()
|
||||
for (const { kind, obj } of vectorObjs) {
|
||||
items.push({
|
||||
id: obj.id,
|
||||
type: kind,
|
||||
name: kind === 'text' ? '"' + (obj.text || '').slice(0, 12) + '"' : (obj.type === 'rect' ? '矩形' : obj.type === 'circle' ? '圆形' : obj.type === 'pen' ? '钢笔形状' : '箭头'),
|
||||
opacity: obj.opacity != null ? obj.opacity : 1,
|
||||
visible: obj.visible !== false,
|
||||
z: obj.z || 0,
|
||||
color: obj.color || S.color,
|
||||
lineWidth: obj.lw || S.brushSize,
|
||||
active: (kind === 'text' && obj.id === S.selectedTextId) || (kind === 'shape' && obj.id === S.selectedShapeId),
|
||||
locked: !!obj.locked,
|
||||
canDelete: true
|
||||
})
|
||||
}
|
||||
|
||||
// 按 z 序倒序排列(最上层在最前)
|
||||
items.sort((a, b) => b.z - a.z)
|
||||
|
||||
let html = ''
|
||||
for (const item of items) {
|
||||
const activeClass = item.active ? ' active' : ''
|
||||
@@ -548,7 +518,7 @@ export function setActiveLayer(id) {
|
||||
html += `<div class="iwb-layer-item${activeClass}${visClass}" draggable="${item.id !== 'base'}" data-layer-id="${item.id}" data-layer-type="${item.type}">
|
||||
<button class="iwb-layer-vis" data-vis-id="${item.id}" data-vis-type="${item.type}" title="${item.visible ? '隐藏' : '显示'}">${visSvg}</button>
|
||||
<span class="iwb-layer-icon">${typeIconSvg}</span>
|
||||
<span class="iwb-layer-name">${item.name}</span>
|
||||
<span class="iwb-layer-name">${item.name}</span>${item.paintTarget ? '<span class="iwb-layer-paint" title="当前画笔 / 橡皮作用图层">✎</span>' : ''}
|
||||
${(item.type === 'bitmap' || item.type === 'shape') ? `<input class="iwb-layer-color" type="color" value="${item.color}" data-color-id="${item.id}" data-color-type="${item.type}" title="调整颜色">` : ''}
|
||||
${(item.type === 'bitmap' || item.type === 'shape') ? `<input class="iwb-layer-width" type="number" min="1" max="300" value="${Math.round(item.lineWidth)}" data-width-id="${item.id}" data-width-type="${item.type}" title="调整边框粗细">` : ''}
|
||||
<button class="iwb-layer-lock${item.locked ? ' active' : ''}" data-lock-id="${item.id}" data-lock-type="${item.type}" title="${item.locked ? '解锁图层' : '锁定图层'}">${lockSvg}</button>
|
||||
@@ -563,17 +533,19 @@ export function setActiveLayer(id) {
|
||||
|
||||
function reorderPanelItem(dragId, targetId) {
|
||||
if (!dragId || !targetId || dragId === targetId || dragId === 'base' || targetId === 'base') return
|
||||
const all = [...S.layers.map(obj => ({ kind: 'bitmap', obj })), ...allObjects()]
|
||||
.sort((a, b) => (a.obj.z || 0) - (b.obj.z || 0))
|
||||
const all = layerUnits()
|
||||
const baseIdx = all.findIndex(u => u.obj.id === 'base')
|
||||
if (baseIdx > 0) { const [b] = all.splice(baseIdx, 1); all.unshift(b) }
|
||||
const from = all.findIndex(item => item.obj.id === dragId)
|
||||
let to = all.findIndex(item => item.obj.id === targetId)
|
||||
if (from < 0 || to < 0) return
|
||||
const to0 = all.findIndex(item => item.obj.id === targetId)
|
||||
if (from < 0 || to0 < 0) return
|
||||
const before = all.map(u => u.obj.id).join('|')
|
||||
const [moved] = all.splice(from, 1)
|
||||
to = all.findIndex(item => item.obj.id === targetId)
|
||||
const to = Math.max(all.findIndex(item => item.obj.id === targetId), baseIdx >= 0 ? 1 : 0)
|
||||
all.splice(to, 0, moved)
|
||||
if (all.map(u => u.obj.id).join('|') === before) return
|
||||
saveSnapshot()
|
||||
all.forEach((item, index) => { item.obj.z = index })
|
||||
S.zCounter = Math.max(S.zCounter, all.length - 1)
|
||||
assignLayerZ(all)
|
||||
reorderLayerCanvas()
|
||||
sortLayersByZ()
|
||||
renderObjects()
|
||||
@@ -606,22 +578,8 @@ export function setActiveLayer(id) {
|
||||
const type = item.dataset.layerType
|
||||
if (type === 'bitmap') {
|
||||
setActiveLayer(id)
|
||||
} else if (type === 'text') {
|
||||
S.selectedLayerId = null
|
||||
S.selectedShapeId = null
|
||||
S.selectedTextId = id
|
||||
S.textDrag = null
|
||||
S.shapeDrag = null
|
||||
S.bitmapDrag = null
|
||||
renderObjects()
|
||||
renderLayerPanel()
|
||||
} else if (type === 'shape') {
|
||||
S.selectedLayerId = null
|
||||
S.selectedTextId = null
|
||||
S.selectedShapeId = id
|
||||
S.textDrag = null
|
||||
S.shapeDrag = null
|
||||
S.bitmapDrag = null
|
||||
} else if (type === 'text' || type === 'shape') {
|
||||
selectUnit(type, id)
|
||||
renderObjects()
|
||||
renderLayerPanel()
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@
|
||||
import { S, runtime } from './state.js'
|
||||
import { boxCorners, clamp, drawRotationHandles, hitRotationHandle, rotPt, unrotPt } from './viewport.js'
|
||||
import { clearOverlay, drawCropPreview } from './crop.js'
|
||||
import { drawBitmapSelection, drawVectorToCanvas, removeVecCanvas, renderLayerPanel, reorderLayerCanvas } from './layers.js'
|
||||
import { drawBitmapSelection, drawVectorToCanvas, hitBitmapAt, removeVecCanvas, renderLayerPanel, reorderLayerCanvas } from './layers.js'
|
||||
import { drawPlacementPreview } from './placement.js'
|
||||
import { drawShape } from './tools.js'
|
||||
import { saveSnapshot, snapshotState } from './history.js'
|
||||
@@ -150,22 +150,58 @@ import { isDrawableLayer } from '../vendor/layer-logic.js'
|
||||
S.shapeLayers.sort(cmp)
|
||||
}
|
||||
|
||||
/** 图层顺序的唯一真源:位图 + 矢量对象,按 z 升序。
|
||||
* 之前位图(S.layers)与矢量(allObjects)各自排序、z 由两套计数器分配,
|
||||
* 导致粘贴图片与图形 z 撞车、面板与画布叠放顺序相反、排序按钮交换 z 无效。 */
|
||||
export function layerUnits() {
|
||||
const arr = S.layers.map(obj => ({ kind: 'bitmap', obj }))
|
||||
.concat(S.textLayers.map(obj => ({ kind: 'text', obj })))
|
||||
.concat(S.shapeLayers.map(obj => ({ kind: 'shape', obj })))
|
||||
arr.sort((a, b) => (a.obj.z || 0) - (b.obj.z || 0))
|
||||
return arr
|
||||
}
|
||||
/** 同一份列表按 z 降序(最上层在前):命中测试 / 图层面板共用 */
|
||||
export function layerUnitsDesc() { return layerUnits().reverse() }
|
||||
/** 重排后按索引重新分配连续 z,并同步 zCounter —— 杜绝重复 z */
|
||||
export function assignLayerZ(units) {
|
||||
units.forEach((u, i) => { u.obj.z = i })
|
||||
S.zCounter = Math.max(S.zCounter, units.length - 1)
|
||||
}
|
||||
/** 选中态唯一入口:四个 id 一起写,杜绝「两个图层同时高亮 / 排序作用错对象」 */
|
||||
export function selectUnit(kind, id) {
|
||||
S.selectedLayerId = kind === 'bitmap' ? id : null
|
||||
S.selectedTextId = kind === 'text' ? id : null
|
||||
S.selectedShapeId = kind === 'shape' ? id : null
|
||||
S.textDrag = null
|
||||
S.shapeDrag = null
|
||||
S.bitmapDrag = null
|
||||
}
|
||||
|
||||
/** 判定一个图层单元是否命中该点(位图用矩形/像素,矢量用包围盒/形状) */
|
||||
export function hitUnit(unit, pos) {
|
||||
const { kind, obj } = unit
|
||||
if (!obj || obj.visible === false) return false
|
||||
if (kind === 'bitmap') return obj.locked ? false : hitBitmapAt(obj, pos)
|
||||
if (kind === 'text') {
|
||||
const b = textBounds(obj)
|
||||
const rot = obj.rotation || 0
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, b.x + b.w / 2, b.y + b.h / 2, rot) : [pos.x, pos.y]
|
||||
return lp[0] >= b.x - 2 && lp[0] <= b.x + b.w + 2 &&
|
||||
lp[1] >= b.y - 2 && lp[1] <= b.y + b.h + 2
|
||||
}
|
||||
return hitShapeObj(pos, obj, true)
|
||||
}
|
||||
|
||||
/** 命中测试唯一入口:位图与矢量放同一份 z 降序列表里比较,最上层优先 */
|
||||
export function hitTopmost(pos) {
|
||||
for (const unit of layerUnitsDesc()) if (hitUnit(unit, pos)) return unit
|
||||
return null
|
||||
}
|
||||
|
||||
export function hitAnyObject(pos) {
|
||||
const list = allObjects()
|
||||
for (let i = list.length - 1; i >= 0; i--) {
|
||||
const { kind, obj } = list[i]
|
||||
if (obj.visible === false) continue
|
||||
if (kind === 'text') {
|
||||
const b = textBounds(obj)
|
||||
const rot = obj.rotation || 0
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, b.x + b.w / 2, b.y + b.h / 2, rot) : [pos.x, pos.y]
|
||||
if (lp[0] >= b.x - 2 && lp[0] <= b.x + b.w + 2 &&
|
||||
lp[1] >= b.y - 2 && lp[1] <= b.y + b.h + 2) {
|
||||
return { kind, obj }
|
||||
}
|
||||
} else if (hitShapeObj(pos, obj, true)) {
|
||||
return { kind, obj }
|
||||
}
|
||||
for (const unit of layerUnitsDesc()) {
|
||||
if (unit.kind === 'bitmap') continue
|
||||
if (hitUnit(unit, pos)) return unit
|
||||
}
|
||||
return null
|
||||
}
|
||||
@@ -178,36 +214,35 @@ import { isDrawableLayer } from '../vendor/layer-logic.js'
|
||||
return null
|
||||
}
|
||||
|
||||
/** 图层顺序调整 */
|
||||
/** 图层顺序调整(置顶 / 上移 / 下移 / 置底)
|
||||
* 旧实现靠「交换相邻两项的 z」:一旦两个图层 z 相同(粘贴图片用 S.layers.length、
|
||||
* 矢量用 ++S.zCounter,必然撞车),交换后仍是同一个值 ⇒ 按钮看起来没反应;
|
||||
* 置底还会算出 -1 被不透明原图盖住。现在改成统一列表重排 + 重新分配连续 z。 */
|
||||
export function moveLayer(dir) {
|
||||
let sel = getSelectedObject()
|
||||
if (sel) { if (sel.obj.locked) return } else {
|
||||
const layer = S.layers.find(l => l.id === S.selectedLayerId && l.id !== 'base' && !l.locked)
|
||||
if (layer) sel = { kind: 'bitmap', obj: layer }
|
||||
}
|
||||
if (!sel) return
|
||||
if (sel.obj.locked) return
|
||||
const all = [...S.layers.map(o => ({ kind: 'bitmap', obj: o })), ...allObjects()].sort((a, b) => (a.obj.z || 0) - (b.obj.z || 0))
|
||||
const idx = all.findIndex(o => o.obj === sel.obj)
|
||||
if (idx < 0) return
|
||||
if ((dir === 'up' || dir === 'top') && idx === all.length - 1) return
|
||||
if ((dir === 'down' || dir === 'bottom') && idx === 0) return
|
||||
if (!sel || sel.obj.locked || sel.obj.id === 'base') return
|
||||
const units = layerUnits()
|
||||
// base 是锁定且不透明的底图,固定在最下层;任何元素都不许置底到它下面(否则等于消失)
|
||||
const baseIdx = units.findIndex(u => u.obj.id === 'base')
|
||||
if (baseIdx > 0) { const [b] = units.splice(baseIdx, 1); units.unshift(b) }
|
||||
const from = units.findIndex(u => u.obj === sel.obj)
|
||||
if (from < 0) return
|
||||
const min = baseIdx >= 0 ? 1 : 0
|
||||
let to
|
||||
if (dir === 'up') to = Math.min(from + 1, units.length - 1)
|
||||
else if (dir === 'down') to = Math.max(from - 1, min)
|
||||
else if (dir === 'top') to = units.length - 1
|
||||
else if (dir === 'bottom') to = min
|
||||
else return
|
||||
if (to === from) return
|
||||
saveSnapshot()
|
||||
if (dir === 'up') {
|
||||
const t = all[idx + 1].obj
|
||||
const tz = t.z
|
||||
t.z = sel.obj.z
|
||||
sel.obj.z = tz
|
||||
} else if (dir === 'down') {
|
||||
const t = all[idx - 1].obj
|
||||
const tz = t.z
|
||||
t.z = sel.obj.z
|
||||
sel.obj.z = tz
|
||||
} else if (dir === 'top') {
|
||||
sel.obj.z = all[all.length - 1].obj.z + 1
|
||||
} else {
|
||||
sel.obj.z = all[0].obj.z - 1
|
||||
}
|
||||
const [moved] = units.splice(from, 1)
|
||||
units.splice(to, 0, moved)
|
||||
assignLayerZ(units)
|
||||
sortLayersByZ()
|
||||
reorderLayerCanvas()
|
||||
renderObjects()
|
||||
@@ -576,7 +611,7 @@ import { isDrawableLayer } from '../vendor/layer-logic.js'
|
||||
z: ++S.zCounter
|
||||
}
|
||||
S.textLayers.push(layer)
|
||||
S.selectedTextId = layer.id
|
||||
selectUnit('text', layer.id)
|
||||
renderObjects()
|
||||
committed = true
|
||||
}
|
||||
|
||||
@@ -60,7 +60,9 @@ import { updateCursor } from './events.js'
|
||||
opacity: 1,
|
||||
visible: true,
|
||||
locked: false,
|
||||
z: S.layers.length
|
||||
// 与矢量对象 / 空白图层共用同一个 z 计数器(旧值 S.layers.length 会和 ++S.zCounter 撞车,
|
||||
// 导致粘贴的截图与图形 z 相同:排序按钮交换 z 无效、命中测试选错对象)
|
||||
z: ++S.zCounter
|
||||
}
|
||||
redrawBitmapLayer(layer)
|
||||
saveSnapshot()
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
'use strict'
|
||||
|
||||
import { S, runtime } from './state.js'
|
||||
import { allObjects, commitTextInput, getSelectedShape, getSelectedText, renderObjects, shapeBounds } from './objects.js'
|
||||
import { commitTextInput, getSelectedShape, getSelectedText, layerUnits, renderObjects, shapeBounds } from './objects.js'
|
||||
import { cancelCrop, clearOverlay, hideCropBar, showCropBar, updateCropInfo } from './crop.js'
|
||||
import { cancelImagePlacement } from './placement.js'
|
||||
import { clamp } from './viewport.js'
|
||||
@@ -246,15 +246,14 @@ import { hexToRgb, rgbToHex } from '../vendor/image-math.js'
|
||||
c.width = S.imgW
|
||||
c.height = S.imgH
|
||||
const cx = c.getContext('2d', { willReadFrequently: true })
|
||||
const units = []
|
||||
for (const l of S.layers) units.push({ z: l.z, el: l.canvas, vis: l.visible, op: l.opacity })
|
||||
for (const { obj } of allObjects()) if (obj._canvas) units.push({ z: obj.z || 0, el: obj._canvas, vis: obj.visible !== false, op: obj.opacity != null ? obj.opacity : 1 })
|
||||
units.sort((a, b) => a.z - b.z)
|
||||
// 与 DOM 叠放 / 图层面板 / 命中测试共用同一份 z 序(唯一真源)
|
||||
cx.globalAlpha = 1
|
||||
for (const u of units) {
|
||||
if (!u.vis) continue
|
||||
cx.globalAlpha = u.op == null ? 1 : u.op
|
||||
cx.drawImage(u.el, 0, 0)
|
||||
for (const u of layerUnits()) {
|
||||
if (u.obj.visible === false) continue
|
||||
const el = u.kind === 'bitmap' ? u.obj.canvas : u.obj._canvas
|
||||
if (!el) continue
|
||||
cx.globalAlpha = u.obj.opacity == null ? 1 : u.obj.opacity
|
||||
cx.drawImage(el, 0, 0)
|
||||
}
|
||||
cx.globalAlpha = 1
|
||||
return c
|
||||
|
||||
+175
-195
@@ -9,7 +9,7 @@
|
||||
var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
id: 'image-annotate',
|
||||
name: '标注 / 修图',
|
||||
version: '1.0.4',
|
||||
version: '1.0.5',
|
||||
apiVersion: 1,
|
||||
capabilities: ['commands', 'nodeActions', 'canvas', 'canvasWrite'],
|
||||
}
|
||||
@@ -161,7 +161,9 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
opacity: 1,
|
||||
visible: true,
|
||||
locked: false,
|
||||
z: S.layers.length
|
||||
// 与矢量对象 / 空白图层共用同一个 z 计数器(旧值 S.layers.length 会和 ++S.zCounter 撞车,
|
||||
// 导致粘贴的截图与图形 z 相同:排序按钮交换 z 无效、命中测试选错对象)
|
||||
z: ++S.zCounter
|
||||
};
|
||||
redrawBitmapLayer(layer);
|
||||
saveSnapshot();
|
||||
@@ -373,14 +375,11 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
return c;
|
||||
}
|
||||
function reorderLayerCanvas() {
|
||||
const units = [];
|
||||
for (const l of S.layers) units.push({ z: l.z, el: l.canvas, vis: l.visible });
|
||||
for (const { obj } of allObjects()) if (obj._canvas) units.push({ z: obj.z || 0, el: obj._canvas, vis: obj.visible !== false });
|
||||
units.sort((a, b) => a.z - b.z);
|
||||
for (const u of units) {
|
||||
if (!u.el) continue;
|
||||
S.canvasStack.insertBefore(u.el, S.strokeCanvas);
|
||||
u.el.style.display = u.vis ? "block" : "none";
|
||||
for (const u of layerUnits()) {
|
||||
const el = u.kind === "bitmap" ? u.obj.canvas : u.obj._canvas;
|
||||
if (!el) continue;
|
||||
S.canvasStack.insertBefore(el, S.strokeCanvas);
|
||||
el.style.display = u.obj.visible === false ? "none" : "block";
|
||||
}
|
||||
}
|
||||
function getActiveLayer() {
|
||||
@@ -493,37 +492,22 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
}
|
||||
return { x: layer.x, y: layer.y, w: layer.w, h: layer.h };
|
||||
}
|
||||
function hitBitmapLayer(pos) {
|
||||
const layers = [...S.layers].sort((a, b) => (a.z || 0) - (b.z || 0));
|
||||
const hitRect = (layer) => {
|
||||
if (!(layer.w > 0 && layer.h > 0)) return false;
|
||||
const rot = layer.rotation || 0;
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, layer.x + layer.w / 2, layer.y + layer.h / 2, rot) : [pos.x, pos.y];
|
||||
return lp[0] >= layer.x && lp[0] <= layer.x + layer.w && lp[1] >= layer.y && lp[1] <= layer.y + layer.h;
|
||||
};
|
||||
const usable = (layer) => layer && layer.visible !== false && !layer.locked;
|
||||
for (let i = layers.length - 1; i >= 0; i--) {
|
||||
const layer = layers[i];
|
||||
if (!usable(layer)) continue;
|
||||
if (!isDrawableLayer(layer)) continue;
|
||||
if (!hitRect(layer)) continue;
|
||||
const rot = layer.rotation || 0;
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, layer.x + layer.w / 2, layer.y + layer.h / 2, rot) : [pos.x, pos.y];
|
||||
const px = Math.round(lp[0]), py = Math.round(lp[1]);
|
||||
if (px < 0 || py < 0 || px >= layer.canvas.width || py >= layer.canvas.height) continue;
|
||||
try {
|
||||
const d = layer.ctx.getImageData(px, py, 1, 1).data;
|
||||
if (d[3] > 8) return layer;
|
||||
} catch (e) {
|
||||
return layer;
|
||||
}
|
||||
function hitBitmapAt(layer, pos) {
|
||||
if (!layer || layer.visible === false || layer.locked) return false;
|
||||
const drawable = isDrawableLayer(layer);
|
||||
if (!drawable && !layer.transformable) return false;
|
||||
if (!(layer.w > 0 && layer.h > 0)) return false;
|
||||
const rot = layer.rotation || 0;
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, layer.x + layer.w / 2, layer.y + layer.h / 2, rot) : [pos.x, pos.y];
|
||||
if (lp[0] < layer.x || lp[0] > layer.x + layer.w || lp[1] < layer.y || lp[1] > layer.y + layer.h) return false;
|
||||
if (!drawable) return true;
|
||||
const px = Math.round(lp[0]), py = Math.round(lp[1]);
|
||||
if (px < 0 || py < 0 || px >= layer.canvas.width || py >= layer.canvas.height) return false;
|
||||
try {
|
||||
return layer.ctx.getImageData(px, py, 1, 1).data[3] > 8;
|
||||
} catch (e) {
|
||||
return true;
|
||||
}
|
||||
for (let i = layers.length - 1; i >= 0; i--) {
|
||||
const layer = layers[i];
|
||||
if (!usable(layer) || !layer.transformable) continue;
|
||||
if (hitRect(layer)) return layer;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
function drawBitmapSelection(layer) {
|
||||
if (!layer) return;
|
||||
@@ -656,11 +640,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
}
|
||||
function setActiveLayer(id) {
|
||||
S.activeLayerId = id;
|
||||
S.selectedLayerId = id;
|
||||
S.selectedTextId = null;
|
||||
S.selectedShapeId = null;
|
||||
S.textDrag = null;
|
||||
S.shapeDrag = null;
|
||||
selectUnit("bitmap", id);
|
||||
renderLayerPanel();
|
||||
renderObjects();
|
||||
}
|
||||
@@ -703,12 +683,9 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
saveSnapshot();
|
||||
if (layer.canvas.parentNode) layer.canvas.parentNode.removeChild(layer.canvas);
|
||||
S.layers.splice(idx, 1);
|
||||
S.layers.forEach((l, i) => {
|
||||
l.z = i;
|
||||
});
|
||||
if (S.activeLayerId === id) {
|
||||
S.activeLayerId = S.layers[S.layers.length - 1].id;
|
||||
}
|
||||
assignLayerZ(layerUnits());
|
||||
if (S.activeLayerId === id) S.activeLayerId = S.layers[S.layers.length - 1].id;
|
||||
if (S.selectedLayerId === id) S.selectedLayerId = null;
|
||||
reorderLayerCanvas();
|
||||
renderLayerPanel();
|
||||
}
|
||||
@@ -807,38 +784,40 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
const list = runtime.el.querySelector(".iwb-editor-layer-list");
|
||||
if (!list) return;
|
||||
const items = [];
|
||||
for (const layer of S.layers) {
|
||||
items.push({
|
||||
id: layer.id,
|
||||
type: "bitmap",
|
||||
name: layer.name,
|
||||
opacity: layer.opacity,
|
||||
visible: layer.visible,
|
||||
z: layer.z,
|
||||
color: layer.color || S.color,
|
||||
lineWidth: layer.lineWidth || S.brushSize,
|
||||
active: layer.id === (S.selectedLayerId || S.activeLayerId),
|
||||
locked: !!layer.locked,
|
||||
canDelete: S.layers.length > 1
|
||||
});
|
||||
for (const { kind, obj } of layerUnitsDesc()) {
|
||||
if (kind === "bitmap") {
|
||||
items.push({
|
||||
id: obj.id,
|
||||
type: "bitmap",
|
||||
name: obj.name,
|
||||
opacity: obj.opacity,
|
||||
visible: obj.visible,
|
||||
z: obj.z,
|
||||
color: obj.color || S.color,
|
||||
lineWidth: obj.lineWidth || S.brushSize,
|
||||
// 高亮只代表「选中」;画笔作用目标独立用 ✎ 标记,避免两个图层同时亮
|
||||
active: obj.id === S.selectedLayerId,
|
||||
paintTarget: obj.id === S.activeLayerId,
|
||||
locked: !!obj.locked,
|
||||
canDelete: S.layers.length > 1
|
||||
});
|
||||
} else {
|
||||
items.push({
|
||||
id: obj.id,
|
||||
type: kind,
|
||||
name: kind === "text" ? '"' + (obj.text || "").slice(0, 12) + '"' : obj.type === "rect" ? "矩形" : obj.type === "circle" ? "圆形" : obj.type === "pen" ? "钢笔形状" : "箭头",
|
||||
opacity: obj.opacity != null ? obj.opacity : 1,
|
||||
visible: obj.visible !== false,
|
||||
z: obj.z || 0,
|
||||
color: obj.color || S.color,
|
||||
lineWidth: obj.lw || S.brushSize,
|
||||
active: kind === "text" && obj.id === S.selectedTextId || kind === "shape" && obj.id === S.selectedShapeId,
|
||||
paintTarget: false,
|
||||
locked: !!obj.locked,
|
||||
canDelete: true
|
||||
});
|
||||
}
|
||||
}
|
||||
const vectorObjs = allObjects();
|
||||
for (const { kind, obj } of vectorObjs) {
|
||||
items.push({
|
||||
id: obj.id,
|
||||
type: kind,
|
||||
name: kind === "text" ? '"' + (obj.text || "").slice(0, 12) + '"' : obj.type === "rect" ? "矩形" : obj.type === "circle" ? "圆形" : obj.type === "pen" ? "钢笔形状" : "箭头",
|
||||
opacity: obj.opacity != null ? obj.opacity : 1,
|
||||
visible: obj.visible !== false,
|
||||
z: obj.z || 0,
|
||||
color: obj.color || S.color,
|
||||
lineWidth: obj.lw || S.brushSize,
|
||||
active: kind === "text" && obj.id === S.selectedTextId || kind === "shape" && obj.id === S.selectedShapeId,
|
||||
locked: !!obj.locked,
|
||||
canDelete: true
|
||||
});
|
||||
}
|
||||
items.sort((a, b) => b.z - a.z);
|
||||
let html = "";
|
||||
for (const item of items) {
|
||||
const activeClass = item.active ? " active" : "";
|
||||
@@ -859,7 +838,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
html += `<div class="iwb-layer-item${activeClass}${visClass}" draggable="${item.id !== "base"}" data-layer-id="${item.id}" data-layer-type="${item.type}">
|
||||
<button class="iwb-layer-vis" data-vis-id="${item.id}" data-vis-type="${item.type}" title="${item.visible ? "隐藏" : "显示"}">${visSvg}</button>
|
||||
<span class="iwb-layer-icon">${typeIconSvg}</span>
|
||||
<span class="iwb-layer-name">${item.name}</span>
|
||||
<span class="iwb-layer-name">${item.name}</span>${item.paintTarget ? '<span class="iwb-layer-paint" title="当前画笔 / 橡皮作用图层">✎</span>' : ""}
|
||||
${item.type === "bitmap" || item.type === "shape" ? `<input class="iwb-layer-color" type="color" value="${item.color}" data-color-id="${item.id}" data-color-type="${item.type}" title="调整颜色">` : ""}
|
||||
${item.type === "bitmap" || item.type === "shape" ? `<input class="iwb-layer-width" type="number" min="1" max="300" value="${Math.round(item.lineWidth)}" data-width-id="${item.id}" data-width-type="${item.type}" title="调整边框粗细">` : ""}
|
||||
<button class="iwb-layer-lock${item.locked ? " active" : ""}" data-lock-id="${item.id}" data-lock-type="${item.type}" title="${item.locked ? "解锁图层" : "锁定图层"}">${lockSvg}</button>
|
||||
@@ -871,18 +850,22 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
}
|
||||
function reorderPanelItem(dragId, targetId) {
|
||||
if (!dragId || !targetId || dragId === targetId || dragId === "base" || targetId === "base") return;
|
||||
const all = [...S.layers.map((obj) => ({ kind: "bitmap", obj })), ...allObjects()].sort((a, b) => (a.obj.z || 0) - (b.obj.z || 0));
|
||||
const all = layerUnits();
|
||||
const baseIdx = all.findIndex((u) => u.obj.id === "base");
|
||||
if (baseIdx > 0) {
|
||||
const [b] = all.splice(baseIdx, 1);
|
||||
all.unshift(b);
|
||||
}
|
||||
const from = all.findIndex((item) => item.obj.id === dragId);
|
||||
let to = all.findIndex((item) => item.obj.id === targetId);
|
||||
if (from < 0 || to < 0) return;
|
||||
const to0 = all.findIndex((item) => item.obj.id === targetId);
|
||||
if (from < 0 || to0 < 0) return;
|
||||
const before = all.map((u) => u.obj.id).join("|");
|
||||
const [moved] = all.splice(from, 1);
|
||||
to = all.findIndex((item) => item.obj.id === targetId);
|
||||
const to = Math.max(all.findIndex((item) => item.obj.id === targetId), baseIdx >= 0 ? 1 : 0);
|
||||
all.splice(to, 0, moved);
|
||||
if (all.map((u) => u.obj.id).join("|") === before) return;
|
||||
saveSnapshot();
|
||||
all.forEach((item, index) => {
|
||||
item.obj.z = index;
|
||||
});
|
||||
S.zCounter = Math.max(S.zCounter, all.length - 1);
|
||||
assignLayerZ(all);
|
||||
reorderLayerCanvas();
|
||||
sortLayersByZ();
|
||||
renderObjects();
|
||||
@@ -920,22 +903,8 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
const type = item.dataset.layerType;
|
||||
if (type === "bitmap") {
|
||||
setActiveLayer(id);
|
||||
} else if (type === "text") {
|
||||
S.selectedLayerId = null;
|
||||
S.selectedShapeId = null;
|
||||
S.selectedTextId = id;
|
||||
S.textDrag = null;
|
||||
S.shapeDrag = null;
|
||||
S.bitmapDrag = null;
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
} else if (type === "shape") {
|
||||
S.selectedLayerId = null;
|
||||
S.selectedTextId = null;
|
||||
S.selectedShapeId = id;
|
||||
S.textDrag = null;
|
||||
S.shapeDrag = null;
|
||||
S.bitmapDrag = null;
|
||||
} else if (type === "text" || type === "shape") {
|
||||
selectUnit(type, id);
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
}
|
||||
@@ -1376,47 +1345,29 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
beginShapeHandleDrag(shH, pos);
|
||||
return;
|
||||
}
|
||||
const hit = hitAnyObject(pos);
|
||||
if (hit) {
|
||||
S.selectedLayerId = null;
|
||||
if (hit.kind === "text") {
|
||||
S.selectedShapeId = null;
|
||||
S.selectedTextId = hit.obj.id;
|
||||
if (hit.obj.locked) {
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
return;
|
||||
}
|
||||
S.textDrag = { mode: "move", start: pos, layer: { x: hit.obj.x, y: hit.obj.y }, pending: snapshotState() };
|
||||
} else {
|
||||
S.selectedTextId = null;
|
||||
S.selectedShapeId = hit.obj.id;
|
||||
if (hit.obj.locked) {
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
return;
|
||||
}
|
||||
S.shapeDrag = { mode: "move", start: pos, shape: { ...hit.obj }, pending: snapshotState() };
|
||||
}
|
||||
const hit = hitTopmost(pos);
|
||||
if (!hit) {
|
||||
selectUnit(null, null);
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
} else {
|
||||
const bitmap = hitBitmapLayer(pos);
|
||||
if (bitmap) {
|
||||
S.selectedLayerId = bitmap.id;
|
||||
S.activeLayerId = bitmap.id;
|
||||
S.selectedTextId = null;
|
||||
S.selectedShapeId = null;
|
||||
beginBitmapDrag(null, pos);
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
} else if (S.selectedLayerId || S.selectedTextId || S.selectedShapeId) {
|
||||
clearSelection();
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
}
|
||||
return;
|
||||
}
|
||||
return;
|
||||
selectUnit(hit.kind, hit.obj.id);
|
||||
if (hit.obj.locked) {
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
return;
|
||||
}
|
||||
if (hit.kind === "bitmap") {
|
||||
S.activeLayerId = hit.obj.id;
|
||||
beginBitmapDrag(null, pos);
|
||||
} else if (hit.kind === "text") {
|
||||
S.textDrag = { mode: "move", start: pos, layer: { x: hit.obj.x, y: hit.obj.y }, pending: snapshotState() };
|
||||
} else {
|
||||
S.shapeDrag = { mode: "move", start: pos, shape: { ...hit.obj }, pending: snapshotState() };
|
||||
}
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
}
|
||||
function onCropMouseDown(pos) {
|
||||
if (S.cropRect) {
|
||||
@@ -1445,7 +1396,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
}
|
||||
const hit = hitTextLayer(pos);
|
||||
if (hit) {
|
||||
S.selectedTextId = hit.id;
|
||||
selectUnit("text", hit.id);
|
||||
if (hit.locked) {
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
@@ -1874,7 +1825,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
z: ++S.zCounter
|
||||
};
|
||||
S.shapeLayers.push(shape);
|
||||
S.selectedShapeId = shape.id;
|
||||
selectUnit("shape", shape.id);
|
||||
}
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
@@ -2368,7 +2319,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
saveSnapshot();
|
||||
const shape = { id: "s" + Date.now() + Math.floor(Math.random() * 1e3), type: "pen", pts: S.penPath.pts.slice(), closed: !!S.penPath.closed, color: S.color, lw: Math.max(2, S.brushSize), fill: !!S.penPath.closed, opacity: 1, visible: true, locked: false, z: ++S.zCounter };
|
||||
S.shapeLayers.push(shape);
|
||||
S.selectedShapeId = shape.id;
|
||||
selectUnit("shape", shape.id);
|
||||
S.penPath = null;
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
@@ -2427,7 +2378,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
if (S.tool !== "text" && S.tool !== "select") return;
|
||||
const hit = hitTextLayer(pos);
|
||||
if (hit) {
|
||||
S.selectedTextId = hit.id;
|
||||
selectUnit("text", hit.id);
|
||||
if (hit.locked) return;
|
||||
renderObjects();
|
||||
showTextInput(hit.x, hit.y, hit);
|
||||
@@ -3045,21 +2996,48 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
S.textLayers.sort(cmp);
|
||||
S.shapeLayers.sort(cmp);
|
||||
}
|
||||
function layerUnits() {
|
||||
const arr = S.layers.map((obj) => ({ kind: "bitmap", obj })).concat(S.textLayers.map((obj) => ({ kind: "text", obj }))).concat(S.shapeLayers.map((obj) => ({ kind: "shape", obj })));
|
||||
arr.sort((a, b) => (a.obj.z || 0) - (b.obj.z || 0));
|
||||
return arr;
|
||||
}
|
||||
function layerUnitsDesc() {
|
||||
return layerUnits().reverse();
|
||||
}
|
||||
function assignLayerZ(units) {
|
||||
units.forEach((u, i) => {
|
||||
u.obj.z = i;
|
||||
});
|
||||
S.zCounter = Math.max(S.zCounter, units.length - 1);
|
||||
}
|
||||
function selectUnit(kind, id) {
|
||||
S.selectedLayerId = kind === "bitmap" ? id : null;
|
||||
S.selectedTextId = kind === "text" ? id : null;
|
||||
S.selectedShapeId = kind === "shape" ? id : null;
|
||||
S.textDrag = null;
|
||||
S.shapeDrag = null;
|
||||
S.bitmapDrag = null;
|
||||
}
|
||||
function hitUnit(unit, pos) {
|
||||
const { kind, obj } = unit;
|
||||
if (!obj || obj.visible === false) return false;
|
||||
if (kind === "bitmap") return obj.locked ? false : hitBitmapAt(obj, pos);
|
||||
if (kind === "text") {
|
||||
const b = textBounds(obj);
|
||||
const rot = obj.rotation || 0;
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, b.x + b.w / 2, b.y + b.h / 2, rot) : [pos.x, pos.y];
|
||||
return lp[0] >= b.x - 2 && lp[0] <= b.x + b.w + 2 && lp[1] >= b.y - 2 && lp[1] <= b.y + b.h + 2;
|
||||
}
|
||||
return hitShapeObj(pos, obj, true);
|
||||
}
|
||||
function hitTopmost(pos) {
|
||||
for (const unit of layerUnitsDesc()) if (hitUnit(unit, pos)) return unit;
|
||||
return null;
|
||||
}
|
||||
function hitAnyObject(pos) {
|
||||
const list = allObjects();
|
||||
for (let i = list.length - 1; i >= 0; i--) {
|
||||
const { kind, obj } = list[i];
|
||||
if (obj.visible === false) continue;
|
||||
if (kind === "text") {
|
||||
const b = textBounds(obj);
|
||||
const rot = obj.rotation || 0;
|
||||
const lp = rot ? unrotPt(pos.x, pos.y, b.x + b.w / 2, b.y + b.h / 2, rot) : [pos.x, pos.y];
|
||||
if (lp[0] >= b.x - 2 && lp[0] <= b.x + b.w + 2 && lp[1] >= b.y - 2 && lp[1] <= b.y + b.h + 2) {
|
||||
return { kind, obj };
|
||||
}
|
||||
} else if (hitShapeObj(pos, obj, true)) {
|
||||
return { kind, obj };
|
||||
}
|
||||
for (const unit of layerUnitsDesc()) {
|
||||
if (unit.kind === "bitmap") continue;
|
||||
if (hitUnit(unit, pos)) return unit;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -3078,29 +3056,27 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
const layer = S.layers.find((l) => l.id === S.selectedLayerId && l.id !== "base" && !l.locked);
|
||||
if (layer) sel = { kind: "bitmap", obj: layer };
|
||||
}
|
||||
if (!sel) return;
|
||||
if (sel.obj.locked) return;
|
||||
const all = [...S.layers.map((o) => ({ kind: "bitmap", obj: o })), ...allObjects()].sort((a, b) => (a.obj.z || 0) - (b.obj.z || 0));
|
||||
const idx = all.findIndex((o) => o.obj === sel.obj);
|
||||
if (idx < 0) return;
|
||||
if ((dir === "up" || dir === "top") && idx === all.length - 1) return;
|
||||
if ((dir === "down" || dir === "bottom") && idx === 0) return;
|
||||
saveSnapshot();
|
||||
if (dir === "up") {
|
||||
const t = all[idx + 1].obj;
|
||||
const tz = t.z;
|
||||
t.z = sel.obj.z;
|
||||
sel.obj.z = tz;
|
||||
} else if (dir === "down") {
|
||||
const t = all[idx - 1].obj;
|
||||
const tz = t.z;
|
||||
t.z = sel.obj.z;
|
||||
sel.obj.z = tz;
|
||||
} else if (dir === "top") {
|
||||
sel.obj.z = all[all.length - 1].obj.z + 1;
|
||||
} else {
|
||||
sel.obj.z = all[0].obj.z - 1;
|
||||
if (!sel || sel.obj.locked || sel.obj.id === "base") return;
|
||||
const units = layerUnits();
|
||||
const baseIdx = units.findIndex((u) => u.obj.id === "base");
|
||||
if (baseIdx > 0) {
|
||||
const [b] = units.splice(baseIdx, 1);
|
||||
units.unshift(b);
|
||||
}
|
||||
const from = units.findIndex((u) => u.obj === sel.obj);
|
||||
if (from < 0) return;
|
||||
const min = baseIdx >= 0 ? 1 : 0;
|
||||
let to;
|
||||
if (dir === "up") to = Math.min(from + 1, units.length - 1);
|
||||
else if (dir === "down") to = Math.max(from - 1, min);
|
||||
else if (dir === "top") to = units.length - 1;
|
||||
else if (dir === "bottom") to = min;
|
||||
else return;
|
||||
if (to === from) return;
|
||||
saveSnapshot();
|
||||
const [moved] = units.splice(from, 1);
|
||||
units.splice(to, 0, moved);
|
||||
assignLayerZ(units);
|
||||
sortLayersByZ();
|
||||
reorderLayerCanvas();
|
||||
renderObjects();
|
||||
@@ -3474,7 +3450,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
z: ++S.zCounter
|
||||
};
|
||||
S.textLayers.push(layer);
|
||||
S.selectedTextId = layer.id;
|
||||
selectUnit("text", layer.id);
|
||||
renderObjects();
|
||||
committed = true;
|
||||
}
|
||||
@@ -3756,15 +3732,13 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
c.width = S.imgW;
|
||||
c.height = S.imgH;
|
||||
const cx = c.getContext("2d", { willReadFrequently: true });
|
||||
const units = [];
|
||||
for (const l of S.layers) units.push({ z: l.z, el: l.canvas, vis: l.visible, op: l.opacity });
|
||||
for (const { obj } of allObjects()) if (obj._canvas) units.push({ z: obj.z || 0, el: obj._canvas, vis: obj.visible !== false, op: obj.opacity != null ? obj.opacity : 1 });
|
||||
units.sort((a, b) => a.z - b.z);
|
||||
cx.globalAlpha = 1;
|
||||
for (const u of units) {
|
||||
if (!u.vis) continue;
|
||||
cx.globalAlpha = u.op == null ? 1 : u.op;
|
||||
cx.drawImage(u.el, 0, 0);
|
||||
for (const u of layerUnits()) {
|
||||
if (u.obj.visible === false) continue;
|
||||
const el = u.kind === "bitmap" ? u.obj.canvas : u.obj._canvas;
|
||||
if (!el) continue;
|
||||
cx.globalAlpha = u.obj.opacity == null ? 1 : u.obj.opacity;
|
||||
cx.drawImage(el, 0, 0);
|
||||
}
|
||||
cx.globalAlpha = 1;
|
||||
return c;
|
||||
@@ -4168,6 +4142,8 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
S.textDrag = null;
|
||||
S.selectedShapeId = null;
|
||||
S.shapeDrag = null;
|
||||
S.selectedLayerId = null;
|
||||
S.bitmapDrag = null;
|
||||
S.zoom = 1;
|
||||
setupCanvas(cropC);
|
||||
S.history = [];
|
||||
@@ -4357,6 +4333,8 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
S.shapeLayers = [];
|
||||
S.selectedShapeId = null;
|
||||
S.shapeDrag = null;
|
||||
S.selectedLayerId = null;
|
||||
S.bitmapDrag = null;
|
||||
renderObjects();
|
||||
renderLayerPanel();
|
||||
clearOverlay();
|
||||
@@ -4429,6 +4407,8 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
S.penDragPt = -1;
|
||||
S.penDragBase = null;
|
||||
S.penDragPending = null;
|
||||
S.selectedLayerId = null;
|
||||
S.bitmapDrag = null;
|
||||
S.zCounter = Math.max(S.layers.length - 1, 0);
|
||||
S.placingImage = null;
|
||||
S.placingHandle = null;
|
||||
@@ -4552,7 +4532,7 @@ var __IWB_ANNOTATE_MANIFEST__ = {
|
||||
};
|
||||
|
||||
// image-annotate/plugin/editor.css
|
||||
var editor_default = '/* IWB 鲜艺抠图节点 - 注入样式 */\n\n/* 抠图按钮 */\n.iwb-cutout-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n\n.iwb-cutout-btn:hover {\n opacity: 1;\n transform: scale(1.15);\n}\n\n.iwb-cutout-btn svg {\n width: 14px;\n height: 14px;\n display: block;\n}\n\n/* 正在处理中的按钮状态 */\n.iwb-cutout-btn.processing {\n animation: iwb-cutout-pulse 1s infinite;\n pointer-events: none;\n}\n\n@keyframes iwb-cutout-pulse {\n 0%, 100% { opacity: 0.4; }\n 50% { opacity: 1; }\n}\n\n/* 全屏遮罩 */\n.iwb-cutout-overlay {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.25);\n display: none;\n align-items: center;\n justify-content: center;\n flex-direction: column;\n gap: 12px;\n z-index: 999999;\n pointer-events: all;\n backdrop-filter: blur(2px);\n}\n\n/* 自定义按钮提示(挂在 body,最高层级,不会被图片遮挡) */\n#iwb-cutout-tip {\n position: fixed;\n z-index: 2147483647;\n background: rgba(30, 30, 46, 0.95);\n color: #fff;\n padding: 5px 10px;\n border-radius: 6px;\n font-size: 12px;\n line-height: 1.5;\n white-space: nowrap;\n pointer-events: none;\n display: none;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);\n border: 1px solid rgba(255, 255, 255, 0.14);\n font-family: system-ui, -apple-system, "Segoe UI", sans-serif;\n max-width: 320px;\n white-space: normal;\n word-break: break-all;\n}\n\n/* 加载动画 */\n.iwb-cutout-spinner {\n width: 48px;\n height: 48px;\n border: 4px solid rgba(255, 255, 255, 0.3);\n border-top-color: #e8a735;\n border-radius: 50%;\n animation: iwb-cutout-spin 0.8s linear infinite;\n}\n\n@keyframes iwb-cutout-spin {\n to { transform: rotate(360deg); }\n}\n\n/* 遮罩文字 */\n.iwb-cutout-text {\n color: #fff;\n font-size: 14px;\n font-weight: 500;\n text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);\n background: rgba(0, 0, 0, 0.6);\n padding: 8px 16px;\n border-radius: 6px;\n}\n\n/* Toast 提示 */\n.iwb-cutout-toast {\n position: fixed;\n bottom: 24px;\n left: 50%;\n transform: translateX(-50%);\n padding: 10px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-weight: 500;\n z-index: 999999;\n transition: opacity 0.3s;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);\n display: none;\n max-width: 400px;\n}\n\n.iwb-cutout-toast-success {\n background: #1a7f37;\n color: #fff;\n}\n\n.iwb-cutout-toast-error {\n background: #cf222e;\n color: #fff;\n}\n\n.iwb-cutout-toast-info {\n background: #1f6feb;\n color: #fff;\n}\n\n/* 白底按钮 */\n.iwb-whitebg-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-whitebg-btn:hover { opacity: 1; transform: scale(1.15); }\n.iwb-whitebg-btn svg { width: 14px; height: 14px; display: block; }\n.iwb-whitebg-btn.processing { animation: iwb-cutout-pulse 1s infinite; pointer-events: none; }\n\n/* 编辑按钮 */\n.iwb-editor-btn-icon {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-editor-btn-icon:hover { opacity: 1; transform: scale(1.15); }\n.iwb-editor-btn-icon svg { width: 14px; height: 14px; display: block; }\n\n/* ==================== 图片编辑器 ==================== */\n\n.iwb-editor-overlay {\n position: fixed;\n top: 0; left: 0;\n width: 100%; height: 100%;\n background: rgba(0, 0, 0, 0.6);\n display: none;\n align-items: center;\n justify-content: center;\n z-index: 999998;\n backdrop-filter: blur(3px);\n}\n\n.iwb-editor-modal {\n background: #1e1e2e;\n border-radius: 0;\n box-shadow: none;\n display: flex;\n flex-direction: column;\n width: 100vw;\n height: 100vh;\n max-width: none;\n max-height: none;\n overflow: visible;\n}\n\n/* 新增:主区域(画布 + 图层面板)flex 布局 */\n.iwb-editor-main {\n display: flex;\n flex: 1 1 auto;\n min-height: 0;\n overflow: visible;\n}\n\n.iwb-editor-header {\n display: flex;\n align-items: center;\n gap: 12px;\n padding: 10px 16px;\n background: #181825;\n border-bottom: 1px solid #313244;\n flex-wrap: nowrap;\n}\n.iwb-editor-header > .iwb-editor-tools {\n position: absolute;\n left: 12px;\n top: 68px;\n width: 44px;\n flex-direction: column;\n padding: 6px;\n background: #181825;\n border: 1px solid #313244;\n border-radius: 8px;\n z-index: 20;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action { position: static; }\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="redo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="reset"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="restore"] {\n position: absolute;\n top: -54px;\n left: auto;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"] { right: 154px; }\n.iwb-editor-header > .iwb-edLine truncated
|
||||
var editor_default = '/* IWB 鲜艺抠图节点 - 注入样式 */\n\n/* 抠图按钮 */\n.iwb-cutout-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n\n.iwb-cutout-btn:hover {\n opacity: 1;\n transform: scale(1.15);\n}\n\n.iwb-cutout-btn svg {\n width: 14px;\n height: 14px;\n display: block;\n}\n\n/* 正在处理中的按钮状态 */\n.iwb-cutout-btn.processing {\n animation: iwb-cutout-pulse 1s infinite;\n pointer-events: none;\n}\n\n@keyframes iwb-cutout-pulse {\n 0%, 100% { opacity: 0.4; }\n 50% { opacity: 1; }\n}\n\n/* 全屏遮罩 */\n.iwb-cutout-overlay {\n position: fixed;\n top: 0;\n left: 0;\n width: 100%;\n height: 100%;\n background: rgba(0, 0, 0, 0.25);\n display: none;\n align-items: center;\n justify-content: center;\n flex-direction: column;\n gap: 12px;\n z-index: 999999;\n pointer-events: all;\n backdrop-filter: blur(2px);\n}\n\n/* 自定义按钮提示(挂在 body,最高层级,不会被图片遮挡) */\n#iwb-cutout-tip {\n position: fixed;\n z-index: 2147483647;\n background: rgba(30, 30, 46, 0.95);\n color: #fff;\n padding: 5px 10px;\n border-radius: 6px;\n font-size: 12px;\n line-height: 1.5;\n white-space: nowrap;\n pointer-events: none;\n display: none;\n box-shadow: 0 2px 8px rgba(0, 0, 0, 0.35);\n border: 1px solid rgba(255, 255, 255, 0.14);\n font-family: system-ui, -apple-system, "Segoe UI", sans-serif;\n max-width: 320px;\n white-space: normal;\n word-break: break-all;\n}\n\n/* 加载动画 */\n.iwb-cutout-spinner {\n width: 48px;\n height: 48px;\n border: 4px solid rgba(255, 255, 255, 0.3);\n border-top-color: #e8a735;\n border-radius: 50%;\n animation: iwb-cutout-spin 0.8s linear infinite;\n}\n\n@keyframes iwb-cutout-spin {\n to { transform: rotate(360deg); }\n}\n\n/* 遮罩文字 */\n.iwb-cutout-text {\n color: #fff;\n font-size: 14px;\n font-weight: 500;\n text-shadow: 0 1px 3px rgba(0, 0, 0, 0.5);\n background: rgba(0, 0, 0, 0.6);\n padding: 8px 16px;\n border-radius: 6px;\n}\n\n/* Toast 提示 */\n.iwb-cutout-toast {\n position: fixed;\n bottom: 24px;\n left: 50%;\n transform: translateX(-50%);\n padding: 10px 20px;\n border-radius: 8px;\n font-size: 13px;\n font-weight: 500;\n z-index: 999999;\n transition: opacity 0.3s;\n box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);\n display: none;\n max-width: 400px;\n}\n\n.iwb-cutout-toast-success {\n background: #1a7f37;\n color: #fff;\n}\n\n.iwb-cutout-toast-error {\n background: #cf222e;\n color: #fff;\n}\n\n.iwb-cutout-toast-info {\n background: #1f6feb;\n color: #fff;\n}\n\n/* 白底按钮 */\n.iwb-whitebg-btn {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-whitebg-btn:hover { opacity: 1; transform: scale(1.15); }\n.iwb-whitebg-btn svg { width: 14px; height: 14px; display: block; }\n.iwb-whitebg-btn.processing { animation: iwb-cutout-pulse 1s infinite; pointer-events: none; }\n\n/* 编辑按钮 */\n.iwb-editor-btn-icon {\n cursor: pointer;\n opacity: 0.7;\n transition: opacity 0.15s, transform 0.15s;\n}\n.iwb-editor-btn-icon:hover { opacity: 1; transform: scale(1.15); }\n.iwb-editor-btn-icon svg { width: 14px; height: 14px; display: block; }\n\n/* ==================== 图片编辑器 ==================== */\n\n.iwb-editor-overlay {\n position: fixed;\n top: 0; left: 0;\n width: 100%; height: 100%;\n background: rgba(0, 0, 0, 0.6);\n display: none;\n align-items: center;\n justify-content: center;\n z-index: 999998;\n backdrop-filter: blur(3px);\n}\n\n.iwb-editor-modal {\n background: #1e1e2e;\n border-radius: 0;\n box-shadow: none;\n display: flex;\n flex-direction: column;\n width: 100vw;\n height: 100vh;\n max-width: none;\n max-height: none;\n overflow: visible;\n}\n\n/* 新增:主区域(画布 + 图层面板)flex 布局 */\n.iwb-editor-main {\n display: flex;\n flex: 1 1 auto;\n min-height: 0;\n overflow: visible;\n}\n\n.iwb-editor-header {\n display: flex;\n align-items: center;\n gap: 12px;\n padding: 10px 16px;\n background: #181825;\n border-bottom: 1px solid #313244;\n flex-wrap: nowrap;\n}\n.iwb-editor-header > .iwb-editor-tools {\n position: absolute;\n left: 12px;\n top: 68px;\n width: 44px;\n flex-direction: column;\n padding: 6px;\n background: #181825;\n border: 1px solid #313244;\n border-radius: 8px;\n z-index: 20;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action { position: static; }\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="redo"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="reset"],\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="restore"] {\n position: absolute;\n top: -54px;\n left: auto;\n}\n.iwb-editor-header > .iwb-editor-tools .iwb-editor-action[data-action="undo"] { right: 154px; }\n.iwb-editor-header > .iwb-edLine truncated
|
||||
|
||||
// image-annotate/plugin/glue.js
|
||||
function createGlue(ctx, css) {
|
||||
|
||||
@@ -133,8 +133,16 @@ check('浮层压住宿主 UI', Number(opened.zIndex) >= 999998, 'z=' + opened.zI
|
||||
// 合成 ClipboardEvent(带 image/png File),粘贴应进入「图片放置」模式;回车确认后图层面板 +1 项。
|
||||
// SKIP_PASTE=1 可跳过(隔离「粘贴」与「覆盖写回」两条链路的排查用)。
|
||||
const SKIP_PASTE = process.env.SKIP_PASTE === '1'
|
||||
const layersBefore = await page.evaluate(() => document.querySelectorAll('.iwb-editor-layer-list > *').length)
|
||||
if (!SKIP_PASTE) {
|
||||
// 先画一个矢量矩形,制造「截图 + 图形」共存场景 —— 这是 z 撞车 / 排序按钮失效的复现前提
|
||||
const imgBox = await page.locator('.iwb-editor-layer-canvas').first().boundingBox()
|
||||
await page.locator('.iwb-editor-tool[data-tool="rect"]').click()
|
||||
await page.mouse.move(imgBox.x + imgBox.width * 0.25, imgBox.y + imgBox.height * 0.25)
|
||||
await page.mouse.down()
|
||||
await page.mouse.move(imgBox.x + imgBox.width * 0.75, imgBox.y + imgBox.height * 0.75, { steps: 8 })
|
||||
await page.mouse.up()
|
||||
await page.waitForTimeout(200)
|
||||
const layersBefore = await page.evaluate(() => document.querySelectorAll('.iwb-editor-layer-list > *').length)
|
||||
await page.evaluate((png) => {
|
||||
const bin = atob(png.split(',')[1])
|
||||
const bytes = new Uint8Array(bin.length)
|
||||
@@ -147,6 +155,40 @@ if (!SKIP_PASTE) {
|
||||
await page.keyboard.press('Enter') // 确认放置 → 落层
|
||||
const pasted = await poll(async () => page.evaluate((n) => document.querySelectorAll('.iwb-editor-layer-list > *').length > n, layersBefore))
|
||||
check('⌘V 粘贴剪贴板图片 → 确认后图层面板 +1', Boolean(pasted), '图层项 ' + layersBefore + ' → ' + await page.evaluate(() => document.querySelectorAll('.iwb-editor-layer-list > *').length))
|
||||
|
||||
// ---------- 4.6) 图层顺序回归(2026-11-04 修):z 唯一 / 选中唯一 / 四个排序按钮 ----------
|
||||
const panelSeq = () => page.evaluate(() => Array.from(document.querySelectorAll('.iwb-editor-layer-list .iwb-layer-item'))
|
||||
.map((n) => n.querySelector('.iwb-layer-name')?.textContent).join(' > '))
|
||||
const activeCount = () => page.evaluate(() => document.querySelectorAll('.iwb-editor-layer-list .iwb-layer-item.active').length)
|
||||
check('图层面板只有一个选中项(画笔目标改用 ✎ 标记)', (await activeCount()) === 1,
|
||||
'active=' + await activeCount() + ' / ' + await panelSeq())
|
||||
|
||||
await page.locator('.iwb-editor-tool[data-tool="select"]').click()
|
||||
await page.mouse.click(imgBox.x + imgBox.width * 0.5, imgBox.y + imgBox.height * 0.5)
|
||||
await page.waitForTimeout(150)
|
||||
const activeItem = await page.evaluate(() => {
|
||||
const n = document.querySelector('.iwb-editor-layer-list .iwb-layer-item.active')
|
||||
return n && { type: n.dataset.layerType, name: n.querySelector('.iwb-layer-name')?.textContent }
|
||||
})
|
||||
check('点击「截图+图形」重叠区选中最上层截图(不被下方图形抢走)',
|
||||
Boolean(activeItem && activeItem.type === 'bitmap' && /图层/.test(activeItem.name)), JSON.stringify(activeItem))
|
||||
|
||||
const before = await panelSeq()
|
||||
await page.locator('.iwb-editor-layer-btn[data-layer="down"]').click()
|
||||
const afterDown = await panelSeq()
|
||||
check('「下移一层」改变图层顺序', before !== afterDown, before + ' → ' + afterDown)
|
||||
await page.locator('.iwb-editor-layer-btn[data-layer="up"]').click()
|
||||
check('「上移一层」恢复原顺序', (await panelSeq()) === before, await panelSeq())
|
||||
await page.locator('.iwb-editor-layer-btn[data-layer="bottom"]').click()
|
||||
const afterBottom = await panelSeq()
|
||||
check('「置底」生效且原图仍在最底(元素不会消失)',
|
||||
afterBottom !== before && afterBottom.endsWith('原图'), afterBottom)
|
||||
await page.locator('.iwb-editor-layer-btn[data-layer="top"]').click()
|
||||
check('「置顶」生效', (await panelSeq()) !== afterBottom, await panelSeq())
|
||||
check('排序后仍只有一个选中项', (await activeCount()) === 1, 'active=' + await activeCount())
|
||||
|
||||
// 复原成画笔工具,避免影响后面「覆盖」链路的画笔取样
|
||||
await page.locator('.iwb-editor-tool[data-tool="brush"]').click()
|
||||
}
|
||||
|
||||
// ---------- 5) 覆盖:回写真机画布 ----------
|
||||
|
||||
Reference in new issue
Block a user