@openmaic/renderer 0.0.5 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -1
- package/dist/SlideCanvas.d.ts +14 -1
- package/dist/SlideCanvas.js +17 -7
- package/dist/SlideCanvas.js.map +1 -1
- package/dist/SlideElement.d.ts +10 -2
- package/dist/SlideElement.js +16 -5
- package/dist/SlideElement.js.map +1 -1
- package/dist/elements/audio/BaseAudioElement.d.ts +10 -0
- package/dist/elements/audio/BaseAudioElement.js +71 -0
- package/dist/elements/audio/BaseAudioElement.js.map +1 -0
- package/dist/elements/image/BaseImageElement.js +1 -1
- package/dist/elements/image/BaseImageElement.js.map +1 -1
- package/dist/elements/index.d.ts +2 -0
- package/dist/elements/index.js +2 -0
- package/dist/elements/index.js.map +1 -1
- package/dist/elements/shape/BaseShapeElement.d.ts +4 -1
- package/dist/elements/shape/BaseShapeElement.js +19 -18
- package/dist/elements/shape/BaseShapeElement.js.map +1 -1
- package/dist/elements/table/BaseTableElement.d.ts +3 -1
- package/dist/elements/table/BaseTableElement.js +3 -2
- package/dist/elements/table/BaseTableElement.js.map +1 -1
- package/dist/elements/table/StaticTable.js +1 -3
- package/dist/elements/table/StaticTable.js.map +1 -1
- package/dist/elements/text/BaseTextElement.d.ts +3 -1
- package/dist/elements/text/BaseTextElement.js +9 -5
- package/dist/elements/text/BaseTextElement.js.map +1 -1
- package/dist/elements/video/BaseVideoElement.js +1 -1
- package/dist/elements/video/BaseVideoElement.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/styles.d.ts +5 -6
- package/dist/styles.js +21 -5
- package/dist/styles.js.map +1 -1
- package/package.json +2 -6
- package/dist/editing/EditableSlideCanvas.d.ts +0 -26
- package/dist/editing/EditableSlideCanvas.js +0 -352
- package/dist/editing/EditableSlideCanvas.js.map +0 -1
- package/dist/editing/core/drag.d.ts +0 -108
- package/dist/editing/core/drag.js +0 -119
- package/dist/editing/core/drag.js.map +0 -1
- package/dist/editing/core/geometry.d.ts +0 -48
- package/dist/editing/core/geometry.js +0 -186
- package/dist/editing/core/geometry.js.map +0 -1
- package/dist/editing/core/intent.d.ts +0 -39
- package/dist/editing/core/intent.js +0 -35
- package/dist/editing/core/intent.js.map +0 -1
- package/dist/editing/core/line-drag.d.ts +0 -19
- package/dist/editing/core/line-drag.js +0 -163
- package/dist/editing/core/line-drag.js.map +0 -1
- package/dist/editing/core/marquee.d.ts +0 -56
- package/dist/editing/core/marquee.js +0 -74
- package/dist/editing/core/marquee.js.map +0 -1
- package/dist/editing/core/resize.d.ts +0 -121
- package/dist/editing/core/resize.js +0 -392
- package/dist/editing/core/resize.js.map +0 -1
- package/dist/editing/core/rotate.d.ts +0 -37
- package/dist/editing/core/rotate.js +0 -74
- package/dist/editing/core/rotate.js.map +0 -1
- package/dist/editing/core/selection.d.ts +0 -68
- package/dist/editing/core/selection.js +0 -108
- package/dist/editing/core/selection.js.map +0 -1
- package/dist/editing/core/snapping.d.ts +0 -54
- package/dist/editing/core/snapping.js +0 -96
- package/dist/editing/core/snapping.js.map +0 -1
- package/dist/editing/handles/BorderLine.d.ts +0 -15
- package/dist/editing/handles/BorderLine.js +0 -17
- package/dist/editing/handles/BorderLine.js.map +0 -1
- package/dist/editing/handles/LineHandles.d.ts +0 -28
- package/dist/editing/handles/LineHandles.js +0 -51
- package/dist/editing/handles/LineHandles.js.map +0 -1
- package/dist/editing/handles/MarqueeBox.d.ts +0 -19
- package/dist/editing/handles/MarqueeBox.js +0 -26
- package/dist/editing/handles/MarqueeBox.js.map +0 -1
- package/dist/editing/handles/ResizeHandles.d.ts +0 -31
- package/dist/editing/handles/ResizeHandles.js +0 -44
- package/dist/editing/handles/ResizeHandles.js.map +0 -1
- package/dist/editing/handles/RotateHandle.d.ts +0 -26
- package/dist/editing/handles/RotateHandle.js +0 -52
- package/dist/editing/handles/RotateHandle.js.map +0 -1
- package/dist/editing/handles/SelectionOverlay.d.ts +0 -18
- package/dist/editing/handles/SelectionOverlay.js +0 -35
- package/dist/editing/handles/SelectionOverlay.js.map +0 -1
- package/dist/editing/index.d.ts +0 -3
- package/dist/editing/index.js +0 -4
- package/dist/editing/index.js.map +0 -1
- package/dist/editing/types.d.ts +0 -107
- package/dist/editing/types.js +0 -7
- package/dist/editing/types.js.map +0 -1
- package/dist/editing/useEditGesture.d.ts +0 -46
- package/dist/editing/useEditGesture.js +0 -217
- package/dist/editing/useEditGesture.js.map +0 -1
- package/dist/editing/useLineHandleGesture.d.ts +0 -35
- package/dist/editing/useLineHandleGesture.js +0 -112
- package/dist/editing/useLineHandleGesture.js.map +0 -1
- package/dist/editing/useMarqueeGesture.d.ts +0 -43
- package/dist/editing/useMarqueeGesture.js +0 -157
- package/dist/editing/useMarqueeGesture.js.map +0 -1
- package/dist/editing/useResizeGesture.d.ts +0 -43
- package/dist/editing/useResizeGesture.js +0 -137
- package/dist/editing/useResizeGesture.js.map +0 -1
- package/dist/editing/useRotateGesture.d.ts +0 -47
- package/dist/editing/useRotateGesture.js +0 -128
- package/dist/editing/useRotateGesture.js.map +0 -1
|
@@ -1,112 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useState, useRef, useEffect } from 'react';
|
|
3
|
-
import { computeLineDrag } from './core/line-drag.js';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Distance (screen px) a handle must travel between pointer-down and pointer-up
|
|
7
|
-
* before the gesture commits a reshape. Below this a handle click is inert (no
|
|
8
|
-
* `element.update`), mirroring the box drag/click threshold in useEditGesture.
|
|
9
|
-
*/
|
|
10
|
-
const DRAG_THRESHOLD_PX = 2;
|
|
11
|
-
/**
|
|
12
|
-
* Owns one line-handle reshape gesture, mirroring {@link useEditGesture} but for
|
|
13
|
-
* a single dragged endpoint/control point. Pointer-down records the pointer
|
|
14
|
-
* start + the ORIGINAL line + the handle; window pointer-move converts the
|
|
15
|
-
* screen delta to canvas units (`/scale`), runs the store-free
|
|
16
|
-
* {@link computeLineDrag}, and republishes the resulting props as a working copy
|
|
17
|
-
* for 60fps preview; pointer-up commits exactly one `element.update` intent when
|
|
18
|
-
* the pointer moved past {@link DRAG_THRESHOLD_PX} and a mutation channel exists,
|
|
19
|
-
* then clears the working copy. `pointercancel` reverts without emitting.
|
|
20
|
-
*
|
|
21
|
-
* Single-pointer by design: a second pointer-down while a gesture is in flight
|
|
22
|
-
* is ignored, and window move/up from any other pointerId is dropped. Pure
|
|
23
|
-
* gesture glue: no store, no `@/` imports.
|
|
24
|
-
*/
|
|
25
|
-
function useLineHandleGesture(args) {
|
|
26
|
-
const { scale, onElementsChange } = args;
|
|
27
|
-
const [lineDrag, setLineDrag] = useState(null);
|
|
28
|
-
// Teardown for the currently-armed gesture's window listeners; the unmount
|
|
29
|
-
// effect removes any listeners still live so a late move/up can't setState on
|
|
30
|
-
// an unmounted host.
|
|
31
|
-
const teardownRef = useRef(null);
|
|
32
|
-
// The pointerId owning the in-flight gesture (single-pointer guard).
|
|
33
|
-
const activePointerRef = useRef(null);
|
|
34
|
-
useEffect(() => () => {
|
|
35
|
-
teardownRef.current?.();
|
|
36
|
-
teardownRef.current = null;
|
|
37
|
-
activePointerRef.current = null;
|
|
38
|
-
}, []);
|
|
39
|
-
const onHandlePointerDown = (element, handle, e) => {
|
|
40
|
-
// Defense-in-depth: never arm on a locked line (the primary guard skips
|
|
41
|
-
// rendering handles for a locked line in EditableSlideCanvas).
|
|
42
|
-
if (element.lock)
|
|
43
|
-
return;
|
|
44
|
-
if (activePointerRef.current !== null)
|
|
45
|
-
return;
|
|
46
|
-
activePointerRef.current = e.pointerId;
|
|
47
|
-
// A handle drag reshapes the line — it must never fall through to the box/
|
|
48
|
-
// line hit layer beneath (which would start a move or change selection).
|
|
49
|
-
e.stopPropagation();
|
|
50
|
-
const startX = e.clientX;
|
|
51
|
-
const startY = e.clientY;
|
|
52
|
-
const effectiveScale = scale || 1;
|
|
53
|
-
try {
|
|
54
|
-
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
55
|
-
}
|
|
56
|
-
catch {
|
|
57
|
-
// jsdom / unsupported: pointer capture is a best-effort nicety.
|
|
58
|
-
}
|
|
59
|
-
const compute = (clientX, clientY) => computeLineDrag({
|
|
60
|
-
element,
|
|
61
|
-
handle,
|
|
62
|
-
deltaCanvas: {
|
|
63
|
-
x: (clientX - startX) / effectiveScale,
|
|
64
|
-
y: (clientY - startY) / effectiveScale,
|
|
65
|
-
},
|
|
66
|
-
});
|
|
67
|
-
const handleMove = (ev) => {
|
|
68
|
-
if (ev.pointerId !== activePointerRef.current)
|
|
69
|
-
return;
|
|
70
|
-
// No mutation channel: a live preview would just follow the pointer and
|
|
71
|
-
// snap back on pointer-up. Skip live movement when nothing can commit.
|
|
72
|
-
if (!onElementsChange)
|
|
73
|
-
return;
|
|
74
|
-
const { props } = compute(ev.clientX, ev.clientY);
|
|
75
|
-
setLineDrag({ id: element.id, props });
|
|
76
|
-
};
|
|
77
|
-
const removeListeners = () => {
|
|
78
|
-
window.removeEventListener('pointermove', handleMove);
|
|
79
|
-
window.removeEventListener('pointerup', handleUp);
|
|
80
|
-
window.removeEventListener('pointercancel', handleCancel);
|
|
81
|
-
};
|
|
82
|
-
const handleUp = (ev) => {
|
|
83
|
-
if (ev.pointerId !== activePointerRef.current)
|
|
84
|
-
return;
|
|
85
|
-
removeListeners();
|
|
86
|
-
teardownRef.current = null;
|
|
87
|
-
activePointerRef.current = null;
|
|
88
|
-
const movedPast = Math.hypot(ev.clientX - startX, ev.clientY - startY) > DRAG_THRESHOLD_PX;
|
|
89
|
-
if (movedPast && onElementsChange) {
|
|
90
|
-
const { props } = compute(ev.clientX, ev.clientY);
|
|
91
|
-
onElementsChange([{ type: 'element.update', id: element.id, props }]);
|
|
92
|
-
}
|
|
93
|
-
setLineDrag(null);
|
|
94
|
-
};
|
|
95
|
-
const handleCancel = (ev) => {
|
|
96
|
-
if (ev.pointerId !== activePointerRef.current)
|
|
97
|
-
return;
|
|
98
|
-
removeListeners();
|
|
99
|
-
teardownRef.current = null;
|
|
100
|
-
activePointerRef.current = null;
|
|
101
|
-
setLineDrag(null);
|
|
102
|
-
};
|
|
103
|
-
window.addEventListener('pointermove', handleMove);
|
|
104
|
-
window.addEventListener('pointerup', handleUp);
|
|
105
|
-
window.addEventListener('pointercancel', handleCancel);
|
|
106
|
-
teardownRef.current = removeListeners;
|
|
107
|
-
};
|
|
108
|
-
return { lineDrag, onHandlePointerDown };
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
export { useLineHandleGesture };
|
|
112
|
-
//# sourceMappingURL=useLineHandleGesture.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useLineHandleGesture.js","sources":["../../src/editing/useLineHandleGesture.ts"],"sourcesContent":["'use client';\n\nimport { useState, useEffect, useRef, type PointerEvent as ReactPointerEvent } from 'react';\nimport type { PPTLineElement } from '@openmaic/dsl';\n\nimport { computeLineDrag, type LineDragResult } from './core/line-drag';\nimport type { EditIntent, LineHandle } from './types';\n\n/**\n * Distance (screen px) a handle must travel between pointer-down and pointer-up\n * before the gesture commits a reshape. Below this a handle click is inert (no\n * `element.update`), mirroring the box drag/click threshold in useEditGesture.\n */\nconst DRAG_THRESHOLD_PX = 2;\n\nexport interface UseLineHandleGestureArgs {\n /** Canvas → screen scale; the inverse converts the pointer delta to canvas units. */\n scale: number;\n onElementsChange?: (intents: EditIntent[]) => void;\n}\n\n/** The in-flight handle drag: which line, and the working props to preview. */\nexport interface LineHandleDrag {\n id: string;\n props: LineDragResult['props'];\n}\n\nexport interface UseLineHandleGestureResult {\n /** The active handle drag's working props (for live preview), else `null`. */\n lineDrag: LineHandleDrag | null;\n /** Arm a reshape gesture from a pointer-down on `element`'s `handle`. */\n onHandlePointerDown: (element: PPTLineElement, handle: LineHandle, e: ReactPointerEvent) => void;\n}\n\n/**\n * Owns one line-handle reshape gesture, mirroring {@link useEditGesture} but for\n * a single dragged endpoint/control point. Pointer-down records the pointer\n * start + the ORIGINAL line + the handle; window pointer-move converts the\n * screen delta to canvas units (`/scale`), runs the store-free\n * {@link computeLineDrag}, and republishes the resulting props as a working copy\n * for 60fps preview; pointer-up commits exactly one `element.update` intent when\n * the pointer moved past {@link DRAG_THRESHOLD_PX} and a mutation channel exists,\n * then clears the working copy. `pointercancel` reverts without emitting.\n *\n * Single-pointer by design: a second pointer-down while a gesture is in flight\n * is ignored, and window move/up from any other pointerId is dropped. Pure\n * gesture glue: no store, no `@/` imports.\n */\nexport function useLineHandleGesture(args: UseLineHandleGestureArgs): UseLineHandleGestureResult {\n const { scale, onElementsChange } = args;\n\n const [lineDrag, setLineDrag] = useState<LineHandleDrag | null>(null);\n\n // Teardown for the currently-armed gesture's window listeners; the unmount\n // effect removes any listeners still live so a late move/up can't setState on\n // an unmounted host.\n const teardownRef = useRef<(() => void) | null>(null);\n // The pointerId owning the in-flight gesture (single-pointer guard).\n const activePointerRef = useRef<number | null>(null);\n\n useEffect(\n () => () => {\n teardownRef.current?.();\n teardownRef.current = null;\n activePointerRef.current = null;\n },\n [],\n );\n\n const onHandlePointerDown = (\n element: PPTLineElement,\n handle: LineHandle,\n e: ReactPointerEvent,\n ) => {\n // Defense-in-depth: never arm on a locked line (the primary guard skips\n // rendering handles for a locked line in EditableSlideCanvas).\n if (element.lock) return;\n if (activePointerRef.current !== null) return;\n activePointerRef.current = e.pointerId;\n\n // A handle drag reshapes the line — it must never fall through to the box/\n // line hit layer beneath (which would start a move or change selection).\n e.stopPropagation();\n\n const startX = e.clientX;\n const startY = e.clientY;\n const effectiveScale = scale || 1;\n\n try {\n (e.currentTarget as Element).setPointerCapture?.(e.pointerId);\n } catch {\n // jsdom / unsupported: pointer capture is a best-effort nicety.\n }\n\n const compute = (clientX: number, clientY: number) =>\n computeLineDrag({\n element,\n handle,\n deltaCanvas: {\n x: (clientX - startX) / effectiveScale,\n y: (clientY - startY) / effectiveScale,\n },\n });\n\n const handleMove = (ev: PointerEvent) => {\n if (ev.pointerId !== activePointerRef.current) return;\n // No mutation channel: a live preview would just follow the pointer and\n // snap back on pointer-up. Skip live movement when nothing can commit.\n if (!onElementsChange) return;\n const { props } = compute(ev.clientX, ev.clientY);\n setLineDrag({ id: element.id, props });\n };\n\n const removeListeners = () => {\n window.removeEventListener('pointermove', handleMove);\n window.removeEventListener('pointerup', handleUp);\n window.removeEventListener('pointercancel', handleCancel);\n };\n\n const handleUp = (ev: PointerEvent) => {\n if (ev.pointerId !== activePointerRef.current) return;\n removeListeners();\n teardownRef.current = null;\n activePointerRef.current = null;\n\n const movedPast = Math.hypot(ev.clientX - startX, ev.clientY - startY) > DRAG_THRESHOLD_PX;\n if (movedPast && onElementsChange) {\n const { props } = compute(ev.clientX, ev.clientY);\n onElementsChange([{ type: 'element.update', id: element.id, props }]);\n }\n\n setLineDrag(null);\n };\n\n const handleCancel = (ev: PointerEvent) => {\n if (ev.pointerId !== activePointerRef.current) return;\n removeListeners();\n teardownRef.current = null;\n activePointerRef.current = null;\n setLineDrag(null);\n };\n\n window.addEventListener('pointermove', handleMove);\n window.addEventListener('pointerup', handleUp);\n window.addEventListener('pointercancel', handleCancel);\n teardownRef.current = removeListeners;\n };\n\n return { lineDrag, onHandlePointerDown };\n}\n"],"names":[],"mappings":";;;;AAQA;;;;AAIG;AACH;AAqBA;;;;;;;;;;;;;AAaG;AACG;AACJ;;;;;AAOA;;AAEA;AAEA;AAEI;AACA;AACA;;;;;;;AAaF;;AACA;;;;AAMA;AACA;AACA;AAEA;;;AAEE;;;;;;AAQE;AACE;AACA;AACD;AACF;AAEH;AACE;;;;AAGA;;AACA;;AAEF;;AAGE;AACA;AACA;AACF;AAEA;AACE;;AACA;AACA;AACA;;AAGA;AACE;AACA;;;AAIJ;AAEA;AACE;;AACA;AACA;AACA;;AAEF;AAEA;AACA;AACA;AACA;AACF;AAEA;AACF;;"}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { type PointerEvent as ReactPointerEvent, type RefObject } from 'react';
|
|
2
|
-
import type { Slide } from '@openmaic/dsl';
|
|
3
|
-
import { type MarqueeRect } from './core/marquee';
|
|
4
|
-
import type { ViewportStyles } from '../hooks/useViewportSize';
|
|
5
|
-
import { type Selection } from './types';
|
|
6
|
-
export interface UseMarqueeGestureArgs {
|
|
7
|
-
slide: Slide;
|
|
8
|
-
scale: number;
|
|
9
|
-
/**
|
|
10
|
-
* The interaction overlay element. Its bounding rect (captured once per
|
|
11
|
-
* gesture, at pointer-down) plus `viewportStyles.left/top` locate the canvas
|
|
12
|
-
* origin on screen, which converts absolute pointer positions to canvas
|
|
13
|
-
* coordinates — the marquee rectangle lives in canvas units.
|
|
14
|
-
*/
|
|
15
|
-
overlayRef: RefObject<HTMLElement | null>;
|
|
16
|
-
/** SlideCanvas centering offset (screen px) inside the overlay. */
|
|
17
|
-
viewportStyles: ViewportStyles;
|
|
18
|
-
selection: Selection;
|
|
19
|
-
onSelectionChange?: (next: Selection) => void;
|
|
20
|
-
}
|
|
21
|
-
export interface UseMarqueeGestureResult {
|
|
22
|
-
/** The live marquee rectangle (canvas units) while past-threshold, else `null`. */
|
|
23
|
-
marqueeRect: MarqueeRect | null;
|
|
24
|
-
/** Arm a marquee gesture from a pointer-down on the blank-canvas capture surface. */
|
|
25
|
-
onCanvasPointerDown: (e: ReactPointerEvent) => void;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Owns one blank-canvas marquee (rubber-band) gesture, mirroring the other
|
|
29
|
-
* gesture hooks. Pointer-down on the capture surface records the canvas origin
|
|
30
|
-
* and the start point; window pointer-move converts the pointer to canvas
|
|
31
|
-
* coordinates and, once the box passes {@link MARQUEE_THRESHOLD_CANVAS} on BOTH
|
|
32
|
-
* axes, republishes a normalized rectangle for a live preview; pointer-up
|
|
33
|
-
* computes the selection ({@link computeMarqueeSelection}) and REPLACES the
|
|
34
|
-
* controlled selection via `onSelectionChange`. A sub-threshold gesture is a
|
|
35
|
-
* blank click: it clears the selection (a no-op when already empty). The mode is
|
|
36
|
-
* chosen from the modifier held at release — plain drag = `contain`, Ctrl/Shift/
|
|
37
|
-
* Meta = `intersect`.
|
|
38
|
-
*
|
|
39
|
-
* Single-pointer by design (one active pointer at a time; window move/up from
|
|
40
|
-
* any other pointerId are dropped) and `pointercancel`-safe (revert, no emit).
|
|
41
|
-
* Pure gesture glue: no store, no `@/` imports.
|
|
42
|
-
*/
|
|
43
|
-
export declare function useMarqueeGesture(args: UseMarqueeGestureArgs): UseMarqueeGestureResult;
|
|
@@ -1,157 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useState, useRef, useEffect } from 'react';
|
|
3
|
-
import { marqueeRect, computeMarqueeSelection } from './core/marquee.js';
|
|
4
|
-
import { EMPTY_SELECTION } from './types.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Minimum size (canvas units) the marquee must reach on BOTH axes before it
|
|
8
|
-
* counts as a drag-select rather than a blank click. Below this the gesture is a
|
|
9
|
-
* blank click that clears the selection; at/above it the box replaces the
|
|
10
|
-
* selection with whatever it covers. App parity (`minSelectionRange = 5`).
|
|
11
|
-
*/
|
|
12
|
-
const MARQUEE_THRESHOLD_CANVAS = 5;
|
|
13
|
-
/**
|
|
14
|
-
* Owns one blank-canvas marquee (rubber-band) gesture, mirroring the other
|
|
15
|
-
* gesture hooks. Pointer-down on the capture surface records the canvas origin
|
|
16
|
-
* and the start point; window pointer-move converts the pointer to canvas
|
|
17
|
-
* coordinates and, once the box passes {@link MARQUEE_THRESHOLD_CANVAS} on BOTH
|
|
18
|
-
* axes, republishes a normalized rectangle for a live preview; pointer-up
|
|
19
|
-
* computes the selection ({@link computeMarqueeSelection}) and REPLACES the
|
|
20
|
-
* controlled selection via `onSelectionChange`. A sub-threshold gesture is a
|
|
21
|
-
* blank click: it clears the selection (a no-op when already empty). The mode is
|
|
22
|
-
* chosen from the modifier held at release — plain drag = `contain`, Ctrl/Shift/
|
|
23
|
-
* Meta = `intersect`.
|
|
24
|
-
*
|
|
25
|
-
* Single-pointer by design (one active pointer at a time; window move/up from
|
|
26
|
-
* any other pointerId are dropped) and `pointercancel`-safe (revert, no emit).
|
|
27
|
-
* Pure gesture glue: no store, no `@/` imports.
|
|
28
|
-
*/
|
|
29
|
-
function useMarqueeGesture(args) {
|
|
30
|
-
const { slide, scale, overlayRef, viewportStyles, selection, onSelectionChange } = args;
|
|
31
|
-
const [liveRect, setLiveRect] = useState(null);
|
|
32
|
-
// Teardown for the currently-armed gesture's window listeners; the unmount
|
|
33
|
-
// effect removes any listeners still live so a late move/up can't setState on
|
|
34
|
-
// an unmounted host.
|
|
35
|
-
const teardownRef = useRef(null);
|
|
36
|
-
// The pointerId owning the in-flight gesture (single-pointer guard).
|
|
37
|
-
const activePointerRef = useRef(null);
|
|
38
|
-
// The LIVE controlled selection, refreshed on every committed render. The
|
|
39
|
-
// window handlers below are created at pointer-down and would otherwise
|
|
40
|
-
// close over the selection as of that render; the host may update it
|
|
41
|
-
// mid-gesture, and the empty-skip/clear decisions on release must use the
|
|
42
|
-
// current value.
|
|
43
|
-
const selectionRef = useRef(selection);
|
|
44
|
-
useEffect(() => {
|
|
45
|
-
selectionRef.current = selection;
|
|
46
|
-
}, [selection]);
|
|
47
|
-
useEffect(() => () => {
|
|
48
|
-
teardownRef.current?.();
|
|
49
|
-
teardownRef.current = null;
|
|
50
|
-
activePointerRef.current = null;
|
|
51
|
-
}, []);
|
|
52
|
-
const onCanvasPointerDown = (e) => {
|
|
53
|
-
// Only the main button arms a marquee: a secondary/middle-button press on
|
|
54
|
-
// blank canvas must neither rubber-band nor clear the selection.
|
|
55
|
-
if (e.button !== 0)
|
|
56
|
-
return;
|
|
57
|
-
// A marquee only exists as a selection action; with no selection channel
|
|
58
|
-
// there is nothing to publish, so don't arm at all.
|
|
59
|
-
if (!onSelectionChange)
|
|
60
|
-
return;
|
|
61
|
-
if (activePointerRef.current !== null)
|
|
62
|
-
return;
|
|
63
|
-
// The marquee rectangle is in canvas units; without a mounted overlay the
|
|
64
|
-
// pointer position cannot be mapped to canvas coordinates, so don't arm.
|
|
65
|
-
const overlay = overlayRef.current;
|
|
66
|
-
if (!overlay)
|
|
67
|
-
return;
|
|
68
|
-
activePointerRef.current = e.pointerId;
|
|
69
|
-
const startClientX = e.clientX;
|
|
70
|
-
const startClientY = e.clientY;
|
|
71
|
-
const effectiveScale = scale || 1;
|
|
72
|
-
// Canvas origin in client coordinates, captured once per gesture (the
|
|
73
|
-
// canvas doesn't scroll/zoom mid-gesture; app parity).
|
|
74
|
-
const overlayRectBox = overlay.getBoundingClientRect();
|
|
75
|
-
const originX = overlayRectBox.left + viewportStyles.left;
|
|
76
|
-
const originY = overlayRectBox.top + viewportStyles.top;
|
|
77
|
-
const toCanvas = (clientX, clientY) => ({
|
|
78
|
-
x: (clientX - originX) / effectiveScale,
|
|
79
|
-
y: (clientY - originY) / effectiveScale,
|
|
80
|
-
});
|
|
81
|
-
const start = toCanvas(startClientX, startClientY);
|
|
82
|
-
try {
|
|
83
|
-
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
84
|
-
}
|
|
85
|
-
catch {
|
|
86
|
-
// jsdom / unsupported: pointer capture is a best-effort nicety.
|
|
87
|
-
}
|
|
88
|
-
// Whether the marquee has passed the both-axes threshold this gesture. Only
|
|
89
|
-
// a past-threshold box is drawn and only a past-threshold release replaces
|
|
90
|
-
// the selection; a sub-threshold release is a blank click (clear).
|
|
91
|
-
const rectPastThreshold = (rect) => rect.maxX - rect.minX >= MARQUEE_THRESHOLD_CANVAS &&
|
|
92
|
-
rect.maxY - rect.minY >= MARQUEE_THRESHOLD_CANVAS;
|
|
93
|
-
const handleMove = (ev) => {
|
|
94
|
-
if (ev.pointerId !== activePointerRef.current)
|
|
95
|
-
return;
|
|
96
|
-
const rect = marqueeRect(start, toCanvas(ev.clientX, ev.clientY));
|
|
97
|
-
// Only show the box once it is a real drag-select on both axes; below the
|
|
98
|
-
// threshold keep it hidden (matches the app, which never paints a tiny box).
|
|
99
|
-
setLiveRect(rectPastThreshold(rect) ? rect : null);
|
|
100
|
-
};
|
|
101
|
-
const removeListeners = () => {
|
|
102
|
-
window.removeEventListener('pointermove', handleMove);
|
|
103
|
-
window.removeEventListener('pointerup', handleUp);
|
|
104
|
-
window.removeEventListener('pointercancel', handleCancel);
|
|
105
|
-
};
|
|
106
|
-
const handleUp = (ev) => {
|
|
107
|
-
if (ev.pointerId !== activePointerRef.current)
|
|
108
|
-
return;
|
|
109
|
-
removeListeners();
|
|
110
|
-
teardownRef.current = null;
|
|
111
|
-
activePointerRef.current = null;
|
|
112
|
-
setLiveRect(null);
|
|
113
|
-
const rect = marqueeRect(start, toCanvas(ev.clientX, ev.clientY));
|
|
114
|
-
// Sub-threshold: a blank click. Clear the selection, but skip the emit
|
|
115
|
-
// when it is already empty (idempotent — no redundant selection change).
|
|
116
|
-
// Read through `selectionRef` (not the pointer-down closure) so a
|
|
117
|
-
// selection the host updated mid-gesture is judged by its live value.
|
|
118
|
-
if (!rectPastThreshold(rect)) {
|
|
119
|
-
if (selectionRef.current.elementIds.length > 0)
|
|
120
|
-
onSelectionChange(EMPTY_SELECTION);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
// Modifier at release picks the containment rule: plain = full containment,
|
|
124
|
-
// Ctrl/Shift/Meta = intersection. Meta is included for mac parity with the
|
|
125
|
-
// resize aspect modifier — a deliberate superset of the app's Ctrl/Shift.
|
|
126
|
-
const mode = ev.ctrlKey || ev.shiftKey || ev.metaKey ? 'intersect' : 'contain';
|
|
127
|
-
const ids = computeMarqueeSelection(rect, slide.elements, { mode });
|
|
128
|
-
// The marquee REPLACES the selection. An empty result clears it (skip the
|
|
129
|
-
// emit only when it was already empty). The top-most matched element (last
|
|
130
|
-
// in z-order) becomes the primary/handle for a subsequent multi-drag.
|
|
131
|
-
if (ids.length === 0) {
|
|
132
|
-
if (selectionRef.current.elementIds.length > 0)
|
|
133
|
-
onSelectionChange(EMPTY_SELECTION);
|
|
134
|
-
return;
|
|
135
|
-
}
|
|
136
|
-
onSelectionChange({ elementIds: ids, primaryId: ids[ids.length - 1] });
|
|
137
|
-
};
|
|
138
|
-
// Browser-initiated cancel: tear down and drop the live box WITHOUT emitting
|
|
139
|
-
// any selection change, then leave the hook ready for a fresh gesture.
|
|
140
|
-
const handleCancel = (ev) => {
|
|
141
|
-
if (ev.pointerId !== activePointerRef.current)
|
|
142
|
-
return;
|
|
143
|
-
removeListeners();
|
|
144
|
-
teardownRef.current = null;
|
|
145
|
-
activePointerRef.current = null;
|
|
146
|
-
setLiveRect(null);
|
|
147
|
-
};
|
|
148
|
-
window.addEventListener('pointermove', handleMove);
|
|
149
|
-
window.addEventListener('pointerup', handleUp);
|
|
150
|
-
window.addEventListener('pointercancel', handleCancel);
|
|
151
|
-
teardownRef.current = removeListeners;
|
|
152
|
-
};
|
|
153
|
-
return { marqueeRect: liveRect, onCanvasPointerDown };
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
export { useMarqueeGesture };
|
|
157
|
-
//# sourceMappingURL=useMarqueeGesture.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useMarqueeGesture.js","sources":["../../src/editing/useMarqueeGesture.ts"],"sourcesContent":["'use client';\n\nimport {\n useState,\n useEffect,\n useRef,\n type PointerEvent as ReactPointerEvent,\n type RefObject,\n} from 'react';\nimport type { Slide } from '@openmaic/dsl';\n\nimport { computeMarqueeSelection, marqueeRect, type MarqueeRect } from './core/marquee';\nimport type { ViewportStyles } from '../hooks/useViewportSize';\nimport { EMPTY_SELECTION, type Selection } from './types';\n\n/**\n * Minimum size (canvas units) the marquee must reach on BOTH axes before it\n * counts as a drag-select rather than a blank click. Below this the gesture is a\n * blank click that clears the selection; at/above it the box replaces the\n * selection with whatever it covers. App parity (`minSelectionRange = 5`).\n */\nconst MARQUEE_THRESHOLD_CANVAS = 5;\n\nexport interface UseMarqueeGestureArgs {\n slide: Slide;\n scale: number;\n /**\n * The interaction overlay element. Its bounding rect (captured once per\n * gesture, at pointer-down) plus `viewportStyles.left/top` locate the canvas\n * origin on screen, which converts absolute pointer positions to canvas\n * coordinates — the marquee rectangle lives in canvas units.\n */\n overlayRef: RefObject<HTMLElement | null>;\n /** SlideCanvas centering offset (screen px) inside the overlay. */\n viewportStyles: ViewportStyles;\n selection: Selection;\n onSelectionChange?: (next: Selection) => void;\n}\n\nexport interface UseMarqueeGestureResult {\n /** The live marquee rectangle (canvas units) while past-threshold, else `null`. */\n marqueeRect: MarqueeRect | null;\n /** Arm a marquee gesture from a pointer-down on the blank-canvas capture surface. */\n onCanvasPointerDown: (e: ReactPointerEvent) => void;\n}\n\n/**\n * Owns one blank-canvas marquee (rubber-band) gesture, mirroring the other\n * gesture hooks. Pointer-down on the capture surface records the canvas origin\n * and the start point; window pointer-move converts the pointer to canvas\n * coordinates and, once the box passes {@link MARQUEE_THRESHOLD_CANVAS} on BOTH\n * axes, republishes a normalized rectangle for a live preview; pointer-up\n * computes the selection ({@link computeMarqueeSelection}) and REPLACES the\n * controlled selection via `onSelectionChange`. A sub-threshold gesture is a\n * blank click: it clears the selection (a no-op when already empty). The mode is\n * chosen from the modifier held at release — plain drag = `contain`, Ctrl/Shift/\n * Meta = `intersect`.\n *\n * Single-pointer by design (one active pointer at a time; window move/up from\n * any other pointerId are dropped) and `pointercancel`-safe (revert, no emit).\n * Pure gesture glue: no store, no `@/` imports.\n */\nexport function useMarqueeGesture(args: UseMarqueeGestureArgs): UseMarqueeGestureResult {\n const { slide, scale, overlayRef, viewportStyles, selection, onSelectionChange } = args;\n\n const [liveRect, setLiveRect] = useState<MarqueeRect | null>(null);\n\n // Teardown for the currently-armed gesture's window listeners; the unmount\n // effect removes any listeners still live so a late move/up can't setState on\n // an unmounted host.\n const teardownRef = useRef<(() => void) | null>(null);\n // The pointerId owning the in-flight gesture (single-pointer guard).\n const activePointerRef = useRef<number | null>(null);\n\n // The LIVE controlled selection, refreshed on every committed render. The\n // window handlers below are created at pointer-down and would otherwise\n // close over the selection as of that render; the host may update it\n // mid-gesture, and the empty-skip/clear decisions on release must use the\n // current value.\n const selectionRef = useRef(selection);\n useEffect(() => {\n selectionRef.current = selection;\n }, [selection]);\n\n useEffect(\n () => () => {\n teardownRef.current?.();\n teardownRef.current = null;\n activePointerRef.current = null;\n },\n [],\n );\n\n const onCanvasPointerDown = (e: ReactPointerEvent) => {\n // Only the main button arms a marquee: a secondary/middle-button press on\n // blank canvas must neither rubber-band nor clear the selection.\n if (e.button !== 0) return;\n // A marquee only exists as a selection action; with no selection channel\n // there is nothing to publish, so don't arm at all.\n if (!onSelectionChange) return;\n if (activePointerRef.current !== null) return;\n // The marquee rectangle is in canvas units; without a mounted overlay the\n // pointer position cannot be mapped to canvas coordinates, so don't arm.\n const overlay = overlayRef.current;\n if (!overlay) return;\n activePointerRef.current = e.pointerId;\n\n const startClientX = e.clientX;\n const startClientY = e.clientY;\n const effectiveScale = scale || 1;\n\n // Canvas origin in client coordinates, captured once per gesture (the\n // canvas doesn't scroll/zoom mid-gesture; app parity).\n const overlayRectBox = overlay.getBoundingClientRect();\n const originX = overlayRectBox.left + viewportStyles.left;\n const originY = overlayRectBox.top + viewportStyles.top;\n\n const toCanvas = (clientX: number, clientY: number) => ({\n x: (clientX - originX) / effectiveScale,\n y: (clientY - originY) / effectiveScale,\n });\n const start = toCanvas(startClientX, startClientY);\n\n try {\n (e.currentTarget as Element).setPointerCapture?.(e.pointerId);\n } catch {\n // jsdom / unsupported: pointer capture is a best-effort nicety.\n }\n\n // Whether the marquee has passed the both-axes threshold this gesture. Only\n // a past-threshold box is drawn and only a past-threshold release replaces\n // the selection; a sub-threshold release is a blank click (clear).\n const rectPastThreshold = (rect: MarqueeRect): boolean =>\n rect.maxX - rect.minX >= MARQUEE_THRESHOLD_CANVAS &&\n rect.maxY - rect.minY >= MARQUEE_THRESHOLD_CANVAS;\n\n const handleMove = (ev: PointerEvent) => {\n if (ev.pointerId !== activePointerRef.current) return;\n const rect = marqueeRect(start, toCanvas(ev.clientX, ev.clientY));\n // Only show the box once it is a real drag-select on both axes; below the\n // threshold keep it hidden (matches the app, which never paints a tiny box).\n setLiveRect(rectPastThreshold(rect) ? rect : null);\n };\n\n const removeListeners = () => {\n window.removeEventListener('pointermove', handleMove);\n window.removeEventListener('pointerup', handleUp);\n window.removeEventListener('pointercancel', handleCancel);\n };\n\n const handleUp = (ev: PointerEvent) => {\n if (ev.pointerId !== activePointerRef.current) return;\n removeListeners();\n teardownRef.current = null;\n activePointerRef.current = null;\n setLiveRect(null);\n\n const rect = marqueeRect(start, toCanvas(ev.clientX, ev.clientY));\n\n // Sub-threshold: a blank click. Clear the selection, but skip the emit\n // when it is already empty (idempotent — no redundant selection change).\n // Read through `selectionRef` (not the pointer-down closure) so a\n // selection the host updated mid-gesture is judged by its live value.\n if (!rectPastThreshold(rect)) {\n if (selectionRef.current.elementIds.length > 0) onSelectionChange(EMPTY_SELECTION);\n return;\n }\n\n // Modifier at release picks the containment rule: plain = full containment,\n // Ctrl/Shift/Meta = intersection. Meta is included for mac parity with the\n // resize aspect modifier — a deliberate superset of the app's Ctrl/Shift.\n const mode = ev.ctrlKey || ev.shiftKey || ev.metaKey ? 'intersect' : 'contain';\n const ids = computeMarqueeSelection(rect, slide.elements, { mode });\n\n // The marquee REPLACES the selection. An empty result clears it (skip the\n // emit only when it was already empty). The top-most matched element (last\n // in z-order) becomes the primary/handle for a subsequent multi-drag.\n if (ids.length === 0) {\n if (selectionRef.current.elementIds.length > 0) onSelectionChange(EMPTY_SELECTION);\n return;\n }\n onSelectionChange({ elementIds: ids, primaryId: ids[ids.length - 1] });\n };\n\n // Browser-initiated cancel: tear down and drop the live box WITHOUT emitting\n // any selection change, then leave the hook ready for a fresh gesture.\n const handleCancel = (ev: PointerEvent) => {\n if (ev.pointerId !== activePointerRef.current) return;\n removeListeners();\n teardownRef.current = null;\n activePointerRef.current = null;\n setLiveRect(null);\n };\n\n window.addEventListener('pointermove', handleMove);\n window.addEventListener('pointerup', handleUp);\n window.addEventListener('pointercancel', handleCancel);\n teardownRef.current = removeListeners;\n };\n\n return { marqueeRect: liveRect, onCanvasPointerDown };\n}\n"],"names":[],"mappings":";;;;;AAeA;;;;;AAKG;AACH;AAyBA;;;;;;;;;;;;;;;AAeG;AACG;AACJ;;;;;AAOA;;AAEA;;;;;;AAOA;;AAEE;AACF;AAEA;AAEI;AACA;AACA;;AAKJ;;;AAGE;;;;AAGA;;AACA;;;;AAGA;AACA;;AACA;AAEA;AACA;AACA;;;AAIA;;;;AAKE;AACA;AACD;;AAGD;;;AAEE;;;;;;AAOF;;AAIA;AACE;;AACA;;;AAGA;AACF;;AAGE;AACA;AACA;AACF;AAEA;AACE;;AACA;AACA;AACA;;AAGA;;;;;AAMA;;;;;;;;;AASA;;;;AAKA;;;;;AAIA;AACF;;;AAIA;AACE;;AACA;AACA;AACA;;AAEF;AAEA;AACA;AACA;AACA;AACF;AAEA;AACF;;"}
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { type PointerEvent as ReactPointerEvent } from 'react';
|
|
2
|
-
import type { Slide } from '@openmaic/dsl';
|
|
3
|
-
import { type PPTBoxElement, type ResizeHandle, type ResizeResult } from './core/resize';
|
|
4
|
-
import type { Guide } from './core/snapping';
|
|
5
|
-
import type { EditIntent, SnappingOptions } from './types';
|
|
6
|
-
export interface UseResizeGestureArgs {
|
|
7
|
-
/** The controlled slide — supplies snap candidates and the canvas bounds. */
|
|
8
|
-
slide: Slide;
|
|
9
|
-
/** Canvas → screen scale; the inverse converts the pointer delta to canvas units. */
|
|
10
|
-
scale: number;
|
|
11
|
-
snapping?: boolean | SnappingOptions;
|
|
12
|
-
onElementsChange?: (intents: EditIntent[]) => void;
|
|
13
|
-
}
|
|
14
|
-
/** The in-flight resize drag: which element, the working box, and its guides. */
|
|
15
|
-
export interface ResizeDrag {
|
|
16
|
-
id: string;
|
|
17
|
-
props: ResizeResult['props'];
|
|
18
|
-
/** Alignment guides for the active resize (computed; drawn by a later PR). */
|
|
19
|
-
guides: Guide[];
|
|
20
|
-
}
|
|
21
|
-
export interface UseResizeGestureResult {
|
|
22
|
-
/** The active resize drag's working box (for live preview), else `null`. */
|
|
23
|
-
resizeDrag: ResizeDrag | null;
|
|
24
|
-
/** Arm a resize gesture from a pointer-down on `element`'s `handle`. */
|
|
25
|
-
onResizeHandlePointerDown: (element: PPTBoxElement, handle: ResizeHandle, e: ReactPointerEvent) => void;
|
|
26
|
-
}
|
|
27
|
-
/**
|
|
28
|
-
* Owns one 8-point resize gesture, mirroring {@link useEditGesture} but for a
|
|
29
|
-
* dragged resize handle. Pointer-down records the pointer start + the ORIGINAL
|
|
30
|
-
* element + the handle; window pointer-move converts the screen delta to canvas
|
|
31
|
-
* units (`/scale`), reads the aspect-lock modifier (Ctrl/Shift/Meta) off the
|
|
32
|
-
* MOVE event (so holding/releasing it mid-drag re-locks live, app parity), runs
|
|
33
|
-
* the store-free {@link computeResize} against the sibling elements, and
|
|
34
|
-
* republishes the resulting box as a working copy for 60fps preview; pointer-up
|
|
35
|
-
* commits exactly one `element.update` intent when the pointer moved past
|
|
36
|
-
* {@link DRAG_THRESHOLD_PX} and a mutation channel exists, then clears the
|
|
37
|
-
* working copy. `pointercancel` reverts without emitting.
|
|
38
|
-
*
|
|
39
|
-
* Single-pointer by design: a second pointer-down while a gesture is in flight
|
|
40
|
-
* is ignored, and window move/up from any other pointerId is dropped. Pure
|
|
41
|
-
* gesture glue: no store, no `@/` imports.
|
|
42
|
-
*/
|
|
43
|
-
export declare function useResizeGesture(args: UseResizeGestureArgs): UseResizeGestureResult;
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useState, useRef, useEffect } from 'react';
|
|
3
|
-
import { computeResize } from './core/resize.js';
|
|
4
|
-
import { resizeIntent } from './core/intent.js';
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* Distance (screen px) a handle must travel between pointer-down and pointer-up
|
|
8
|
-
* before the gesture commits a resize. Below this a handle click is inert (no
|
|
9
|
-
* `element.update`), mirroring the box drag/click threshold in useEditGesture.
|
|
10
|
-
*/
|
|
11
|
-
const DRAG_THRESHOLD_PX = 2;
|
|
12
|
-
/**
|
|
13
|
-
* Owns one 8-point resize gesture, mirroring {@link useEditGesture} but for a
|
|
14
|
-
* dragged resize handle. Pointer-down records the pointer start + the ORIGINAL
|
|
15
|
-
* element + the handle; window pointer-move converts the screen delta to canvas
|
|
16
|
-
* units (`/scale`), reads the aspect-lock modifier (Ctrl/Shift/Meta) off the
|
|
17
|
-
* MOVE event (so holding/releasing it mid-drag re-locks live, app parity), runs
|
|
18
|
-
* the store-free {@link computeResize} against the sibling elements, and
|
|
19
|
-
* republishes the resulting box as a working copy for 60fps preview; pointer-up
|
|
20
|
-
* commits exactly one `element.update` intent when the pointer moved past
|
|
21
|
-
* {@link DRAG_THRESHOLD_PX} and a mutation channel exists, then clears the
|
|
22
|
-
* working copy. `pointercancel` reverts without emitting.
|
|
23
|
-
*
|
|
24
|
-
* Single-pointer by design: a second pointer-down while a gesture is in flight
|
|
25
|
-
* is ignored, and window move/up from any other pointerId is dropped. Pure
|
|
26
|
-
* gesture glue: no store, no `@/` imports.
|
|
27
|
-
*/
|
|
28
|
-
function useResizeGesture(args) {
|
|
29
|
-
const { slide, scale, snapping, onElementsChange } = args;
|
|
30
|
-
const [resizeDrag, setResizeDrag] = useState(null);
|
|
31
|
-
// Teardown for the currently-armed gesture's window listeners; the unmount
|
|
32
|
-
// effect removes any listeners still live so a late move/up can't setState on
|
|
33
|
-
// an unmounted host.
|
|
34
|
-
const teardownRef = useRef(null);
|
|
35
|
-
// The pointerId owning the in-flight gesture (single-pointer guard).
|
|
36
|
-
const activePointerRef = useRef(null);
|
|
37
|
-
useEffect(() => () => {
|
|
38
|
-
teardownRef.current?.();
|
|
39
|
-
teardownRef.current = null;
|
|
40
|
-
activePointerRef.current = null;
|
|
41
|
-
}, []);
|
|
42
|
-
const onResizeHandlePointerDown = (element, handle, e) => {
|
|
43
|
-
// Defense-in-depth: never arm on a locked element (the primary guard skips
|
|
44
|
-
// rendering handles for a locked element in EditableSlideCanvas).
|
|
45
|
-
if (element.lock)
|
|
46
|
-
return;
|
|
47
|
-
if (activePointerRef.current !== null)
|
|
48
|
-
return;
|
|
49
|
-
activePointerRef.current = e.pointerId;
|
|
50
|
-
// A handle drag resizes the element — it must never fall through to the
|
|
51
|
-
// box hit layer beneath (which would start a move or change selection).
|
|
52
|
-
e.stopPropagation();
|
|
53
|
-
const startX = e.clientX;
|
|
54
|
-
const startY = e.clientY;
|
|
55
|
-
const effectiveScale = scale || 1;
|
|
56
|
-
const others = slide.elements.filter((o) => o.id !== element.id);
|
|
57
|
-
const viewport = {
|
|
58
|
-
width: slide.viewportSize,
|
|
59
|
-
height: slide.viewportSize * slide.viewportRatio,
|
|
60
|
-
};
|
|
61
|
-
try {
|
|
62
|
-
e.currentTarget.setPointerCapture?.(e.pointerId);
|
|
63
|
-
}
|
|
64
|
-
catch {
|
|
65
|
-
// jsdom / unsupported: pointer capture is a best-effort nicety.
|
|
66
|
-
}
|
|
67
|
-
const compute = (clientX, clientY, aspectModifier) => computeResize({
|
|
68
|
-
element,
|
|
69
|
-
handle,
|
|
70
|
-
others,
|
|
71
|
-
viewport,
|
|
72
|
-
deltaCanvas: {
|
|
73
|
-
x: (clientX - startX) / effectiveScale,
|
|
74
|
-
y: (clientY - startY) / effectiveScale,
|
|
75
|
-
},
|
|
76
|
-
aspectModifier,
|
|
77
|
-
snapping,
|
|
78
|
-
});
|
|
79
|
-
// The aspect-lock modifier is re-read from EVERY pointer event, so the
|
|
80
|
-
// lock engages/disengages live as the user presses/releases the key
|
|
81
|
-
// mid-drag (deltas always re-apply to the ORIGINAL element, so toggling
|
|
82
|
-
// never accumulates error).
|
|
83
|
-
const modifierOf = (ev) => ev.ctrlKey || ev.shiftKey || ev.metaKey;
|
|
84
|
-
const handleMove = (ev) => {
|
|
85
|
-
if (ev.pointerId !== activePointerRef.current)
|
|
86
|
-
return;
|
|
87
|
-
// No mutation channel: a live preview would just follow the pointer and
|
|
88
|
-
// snap back on pointer-up. Skip live movement when nothing can commit.
|
|
89
|
-
if (!onElementsChange)
|
|
90
|
-
return;
|
|
91
|
-
const { props, guides } = compute(ev.clientX, ev.clientY, modifierOf(ev));
|
|
92
|
-
setResizeDrag({ id: element.id, props, guides });
|
|
93
|
-
};
|
|
94
|
-
const removeListeners = () => {
|
|
95
|
-
window.removeEventListener('pointermove', handleMove);
|
|
96
|
-
window.removeEventListener('pointerup', handleUp);
|
|
97
|
-
window.removeEventListener('pointercancel', handleCancel);
|
|
98
|
-
};
|
|
99
|
-
const handleUp = (ev) => {
|
|
100
|
-
if (ev.pointerId !== activePointerRef.current)
|
|
101
|
-
return;
|
|
102
|
-
removeListeners();
|
|
103
|
-
teardownRef.current = null;
|
|
104
|
-
activePointerRef.current = null;
|
|
105
|
-
const movedPast = Math.hypot(ev.clientX - startX, ev.clientY - startY) > DRAG_THRESHOLD_PX;
|
|
106
|
-
if (movedPast && onElementsChange) {
|
|
107
|
-
const { props } = compute(ev.clientX, ev.clientY, modifierOf(ev));
|
|
108
|
-
// Snapping can pull the box back to exactly its original geometry; a
|
|
109
|
-
// no-op update would still cost the host an undo entry, so skip it
|
|
110
|
-
// (same guard as the rotate gesture's unchanged-angle case).
|
|
111
|
-
const unchanged = props.left === element.left &&
|
|
112
|
-
props.top === element.top &&
|
|
113
|
-
props.width === element.width &&
|
|
114
|
-
props.height === element.height;
|
|
115
|
-
if (!unchanged)
|
|
116
|
-
onElementsChange([resizeIntent(element.id, props)]);
|
|
117
|
-
}
|
|
118
|
-
setResizeDrag(null);
|
|
119
|
-
};
|
|
120
|
-
const handleCancel = (ev) => {
|
|
121
|
-
if (ev.pointerId !== activePointerRef.current)
|
|
122
|
-
return;
|
|
123
|
-
removeListeners();
|
|
124
|
-
teardownRef.current = null;
|
|
125
|
-
activePointerRef.current = null;
|
|
126
|
-
setResizeDrag(null);
|
|
127
|
-
};
|
|
128
|
-
window.addEventListener('pointermove', handleMove);
|
|
129
|
-
window.addEventListener('pointerup', handleUp);
|
|
130
|
-
window.addEventListener('pointercancel', handleCancel);
|
|
131
|
-
teardownRef.current = removeListeners;
|
|
132
|
-
};
|
|
133
|
-
return { resizeDrag, onResizeHandlePointerDown };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
export { useResizeGesture };
|
|
137
|
-
//# sourceMappingURL=useResizeGesture.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useResizeGesture.js","sources":["../../src/editing/useResizeGesture.ts"],"sourcesContent":["'use client';\n\nimport { useState, useEffect, useRef, type PointerEvent as ReactPointerEvent } from 'react';\nimport type { Slide } from '@openmaic/dsl';\n\nimport {\n computeResize,\n type PPTBoxElement,\n type ResizeHandle,\n type ResizeResult,\n} from './core/resize';\nimport { resizeIntent } from './core/intent';\nimport type { Guide } from './core/snapping';\nimport type { EditIntent, SnappingOptions } from './types';\n\n/**\n * Distance (screen px) a handle must travel between pointer-down and pointer-up\n * before the gesture commits a resize. Below this a handle click is inert (no\n * `element.update`), mirroring the box drag/click threshold in useEditGesture.\n */\nconst DRAG_THRESHOLD_PX = 2;\n\nexport interface UseResizeGestureArgs {\n /** The controlled slide — supplies snap candidates and the canvas bounds. */\n slide: Slide;\n /** Canvas → screen scale; the inverse converts the pointer delta to canvas units. */\n scale: number;\n snapping?: boolean | SnappingOptions;\n onElementsChange?: (intents: EditIntent[]) => void;\n}\n\n/** The in-flight resize drag: which element, the working box, and its guides. */\nexport interface ResizeDrag {\n id: string;\n props: ResizeResult['props'];\n /** Alignment guides for the active resize (computed; drawn by a later PR). */\n guides: Guide[];\n}\n\nexport interface UseResizeGestureResult {\n /** The active resize drag's working box (for live preview), else `null`. */\n resizeDrag: ResizeDrag | null;\n /** Arm a resize gesture from a pointer-down on `element`'s `handle`. */\n onResizeHandlePointerDown: (\n element: PPTBoxElement,\n handle: ResizeHandle,\n e: ReactPointerEvent,\n ) => void;\n}\n\n/**\n * Owns one 8-point resize gesture, mirroring {@link useEditGesture} but for a\n * dragged resize handle. Pointer-down records the pointer start + the ORIGINAL\n * element + the handle; window pointer-move converts the screen delta to canvas\n * units (`/scale`), reads the aspect-lock modifier (Ctrl/Shift/Meta) off the\n * MOVE event (so holding/releasing it mid-drag re-locks live, app parity), runs\n * the store-free {@link computeResize} against the sibling elements, and\n * republishes the resulting box as a working copy for 60fps preview; pointer-up\n * commits exactly one `element.update` intent when the pointer moved past\n * {@link DRAG_THRESHOLD_PX} and a mutation channel exists, then clears the\n * working copy. `pointercancel` reverts without emitting.\n *\n * Single-pointer by design: a second pointer-down while a gesture is in flight\n * is ignored, and window move/up from any other pointerId is dropped. Pure\n * gesture glue: no store, no `@/` imports.\n */\nexport function useResizeGesture(args: UseResizeGestureArgs): UseResizeGestureResult {\n const { slide, scale, snapping, onElementsChange } = args;\n\n const [resizeDrag, setResizeDrag] = useState<ResizeDrag | null>(null);\n\n // Teardown for the currently-armed gesture's window listeners; the unmount\n // effect removes any listeners still live so a late move/up can't setState on\n // an unmounted host.\n const teardownRef = useRef<(() => void) | null>(null);\n // The pointerId owning the in-flight gesture (single-pointer guard).\n const activePointerRef = useRef<number | null>(null);\n\n useEffect(\n () => () => {\n teardownRef.current?.();\n teardownRef.current = null;\n activePointerRef.current = null;\n },\n [],\n );\n\n const onResizeHandlePointerDown = (\n element: PPTBoxElement,\n handle: ResizeHandle,\n e: ReactPointerEvent,\n ) => {\n // Defense-in-depth: never arm on a locked element (the primary guard skips\n // rendering handles for a locked element in EditableSlideCanvas).\n if (element.lock) return;\n if (activePointerRef.current !== null) return;\n activePointerRef.current = e.pointerId;\n\n // A handle drag resizes the element — it must never fall through to the\n // box hit layer beneath (which would start a move or change selection).\n e.stopPropagation();\n\n const startX = e.clientX;\n const startY = e.clientY;\n const effectiveScale = scale || 1;\n\n const others = slide.elements.filter((o) => o.id !== element.id);\n const viewport = {\n width: slide.viewportSize,\n height: slide.viewportSize * slide.viewportRatio,\n };\n\n try {\n (e.currentTarget as Element).setPointerCapture?.(e.pointerId);\n } catch {\n // jsdom / unsupported: pointer capture is a best-effort nicety.\n }\n\n const compute = (clientX: number, clientY: number, aspectModifier: boolean) =>\n computeResize({\n element,\n handle,\n others,\n viewport,\n deltaCanvas: {\n x: (clientX - startX) / effectiveScale,\n y: (clientY - startY) / effectiveScale,\n },\n aspectModifier,\n snapping,\n });\n\n // The aspect-lock modifier is re-read from EVERY pointer event, so the\n // lock engages/disengages live as the user presses/releases the key\n // mid-drag (deltas always re-apply to the ORIGINAL element, so toggling\n // never accumulates error).\n const modifierOf = (ev: PointerEvent) => ev.ctrlKey || ev.shiftKey || ev.metaKey;\n\n const handleMove = (ev: PointerEvent) => {\n if (ev.pointerId !== activePointerRef.current) return;\n // No mutation channel: a live preview would just follow the pointer and\n // snap back on pointer-up. Skip live movement when nothing can commit.\n if (!onElementsChange) return;\n const { props, guides } = compute(ev.clientX, ev.clientY, modifierOf(ev));\n setResizeDrag({ id: element.id, props, guides });\n };\n\n const removeListeners = () => {\n window.removeEventListener('pointermove', handleMove);\n window.removeEventListener('pointerup', handleUp);\n window.removeEventListener('pointercancel', handleCancel);\n };\n\n const handleUp = (ev: PointerEvent) => {\n if (ev.pointerId !== activePointerRef.current) return;\n removeListeners();\n teardownRef.current = null;\n activePointerRef.current = null;\n\n const movedPast = Math.hypot(ev.clientX - startX, ev.clientY - startY) > DRAG_THRESHOLD_PX;\n if (movedPast && onElementsChange) {\n const { props } = compute(ev.clientX, ev.clientY, modifierOf(ev));\n // Snapping can pull the box back to exactly its original geometry; a\n // no-op update would still cost the host an undo entry, so skip it\n // (same guard as the rotate gesture's unchanged-angle case).\n const unchanged =\n props.left === element.left &&\n props.top === element.top &&\n props.width === element.width &&\n props.height === element.height;\n if (!unchanged) onElementsChange([resizeIntent(element.id, props)]);\n }\n\n setResizeDrag(null);\n };\n\n const handleCancel = (ev: PointerEvent) => {\n if (ev.pointerId !== activePointerRef.current) return;\n removeListeners();\n teardownRef.current = null;\n activePointerRef.current = null;\n setResizeDrag(null);\n };\n\n window.addEventListener('pointermove', handleMove);\n window.addEventListener('pointerup', handleUp);\n window.addEventListener('pointercancel', handleCancel);\n teardownRef.current = removeListeners;\n };\n\n return { resizeDrag, onResizeHandlePointerDown };\n}\n"],"names":[],"mappings":";;;;;AAeA;;;;AAIG;AACH;AA8BA;;;;;;;;;;;;;;;AAeG;AACG;;;;;;AAQJ;;AAEA;AAEA;AAEI;AACA;AACA;;;;;;;AAaF;;AACA;;;;AAMA;AACA;AACA;;AAGA;;AAEE;;AAGF;;;AAEE;;;AAIF;;;;;AAMI;AACE;AACA;AACD;;;AAGF;;;;;AAMH;AAEA;AACE;;;;AAGA;;;AAEA;AACF;;AAGE;AACA;AACA;AACF;AAEA;AACE;;AACA;AACA;AACA;;AAGA;AACE;;;;;AAME;AACA;AACA;AACF;AAAgB;;;AAIpB;AAEA;AACE;;AACA;AACA;AACA;;AAEF;AAEA;AACA;AACA;AACA;AACF;AAEA;AACF;;"}
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import { type PointerEvent as ReactPointerEvent, type RefObject } from 'react';
|
|
2
|
-
import type { PPTBoxElement } from './core/resize';
|
|
3
|
-
import type { ViewportStyles } from '../hooks/useViewportSize';
|
|
4
|
-
import type { EditIntent } from './types';
|
|
5
|
-
export interface UseRotateGestureArgs {
|
|
6
|
-
/**
|
|
7
|
-
* The interaction overlay element. Its bounding rect (captured once per
|
|
8
|
-
* gesture, at pointer-down) plus `viewportStyles.left/top` locate the canvas
|
|
9
|
-
* origin on screen, which is what converts absolute pointer positions to
|
|
10
|
-
* canvas coordinates — rotation is computed from the ABSOLUTE pointer
|
|
11
|
-
* position relative to the element center, not from a pointer delta.
|
|
12
|
-
*/
|
|
13
|
-
overlayRef: RefObject<HTMLElement | null>;
|
|
14
|
-
/** SlideCanvas centering offset (screen px) inside the overlay. */
|
|
15
|
-
viewportStyles: ViewportStyles;
|
|
16
|
-
/** Canvas → screen scale; the inverse converts pointer positions to canvas units. */
|
|
17
|
-
scale: number;
|
|
18
|
-
onElementsChange?: (intents: EditIntent[]) => void;
|
|
19
|
-
}
|
|
20
|
-
/** The in-flight rotate drag: which element and the working angle to preview. */
|
|
21
|
-
export interface RotateDrag {
|
|
22
|
-
id: string;
|
|
23
|
-
rotate: number;
|
|
24
|
-
}
|
|
25
|
-
export interface UseRotateGestureResult {
|
|
26
|
-
/** The active rotate drag's working angle (for live preview), else `null`. */
|
|
27
|
-
rotateDrag: RotateDrag | null;
|
|
28
|
-
/** Arm a rotate gesture from a pointer-down on `element`'s rotate handle. */
|
|
29
|
-
onRotateHandlePointerDown: (element: PPTBoxElement, e: ReactPointerEvent) => void;
|
|
30
|
-
}
|
|
31
|
-
/**
|
|
32
|
-
* Owns one rotate-handle gesture, mirroring {@link useEditGesture} but for the
|
|
33
|
-
* rotate handle above a box's top edge. Pointer-down records the ORIGINAL
|
|
34
|
-
* element (whose center is the rotation pivot for the whole gesture) and the
|
|
35
|
-
* canvas origin's screen position; window pointer-move converts the pointer's
|
|
36
|
-
* absolute position to canvas coordinates, runs the store-free
|
|
37
|
-
* {@link computeRotate} (signed angle, 45° snap), and republishes the angle as
|
|
38
|
-
* a working copy for 60fps preview; pointer-up commits exactly one
|
|
39
|
-
* `element.update` intent when the pointer moved past {@link DRAG_THRESHOLD_PX},
|
|
40
|
-
* a mutation channel exists, and the angle actually changed, then clears the
|
|
41
|
-
* working copy. `pointercancel` reverts without emitting.
|
|
42
|
-
*
|
|
43
|
-
* Single-pointer by design: a second pointer-down while a gesture is in flight
|
|
44
|
-
* is ignored, and window move/up from any other pointerId is dropped. Pure
|
|
45
|
-
* gesture glue: no store, no `@/` imports.
|
|
46
|
-
*/
|
|
47
|
-
export declare function useRotateGesture(args: UseRotateGestureArgs): UseRotateGestureResult;
|