@vitavision/stage2d 0.15.1 → 0.16.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/dist/index.d.ts +70 -1
- package/dist/index.js +158 -4
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/components/PointMarquee.tsx +229 -0
- package/src/index.ts +7 -0
package/package.json
CHANGED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A rectangular marquee over a point set: hold a modifier, drag, and the ids of the points
|
|
3
|
+
* inside the rectangle are reported.
|
|
4
|
+
*
|
|
5
|
+
* Opt-in and inert without its modifier: a press without it is left to the stage (which pans),
|
|
6
|
+
* the wheel still zooms, and the layers below keep their own targets. The rectangle is drawn
|
|
7
|
+
* with `MarqueeRect` while the pointer moves.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
|
|
11
|
+
import type { PointerEvent as ReactPointerEvent, Ref } from "react";
|
|
12
|
+
|
|
13
|
+
import type { Point } from "./measureGeometry";
|
|
14
|
+
import { MarqueeRect } from "./DraftShape";
|
|
15
|
+
import { buildPointIndexFrom, pointsInRect, type PointId, type PointItem } from "./pointIndex";
|
|
16
|
+
import { useStage } from "./stage/ImageStage";
|
|
17
|
+
import type { StageDrag, StagePress } from "./stage/StageSurface";
|
|
18
|
+
import { useStageDrag } from "./stage/StageSurface";
|
|
19
|
+
import { imageViewBox, type Rect } from "./stage/view";
|
|
20
|
+
|
|
21
|
+
/** The key that arms a `PointMarquee`. `"meta"` is ⌘ on macOS and Ctrl elsewhere. */
|
|
22
|
+
export type PointMarqueeModifier = "shift" | "alt" | "meta";
|
|
23
|
+
|
|
24
|
+
/** How a marquee combines with the current selection: replaces it, or adds to it. */
|
|
25
|
+
export type PointMarqueeMode = "replace" | "add";
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* What `PointMarquee`'s `ref` exposes, for an app that already has a `StageSurface` (a stage
|
|
29
|
+
* has one full-frame press target) and starts the marquee from its `onPress`.
|
|
30
|
+
*/
|
|
31
|
+
export interface PointMarqueeHandle {
|
|
32
|
+
/**
|
|
33
|
+
* The marquee as a `StageDrag`, to return from `StageSurface`'s `onPress`.
|
|
34
|
+
*
|
|
35
|
+
* @param press - The press the surface was given.
|
|
36
|
+
* @returns The drag when the press holds the marquee's modifier, `undefined` otherwise: a
|
|
37
|
+
* declined press is for the surface's other tools or the stage's pan. A touch has no
|
|
38
|
+
* modifier, so it is never started here.
|
|
39
|
+
*/
|
|
40
|
+
sweepDrag(press: StagePress): StageDrag | undefined;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Props of `PointMarquee`. */
|
|
44
|
+
export interface PointMarqueeProps {
|
|
45
|
+
/** The points the marquee selects among, in image coordinates. Pass a stable array: a new one rebuilds the index. */
|
|
46
|
+
items: readonly PointItem[];
|
|
47
|
+
/**
|
|
48
|
+
* Called on release with the ids of the points inside the rectangle (edges included), in
|
|
49
|
+
* item order; an empty list for a rectangle that holds none. Not called for a modifier-press
|
|
50
|
+
* that did not move (a click is not a rectangle), nor after `Escape`.
|
|
51
|
+
*
|
|
52
|
+
* `mode` is `"add"` when ⌘/Ctrl is held as well as the modifier, `"replace"` otherwise. When
|
|
53
|
+
* the modifier is `"meta"` itself, the mode is always `"replace"`.
|
|
54
|
+
*/
|
|
55
|
+
onSelect: (ids: PointId[], mode: PointMarqueeMode) => void;
|
|
56
|
+
/** Called with the rectangle (image coordinates) while it is dragged, and with `null` when it ends. */
|
|
57
|
+
onBandChange?: ((band: Rect | null) => void) | undefined;
|
|
58
|
+
/** Called when a marquee is abandoned: `Escape`, or the pointer was cancelled. `onSelect` is not called. */
|
|
59
|
+
onCancel?: (() => void) | undefined;
|
|
60
|
+
/** The key that arms the marquee. Defaults to `"shift"`. */
|
|
61
|
+
modifier?: PointMarqueeModifier | undefined;
|
|
62
|
+
/**
|
|
63
|
+
* Whether the layer has a full-frame press target of its own. Defaults to `true`. The target
|
|
64
|
+
* exists only while the modifier is held, so it never covers the layers below otherwise. Turn
|
|
65
|
+
* it off to start the marquee from your own `StageSurface` through `ref`.
|
|
66
|
+
*/
|
|
67
|
+
surface?: boolean | undefined;
|
|
68
|
+
/** Turn the marquee off without unmounting it. Defaults to `true`. */
|
|
69
|
+
enabled?: boolean | undefined;
|
|
70
|
+
/** CSS colour of the rectangle. Defaults to the overlay `selection` role. */
|
|
71
|
+
stroke?: string | undefined;
|
|
72
|
+
/** Start a marquee from a press elsewhere; see `PointMarqueeHandle`. */
|
|
73
|
+
ref?: Ref<PointMarqueeHandle> | undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** Whether a pointer or key event holds the modifier. */
|
|
77
|
+
function holds(modifier: PointMarqueeModifier, event: { shiftKey: boolean; altKey: boolean; metaKey: boolean; ctrlKey: boolean }): boolean {
|
|
78
|
+
if (modifier === "shift") return event.shiftKey;
|
|
79
|
+
if (modifier === "alt") return event.altKey;
|
|
80
|
+
return event.metaKey || event.ctrlKey;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function boxBetween(a: Point, b: Point): Rect {
|
|
84
|
+
return { x: Math.min(a.x, b.x), y: Math.min(a.y, b.y), width: Math.abs(b.x - a.x), height: Math.abs(b.y - a.y) };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* A modifier-drag rectangle that selects points, inside an `ImageStage`.
|
|
89
|
+
*
|
|
90
|
+
* - **Opt-in**: a press without the modifier is left alone, so pan, wheel zoom and the layers
|
|
91
|
+
* below behave as without it. While the modifier is held the layer takes presses over the
|
|
92
|
+
* whole image and the cursor is a crosshair.
|
|
93
|
+
* - **Reports ids**, by the points' centres, through a spatial index: thousands of points cost
|
|
94
|
+
* microseconds.
|
|
95
|
+
* - **Escape** abandons a marquee in flight; releasing the pointer without moving does nothing.
|
|
96
|
+
* - **Touch** has no modifier: a finger pans as usual.
|
|
97
|
+
*
|
|
98
|
+
* It draws only the rectangle (`data-marquee`); selection feedback is the point layer's, fed by
|
|
99
|
+
* the ids the app keeps. The layer carries `data-armed` while the modifier is held and
|
|
100
|
+
* `data-sweeping` while a rectangle is being dragged.
|
|
101
|
+
*/
|
|
102
|
+
export function PointMarquee({
|
|
103
|
+
items,
|
|
104
|
+
onSelect,
|
|
105
|
+
onBandChange,
|
|
106
|
+
onCancel,
|
|
107
|
+
modifier = "shift",
|
|
108
|
+
surface = true,
|
|
109
|
+
enabled = true,
|
|
110
|
+
stroke,
|
|
111
|
+
ref,
|
|
112
|
+
}: PointMarqueeProps) {
|
|
113
|
+
const stage = useStage();
|
|
114
|
+
const startDrag = useStageDrag();
|
|
115
|
+
const index = useMemo(() => buildPointIndexFrom(items), [items]);
|
|
116
|
+
const [band, setBand] = useState<Rect | null>(null);
|
|
117
|
+
const [held, setHeld] = useState(false);
|
|
118
|
+
const armed = enabled && surface && held;
|
|
119
|
+
// An Escape ends the marquee at once; the pointer is still down until it is released.
|
|
120
|
+
const abandoned = useRef(false);
|
|
121
|
+
const live = useRef({ onSelect, onBandChange, onCancel });
|
|
122
|
+
useEffect(() => {
|
|
123
|
+
live.current = { onSelect, onBandChange, onCancel };
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const publish = (next: Rect | null) => {
|
|
127
|
+
setBand(next);
|
|
128
|
+
live.current.onBandChange?.(next);
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
// The modifier is read from key and pointer events on the window, so the target appears when
|
|
132
|
+
// the key goes down and goes when it is released or the window loses focus.
|
|
133
|
+
useEffect(() => {
|
|
134
|
+
if (!enabled || !surface) return;
|
|
135
|
+
const sync = (event: { shiftKey: boolean; altKey: boolean; metaKey: boolean; ctrlKey: boolean }) => setHeld(holds(modifier, event));
|
|
136
|
+
const off = () => setHeld(false);
|
|
137
|
+
window.addEventListener("keydown", sync);
|
|
138
|
+
window.addEventListener("keyup", sync);
|
|
139
|
+
window.addEventListener("pointermove", sync);
|
|
140
|
+
window.addEventListener("blur", off);
|
|
141
|
+
return () => {
|
|
142
|
+
window.removeEventListener("keydown", sync);
|
|
143
|
+
window.removeEventListener("keyup", sync);
|
|
144
|
+
window.removeEventListener("pointermove", sync);
|
|
145
|
+
window.removeEventListener("blur", off);
|
|
146
|
+
};
|
|
147
|
+
}, [enabled, surface, modifier]);
|
|
148
|
+
|
|
149
|
+
// Escape abandons the marquee in flight.
|
|
150
|
+
const sweeping = band !== null;
|
|
151
|
+
useEffect(() => {
|
|
152
|
+
if (!sweeping) return;
|
|
153
|
+
const onKey = (event: KeyboardEvent) => {
|
|
154
|
+
if (event.key !== "Escape" || abandoned.current) return;
|
|
155
|
+
abandoned.current = true;
|
|
156
|
+
publish(null);
|
|
157
|
+
live.current.onCancel?.();
|
|
158
|
+
};
|
|
159
|
+
window.addEventListener("keydown", onKey);
|
|
160
|
+
return () => window.removeEventListener("keydown", onKey);
|
|
161
|
+
}, [sweeping]);
|
|
162
|
+
|
|
163
|
+
const dragFrom = (from: Point, additive: boolean, claimsTouch: boolean): StageDrag => {
|
|
164
|
+
abandoned.current = false;
|
|
165
|
+
publish({ x: from.x, y: from.y, width: 0, height: 0 });
|
|
166
|
+
return {
|
|
167
|
+
claimsTouch,
|
|
168
|
+
onMove: (p) => {
|
|
169
|
+
if (!abandoned.current) publish(boxBetween(from, p));
|
|
170
|
+
},
|
|
171
|
+
onEnd: (p, _event, moved) => {
|
|
172
|
+
if (abandoned.current) return;
|
|
173
|
+
publish(null);
|
|
174
|
+
if (moved)
|
|
175
|
+
live.current.onSelect(
|
|
176
|
+
pointsInRect(index, boxBetween(from, p)).map((i) => idAt(i)),
|
|
177
|
+
additive ? "add" : "replace",
|
|
178
|
+
);
|
|
179
|
+
},
|
|
180
|
+
onCancel: () => {
|
|
181
|
+
if (abandoned.current) return;
|
|
182
|
+
abandoned.current = true;
|
|
183
|
+
publish(null);
|
|
184
|
+
live.current.onCancel?.();
|
|
185
|
+
},
|
|
186
|
+
};
|
|
187
|
+
};
|
|
188
|
+
const idAt = (i: number): PointId => (index.ids ? index.ids[i]! : i);
|
|
189
|
+
const additiveOf = (event: { metaKey: boolean; ctrlKey: boolean }) => modifier !== "meta" && (event.metaKey || event.ctrlKey);
|
|
190
|
+
|
|
191
|
+
useImperativeHandle(ref, () => ({
|
|
192
|
+
sweepDrag: (press) => {
|
|
193
|
+
if (!enabled || press.touch) return undefined;
|
|
194
|
+
const held = modifier === "shift" ? press.shiftKey : modifier === "alt" ? press.altKey : press.metaKey;
|
|
195
|
+
return held ? dragFrom(press.point, modifier !== "meta" && press.metaKey, false) : undefined;
|
|
196
|
+
},
|
|
197
|
+
}));
|
|
198
|
+
|
|
199
|
+
const onPointerDown = (event: ReactPointerEvent<SVGRectElement>) => {
|
|
200
|
+
if (!enabled || stage.panMode || event.button !== 0 || event.pointerType === "touch" || !holds(modifier, event)) return;
|
|
201
|
+
startDrag(event, dragFrom(stage.toImage({ x: event.clientX, y: event.clientY }), additiveOf(event), false));
|
|
202
|
+
};
|
|
203
|
+
|
|
204
|
+
return (
|
|
205
|
+
<>
|
|
206
|
+
{armed && (
|
|
207
|
+
<svg
|
|
208
|
+
viewBox={imageViewBox(stage.image)}
|
|
209
|
+
className="pointer-events-none absolute inset-0 h-full w-full overflow-visible"
|
|
210
|
+
aria-hidden
|
|
211
|
+
data-armed=""
|
|
212
|
+
data-sweeping={sweeping ? "" : undefined}
|
|
213
|
+
>
|
|
214
|
+
<rect
|
|
215
|
+
data-marquee-surface=""
|
|
216
|
+
x={-0.5}
|
|
217
|
+
y={-0.5}
|
|
218
|
+
width={stage.image.width}
|
|
219
|
+
height={stage.image.height}
|
|
220
|
+
fill="transparent"
|
|
221
|
+
className="pointer-events-auto cursor-crosshair"
|
|
222
|
+
onPointerDown={onPointerDown}
|
|
223
|
+
/>
|
|
224
|
+
</svg>
|
|
225
|
+
)}
|
|
226
|
+
<MarqueeRect rect={band} stroke={stroke} />
|
|
227
|
+
</>
|
|
228
|
+
);
|
|
229
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -130,6 +130,13 @@ export {
|
|
|
130
130
|
type PolylineIndex,
|
|
131
131
|
} from "./components/polylineIndex";
|
|
132
132
|
export { PointSet, type PointSetItem, type PointSetProps } from "./components/PointSet";
|
|
133
|
+
export {
|
|
134
|
+
PointMarquee,
|
|
135
|
+
type PointMarqueeHandle,
|
|
136
|
+
type PointMarqueeMode,
|
|
137
|
+
type PointMarqueeModifier,
|
|
138
|
+
type PointMarqueeProps,
|
|
139
|
+
} from "./components/PointMarquee";
|
|
133
140
|
export {
|
|
134
141
|
buildPointIndex,
|
|
135
142
|
buildPointIndexFrom,
|