feat(image-annotate): 1.2.2 手柄仅选择工具/高亮跟随绘制目标/选项栏快捷点与裁剪参数/Enter-Esc 裁剪

This commit is contained in:
qiqi committed 2026-11-04 20:30:49 +08:00
1 parent 1bf8220c05
commit f50b734fd7
11 files changed
+340 -164

No files matched your search

+4 -2
View File
@@ -352,5 +352,7 @@ import { toast } from './commands.js'
}
// ============ 裁剪栏 ============
export function showCropBar() { runtime.el.querySelector('.iwb-editor-crop-bar').style.display = 'flex' }
export function hideCropBar() { runtime.el.querySelector('.iwb-editor-crop-bar').style.display = 'none' }
// 裁剪参数已迁入选项栏(optionBarHTML('crop'),随工具切换自动出现/消失),
// 画布下方的浮窗 DOM 已删除。这两个入口保留为空操作,调用点(setTool / 拉框)无需改动。
export function showCropBar() { /* 选项栏自动承载,见 events.js#optionBarHTML */ }
export function hideCropBar() { /* 同上 */ }
+2 -26
View File
@@ -67,11 +67,9 @@ import { bindEvents } from './events.js'
<span class="iwb-editor-divider"></span>
<button class="iwb-editor-tool" data-tool="crop" title="裁剪"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6.13 1L6 16a2 2 0 0 0 2 2h15"/><path d="M1 6.13L16 6a2 2 0 0 1 2 2v15"/></svg></button>
<button class="iwb-editor-tool" data-tool="crop" title="裁剪(参数在选项栏;Enter 确认 / Esc 取消)"><svg width="15" height="15" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M6.13 1L6 16a2 2 0 0 0 2 2h15"/><path d="M1 6.13L16 6a2 2 0 0 1 2 2v15"/></svg></button>
<span class="iwb-editor-divider"></span>
<button class="iwb-editor-fill-btn" data-action="toggleFill" title="矩形实心填充开关(选中矩形时切换即时生效)">实心</button>
</div>
<span class="iwb-editor-divider"></span>
@@ -150,29 +148,7 @@ import { bindEvents } from './events.js'
</div>
<input class="iwb-editor-text-input" type="text" placeholder="输入文字,Enter确认,Esc取消">
<div class="iwb-editor-picker-tip"><span class="iwb-editor-picker-chip"></span><span class="iwb-editor-picker-hex">#ffffff</span></div>
<div class="iwb-editor-crop-bar">
<div class="iwb-editor-crop-row"><div class="iwb-editor-constraint-group">
<span class="iwb-editor-constraint-label">比例</span>
<button class="iwb-editor-constraint-btn active" data-ratio="free">自由</button>
<button class="iwb-editor-constraint-btn" data-ratio="1:1">1:1</button>
<button class="iwb-editor-constraint-btn" data-ratio="4:3">4:3</button>
<button class="iwb-editor-constraint-btn" data-ratio="3:4">3:4</button>
<button class="iwb-editor-constraint-btn" data-ratio="16:9">16:9</button>
<button class="iwb-editor-constraint-btn" data-ratio="9:16">9:16</button>
</div></div>
<div class="iwb-editor-crop-row"><div class="iwb-editor-constraint-group">
<span class="iwb-editor-constraint-label">尺寸</span>
<input class="iwb-editor-size-input iwb-editor-size-w" type="number" min="1" placeholder="宽" value="800">
<span class="iwb-editor-size-x">×</span>
<input class="iwb-editor-size-input iwb-editor-size-h" type="number" min="1" placeholder="高" value="800">
<button class="iwb-editor-constraint-btn iwb-editor-size-toggle" data-action="toggleSizeConstraint" aria-pressed="false">启用尺寸</button>
<button class="iwb-editor-constraint-btn iwb-editor-size-apply" data-action="applySize">应用</button>
</div></div>
<div class="iwb-editor-crop-row iwb-editor-crop-actions"><span class="iwb-editor-crop-info">--</span>
<button class="iwb-editor-crop-confirm" data-action="confirmCrop">✓ 确认</button>
<button class="iwb-editor-crop-cancel" data-action="cancelCrop">✗ 取消</button>
</div>
</div>
<!-- 裁剪参数已迁入选项栏(v1.2.2),画布下方浮窗删除 -->
<div class="iwb-editor-sel-bar">
<span class="iwb-editor-sel-info">未框选</span>
<button data-sel-cmd="sel.copy" data-needs-sel="1" title="把当前图层选区复制为新图层 (Ctrl+J)">复制</button>
+93 -45
View File
@@ -210,18 +210,25 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
/** 画笔工具的按下:无可写图层则自动落「标注」层(用户裁定:默认不预建画笔层),
* 否则存快照并落下第一点。注意 ensureWritableLayer 必须在 saveSnapshot 之前,
* 让首笔 Ctrl+Z 连自动建的层一起撤掉。 */
* 让首笔 Ctrl+Z 连自动建的层一起撤掉。
* 选中态同步:面板高亮(selectedLayerId)必须跟随真正的绘制目标(activeLayerId),
* 否则会出现「面板没选中这层却能画上去」的逻辑矛盾(用户反馈 2026-11-04)。 */
function onBrushMouseDown(pos) {
if (!ensureWritableLayer()) return
const layer = ensureWritableLayer()
if (!layer) return
S.selectedLayerId = layer.id
S.isDrawing = true
saveSnapshot()
beginStroke()
strokeSegment(pos.x, pos.y, pos.x, pos.y)
}
/** 橡皮工具的按下:与画笔同构,存快照并擦除第一段 */
/** 橡皮工具的按下:与画笔同构但**不自动建层**(减内容语义,没有可写层就忽略),
* 同样把面板高亮同步到真正的擦除目标。 */
function onEraserMouseDown(pos) {
if (!getActiveCtx()) return
const layer = getActiveLayer()
if (!layer || layer.locked) return
S.selectedLayerId = layer.id
S.isDrawing = true
saveSnapshot()
beginStroke()
@@ -817,10 +824,41 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
<input type="number" class="iwb-opt-num" data-opt="${key}" min="${min}" max="${max}" step="1" value="${value}">${unit ? `<span class="iwb-opt-unit">${unit}</span>` : ''}`
}
/** 快捷值圆点(用户裁定 2026-11-04:笔刷大小/矢量描边都要推荐值,跟侧栏快捷点同款交互) */
function presetDots(key, values) {
return `<span class="iwb-opt-dots">${values.map(v =>
`<button type="button" class="iwb-opt-dot" data-opt-preset="${key}" data-value="${v}" title="${v}"><i></i></button>`).join('')}</span>`
}
/** 裁剪参数行(用户裁定 2026-11-04:从画布下方浮窗搬进选项栏;Enter 确认 / Esc 取消) */
function cropOptionHTML() {
const c = S.cropConstraint
const sizeOn = c.type === 'size'
return `<span class="iwb-opt-label">比例</span>
<button type="button" class="iwb-editor-constraint-btn${c.type === 'free' ? ' active' : ''}" data-ratio="free">自由</button>
<button type="button" class="iwb-editor-constraint-btn" data-ratio="1:1">1:1</button>
<button type="button" class="iwb-editor-constraint-btn" data-ratio="4:3">4:3</button>
<button type="button" class="iwb-editor-constraint-btn" data-ratio="3:4">3:4</button>
<button type="button" class="iwb-editor-constraint-btn" data-ratio="16:9">16:9</button>
<button type="button" class="iwb-editor-constraint-btn" data-ratio="9:16">9:16</button>
<span class="iwb-opt-label">尺寸</span>
<input type="number" class="iwb-editor-size-input iwb-editor-size-w" min="1" placeholder="宽" value="800">
<span class="iwb-opt-unit">×</span>
<input type="number" class="iwb-editor-size-input iwb-editor-size-h" min="1" placeholder="高" value="800">
<button type="button" class="iwb-editor-constraint-btn iwb-editor-size-toggle" data-action="toggleSizeConstraint" aria-pressed="${sizeOn}">启用尺寸</button>
<button type="button" class="iwb-editor-constraint-btn iwb-editor-size-apply" data-action="applySize">应用</button>
<span class="iwb-editor-crop-info">--</span>
<button type="button" class="iwb-editor-crop-confirm" data-action="confirmCrop" title="Enter">✓ 确认</button>
<button type="button" class="iwb-editor-crop-cancel" data-action="cancelCrop" title="Esc">✗ 取消</button>
<span class="iwb-opt-hint">拖出裁剪框;Enter 确认 · Esc 取消;裁剪后可 Ctrl+Z</span>`
}
/** 每个工具只露最常用的 2–4 个参数,全部直接写回 S(右侧参数面板同步) */
function optionBarHTML(tool) {
if (tool === 'crop') return cropOptionHTML()
if (tool === 'brush' || tool === 'eraser') {
return rangeOpt('brushSize', '大小', 1, 300, S.brushSize, '')
+ presetDots('brushSize', [2, 4, 8, 12, 16, 24, 32])
+ `<span class="iwb-opt-label">硬度</span>
<input type="range" class="iwb-opt-range" data-opt="brushHardness" min="0" max="100" step="1" value="${S.brushHardness}">
<span class="iwb-opt-val" data-opt-val="brushHardness">${S.brushHardness}</span>
@@ -830,12 +868,14 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
}
if (tool === 'rect' || tool === 'circle') {
return rangeOpt('shapeLineWidth', '线宽', 1, 60, S.shapeLineWidth, '')
+ presetDots('shapeLineWidth', [1, 2, 3, 6, 10, 16, 24])
+ `<span class="iwb-opt-label">样式</span>
<button class="iwb-opt-btn${S.rectFill ? '' : ' active'}" data-opt-toggle="stroke" data-value="stroke">描边</button>
<button class="iwb-opt-btn${S.rectFill ? ' active' : ''}" data-opt-toggle="fill" data-value="fill">实心</button>`
}
if (tool === 'arrow' || tool === 'pen') {
return rangeOpt('shapeLineWidth', '线宽', 1, 60, S.shapeLineWidth, '')
+ presetDots('shapeLineWidth', [1, 2, 3, 6, 10, 16, 24])
+ `<span class="iwb-opt-hint">${tool === 'pen' ? '逐点点击,点回起点闭合' : '拖出箭头,松手完成'}</span>`
}
if (tool === 'text') {
@@ -865,6 +905,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
if (!force && body.dataset.tool === S.tool) { syncOptionBar(); return }
body.dataset.tool = S.tool
body.innerHTML = optionBarHTML(S.tool)
if (S.tool === 'crop') updateCropInfo() // 重渲染后裁剪尺寸摘要要立即就位
syncOptionBar()
}
@@ -1063,7 +1104,49 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
if (toggle) {
e.stopPropagation()
setShapeFill(toggle.dataset.value === 'fill')
return
}
// 快捷值圆点:笔刷大小 / 矢量描边(用户裁定进选项栏)
const dot = e.target.closest('[data-opt-preset]')
if (dot) {
e.stopPropagation()
setOptionFromControl({ dataset: { opt: dot.dataset.optPreset }, value: dot.dataset.value }, true)
return
}
// ---- 裁剪控件(从画布下方浮窗搬进选项栏,委托绑定防重渲染丢事件) ----
const ratioBtn = e.target.closest('[data-ratio]')
if (ratioBtn) {
e.stopPropagation()
const ratio = parseRatio(ratioBtn.dataset.ratio)
body.querySelectorAll('.iwb-editor-constraint-btn[data-ratio]').forEach(b => b.classList.toggle('active', b === ratioBtn))
setConstraint(ratio > 0 ? 'ratio' : 'free', ratio > 0 ? ratio : 0, 0, 0)
return
}
if (e.target.closest('[data-action="toggleSizeConstraint"]')) {
e.stopPropagation()
if (S.cropConstraint.type === 'size') { setConstraint('free', 0, 0, 0) }
else {
const w = parseInt(body.querySelector('.iwb-editor-size-w').value, 10)
const h = parseInt(body.querySelector('.iwb-editor-size-h').value, 10)
if (w > 0 && h > 0) {
body.querySelectorAll('.iwb-editor-constraint-btn[data-ratio]').forEach(b => b.classList.remove('active'))
setConstraint('size', w / h, w, h)
}
}
return
}
if (e.target.closest('[data-action="applySize"]')) {
e.stopPropagation()
const w = parseInt(body.querySelector('.iwb-editor-size-w').value, 10)
const h = parseInt(body.querySelector('.iwb-editor-size-h').value, 10)
if (w > 0 && h > 0) {
body.querySelectorAll('.iwb-editor-constraint-btn[data-ratio]').forEach(b => b.classList.remove('active'))
setConstraint('size', w / h, w, h)
}
return
}
if (e.target.closest('[data-action="confirmCrop"]')) { e.stopPropagation(); confirmCrop(); return }
if (e.target.closest('[data-action="cancelCrop"]')) { e.stopPropagation(); cancelCrop(); return }
})
}
@@ -1098,7 +1181,6 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
export function bindEvents() {
bindToolButtons()
bindColorPanel()
bindCropConstraintControls()
bindColorPalette()
bindFillToggle()
bindLayerOrderButtons()
@@ -1106,7 +1188,6 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
bindBrushParams()
bindZoomButtons()
bindHistoryActions()
bindCropActions()
bindLayerAddActions()
bindExportActions()
bindWheelZoom()
@@ -1164,39 +1245,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
if (hue) { hue.addEventListener('pointerdown', e => { pick(hue, e) }); hue.addEventListener('pointermove', e => { if (e.buttons) pick(hue, e) }) }
}
/** 裁剪约束控件:比例按钮、尺寸约束开关、应用尺寸按钮 */
function bindCropConstraintControls() {
// 比例约束按钮
runtime.el.querySelectorAll('.iwb-editor-constraint-btn[data-ratio]').forEach(btn => {
btn.addEventListener('mousedown', (e) => e.stopPropagation())
btn.addEventListener('click', () => {
const r = btn.dataset.ratio
runtime.el.querySelectorAll('.iwb-editor-constraint-btn[data-ratio]').forEach(b => b.classList.remove('active'))
btn.classList.add('active')
const ratio = parseRatio(r)
if (ratio > 0) {
setConstraint('ratio', ratio, 0, 0)
} else {
setConstraint('free', 0, 0, 0)
}
})
})
// 尺寸约束开关
const sizeToggle = runtime.el.querySelector('[data-action="toggleSizeConstraint"]')
if (sizeToggle) sizeToggle.addEventListener('click', () => { if (S.cropConstraint.type === 'size') setConstraint('free', 0, 0, 0); else { const w = parseInt(runtime.el.querySelector('.iwb-editor-size-w').value, 10), h = parseInt(runtime.el.querySelector('.iwb-editor-size-h').value, 10); if (w > 0 && h > 0) setConstraint('size', w / h, w, h) } })
// 应用尺寸按钮
bindIf('[data-action="applySize"]', 'mousedown', (e) => e.stopPropagation())
bindIf('[data-action="applySize"]', 'click', () => {
const w = parseInt(runtime.el.querySelector('.iwb-editor-size-w').value, 10)
const h = parseInt(runtime.el.querySelector('.iwb-editor-size-h').value, 10)
if (w > 0 && h > 0) {
runtime.el.querySelectorAll('.iwb-editor-constraint-btn[data-ratio]').forEach(b => b.classList.remove('active'))
setConstraint('size', w / h, w, h)
}
})
}
/** 裁剪约束控件已迁入选项栏(bindLiteUI 的点击委托统一处理),旧的直连绑定移除。 */
/** 调色板色块与自定义取色 input */
function bindColorPalette() {
@@ -1328,12 +1377,7 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
restoreBtn && restoreBtn.addEventListener('click', restoreOriginal)
}
/** 裁剪确认 / 取消按钮 */
function bindCropActions() {
// 裁剪
bindIf('[data-action="confirmCrop"]', 'click', confirmCrop)
bindIf('[data-action="cancelCrop"]', 'click', cancelCrop)
}
/** 裁剪确认/取消已迁入选项栏(bindLiteUI 委托),旧直连绑定移除。 */
/** 添加位图图层(导入图片)与新建空白图层 */
function bindLayerAddActions() {
@@ -1622,8 +1666,12 @@ import { hexToRgb, parseRatio, rgbToHex } from '../vendor/image-math.js'
}
if (e.key === 'Shift') S._shiftDown = true
if (e.key === 'Enter' && S.tool === 'pen' && S.penPath) { e.preventDefault(); commitPenPath(); return }
// 裁剪:Enter 确认 / Esc 取消(用户裁定,替代浮窗上的两个按钮 —— 按钮也保留在选项栏)
if (e.key === 'Enter' && S.tool === 'crop' && S.cropRect) { e.preventDefault(); confirmCrop(); return }
if (e.key === 'Escape') {
if (isTyping(e)) { hideTextInput(); return }
// 裁剪优先:有裁剪框时 Esc 取消裁剪
if (S.tool === 'crop' && S.cropRect) { e.preventDefault(); cancelCrop(); 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 }
+1
View File
@@ -291,6 +291,7 @@ import { toast } from './commands.js'
selectedShapeId: S.selectedShapeId,
selectedTextId: S.selectedTextId,
activeLayerId: S.activeLayerId,
selectedLayerId: S.selectedLayerId,
selection: selectionInfo(),
toolOptions: {
brushSize: S.brushSize,
+3
View File
@@ -193,6 +193,9 @@ import { isDrawableLayer } from '../vendor/layer-logic.js'
export function drawBitmapSelection(layer) {
if (!layer) return
// 变换框/手柄只在「选择」工具下出现(用户裁定 2026-11-04):
// 画笔/橡皮等绘制工具下不显示调节杆,避免误抓手柄也减少视觉噪音。
if (S.tool !== 'select') return
const drawable = isDrawableLayer(layer)
if (!layer.transformable && !drawable) return
const box = layerFrame(layer)