fix(image-annotate): 1.1.1 反选作用区域/裁剪态选区互斥/Delete 优先级/退出结算宿主 Promise(含 1.1.0 布局修复)

This commit is contained in:
qiqi committed 2026-11-04 17:54:40 +08:00
1 parent be05f31479
commit 9a0f5bab69
8 files changed
+259 -20

No files matched your search

+6
View File
@@ -41,6 +41,12 @@ export function execute(label, mutator) {
export function runCommand(name) {
const def = COMMANDS[name]
if (!def) return false
// 选区与裁剪互斥:只在 setTool('crop') 清一次选区不够 —— 快捷键 / 更多菜单 /
// 右键菜单都还能再建选区。三条入口都经过这里,统一在工作层拦掉 sel.*。
if (S.tool === 'crop' && String(name).indexOf('sel.') === 0) {
toast('裁剪中不可使用选区命令,请先切换工具', 'warn')
return false
}
if (typeof def.enabled === 'function' && !def.enabled()) {
toast(def.hint || '当前状态下不可用', 'warn')
return false
+7
View File
@@ -8,6 +8,7 @@
import { S, runtime } from './state.js'
import { getCompositeCanvas } from './tools.js'
import { setupCanvas } from './viewport.js'
import { disposeSelection } from './selection.js'
// ============ 裁剪 ============
@@ -238,6 +239,10 @@ import { setupCanvas } from './viewport.js'
if (!S.cropRect) return
const { x, y, w, h } = S.cropRect
if (w < 2 || h < 2) { cancelCrop(); return }
// 画布尺寸即将变化:旧坐标系下的选区/拉框/蚂蚁线/浮动条全部失效,
// 必须按 disposeSelection 级别清理,否则之后 Delete 会按旧 bbox
// 在新画布上开超大掩膜,把当前层整层擦掉。
disposeSelection()
// 合成所有可见图层
const tmp = getCompositeCanvas()
const sx = Math.round(x), sy = Math.round(y)
@@ -313,6 +318,8 @@ import { setupCanvas } from './viewport.js'
S.moveStart = null
clearOverlay()
updateCropInfo()
// 尺寸/坐标系已变,统一重绘出口要在新画布上重建选区层与浮动条位置
if (S.renderAll) S.renderAll()
}
export function cancelCrop() {
+18 -2
View File
@@ -157,7 +157,19 @@ import { toast } from './commands.js'
runtime.el.classList.toggle('panels-collapsed', saved || auto)
}
export function close() {
/**
* 关闭编辑器。reason:'cancel'(用户点退出)| 'done'(导出成功后关闭)。
* 必须先通知宿主再置空 S.callbacks —— glue.js 的 openEditor() 只在
* onResult / onError / onClose 三处结算,「退出」曾经没有任何回调,宿主
* Promise 永远悬挂;回调异常不能阻断后面的内存释放。
*/
export function close(reason) {
const callbacks = S.callbacks
try {
if (callbacks && typeof callbacks.onClose === 'function') callbacks.onClose(reason || 'cancel')
} catch (e) {
console.error('[IWB编辑器] onClose 回调异常:', e)
}
if (runtime.el) {
hideTextInput()
runtime.el.style.display = 'none'
@@ -234,7 +246,7 @@ import { toast } from './commands.js'
const { blob } = await prepareOutput()
if (mode === 'download') { downloadBlob(blob); return }
if (S.callbacks && S.callbacks.onResult) S.callbacks.onResult(blob, mode)
close()
close('done')
} catch (e) {
console.error('[IWB编辑器] 导出失败:', e)
toast('导出失败:' + ((e && e.message) || '图片可能跨域'), 'error')
@@ -267,6 +279,10 @@ import { toast } from './commands.js'
tool: S.tool,
doc: { w: S.imgW, h: S.imgH },
layers: S.layers.map(l => ({ id: l.id, name: l.name, z: l.z, locked: !!l.locked, visible: l.visible !== false })),
shapes: S.shapeLayers.map(s => ({ id: s.id, type: s.type })),
texts: S.textLayers.map(t => ({ id: t.id, text: t.text })),
selectedShapeId: S.selectedShapeId,
selectedTextId: S.selectedTextId,
activeLayerId: S.activeLayerId,
selection: selectionInfo(),
history: S.history.length,
+29 -7
View File
@@ -24,7 +24,7 @@ import { createLayerCanvas, getActiveLayer, reorderLayerCanvas, setActiveLayer,
export function selectionInfo() {
if (!hasSelection()) return null
const b = S.selection.bbox
return { kind: S.selection.kind, x: Math.round(b.x), y: Math.round(b.y), w: Math.round(b.w), h: Math.round(b.h), feather: S.selection.feather || 0 }
return { kind: S.selection.kind, x: Math.round(b.x), y: Math.round(b.y), w: Math.round(b.w), h: Math.round(b.h), feather: S.selection.feather || 0, inverted: !!S.selection.inverted }
}
// ============ 选区图层(独立 canvas) ============
@@ -159,6 +159,10 @@ import { createLayerCanvas, getActiveLayer, reorderLayerCanvas, setActiveLayer,
} else {
path.addPath(outlinePath)
}
// 建立像素选区即接管 Delete 语义:清掉矢量对象选中态,否则
// 「选中形状 → Ctrl+A → Delete」会走删对象分支,而不是清像素。
S.selectedTextId = null
S.selectedShapeId = null
S.selection = {
kind, path, outlinePath, bbox,
fillRule: inverted ? 'evenodd' : 'nonzero',
@@ -258,27 +262,45 @@ import { createLayerCanvas, getActiveLayer, reorderLayerCanvas, setActiveLayer,
// ============ 局部掩膜(羽化软边) ============
/**
* 返回覆盖 bbox 的 alpha 掩膜(局部 tile,不是全幅)。
* 掩膜必须覆盖的范围(整数基准,避免 tile 原点与平移量差 1px)。
* 反选的 path 是「整图矩形 + 原选区」,只按原 bbox 开 tile 会把 tile 之外
* 的反选区域整块丢掉 —— 填充走全幅 fill(path) 不受影响,于是 Delete/复制/剪切
* 只作用原 bbox,与填充互相矛盾。反选时扩成整幅,临时画布用完即弃。
*/
function maskExtent() {
const sel = S.selection
const b = sel.inverted ? { x: 0, y: 0, w: S.imgW, h: S.imgH } : sel.bbox
const x = Math.floor(b.x)
const y = Math.floor(b.y)
return {
x, y,
w: Math.max(1, Math.ceil(b.x + b.w) - x),
h: Math.max(1, Math.ceil(b.y + b.h) - y)
}
}
/**
* 返回覆盖 bbox 的 alpha 掩膜(局部 tile,不是全幅;反选时整幅)。
* feather>0 用 Canvas2D 原生 filter 模糊填充边缘,CSP 不限制。
*/
function buildMask() {
const sel = S.selection
const feather = sel.feather || 0
const pad = Math.ceil(feather * 2)
const b = sel.bbox
const w = Math.max(1, Math.ceil(b.w) + pad * 2)
const h = Math.max(1, Math.ceil(b.h) + pad * 2)
const e = maskExtent()
const w = e.w + pad * 2
const h = e.h + pad * 2
const c = document.createElement('canvas')
c.width = w
c.height = h
const cx = c.getContext('2d')
cx.save()
cx.translate(-b.x + pad, -b.y + pad)
cx.translate(-e.x + pad, -e.y + pad)
if (feather > 0) cx.filter = 'blur(' + feather + 'px)'
cx.fillStyle = '#ffffff'
cx.fill(sel.path, sel.fillRule)
cx.restore()
return { canvas: c, x: Math.floor(b.x) - pad, y: Math.floor(b.y) - pad, w, h }
return { canvas: c, x: e.x - pad, y: e.y - pad, w, h }
}
function writableLayer() {
+9
View File
@@ -67,6 +67,9 @@ export function createGlue(ctx, css) {
ensureStyle()
return new Promise(function (resolve, reject) {
let settled = false
// onResult 一旦接管(异步编码 + 写回节点),close() 触发的 onClose
// 就不能再抢先结算,否则会变成「节点已写回但 Promise 报取消」。
let handedOff = false
const settle = function (fn) { if (!settled) { settled = true; fn() } }
Promise.resolve(editor.open({
@@ -76,7 +79,13 @@ export function createGlue(ctx, css) {
onError: function (message) {
settle(function () { reject(new Error(String(message || '编辑失败'))) })
},
onClose: function () {
// 点「退出」:编辑器不产出结果,必须在这里结算,否则 Promise 永远悬挂。
if (handedOff) return
settle(function () { resolve('cancel') })
},
onResult: function (out, mode) {
handedOff = true
imageToDataUrl(out).then(function (dataUrl) {
const replaced = mode === 'replace'
const ok = replaced