@bevelo/dnd 0.0.0-stage → 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.
Files changed (65) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +48 -2
  3. package/dist/a11y-BOpF8vx8.d.cts +30 -0
  4. package/dist/a11y-Bbm_a0mw.d.ts +30 -0
  5. package/dist/index-DDuIvuzA.d.cts +258 -0
  6. package/dist/index-kH3SGg1R.d.ts +258 -0
  7. package/dist/index.cjs +413 -0
  8. package/dist/index.d.cts +139 -0
  9. package/dist/index.d.ts +139 -0
  10. package/dist/index.js +357 -0
  11. package/dist/live/animate-settle.cjs +20 -0
  12. package/dist/live/animate-settle.d.cts +14 -0
  13. package/dist/live/animate-settle.d.ts +14 -0
  14. package/dist/live/animate-settle.js +19 -0
  15. package/dist/live/flip.cjs +39 -0
  16. package/dist/live/flip.d.cts +9 -0
  17. package/dist/live/flip.d.ts +9 -0
  18. package/dist/live/flip.js +38 -0
  19. package/dist/live/index.cjs +22 -0
  20. package/dist/live/index.d.cts +4 -0
  21. package/dist/live/index.d.ts +4 -0
  22. package/dist/live/index.js +4 -0
  23. package/dist/live-BUYwbSUp.cjs +899 -0
  24. package/dist/live-Clc3aswx.js +840 -0
  25. package/dist/pure/a11y.cjs +85 -0
  26. package/dist/pure/a11y.d.cts +2 -0
  27. package/dist/pure/a11y.d.ts +2 -0
  28. package/dist/pure/a11y.js +81 -0
  29. package/dist/pure/apply.cjs +130 -0
  30. package/dist/pure/apply.d.cts +29 -0
  31. package/dist/pure/apply.d.ts +29 -0
  32. package/dist/pure/apply.js +127 -0
  33. package/dist/pure/reorder.cjs +121 -0
  34. package/dist/pure/reorder.d.cts +77 -0
  35. package/dist/pure/reorder.d.ts +77 -0
  36. package/dist/pure/reorder.js +117 -0
  37. package/dist/pure/sensors.cjs +243 -0
  38. package/dist/pure/sensors.d.cts +2 -0
  39. package/dist/pure/sensors.d.ts +2 -0
  40. package/dist/pure/sensors.js +240 -0
  41. package/dist/pure/spatial.cjs +121 -0
  42. package/dist/pure/spatial.d.cts +64 -0
  43. package/dist/pure/spatial.d.ts +64 -0
  44. package/dist/pure/spatial.js +119 -0
  45. package/dist/pure/state.cjs +15 -0
  46. package/dist/pure/state.d.cts +2 -0
  47. package/dist/pure/state.d.ts +2 -0
  48. package/dist/pure/state.js +1 -0
  49. package/dist/pure/tree.cjs +164 -0
  50. package/dist/pure/tree.d.cts +36 -0
  51. package/dist/pure/tree.d.ts +36 -0
  52. package/dist/pure/tree.js +161 -0
  53. package/dist/rows-B2C7Yf94.cjs +56 -0
  54. package/dist/rows-BIT_Rkhr.js +45 -0
  55. package/dist/sensors-C9RnMr6P.d.cts +212 -0
  56. package/dist/sensors-C9RnMr6P.d.ts +212 -0
  57. package/dist/settle-BiwZ0ZDt.js +235 -0
  58. package/dist/settle-COFsW7ul.cjs +282 -0
  59. package/dist/settle-Dl5Y9uOw.d.ts +59 -0
  60. package/dist/settle-DzdqbH4S.d.cts +59 -0
  61. package/dist/state-BEKGNZY1.d.cts +323 -0
  62. package/dist/state-BEKGNZY1.d.ts +323 -0
  63. package/dist/state-BK9W_RZn.cjs +672 -0
  64. package/dist/state-CYXS6GIb.js +595 -0
  65. package/package.json +89 -3
@@ -0,0 +1,240 @@
1
+ // src/pure/sensors.ts
2
+ const IDLE_POINTER = { phase: "idle" };
3
+ /**
4
+ * A drag that starts after N pixels of travel or a long press.
5
+ *
6
+ * The threshold is deliberately checked on pointermove rather than on
7
+ * pointerdown: starting on the press would swallow every click, and the whole
8
+ * point of the pending state is that a click and a drag can share a press.
9
+ *
10
+ * Events from a pointer other than the one that pressed are ignored, because a
11
+ * second finger must not steer the drag the first one started.
12
+ */
13
+ function createPointerSensor(options) {
14
+ const { activationDistance = 4, delayMs = 0, delayTolerance, clock } = options;
15
+ return (state, event) => {
16
+ const current = state ?? IDLE_POINTER;
17
+ if (current.phase === "idle") {
18
+ if (event.type !== "pointerdown") return { state: current };
19
+ const now = clock.now();
20
+ return {
21
+ state: {
22
+ phase: "pressed",
23
+ id: event.id,
24
+ pointerId: event.pointerId,
25
+ origin: event.at,
26
+ pressedAt: now
27
+ },
28
+ emit: [{
29
+ type: "start",
30
+ id: event.id,
31
+ atTime: now
32
+ }]
33
+ };
34
+ }
35
+ if (current.phase === "pressed") {
36
+ if (event.type === "pointerdown") return { state: current };
37
+ if (event.pointerId !== current.pointerId) return { state: current };
38
+ if (event.type === "pointerup") return {
39
+ state: IDLE_POINTER,
40
+ emit: [{
41
+ type: "cancel",
42
+ reason: "released"
43
+ }]
44
+ };
45
+ if (event.type === "pointercancel") return {
46
+ state: IDLE_POINTER,
47
+ emit: [{
48
+ type: "cancel",
49
+ reason: "pointercancel"
50
+ }]
51
+ };
52
+ const now = clock.now();
53
+ const elapsed = now - current.pressedAt;
54
+ const distance = Math.hypot(event.at.x - current.origin.x, event.at.y - current.origin.y);
55
+ const held = delayMs > 0 && elapsed >= delayMs;
56
+ if (delayMs > 0 && delayTolerance !== void 0 && !held && distance > delayTolerance) return {
57
+ state: IDLE_POINTER,
58
+ emit: [{
59
+ type: "cancel",
60
+ reason: "tolerance"
61
+ }]
62
+ };
63
+ if (held || distance >= activationDistance) return {
64
+ state: {
65
+ phase: "active",
66
+ id: current.id,
67
+ pointerId: current.pointerId,
68
+ last: current.origin,
69
+ startedAt: now
70
+ },
71
+ emit: [{ type: "activate" }]
72
+ };
73
+ return { state: current };
74
+ }
75
+ if (event.type === "pointerdown") return { state: current };
76
+ if (event.pointerId !== current.pointerId) return { state: current };
77
+ if (event.type === "pointerup") return {
78
+ state: IDLE_POINTER,
79
+ emit: [{ type: "drop" }]
80
+ };
81
+ if (event.type === "pointercancel") return {
82
+ state: IDLE_POINTER,
83
+ emit: [{
84
+ type: "cancel",
85
+ reason: "pointercancel"
86
+ }]
87
+ };
88
+ const by = {
89
+ x: event.at.x - current.last.x,
90
+ y: event.at.y - current.last.y
91
+ };
92
+ return {
93
+ state: {
94
+ ...current,
95
+ last: event.at
96
+ },
97
+ emit: [{
98
+ type: "move",
99
+ by,
100
+ pointer: event.at
101
+ }]
102
+ };
103
+ };
104
+ }
105
+ const IDLE_KEYBOARD = { phase: "idle" };
106
+ /** Space arrives as " " in modern browsers and as "Spacebar" or "Space" in older ones. */
107
+ const ACTIVATE_KEYS = /* @__PURE__ */ new Set([
108
+ " ",
109
+ "Spacebar",
110
+ "Space",
111
+ "Enter"
112
+ ]);
113
+ const CANCEL_KEYS = /* @__PURE__ */ new Set(["Escape", "Esc"]);
114
+ /**
115
+ * A keyboard drag: Space or Enter lifts the focused item, arrows move it, Space
116
+ * or Enter drops it and Escape cancels.
117
+ *
118
+ * The sensor emits exactly the events the pointer sensor emits, so the machine
119
+ * (and its sorting) runs unchanged for both input methods. Shift picks the large
120
+ * step, because the key set is arrows/space/enter/escape and there is
121
+ * no other way to ask for a bigger jump.
122
+ */
123
+ function createKeyboardSensor(options) {
124
+ const { step = 20, stepLarge = 40, clock } = options;
125
+ const read = (value) => typeof value === "function" ? value() : value;
126
+ return (state, event) => {
127
+ const current = state ?? IDLE_KEYBOARD;
128
+ if (current.phase === "idle") {
129
+ if (!ACTIVATE_KEYS.has(event.key) || event.id === void 0) return { state: current };
130
+ const now = clock.now();
131
+ return {
132
+ state: {
133
+ phase: "active",
134
+ id: event.id,
135
+ startedAt: now
136
+ },
137
+ emit: [{
138
+ type: "start",
139
+ id: event.id,
140
+ atTime: now
141
+ }, { type: "activate" }]
142
+ };
143
+ }
144
+ if (CANCEL_KEYS.has(event.key)) return {
145
+ state: IDLE_KEYBOARD,
146
+ emit: [{
147
+ type: "cancel",
148
+ reason: "escape"
149
+ }]
150
+ };
151
+ if (ACTIVATE_KEYS.has(event.key)) return {
152
+ state: IDLE_KEYBOARD,
153
+ emit: [{ type: "drop" }]
154
+ };
155
+ const size = event.shiftKey === true ? read(stepLarge) : read(step);
156
+ const by = keyDelta(event.key, size);
157
+ if (by === void 0) return { state: current };
158
+ return {
159
+ state: current,
160
+ emit: [{
161
+ type: "move",
162
+ by
163
+ }]
164
+ };
165
+ };
166
+ }
167
+ /** The movement one arrow key asks for, or undefined for a key we do not handle. */
168
+ function keyDelta(key, step) {
169
+ switch (key) {
170
+ case "ArrowUp": return {
171
+ x: 0,
172
+ y: -step
173
+ };
174
+ case "ArrowDown": return {
175
+ x: 0,
176
+ y: step
177
+ };
178
+ case "ArrowLeft": return {
179
+ x: -step,
180
+ y: 0
181
+ };
182
+ case "ArrowRight": return {
183
+ x: step,
184
+ y: 0
185
+ };
186
+ default: return;
187
+ }
188
+ }
189
+ /**
190
+ * Composes the pointer and keyboard sensors behind one reducer.
191
+ *
192
+ * The composition is small, but it is the part every strict drag primitive would
193
+ * otherwise copy: sensor state is channel-independent, device time is an input
194
+ * rather than a platform read, and activation options are re-read without
195
+ * resetting sensor state in the middle of a gesture.
196
+ */
197
+ function createDragInputSensor(options) {
198
+ let atTime = 0;
199
+ const clock = { now: () => atTime };
200
+ const pointer = (state, event) => {
201
+ const activation = options.activation();
202
+ return createPointerSensor({
203
+ activationDistance: activation.distance,
204
+ delayMs: activation.delayMs,
205
+ ...activation.delayTolerance === void 0 ? {} : { delayTolerance: activation.delayTolerance },
206
+ clock
207
+ })(state, event);
208
+ };
209
+ const keyboard = createKeyboardSensor({
210
+ step: options.keyboardStep,
211
+ stepLarge: options.keyboardStepLarge ?? (() => options.keyboardStep() * 2),
212
+ clock
213
+ });
214
+ return {
215
+ setTime(next) {
216
+ atTime = next;
217
+ },
218
+ reducer(state, input) {
219
+ if (input.channel === "pointer") {
220
+ const result = pointer(state?.pointer, input.event);
221
+ return {
222
+ state: {
223
+ ...state ?? {},
224
+ pointer: result.state
225
+ },
226
+ ...result.emit === void 0 ? {} : { emit: result.emit }
227
+ };
228
+ }
229
+ const result = keyboard(state?.keyboard, input.event);
230
+ return {
231
+ state: {
232
+ ...state ?? {},
233
+ keyboard: result.state
234
+ },
235
+ ...result.emit === void 0 ? {} : { emit: result.emit }
236
+ };
237
+ }
238
+ };
239
+ }
240
+ export { createDragInputSensor, createKeyboardSensor, createPointerSensor };
@@ -0,0 +1,121 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ let _bevelo_runtime_geometry = require("@bevelo/runtime/geometry");
3
+ // src/pure/spatial.ts
4
+ /** Unit vector per direction; the dot product against the offset is the projection. */
5
+ const AXES = {
6
+ up: {
7
+ x: 0,
8
+ y: -1
9
+ },
10
+ down: {
11
+ x: 0,
12
+ y: 1
13
+ },
14
+ left: {
15
+ x: -1,
16
+ y: 0
17
+ },
18
+ right: {
19
+ x: 1,
20
+ y: 0
21
+ }
22
+ };
23
+ /**
24
+ * The candidate nearest `from` in `direction`, or undefined when nothing lies
25
+ * that way.
26
+ *
27
+ * A candidate must project positively onto the direction axis to qualify at all.
28
+ * Among those, one inside the 45 degree cone ahead wins over one outside it, and
29
+ * within the same cone class the lowest weighted distance
30
+ * (`forward + 2 * sideways`) wins. Ties keep the earlier candidate in the input,
31
+ * so the answer never depends on object identity or number noise. A candidate
32
+ * whose rect measures NaN never wins: its projection fails the positive test.
33
+ */
34
+ function nearestInDirection(candidates, from, direction, options = {}) {
35
+ const axis = AXES[direction];
36
+ const predicate = options.predicate;
37
+ let best;
38
+ let bestScore = 0;
39
+ let bestInCone = false;
40
+ for (let index = 0; index < candidates.length; index += 1) {
41
+ const candidate = candidates[index];
42
+ if (candidate === void 0) continue;
43
+ if (predicate !== void 0 && !predicate(candidate, index)) continue;
44
+ const point = (0, _bevelo_runtime_geometry.center)(candidate.rect);
45
+ const dx = point.x - from.x;
46
+ const dy = point.y - from.y;
47
+ const forward = dx * axis.x + dy * axis.y;
48
+ if (!(forward > 0)) continue;
49
+ const sideways = axis.x === 0 ? Math.abs(dx) : Math.abs(dy);
50
+ const inCone = sideways <= forward;
51
+ const score = forward + sideways * 2;
52
+ if (best === void 0 || inCone && !bestInCone || inCone === bestInCone && score < bestScore) {
53
+ best = candidate;
54
+ bestScore = score;
55
+ bestInCone = inCone;
56
+ }
57
+ }
58
+ return best;
59
+ }
60
+ /** Arrow keys this module understands; Home and End are handled by row. */
61
+ const KEY_DIRECTIONS = {
62
+ ArrowUp: "up",
63
+ ArrowDown: "down",
64
+ ArrowLeft: "left",
65
+ ArrowRight: "right"
66
+ };
67
+ /**
68
+ * Maps a key to the id it moves to, or undefined when there is nothing there —
69
+ * the end of a row, a gap in the grid, an unknown key, an id that is not in the
70
+ * list.
71
+ *
72
+ * Arrow keys go to the nearest candidate in that direction (see
73
+ * {@link nearestInDirection}). Home and End go to the first and last candidate in
74
+ * the current row: leftmost and rightmost when `rtl` is false, swapped when it
75
+ * is true. Nothing wraps: ArrowRight at the end of a row returns undefined rather
76
+ * than jumping to the next row, because a wrap that the user did not ask for is
77
+ * how a keyboard user loses their place.
78
+ *
79
+ * Modifier handling stays out: the caller knows whether Alt or Meta was held, and
80
+ * this function only receives a key.
81
+ */
82
+ function spatialMove(candidates, currentId, key, options = {}) {
83
+ const current = candidates.find((candidate) => candidate.id === currentId);
84
+ if (current === void 0) return;
85
+ const direction = keyDirection(key, options.rtl ?? false);
86
+ if (direction !== void 0) return nearestInDirection(candidates, (0, _bevelo_runtime_geometry.center)(current.rect), direction, options)?.id;
87
+ if (key !== "Home" && key !== "End") return;
88
+ return rowEdge(candidates, current, key, options)?.id;
89
+ }
90
+ /** The direction a key names, after the reading-direction swap. */
91
+ function keyDirection(key, rtl) {
92
+ const direction = KEY_DIRECTIONS[key];
93
+ if (direction === void 0 || !rtl) return direction;
94
+ if (direction === "left") return "right";
95
+ if (direction === "right") return "left";
96
+ return direction;
97
+ }
98
+ /** The first or last candidate on the current candidate's row. */
99
+ function rowEdge(candidates, current, key, options) {
100
+ const origin = (0, _bevelo_runtime_geometry.center)(current.rect);
101
+ const row = options.row ?? 0;
102
+ const wantsSmallestX = key === "Home" !== (options.rtl ?? false);
103
+ const predicate = options.predicate;
104
+ let best;
105
+ let bestX = 0;
106
+ for (let index = 0; index < candidates.length; index += 1) {
107
+ const candidate = candidates[index];
108
+ if (candidate === void 0 || candidate.id === current.id) continue;
109
+ if (predicate !== void 0 && !predicate(candidate, index)) continue;
110
+ const point = (0, _bevelo_runtime_geometry.center)(candidate.rect);
111
+ if (!(Math.abs(point.y - origin.y) <= row)) continue;
112
+ if (!(wantsSmallestX ? point.x < origin.x : point.x > origin.x)) continue;
113
+ if (best === void 0 || (wantsSmallestX ? point.x < bestX : point.x > bestX)) {
114
+ best = candidate;
115
+ bestX = point.x;
116
+ }
117
+ }
118
+ return best;
119
+ }
120
+ exports.nearestInDirection = nearestInDirection;
121
+ exports.spatialMove = spatialMove;
@@ -0,0 +1,64 @@
1
+ import { Point, Rect } from "@bevelo/runtime/geometry";
2
+ /** A compass direction, as an arrow key names it. */
3
+ export type Direction = "up" | "down" | "left" | "right";
4
+ /** A box that can receive focus, identified by the id the caller already holds. */
5
+ export interface SpatialCandidate {
6
+ readonly id: string;
7
+ readonly rect: Rect;
8
+ }
9
+ /** Options for {@link nearestInDirection}. */
10
+ export interface NearestInDirectionOptions<C extends SpatialCandidate = SpatialCandidate> {
11
+ /**
12
+ * Skips a candidate that cannot take focus, normally a disabled one. The
13
+ * predicate receives the candidate and its index in the input, so no caller
14
+ * has to rebuild the array to filter it.
15
+ */
16
+ readonly predicate?: (candidate: C, index: number) => boolean;
17
+ }
18
+ /**
19
+ * The candidate nearest `from` in `direction`, or undefined when nothing lies
20
+ * that way.
21
+ *
22
+ * A candidate must project positively onto the direction axis to qualify at all.
23
+ * Among those, one inside the 45 degree cone ahead wins over one outside it, and
24
+ * within the same cone class the lowest weighted distance
25
+ * (`forward + 2 * sideways`) wins. Ties keep the earlier candidate in the input,
26
+ * so the answer never depends on object identity or number noise. A candidate
27
+ * whose rect measures NaN never wins: its projection fails the positive test.
28
+ */
29
+ export declare function nearestInDirection<C extends SpatialCandidate>(candidates: readonly C[], from: Point, direction: Direction, options?: NearestInDirectionOptions<C>): C | undefined;
30
+ /** Options for {@link spatialMove}. */
31
+ export interface SpatialMoveOptions<C extends SpatialCandidate = SpatialCandidate> extends NearestInDirectionOptions<C> {
32
+ /**
33
+ * Swaps Left and Right, and the ends of a row, so navigation follows reading
34
+ * order instead of the x axis. Defaults to false: physical arrows.
35
+ *
36
+ * This is the whole RTL policy: a caller that lays items out right-to-left
37
+ * passes `rtl: true`, and a caller that navigates a picture (a map) leaves it
38
+ * off so direction keeps meaning compass direction.
39
+ */
40
+ readonly rtl?: boolean;
41
+ /**
42
+ * How far from the current candidate's centre, in pixels, a candidate may sit
43
+ * vertically and still count as the same row for Home and End. The default 0
44
+ * requires exactly aligned centres, which is what a wrapped grid produces;
45
+ * pass a tolerance for items of different heights.
46
+ */
47
+ readonly row?: number;
48
+ }
49
+ /**
50
+ * Maps a key to the id it moves to, or undefined when there is nothing there —
51
+ * the end of a row, a gap in the grid, an unknown key, an id that is not in the
52
+ * list.
53
+ *
54
+ * Arrow keys go to the nearest candidate in that direction (see
55
+ * {@link nearestInDirection}). Home and End go to the first and last candidate in
56
+ * the current row: leftmost and rightmost when `rtl` is false, swapped when it
57
+ * is true. Nothing wraps: ArrowRight at the end of a row returns undefined rather
58
+ * than jumping to the next row, because a wrap that the user did not ask for is
59
+ * how a keyboard user loses their place.
60
+ *
61
+ * Modifier handling stays out: the caller knows whether Alt or Meta was held, and
62
+ * this function only receives a key.
63
+ */
64
+ export declare function spatialMove<C extends SpatialCandidate>(candidates: readonly C[], currentId: string, key: string, options?: SpatialMoveOptions<C>): string | undefined;
@@ -0,0 +1,64 @@
1
+ import { Point, Rect } from "@bevelo/runtime/geometry";
2
+ /** A compass direction, as an arrow key names it. */
3
+ export type Direction = "up" | "down" | "left" | "right";
4
+ /** A box that can receive focus, identified by the id the caller already holds. */
5
+ export interface SpatialCandidate {
6
+ readonly id: string;
7
+ readonly rect: Rect;
8
+ }
9
+ /** Options for {@link nearestInDirection}. */
10
+ export interface NearestInDirectionOptions<C extends SpatialCandidate = SpatialCandidate> {
11
+ /**
12
+ * Skips a candidate that cannot take focus, normally a disabled one. The
13
+ * predicate receives the candidate and its index in the input, so no caller
14
+ * has to rebuild the array to filter it.
15
+ */
16
+ readonly predicate?: (candidate: C, index: number) => boolean;
17
+ }
18
+ /**
19
+ * The candidate nearest `from` in `direction`, or undefined when nothing lies
20
+ * that way.
21
+ *
22
+ * A candidate must project positively onto the direction axis to qualify at all.
23
+ * Among those, one inside the 45 degree cone ahead wins over one outside it, and
24
+ * within the same cone class the lowest weighted distance
25
+ * (`forward + 2 * sideways`) wins. Ties keep the earlier candidate in the input,
26
+ * so the answer never depends on object identity or number noise. A candidate
27
+ * whose rect measures NaN never wins: its projection fails the positive test.
28
+ */
29
+ export declare function nearestInDirection<C extends SpatialCandidate>(candidates: readonly C[], from: Point, direction: Direction, options?: NearestInDirectionOptions<C>): C | undefined;
30
+ /** Options for {@link spatialMove}. */
31
+ export interface SpatialMoveOptions<C extends SpatialCandidate = SpatialCandidate> extends NearestInDirectionOptions<C> {
32
+ /**
33
+ * Swaps Left and Right, and the ends of a row, so navigation follows reading
34
+ * order instead of the x axis. Defaults to false: physical arrows.
35
+ *
36
+ * This is the whole RTL policy: a caller that lays items out right-to-left
37
+ * passes `rtl: true`, and a caller that navigates a picture (a map) leaves it
38
+ * off so direction keeps meaning compass direction.
39
+ */
40
+ readonly rtl?: boolean;
41
+ /**
42
+ * How far from the current candidate's centre, in pixels, a candidate may sit
43
+ * vertically and still count as the same row for Home and End. The default 0
44
+ * requires exactly aligned centres, which is what a wrapped grid produces;
45
+ * pass a tolerance for items of different heights.
46
+ */
47
+ readonly row?: number;
48
+ }
49
+ /**
50
+ * Maps a key to the id it moves to, or undefined when there is nothing there —
51
+ * the end of a row, a gap in the grid, an unknown key, an id that is not in the
52
+ * list.
53
+ *
54
+ * Arrow keys go to the nearest candidate in that direction (see
55
+ * {@link nearestInDirection}). Home and End go to the first and last candidate in
56
+ * the current row: leftmost and rightmost when `rtl` is false, swapped when it
57
+ * is true. Nothing wraps: ArrowRight at the end of a row returns undefined rather
58
+ * than jumping to the next row, because a wrap that the user did not ask for is
59
+ * how a keyboard user loses their place.
60
+ *
61
+ * Modifier handling stays out: the caller knows whether Alt or Meta was held, and
62
+ * this function only receives a key.
63
+ */
64
+ export declare function spatialMove<C extends SpatialCandidate>(candidates: readonly C[], currentId: string, key: string, options?: SpatialMoveOptions<C>): string | undefined;
@@ -0,0 +1,119 @@
1
+ import { center } from "@bevelo/runtime/geometry";
2
+ // src/pure/spatial.ts
3
+ /** Unit vector per direction; the dot product against the offset is the projection. */
4
+ const AXES = {
5
+ up: {
6
+ x: 0,
7
+ y: -1
8
+ },
9
+ down: {
10
+ x: 0,
11
+ y: 1
12
+ },
13
+ left: {
14
+ x: -1,
15
+ y: 0
16
+ },
17
+ right: {
18
+ x: 1,
19
+ y: 0
20
+ }
21
+ };
22
+ /**
23
+ * The candidate nearest `from` in `direction`, or undefined when nothing lies
24
+ * that way.
25
+ *
26
+ * A candidate must project positively onto the direction axis to qualify at all.
27
+ * Among those, one inside the 45 degree cone ahead wins over one outside it, and
28
+ * within the same cone class the lowest weighted distance
29
+ * (`forward + 2 * sideways`) wins. Ties keep the earlier candidate in the input,
30
+ * so the answer never depends on object identity or number noise. A candidate
31
+ * whose rect measures NaN never wins: its projection fails the positive test.
32
+ */
33
+ function nearestInDirection(candidates, from, direction, options = {}) {
34
+ const axis = AXES[direction];
35
+ const predicate = options.predicate;
36
+ let best;
37
+ let bestScore = 0;
38
+ let bestInCone = false;
39
+ for (let index = 0; index < candidates.length; index += 1) {
40
+ const candidate = candidates[index];
41
+ if (candidate === void 0) continue;
42
+ if (predicate !== void 0 && !predicate(candidate, index)) continue;
43
+ const point = center(candidate.rect);
44
+ const dx = point.x - from.x;
45
+ const dy = point.y - from.y;
46
+ const forward = dx * axis.x + dy * axis.y;
47
+ if (!(forward > 0)) continue;
48
+ const sideways = axis.x === 0 ? Math.abs(dx) : Math.abs(dy);
49
+ const inCone = sideways <= forward;
50
+ const score = forward + sideways * 2;
51
+ if (best === void 0 || inCone && !bestInCone || inCone === bestInCone && score < bestScore) {
52
+ best = candidate;
53
+ bestScore = score;
54
+ bestInCone = inCone;
55
+ }
56
+ }
57
+ return best;
58
+ }
59
+ /** Arrow keys this module understands; Home and End are handled by row. */
60
+ const KEY_DIRECTIONS = {
61
+ ArrowUp: "up",
62
+ ArrowDown: "down",
63
+ ArrowLeft: "left",
64
+ ArrowRight: "right"
65
+ };
66
+ /**
67
+ * Maps a key to the id it moves to, or undefined when there is nothing there —
68
+ * the end of a row, a gap in the grid, an unknown key, an id that is not in the
69
+ * list.
70
+ *
71
+ * Arrow keys go to the nearest candidate in that direction (see
72
+ * {@link nearestInDirection}). Home and End go to the first and last candidate in
73
+ * the current row: leftmost and rightmost when `rtl` is false, swapped when it
74
+ * is true. Nothing wraps: ArrowRight at the end of a row returns undefined rather
75
+ * than jumping to the next row, because a wrap that the user did not ask for is
76
+ * how a keyboard user loses their place.
77
+ *
78
+ * Modifier handling stays out: the caller knows whether Alt or Meta was held, and
79
+ * this function only receives a key.
80
+ */
81
+ function spatialMove(candidates, currentId, key, options = {}) {
82
+ const current = candidates.find((candidate) => candidate.id === currentId);
83
+ if (current === void 0) return;
84
+ const direction = keyDirection(key, options.rtl ?? false);
85
+ if (direction !== void 0) return nearestInDirection(candidates, center(current.rect), direction, options)?.id;
86
+ if (key !== "Home" && key !== "End") return;
87
+ return rowEdge(candidates, current, key, options)?.id;
88
+ }
89
+ /** The direction a key names, after the reading-direction swap. */
90
+ function keyDirection(key, rtl) {
91
+ const direction = KEY_DIRECTIONS[key];
92
+ if (direction === void 0 || !rtl) return direction;
93
+ if (direction === "left") return "right";
94
+ if (direction === "right") return "left";
95
+ return direction;
96
+ }
97
+ /** The first or last candidate on the current candidate's row. */
98
+ function rowEdge(candidates, current, key, options) {
99
+ const origin = center(current.rect);
100
+ const row = options.row ?? 0;
101
+ const wantsSmallestX = key === "Home" !== (options.rtl ?? false);
102
+ const predicate = options.predicate;
103
+ let best;
104
+ let bestX = 0;
105
+ for (let index = 0; index < candidates.length; index += 1) {
106
+ const candidate = candidates[index];
107
+ if (candidate === void 0 || candidate.id === current.id) continue;
108
+ if (predicate !== void 0 && !predicate(candidate, index)) continue;
109
+ const point = center(candidate.rect);
110
+ if (!(Math.abs(point.y - origin.y) <= row)) continue;
111
+ if (!(wantsSmallestX ? point.x < origin.x : point.x > origin.x)) continue;
112
+ if (best === void 0 || (wantsSmallestX ? point.x < bestX : point.x > bestX)) {
113
+ best = candidate;
114
+ bestX = point.x;
115
+ }
116
+ }
117
+ return best;
118
+ }
119
+ export { nearestInDirection, spatialMove };
@@ -0,0 +1,15 @@
1
+ Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
2
+ const require_state = require("../state-BK9W_RZn.cjs");
3
+ exports.boardZones = require_state.boardZones;
4
+ exports.carrySelection = require_state.carrySelection;
5
+ exports.dragReducer = require_state.dragReducer;
6
+ exports.hitTest = require_state.hitTest;
7
+ exports.idle = require_state.idle;
8
+ exports.isDragged = require_state.isDragged;
9
+ exports.noModifiers = require_state.noModifiers;
10
+ exports.operationFor = require_state.operationFor;
11
+ exports.operations = require_state.operations;
12
+ exports.placeholderOf = require_state.placeholderOf;
13
+ exports.resultOf = require_state.resultOf;
14
+ exports.shiftOf = require_state.shiftOf;
15
+ exports.zoneFlags = require_state.zoneFlags;
@@ -0,0 +1,2 @@
1
+ import { A as ZoneBase, B as boardZones, C as Placement, D as TreeRules, E as StepDirection, F as noModifiers, H as operationFor, I as placeholderOf, L as resultOf, M as hitTest, N as idle, O as TreeZone, P as isDragged, R as shiftOf, S as Placeholder, T as Root, U as operations, V as carrySelection, _ as ItemTarget, a as DragLayout, b as Operation, c as DragState, d as DropResult, f as DropTarget, g as ItemHit, h as InputKind, i as DragInput, j as dragReducer, k as Zone, l as Dragging, m as Hit, n as Carry, o as DragPlugin, p as GapTarget, r as DragConfig, s as DragSource, t as AnyPlugin, u as DropPolicy, v as ListZone, w as Proposal, x as Outline, y as Modifiers, z as zoneFlags } from "../state-BEKGNZY1.cjs";
2
+ export { AnyPlugin, Carry, DragConfig, DragInput, DragLayout, DragPlugin, DragSource, DragState, Dragging, DropPolicy, DropResult, DropTarget, GapTarget, Hit, InputKind, ItemHit, ItemTarget, ListZone, Modifiers, Operation, Outline, Placeholder, Placement, Proposal, Root, StepDirection, TreeRules, TreeZone, Zone, ZoneBase, boardZones, carrySelection, dragReducer, hitTest, idle, isDragged, noModifiers, operationFor, operations, placeholderOf, resultOf, shiftOf, zoneFlags };
@@ -0,0 +1,2 @@
1
+ import { A as ZoneBase, B as boardZones, C as Placement, D as TreeRules, E as StepDirection, F as noModifiers, H as operationFor, I as placeholderOf, L as resultOf, M as hitTest, N as idle, O as TreeZone, P as isDragged, R as shiftOf, S as Placeholder, T as Root, U as operations, V as carrySelection, _ as ItemTarget, a as DragLayout, b as Operation, c as DragState, d as DropResult, f as DropTarget, g as ItemHit, h as InputKind, i as DragInput, j as dragReducer, k as Zone, l as Dragging, m as Hit, n as Carry, o as DragPlugin, p as GapTarget, r as DragConfig, s as DragSource, t as AnyPlugin, u as DropPolicy, v as ListZone, w as Proposal, x as Outline, y as Modifiers, z as zoneFlags } from "../state-BEKGNZY1.js";
2
+ export { AnyPlugin, Carry, DragConfig, DragInput, DragLayout, DragPlugin, DragSource, DragState, Dragging, DropPolicy, DropResult, DropTarget, GapTarget, Hit, InputKind, ItemHit, ItemTarget, ListZone, Modifiers, Operation, Outline, Placeholder, Placement, Proposal, Root, StepDirection, TreeRules, TreeZone, Zone, ZoneBase, boardZones, carrySelection, dragReducer, hitTest, idle, isDragged, noModifiers, operationFor, operations, placeholderOf, resultOf, shiftOf, zoneFlags };
@@ -0,0 +1 @@
1
+ export { u as boardZones, d as carrySelection, t as dragReducer, n as hitTest, r as idle, i as isDragged, a as noModifiers, f as operationFor, p as operations, o as placeholderOf, s as resultOf, c as shiftOf, l as zoneFlags } from "../state-CYXS6GIb.js";