feat(image-annotate): 选区编辑 + 便利版 UI + 单张输出预设(1.1.0)
This commit is contained in:
1 parent
b9ace636ef
commit
662d7eae77
22 files changed
+3795
-343
No files matched your search
@@ -9,13 +9,16 @@
|
||||
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, removeVecCanvas, renderLayerPanel } from './layers.js'
|
||||
import { applyColorChange, beginStroke, drawToolRing, endStroke, eraseSegment, exitPicker, hidePickerTip, sampleColor, setTool, showPickerTip, strokeSegment } from './tools.js'
|
||||
import { applyColorChange, beginStroke, clearToolRing, drawToolRing, endStroke, eraseSegment, exitPicker, hidePickerTip, sampleColor, setTool, showPickerTip, strokeSegment } from './tools.js'
|
||||
import { beginMarquee, updateMarquee, endMarquee, beginLasso, updateLasso, endLasso, clearMarquee, syncAnts, renderSelection } from './selection.js'
|
||||
import { runCommand, toast } from './commands.js'
|
||||
import { showSelectionContext, hideSelectionContext } from './floating.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, 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'
|
||||
import { pushHistory, redo, resetDraw, restoreOriginal, saveSnapshot, snapshotState, undo } from './history.js'
|
||||
import { pushHistory, redo, resetDraw, restoreOriginal, restoreAll, saveSnapshot, snapshotState, undo } from './history.js'
|
||||
import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
|
||||
|
||||
@@ -54,6 +57,11 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
const pos = getPos(e)
|
||||
S.startX = pos.x
|
||||
S.startY = pos.y
|
||||
hideSelectionContext()
|
||||
|
||||
// 选区工具:拉框 / 套索期间独占交互,不与选择/绘制/裁剪分支纠缠
|
||||
if (S.tool === 'marquee') { beginMarquee(pos); return }
|
||||
if (S.tool === 'lasso') { beginLasso(pos); return }
|
||||
|
||||
// 钢笔节点编辑优先:点节点/点边直接接管;点空白退出编辑后继续走下方工具逻辑
|
||||
if ((S.tool === 'select' || S.tool === 'pen')) {
|
||||
@@ -278,6 +286,8 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
onPanMouseMove(e); return
|
||||
}
|
||||
const pos0 = getPos(e)
|
||||
if (S.marquee) { updateMarquee(pos0); return }
|
||||
if (S.lassoPts) { updateLasso(pos0); return }
|
||||
if (S.bitmapDrag) {
|
||||
onBitmapDragMouseMove(pos0); return
|
||||
}
|
||||
@@ -468,6 +478,8 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
}
|
||||
|
||||
function onMouseUp(e) {
|
||||
if (S.marquee) { endMarquee(); return }
|
||||
if (S.lassoPts) { endLasso(); return }
|
||||
if (S.penDragPt >= 0) {
|
||||
onPenEditMouseUp(); return
|
||||
}
|
||||
@@ -674,6 +686,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
export function updateZoomUI() {
|
||||
const val = runtime.el.querySelector('.iwb-editor-zoom-val')
|
||||
if (val) val.textContent = Math.round(S.zoom * 100) + '%'
|
||||
updateStatusbar()
|
||||
}
|
||||
|
||||
export function updateToolUI() {
|
||||
@@ -681,6 +694,8 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
btn.classList.toggle('active', btn.dataset.tool === S.tool)
|
||||
})
|
||||
updateCursor()
|
||||
renderOptionBar()
|
||||
updateStatusbar()
|
||||
}
|
||||
|
||||
export function updateCursor() {
|
||||
@@ -776,6 +791,149 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
})
|
||||
}
|
||||
|
||||
// ============ 便利版 UI:就地选项栏 / 状态栏 / 更多菜单 ============
|
||||
|
||||
const TOOL_NAMES = {
|
||||
select: '选择 / 移动', marquee: '框选', lasso: '套索', brush: '画笔', eraser: '橡皮',
|
||||
pen: '钢笔', text: '文字', rect: '矩形', circle: '圆形', arrow: '箭头',
|
||||
picker: '吸管', hand: '抓手', crop: '裁剪'
|
||||
}
|
||||
|
||||
/** 选项栏只露当前工具 2–3 个参数;用独立控件,避免与右侧旧参数区抢 querySelector */
|
||||
export function renderOptionBar() {
|
||||
const body = runtime.el && runtime.el.querySelector('.iwb-editor-optionbar-body')
|
||||
if (!body) return
|
||||
let html = ''
|
||||
if (S.tool === 'marquee') {
|
||||
html = `<span class="iwb-opt-label">形状</span>
|
||||
<button class="iwb-opt-btn${S.selectionKind === 'rect' ? ' active' : ''}" data-opt="shape" data-value="rect">矩形</button>
|
||||
<button class="iwb-opt-btn${S.selectionKind === 'ellipse' ? ' active' : ''}" data-opt="shape" data-value="ellipse">椭圆</button>
|
||||
<label class="iwb-opt-label">羽化 <input type="number" class="iwb-opt-num" data-opt="feather" min="0" max="20" step="1" value="${S.selectionFeather}"> px</label>
|
||||
<span class="iwb-opt-hint">拖出选区 → 操作条就在选区旁边</span>`
|
||||
} else if (S.tool === 'lasso') {
|
||||
html = `<span class="iwb-opt-label">套索</span>
|
||||
<label class="iwb-opt-label">羽化 <input type="number" class="iwb-opt-num" data-opt="feather" min="0" max="20" step="1" value="${S.selectionFeather}"> px</label>
|
||||
<span class="iwb-opt-hint">按住拖动画圈,松手闭合</span>`
|
||||
} else {
|
||||
const hints = {
|
||||
crop: '裁剪栏在画布下方;裁剪与选区互斥',
|
||||
brush: '选区激活时笔刷只画在选区内',
|
||||
eraser: '橡皮只擦当前画笔目标图层(✎)',
|
||||
select: '点击选中元素,拖拽移动,手柄缩放'
|
||||
}
|
||||
html = `<span class="iwb-opt-current">当前工具:${TOOL_NAMES[S.tool] || S.tool}</span>
|
||||
<span class="iwb-opt-hint">${hints[S.tool] || '选区工具按 M / L;Ctrl+J 复制选区,Delete 清除当前层'}</span>`
|
||||
}
|
||||
body.innerHTML = html
|
||||
}
|
||||
|
||||
/** 状态栏:缩放 / 尺寸 / 光标 / 选区 / 当前层 / 工具 */
|
||||
export function updateStatusbar() {
|
||||
if (!runtime.el) return
|
||||
const put = (sel, text) => {
|
||||
const el = runtime.el.querySelector(sel)
|
||||
if (el && el.textContent !== text) el.textContent = text
|
||||
}
|
||||
put('.iwb-editor-status-zoom', Math.round(S.zoom * 100) + '%')
|
||||
put('.iwb-editor-status-size', S.imgW + '×' + S.imgH)
|
||||
const p = S.lastPos
|
||||
put('.iwb-editor-status-pos', p ? ('x ' + Math.round(p.x) + ' y ' + Math.round(p.y)) : 'x — y —')
|
||||
const sel = S.selection && S.selection.active ? S.selection : null
|
||||
put('.iwb-editor-status-sel', sel
|
||||
? '选区 ' + Math.round(sel.bbox.w) + '×' + Math.round(sel.bbox.h) + (sel.feather ? ' · 羽化 ' + sel.feather : '') + (sel.inverted ? ' · 反选' : '')
|
||||
: '无选区')
|
||||
const layer = S.layers.find(l => l.id === S.activeLayerId)
|
||||
put('.iwb-editor-status-layer', '当前层:' + (layer ? layer.name : '—'))
|
||||
put('.iwb-editor-status-tool', TOOL_NAMES[S.tool] || S.tool)
|
||||
}
|
||||
|
||||
/** 更多菜单 / 面板收起 / 选项栏控件:只绑定一次,DOM 重绘不丢事件(委托) */
|
||||
export function bindLiteUI() {
|
||||
const root = runtime.el
|
||||
if (!root || root.dataset.liteBound === '1') return
|
||||
root.dataset.liteBound = '1'
|
||||
|
||||
const moreBtn = root.querySelector('[data-action="toggleMore"]')
|
||||
const moreMenu = root.querySelector('.iwb-editor-more-menu')
|
||||
const closeMenus = () => {
|
||||
if (moreMenu) moreMenu.classList.remove('open')
|
||||
if (moreBtn) moreBtn.setAttribute('aria-expanded', 'false')
|
||||
const panel = root.querySelector('.iwb-editor-export-settings')
|
||||
if (panel) panel.classList.remove('open')
|
||||
const toggle = root.querySelector('[data-action="toggleExportSettings"]')
|
||||
if (toggle) toggle.setAttribute('aria-expanded', 'false')
|
||||
}
|
||||
if (moreBtn && moreMenu) {
|
||||
moreBtn.addEventListener('mousedown', (e) => e.stopPropagation())
|
||||
moreBtn.addEventListener('click', (e) => {
|
||||
e.stopPropagation()
|
||||
const open = moreMenu.classList.toggle('open')
|
||||
moreBtn.setAttribute('aria-expanded', String(open))
|
||||
})
|
||||
}
|
||||
if (moreMenu) {
|
||||
moreMenu.addEventListener('mousedown', (e) => e.stopPropagation())
|
||||
moreMenu.addEventListener('click', (e) => {
|
||||
const btn = e.target.closest('button')
|
||||
if (!btn) return
|
||||
e.stopPropagation()
|
||||
const key = btn.dataset.more
|
||||
if (key && key.indexOf('sel.') === 0) runCommand(key)
|
||||
else if (key === 'addBlankLayer') addBlankLayer()
|
||||
else if (key === 'addBitmapLayer') {
|
||||
const b = root.querySelector('.iwb-editor-layer-add-btn[data-action="addBitmapLayer"]')
|
||||
if (b) b.click()
|
||||
} else if (key === 'zoomFit') setZoomFit()
|
||||
else if (key === 'reset') restoreAll()
|
||||
closeMenus()
|
||||
})
|
||||
}
|
||||
|
||||
root.querySelectorAll('[data-action="togglePanels"]').forEach(btn => {
|
||||
btn.addEventListener('mousedown', (e) => e.stopPropagation())
|
||||
btn.addEventListener('click', (e) => {
|
||||
e.stopPropagation()
|
||||
const collapsed = !root.classList.contains('panels-collapsed')
|
||||
root.classList.toggle('panels-collapsed', collapsed)
|
||||
const st = S.callbacks && S.callbacks.settings
|
||||
if (st && typeof st.set === 'function') {
|
||||
try { st.set('panelsCollapsed', collapsed) } catch (err) { /* ignore */ }
|
||||
}
|
||||
toast(collapsed ? '侧栏已收起' : '侧栏已展开', 'info')
|
||||
closeMenus()
|
||||
})
|
||||
})
|
||||
|
||||
const body = root.querySelector('.iwb-editor-optionbar-body')
|
||||
if (body) {
|
||||
body.addEventListener('mousedown', (e) => e.stopPropagation())
|
||||
body.addEventListener('click', (e) => {
|
||||
const btn = e.target.closest('[data-opt="shape"]')
|
||||
if (!btn) return
|
||||
e.stopPropagation()
|
||||
S.selectionKind = btn.dataset.value === 'ellipse' ? 'ellipse' : 'rect'
|
||||
renderOptionBar()
|
||||
toast('选区形状:' + (S.selectionKind === 'ellipse' ? '椭圆' : '矩形'), 'info')
|
||||
})
|
||||
const onFeather = (e) => {
|
||||
const input = e.target.closest('[data-opt="feather"]')
|
||||
if (!input) return
|
||||
S.selectionFeather = clamp(parseInt(input.value, 10) || 0, 0, 20)
|
||||
if (S.selection && S.selection.active) S.selection.feather = S.selectionFeather
|
||||
updateStatusbar()
|
||||
}
|
||||
body.addEventListener('input', onFeather)
|
||||
body.addEventListener('change', onFeather)
|
||||
}
|
||||
|
||||
// 点画布 / 面板其它地方收起下拉。必须用捕获阶段:modal 自身有个
|
||||
// mousedown stopPropagation(阻止宿主抢事件),冒泡阶段收不到。
|
||||
const modal = root.querySelector('.iwb-editor-modal') || root
|
||||
modal.addEventListener('mousedown', (e) => {
|
||||
if (!e.target.closest('.iwb-editor-more-wrap') && !e.target.closest('.iwb-editor-export-settings-wrap')) closeMenus()
|
||||
}, true)
|
||||
}
|
||||
|
||||
// ============ 事件绑定 ============
|
||||
/**
|
||||
* 安全绑定:选择器找不到元素时只告警一次、不抛异常。
|
||||
@@ -1072,6 +1230,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
function bindExportActions() {
|
||||
// 底部
|
||||
bindIf('[data-action="cancel"]', 'click', close)
|
||||
bindIf('[data-action="download"]', 'click', () => exportResult('download'))
|
||||
bindIf('[data-action="newNode"]', 'click', () => exportResult('newNode'))
|
||||
bindIf('[data-action="replace"]', 'click', () => exportResult('replace'))
|
||||
}
|
||||
@@ -1132,10 +1291,13 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
S.canvasStack.addEventListener('mousemove', onMouseMove)
|
||||
S.canvasStack.addEventListener('mousemove', onCanvasHover)
|
||||
window.addEventListener('mouseup', onMouseUp)
|
||||
S.canvasStack.addEventListener('contextmenu', (e) => e.preventDefault())
|
||||
S.canvasStack.addEventListener('contextmenu', (e) => {
|
||||
e.preventDefault()
|
||||
if (S.open && !S.placingImage) showSelectionContext(e)
|
||||
})
|
||||
S.canvasStack.addEventListener('mouseleave', () => {
|
||||
if (S.placingImage) return
|
||||
if ((S.tool === 'brush' || S.tool === 'eraser') && !S.isDrawing) clearOverlay()
|
||||
if ((S.tool === 'brush' || S.tool === 'eraser') && !S.isDrawing) clearToolRing()
|
||||
if (S.tool === 'picker') hidePickerTip()
|
||||
})
|
||||
|
||||
@@ -1196,6 +1358,8 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
/** 悬停分发表:各工具的光标与预览(编排器负责前置守卫与旋转手柄优先) */
|
||||
const HOVER_HANDLERS = {
|
||||
select: onHoverSelect,
|
||||
marquee: onHoverCrosshair,
|
||||
lasso: onHoverCrosshair,
|
||||
picker: onHoverPicker,
|
||||
hand: onHoverHand,
|
||||
brush: onHoverBrushRing,
|
||||
@@ -1213,6 +1377,10 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
if (S.panning || S.spaceDown) return
|
||||
if (S.isDrawing || S.textDrag || S.shapeDrag) return
|
||||
const p = getPos(e)
|
||||
if (!S.lastPos || Math.abs(p.x - S.lastPos.x) > 0.5 || Math.abs(p.y - S.lastPos.y) > 0.5) {
|
||||
S.lastPos = p
|
||||
updateStatusbar()
|
||||
}
|
||||
if (isOverRotationHandle(p)) { S.drawCanvasCursor(ROT_CURSOR); return }
|
||||
const handler = Object.prototype.hasOwnProperty.call(HOVER_HANDLERS, S.tool) ? HOVER_HANDLERS[S.tool] : null
|
||||
if (handler) handler(p, e)
|
||||
@@ -1300,6 +1468,8 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
if (e.key === 'Enter' && S.tool === 'pen' && S.penPath) { e.preventDefault(); commitPenPath(); return }
|
||||
if (e.key === 'Escape') {
|
||||
if (isTyping(e)) { hideTextInput(); return }
|
||||
// 选区优先:Esc 一步取消,不进菜单
|
||||
if (S.marquee || S.lassoPts || (S.selection && S.selection.active)) { e.preventDefault(); clearMarquee(); return }
|
||||
if (S.penPath) { e.preventDefault(); S.penPath = null; renderObjects(); return }
|
||||
if (getPenEditingShape()) { e.preventDefault(); exitPenEdit(); return }
|
||||
if (S.selectedLayerId || S.selectedTextId || S.selectedShapeId || S.bitmapDrag || S.textDrag || S.shapeDrag) {
|
||||
@@ -1311,13 +1481,37 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
return
|
||||
}
|
||||
if (isTyping(e)) return
|
||||
|
||||
const mod = e.ctrlKey || e.metaKey
|
||||
const key = String(e.key || '').toLowerCase()
|
||||
// ---- 选区快捷键:与浮动条 / 右键菜单共用同一命令层 ----
|
||||
if (mod && key === 'a') { e.preventDefault(); runCommand('sel.all'); return }
|
||||
if (mod && e.shiftKey && key === 'i') { e.preventDefault(); runCommand('sel.invert'); return }
|
||||
if (mod && key === 'd') { e.preventDefault(); runCommand('sel.none'); return }
|
||||
if (mod && key === 'j') { e.preventDefault(); runCommand('sel.copy'); return }
|
||||
if (mod && key === 'x' && S.selection && S.selection.active) { e.preventDefault(); runCommand('sel.cut'); return }
|
||||
|
||||
// 数字键 1–0 保留兼容;另加 PS 习惯的字母键(无修饰键时才生效)
|
||||
const toolByKey = { '1': 'select', '2': 'brush', '3': 'eraser', '4': 'pen', '5': 'text', '6': 'rect', '7': 'circle', '8': 'arrow', '9': 'picker', '0': 'crop' }
|
||||
if (!e.ctrlKey && !e.metaKey && !e.altKey && Object.prototype.hasOwnProperty.call(toolByKey, e.key)) {
|
||||
const letterTools = { m: 'marquee', l: 'lasso', v: 'select', b: 'brush', e: 'eraser', t: 'text', c: 'crop' }
|
||||
if (!mod && !e.altKey && Object.prototype.hasOwnProperty.call(toolByKey, e.key)) {
|
||||
e.preventDefault()
|
||||
setTool(toolByKey[e.key])
|
||||
return
|
||||
}
|
||||
if (!mod && !e.altKey && Object.prototype.hasOwnProperty.call(letterTools, key)) {
|
||||
e.preventDefault()
|
||||
setTool(letterTools[key])
|
||||
return
|
||||
}
|
||||
|
||||
if (e.key === 'Delete' || e.key === 'Backspace') {
|
||||
// 有选区且没有选中矢量对象:只清当前层(用户裁定),不走「删对象」
|
||||
if (S.selection && S.selection.active && !S.selectedTextId && !S.selectedShapeId) {
|
||||
e.preventDefault()
|
||||
runCommand('sel.delete')
|
||||
return
|
||||
}
|
||||
if (S.selectedTextId) { e.preventDefault(); deleteSelectedText(); renderLayerPanel(); return }
|
||||
if (S.selectedShapeId) { e.preventDefault(); deleteSelectedShape(); renderLayerPanel(); return }
|
||||
}
|
||||
@@ -1326,13 +1520,13 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
|
||||
if (!S.spaceDown) { S.spaceDown = true; updateCursor() }
|
||||
return
|
||||
}
|
||||
if ((e.ctrlKey || e.metaKey) && (e.key === ']' || e.key === '[')) {
|
||||
if (mod && (e.key === ']' || e.key === '[')) {
|
||||
e.preventDefault()
|
||||
const up = e.key === ']'
|
||||
moveLayer(e.shiftKey ? (up ? 'top' : 'bottom') : (up ? 'up' : 'down'))
|
||||
return
|
||||
}
|
||||
if ((e.ctrlKey || e.metaKey) && e.key === 'z') {
|
||||
if (mod && key === 'z') {
|
||||
e.preventDefault()
|
||||
if (e.shiftKey) redo(); else undo()
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user