refactor(editor): keep the press and block-drag helpers within 5 locals
Split onClick (place / select click), onPointerDown (targetAt) and useBlockDrag (dragActions) to respect the 5-locals limit. No behaviour change; no test changed.
This commit is contained in:
1 parent
08d266c89f
commit
35e4a4c75a
2 files changed
+54
-32
No files matched your search
@@ -47,18 +47,18 @@ function useDropOnChange(target: MoveTarget | null, drop: () => void) {
|
||||
useEffect(() => dropRef.current(), [id, text])
|
||||
}
|
||||
|
||||
/**
|
||||
* The selection moved as a block (`drag_shape`, `drag_point`): the overlay previews the
|
||||
* translated outline, kept local to the canvas for speed; the drop commits once.
|
||||
*/
|
||||
export function useBlockDrag(input: BlockDragInput) {
|
||||
const drag = useRef<Drag | null>(null)
|
||||
const [preview, setPreview] = useState<Geometry | null>(null)
|
||||
type SetPreview = (geometry: Geometry | null) => void
|
||||
|
||||
/** Start, follow, drop and cancel of the block drag held in `drag`. */
|
||||
function dragActions(
|
||||
input: BlockDragInput,
|
||||
drag: RefObject<Drag | null>,
|
||||
setPreview: SetPreview,
|
||||
) {
|
||||
const cancel = () => {
|
||||
drag.current = null
|
||||
setPreview(null)
|
||||
}
|
||||
useDropOnChange(input.target, cancel)
|
||||
const start = (pressedAt: XY, client: XY) => {
|
||||
const origin = clientToDrawing(input.overlay.current, pressedAt)
|
||||
if (!input.target || !origin) return
|
||||
@@ -77,5 +77,17 @@ export function useBlockDrag(input: BlockDragInput) {
|
||||
cancel()
|
||||
if (gesture && dropped) input.onDrop(gesture.target.id, dropped)
|
||||
}
|
||||
return { preview, start, follow, drop, cancel }
|
||||
return { start, follow, drop, cancel }
|
||||
}
|
||||
|
||||
/**
|
||||
* The selection moved as a block (`drag_shape`, `drag_point`): the overlay previews the
|
||||
* translated outline, kept local to the canvas for speed; the drop commits once.
|
||||
*/
|
||||
export function useBlockDrag(input: BlockDragInput) {
|
||||
const drag = useRef<Drag | null>(null)
|
||||
const [preview, setPreview] = useState<Geometry | null>(null)
|
||||
const actions = dragActions(input, drag, setPreview)
|
||||
useDropOnChange(input.target, actions.cancel)
|
||||
return { preview, ...actions }
|
||||
}
|
||||
@@ -136,21 +136,26 @@ interface Feed {
|
||||
drag: ReturnType<typeof useBlockDrag>
|
||||
}
|
||||
|
||||
/** The click effect: a drawing tool places, the select tool selects or deselects. */
|
||||
function onClick(f: Feed, target: PressTarget, event: PointerEvent): void {
|
||||
const { input } = f
|
||||
const at = clientOf(event)
|
||||
if (input.tool !== 'select') {
|
||||
const drawn = clientToDrawing(input.overlay.current, at)
|
||||
if (drawn) input.onPlace(drawn)
|
||||
return
|
||||
}
|
||||
/** A click with a drawing tool places at the pointer-up, in drawing units. */
|
||||
function place(input: PressInput, event: PointerEvent): void {
|
||||
const drawn = clientToDrawing(input.overlay.current, clientOf(event))
|
||||
if (drawn) input.onPlace(drawn)
|
||||
}
|
||||
|
||||
/** A click with the select tool: its action, or `open` as the second of a double click. */
|
||||
function selectClick(f: Feed, target: PressTarget, event: PointerEvent): void {
|
||||
const m = f.machine.current
|
||||
const sel = selectionOf(input.selectedId)
|
||||
const at = clientOf(event)
|
||||
const sel = selectionOf(f.input.selectedId)
|
||||
const double = isDoubleClick(m.lastClick, at, event.timeStamp)
|
||||
m.lastClick = double ? null : { at, time: event.timeStamp }
|
||||
const action = clickAction(target, sel)
|
||||
runClick(double ? doubled(action, target, sel) : action, input)
|
||||
runClick(double ? doubled(action, target, sel) : action, f.input)
|
||||
}
|
||||
|
||||
function onClick(f: Feed, target: PressTarget, event: PointerEvent): void {
|
||||
if (f.input.tool === 'select') selectClick(f, target, event)
|
||||
else place(f.input, event)
|
||||
}
|
||||
|
||||
const BLOCK_DRAGS = new Set(['drag_shape', 'drag_point'])
|
||||
@@ -234,23 +239,28 @@ function startPress(latest: RefObject<Feed>, event: PointerEvent): void {
|
||||
m.stop = watchOtherPointers(latest)
|
||||
}
|
||||
|
||||
/** The press's target: the selection's handles, then the topmost annotation. */
|
||||
function targetAt(input: PressInput, at: XY): PressTarget {
|
||||
const hits = hitsAt(input.viewport.current, at, pointIdOf(input.target))
|
||||
return pressTarget(hits, at, selectionOf(input.selectedId))
|
||||
}
|
||||
|
||||
function onPointerDown(latest: RefObject<Feed>, event: PointerEvent): void {
|
||||
const f = latest.current
|
||||
const m = f.machine.current
|
||||
const m = latest.current.machine.current
|
||||
const at = clientOf(event)
|
||||
const hits = hitsAt(f.input.viewport.current, at, pointIdOf(f.input.target))
|
||||
m.target = pressTarget(hits, at, selectionOf(f.input.selectedId))
|
||||
m.target = targetAt(latest.current.input, at)
|
||||
if (event.button !== 0) return
|
||||
if (isFresh(m, event)) startPress(latest, event)
|
||||
m.pointers.add(event.pointerId)
|
||||
const { pointerId } = event
|
||||
const kind = pointerKindOf(event.pointerType)
|
||||
const pointers = m.pointers.size
|
||||
feed(
|
||||
f,
|
||||
{ type: 'down', pointerId, kind, at, target: m.target, pointers },
|
||||
event,
|
||||
)
|
||||
const down: PressEvent = {
|
||||
type: 'down',
|
||||
pointerId: event.pointerId,
|
||||
kind: pointerKindOf(event.pointerType),
|
||||
at,
|
||||
target: m.target,
|
||||
pointers: m.pointers.size,
|
||||
}
|
||||
feed(latest.current, down, event)
|
||||
}
|
||||
|
||||
function onPointerMove(f: Feed, event: PointerEvent): void {
|
||||
|
||||
Reference in new issue
Block a user