feat(image-annotate): 选区编辑 + 便利版 UI + 单张输出预设(1.1.0)

This commit is contained in:
qiqi committed 2026-11-04 17:00:49 +08:00
1 parent b9ace636ef
commit 662d7eae77
22 files changed
+3795 -343

No files matched your search

+94 -37
View File
@@ -28,18 +28,24 @@
import { S, runtime } from './state.js'
import { HISTORY_MAX_BYTES, HISTORY_MAX_ENTRIES, historyBudgetBytes, snapshotStats } from './history.js'
import { cancelImagePlacement } from './placement.js'
import { clearSelection, purgeVectorCanvases } from './layers.js'
import { clearSelection, purgeVectorCanvases, renderLayerPanel } from './layers.js'
import { createEl, loadImage } from './dom.js'
import { getCompositeCanvas } from './tools.js'
import { hideTextInput } from './objects.js'
import { renderObjects, hideTextInput } from './objects.js'
import { setupCanvas } from './viewport.js'
import { updateAllUI } from './events.js'
import { updateAllUI, bindLiteUI, renderOptionBar, updateStatusbar } from './events.js'
import { disposeSelection, renderSelection, selectionInfo, syncAnts, registerSelectionCommands } from './selection.js'
import { bindFloating, positionFloating, hideSelectionContext } from './floating.js'
import { loadExportOptions, prepareOutput, downloadBlob, bindExportUI, renderExportUI } from './export.js'
import { toast } from './commands.js'
// ============ 打开编辑器 ============
export async function open(options) {
if (!options || !options.imageBase64) return
createEl()
S.callbacks = options
// 命令层 / 蚂蚁线的渲染出口走注入,避免新模块与 events.js 形成顶层求值循环
S.renderAll = renderAll
S.positionFloating = positionFloating
try {
const img = await loadImage(options.imageBase64)
@@ -52,6 +58,13 @@ import { updateAllUI } from './events.js'
setupCanvas(img)
resetState()
updateAllUI()
registerSelectionCommands()
loadExportOptions()
bindLiteUI()
bindFloating()
bindExportUI()
initPanels()
renderAll()
} catch (e) {
console.error('[IWB编辑器] 打开失败:', e)
if (runtime.el) runtime.el.style.display = 'none'
@@ -104,6 +117,44 @@ import { updateAllUI } from './events.js'
S.placingStart = null
const sizeToggle = runtime.el && runtime.el.querySelector('.iwb-editor-size-toggle')
if (sizeToggle) { sizeToggle.classList.remove('active'); sizeToggle.setAttribute('aria-pressed', 'false'); sizeToggle.textContent = '启用尺寸' }
// 重开时旧选区 / 蚂蚁线 / 浮动条不能残留
S.selection = null
S.marquee = null
S.lassoPts = null
S.interaction = 'idle'
S.lastExport = null
if (S._antFrame) { cancelAnimationFrame(S._antFrame); S._antFrame = 0 }
hideSelectionContext()
if (S.selectionCtx) renderSelection()
if (S.cursorCtx) {
const pad = S.overlayPad || 0
S.cursorCtx.setTransform(1, 0, 0, 1, 0, 0)
S.cursorCtx.clearRect(0, 0, S.imgW + pad * 2, S.imgH + pad * 2)
S.cursorCtx.setTransform(1, 0, 0, 1, pad, pad)
}
}
/** 命令层统一重绘出口:所有新操作的像素/UI/选区状态在这里对齐 */
function renderAll() {
if (!S.open || !runtime.el) return
renderObjects()
renderLayerPanel()
renderOptionBar()
updateStatusbar()
renderExportUI()
renderSelection()
syncAnts()
positionFloating()
}
/** 侧栏开合:记住上次选择,窄窗口默认收起(画布优先) */
function initPanels() {
if (!runtime.el) return
const st = S.callbacks && S.callbacks.settings
let saved = false
if (st && typeof st.get === 'function') { try { saved = !!st.get('panelsCollapsed') } catch (e) { /* ignore */ } }
const auto = (window.innerWidth || 1440) < 1100
runtime.el.classList.toggle('panels-collapsed', saved || auto)
}
export function close() {
@@ -120,6 +171,8 @@ import { updateAllUI } from './events.js'
S.penPath = null
S.textEditingId = null
clearSelection()
hideSelectionContext()
disposeSelection()
}
/**
@@ -158,7 +211,7 @@ import { updateAllUI } from './events.js'
}
S.layers = []
// 5) 工作 canvas:text / stroke / overlay(overlay 含 18% 交互边距,4K 图约 74MB)
for (const c of [S.textCanvas, S.strokeCanvas, S.overlayCanvas]) {
for (const c of [S.textCanvas, S.strokeCanvas, S.overlayCanvas, S.selectionCanvas, S.cursorCanvas]) {
try { if (c) { c.width = 0; c.height = 0 } } catch (e) { /* ignore */ }
}
// 6) 矢量对象的画布缓存
@@ -171,40 +224,21 @@ import { updateAllUI } from './events.js'
* 用 toBlob 而非 toDataURL —— 编码结果落在浏览器 Blob 存储(堆外),
* 省掉全幅 base64 字符串(4K 图约 44MB JS 堆驻留);onResult 收到 Blob 或 dataURL 均可。
*/
export function exportResult(mode) {
/**
* 单张输出:prepareOutput() 负责合成 → 尺寸/背景 → 编码(含 ≤500KB 降质)。
* 一次只产出一个结果;download 只下载、不关编辑器、不回写宿主节点。
*/
export async function exportResult(mode) {
if (S.placingImage) cancelImagePlacement()
const tmp = getCompositeCanvas()
let done = false
const finish = (out) => {
if (done) return
done = true
tmp.width = 0 // 合成画布用完即弃(33MB@4K)
tmp.height = 0
if (S.callbacks && S.callbacks.onResult) {
S.callbacks.onResult(out, mode)
}
close()
}
const fail = (e) => {
if (done) return
done = true
tmp.width = 0
tmp.height = 0
console.error('[IWB编辑器] 导出失败:', e)
if (S.callbacks && S.callbacks.onError) S.callbacks.onError('图片跨域无法导出')
}
try {
if (typeof tmp.toBlob === 'function') {
tmp.toBlob((blob) => {
if (blob) { finish(blob); return }
// toBlob 对非 origin-clean 画布返回 null → 退回 toDataURL 让它抛出明确的 SecurityError
try { finish(tmp.toDataURL('image/png')) } catch (e) { fail(e) }
}, 'image/png')
return
}
finish(tmp.toDataURL('image/png'))
const { blob } = await prepareOutput()
if (mode === 'download') { downloadBlob(blob); return }
if (S.callbacks && S.callbacks.onResult) S.callbacks.onResult(blob, mode)
close()
} catch (e) {
fail(e)
console.error('[IWB编辑器] 导出失败:', e)
toast('导出失败:' + ((e && e.message) || '图片可能跨域'), 'error')
if (S.callbacks && S.callbacks.onError) S.callbacks.onError('导出失败:' + ((e && e.message) || '图片跨域'))
}
}
@@ -227,8 +261,31 @@ import { updateAllUI } from './events.js'
...snapshotStats
})
/** 开发/回归观测口:本地探针与真站 e2e 用它断言图层、选区与像素,不参与业务逻辑 */
const __debug = () => ({
open: S.open,
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 })),
activeLayerId: S.activeLayerId,
selection: selectionInfo(),
history: S.history.length,
historyLabels: S.history.map(s => s.__label || ''),
redo: S.redoStack.length,
exportOptions: S.exportOptions ? { ...S.exportOptions } : null,
lastExport: S.lastExport ? { ...S.lastExport } : null,
interaction: S.interaction,
ants: !!S._antFrame,
pixel: (id, x, y) => {
const l = S.layers.find(v => v.id === id)
if (!l || !l.ctx) return null
try { return Array.from(l.ctx.getImageData(Math.round(x), Math.round(y), 1, 1).data) } catch (e) { return null }
}
})
window.__iwbEditor = {
open,
close,
__stats
__stats,
__debug
}