Files

112 lines
4.0 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* floating.js - 选区浮动操作条(便利版 UI 的核心)
*
* 它是纯视图:按钮只调用 commands.runCommand(),因此浮动条 / 右键菜单 / 快捷键
* 三条入口天然一致;日后用户体验后决定保留、换位置或移除,都不影响选区功能。
*
* DOM 元素由 dom.js 静态生成(.iwb-editor-sel-bar),本模块只负责绑定、定位与显隐。
*/
'use strict'
import { S, runtime } from './state.js'
import { runCommand, toast } from './commands.js'
import { hasSelection, selectionInfo } from './selection.js'
let contextMode = false
function bar() {
return runtime.el && runtime.el.querySelector('.iwb-editor-sel-bar')
}
/** 事件委托:所有 [data-sel-cmd] 走同一命令层 */
export function bindFloating() {
const el = bar()
if (!el || el.dataset.bound === '1') return
el.dataset.bound = '1'
el.addEventListener('mousedown', (e) => e.stopPropagation())
el.addEventListener('click', (e) => {
const btn = e.target.closest('[data-sel-cmd]')
if (!btn) return
e.stopPropagation()
runCommand(btn.dataset.selCmd)
contextMode = false
positionFloating()
})
}
/** 图片坐标 → canvas-wrap 内屏幕坐标 */
function imageToWrap(x, y) {
const stack = S.canvasStack.getBoundingClientRect()
const wrap = S.canvasWrap.getBoundingClientRect()
const scale = S.imgW ? (S.fitW * S.zoom) / S.imgW : 1
const pad = S.overlayPad || 0
return {
x: stack.left - wrap.left + (pad + x) * scale,
y: stack.top - wrap.top + (pad + y) * scale
}
}
/**
* 定位:默认贴在选区右下角外侧;靠边时自动翻到左侧/上方,始终留在工作区内。
* 蚂蚁线 rAF 每帧调用,因此缩放/平移时它会跟着选区走。
*/
export function positionFloating() {
const el = bar()
if (!el || !S.open) return
const active = hasSelection()
if (!active && !contextMode) { el.style.display = 'none'; return }
el.style.display = 'flex'
el.classList.toggle('as-menu', contextMode)
const wrapW = S.canvasWrap.clientWidth
const wrapH = S.canvasWrap.clientHeight
const bw = el.offsetWidth || 300
const bh = el.offsetHeight || 36
let left, top
if (contextMode && el.dataset.cx !== undefined) {
left = Number(el.dataset.cx)
top = Number(el.dataset.cy)
} else {
const b = S.selection.bbox
const p = imageToWrap(b.x + b.w, b.y + b.h)
left = p.x + 10
top = p.y + 8
if (left + bw > wrapW - 8) left = imageToWrap(b.x, b.y).x + (b.w * (S.fitW * S.zoom) / S.imgW) - bw
if (top + bh > wrapH - 8) top = imageToWrap(b.x, b.y).y - bh - 8
}
el.style.left = Math.max(8, Math.min(left, Math.max(8, wrapW - bw - 8))) + 'px'
el.style.top = Math.max(8, Math.min(top, Math.max(8, wrapH - bh - 8))) + 'px'
// 无选区时禁用「需要选区」的按钮(右键菜单里仍能看到「全选」)
el.querySelectorAll('[data-sel-cmd]').forEach(btn => {
const need = btn.dataset.needsSel === '1'
btn.disabled = need && !active
})
const info = el.querySelector('.iwb-editor-sel-info')
if (info) {
const s = selectionInfo()
info.textContent = s ? (s.w + '×' + s.h + (s.feather ? ' · 羽化 ' + s.feather : '')) : '未框选'
}
}
/** 右键菜单:同一条操作条,定位到鼠标处 */
export function showSelectionContext(e) {
const el = bar()
if (!el) return
const wrap = S.canvasWrap.getBoundingClientRect()
contextMode = true
el.dataset.cx = String(e.clientX - wrap.left)
el.dataset.cy = String(e.clientY - wrap.top)
positionFloating()
if (!hasSelection()) toast('右键菜单:可先「全选」,或框选后直接操作', 'info')
}
export function hideSelectionContext() {
if (!contextMode) return
contextMode = false
const el = bar()
if (el) el.classList.remove('as-menu')
positionFloating()
}
export function isSelectionContextOpen() { return contextMode }