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
+52
-30
No files matched your search
@@ -47,18 +47,18 @@ function useDropOnChange(target: MoveTarget | null, drop: () => void) {
|
|||||||
useEffect(() => dropRef.current(), [id, text])
|
useEffect(() => dropRef.current(), [id, text])
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
type SetPreview = (geometry: Geometry | null) => void
|
||||||
* 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.
|
/** Start, follow, drop and cancel of the block drag held in `drag`. */
|
||||||
*/
|
function dragActions(
|
||||||
export function useBlockDrag(input: BlockDragInput) {
|
input: BlockDragInput,
|
||||||
const drag = useRef<Drag | null>(null)
|
drag: RefObject<Drag | null>,
|
||||||
const [preview, setPreview] = useState<Geometry | null>(null)
|
setPreview: SetPreview,
|
||||||
|
) {
|
||||||
const cancel = () => {
|
const cancel = () => {
|
||||||
drag.current = null
|
drag.current = null
|
||||||
setPreview(null)
|
setPreview(null)
|
||||||
}
|
}
|
||||||
useDropOnChange(input.target, cancel)
|
|
||||||
const start = (pressedAt: XY, client: XY) => {
|
const start = (pressedAt: XY, client: XY) => {
|
||||||
const origin = clientToDrawing(input.overlay.current, pressedAt)
|
const origin = clientToDrawing(input.overlay.current, pressedAt)
|
||||||
if (!input.target || !origin) return
|
if (!input.target || !origin) return
|
||||||
@@ -77,5 +77,17 @@ export function useBlockDrag(input: BlockDragInput) {
|
|||||||
cancel()
|
cancel()
|
||||||
if (gesture && dropped) input.onDrop(gesture.target.id, dropped)
|
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>
|
drag: ReturnType<typeof useBlockDrag>
|
||||||
}
|
}
|
||||||
|
|
||||||
/** The click effect: a drawing tool places, the select tool selects or deselects. */
|
/** A click with a drawing tool places at the pointer-up, in drawing units. */
|
||||||
function onClick(f: Feed, target: PressTarget, event: PointerEvent): void {
|
function place(input: PressInput, event: PointerEvent): void {
|
||||||
const { input } = f
|
const drawn = clientToDrawing(input.overlay.current, clientOf(event))
|
||||||
const at = clientOf(event)
|
|
||||||
if (input.tool !== 'select') {
|
|
||||||
const drawn = clientToDrawing(input.overlay.current, at)
|
|
||||||
if (drawn) input.onPlace(drawn)
|
if (drawn) input.onPlace(drawn)
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 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 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)
|
const double = isDoubleClick(m.lastClick, at, event.timeStamp)
|
||||||
m.lastClick = double ? null : { at, time: event.timeStamp }
|
m.lastClick = double ? null : { at, time: event.timeStamp }
|
||||||
const action = clickAction(target, sel)
|
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'])
|
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)
|
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 {
|
function onPointerDown(latest: RefObject<Feed>, event: PointerEvent): void {
|
||||||
const f = latest.current
|
const m = latest.current.machine.current
|
||||||
const m = f.machine.current
|
|
||||||
const at = clientOf(event)
|
const at = clientOf(event)
|
||||||
const hits = hitsAt(f.input.viewport.current, at, pointIdOf(f.input.target))
|
m.target = targetAt(latest.current.input, at)
|
||||||
m.target = pressTarget(hits, at, selectionOf(f.input.selectedId))
|
|
||||||
if (event.button !== 0) return
|
if (event.button !== 0) return
|
||||||
if (isFresh(m, event)) startPress(latest, event)
|
if (isFresh(m, event)) startPress(latest, event)
|
||||||
m.pointers.add(event.pointerId)
|
m.pointers.add(event.pointerId)
|
||||||
const { pointerId } = event
|
const down: PressEvent = {
|
||||||
const kind = pointerKindOf(event.pointerType)
|
type: 'down',
|
||||||
const pointers = m.pointers.size
|
pointerId: event.pointerId,
|
||||||
feed(
|
kind: pointerKindOf(event.pointerType),
|
||||||
f,
|
at,
|
||||||
{ type: 'down', pointerId, kind, at, target: m.target, pointers },
|
target: m.target,
|
||||||
event,
|
pointers: m.pointers.size,
|
||||||
)
|
}
|
||||||
|
feed(latest.current, down, event)
|
||||||
}
|
}
|
||||||
|
|
||||||
function onPointerMove(f: Feed, event: PointerEvent): void {
|
function onPointerMove(f: Feed, event: PointerEvent): void {
|
||||||
|
|||||||
Reference in new issue
Block a user