@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.
- package/LICENSE +21 -0
- package/README.md +48 -2
- package/dist/a11y-BOpF8vx8.d.cts +30 -0
- package/dist/a11y-Bbm_a0mw.d.ts +30 -0
- package/dist/index-DDuIvuzA.d.cts +258 -0
- package/dist/index-kH3SGg1R.d.ts +258 -0
- package/dist/index.cjs +413 -0
- package/dist/index.d.cts +139 -0
- package/dist/index.d.ts +139 -0
- package/dist/index.js +357 -0
- package/dist/live/animate-settle.cjs +20 -0
- package/dist/live/animate-settle.d.cts +14 -0
- package/dist/live/animate-settle.d.ts +14 -0
- package/dist/live/animate-settle.js +19 -0
- package/dist/live/flip.cjs +39 -0
- package/dist/live/flip.d.cts +9 -0
- package/dist/live/flip.d.ts +9 -0
- package/dist/live/flip.js +38 -0
- package/dist/live/index.cjs +22 -0
- package/dist/live/index.d.cts +4 -0
- package/dist/live/index.d.ts +4 -0
- package/dist/live/index.js +4 -0
- package/dist/live-BUYwbSUp.cjs +899 -0
- package/dist/live-Clc3aswx.js +840 -0
- package/dist/pure/a11y.cjs +85 -0
- package/dist/pure/a11y.d.cts +2 -0
- package/dist/pure/a11y.d.ts +2 -0
- package/dist/pure/a11y.js +81 -0
- package/dist/pure/apply.cjs +130 -0
- package/dist/pure/apply.d.cts +29 -0
- package/dist/pure/apply.d.ts +29 -0
- package/dist/pure/apply.js +127 -0
- package/dist/pure/reorder.cjs +121 -0
- package/dist/pure/reorder.d.cts +77 -0
- package/dist/pure/reorder.d.ts +77 -0
- package/dist/pure/reorder.js +117 -0
- package/dist/pure/sensors.cjs +243 -0
- package/dist/pure/sensors.d.cts +2 -0
- package/dist/pure/sensors.d.ts +2 -0
- package/dist/pure/sensors.js +240 -0
- package/dist/pure/spatial.cjs +121 -0
- package/dist/pure/spatial.d.cts +64 -0
- package/dist/pure/spatial.d.ts +64 -0
- package/dist/pure/spatial.js +119 -0
- package/dist/pure/state.cjs +15 -0
- package/dist/pure/state.d.cts +2 -0
- package/dist/pure/state.d.ts +2 -0
- package/dist/pure/state.js +1 -0
- package/dist/pure/tree.cjs +164 -0
- package/dist/pure/tree.d.cts +36 -0
- package/dist/pure/tree.d.ts +36 -0
- package/dist/pure/tree.js +161 -0
- package/dist/rows-B2C7Yf94.cjs +56 -0
- package/dist/rows-BIT_Rkhr.js +45 -0
- package/dist/sensors-C9RnMr6P.d.cts +212 -0
- package/dist/sensors-C9RnMr6P.d.ts +212 -0
- package/dist/settle-BiwZ0ZDt.js +235 -0
- package/dist/settle-COFsW7ul.cjs +282 -0
- package/dist/settle-Dl5Y9uOw.d.ts +59 -0
- package/dist/settle-DzdqbH4S.d.cts +59 -0
- package/dist/state-BEKGNZY1.d.cts +323 -0
- package/dist/state-BEKGNZY1.d.ts +323 -0
- package/dist/state-BK9W_RZn.cjs +672 -0
- package/dist/state-CYXS6GIb.js +595 -0
- package/package.json +89 -3
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { Point } from "@bevelo/runtime/geometry";
|
|
2
|
+
/** The only way this package reads the time. Injected, never imported as a global. */
|
|
3
|
+
interface Clock {
|
|
4
|
+
/** Milliseconds on an arbitrary origin; only differences are meaningful. */
|
|
5
|
+
now(): number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The real clock, for the DOM binding and for applications.
|
|
9
|
+
*
|
|
10
|
+
* performance.now() is preferred because it does not jump when the system clock
|
|
11
|
+
* is corrected mid-drag; Date.now() is the fallback for hosts (older SSR
|
|
12
|
+
* runtimes) that define neither. Nothing above the binding layer calls this.
|
|
13
|
+
*/
|
|
14
|
+
declare const systemClock: Clock;
|
|
15
|
+
/** Why a drag was cancelled; the binding forwards this to its announcements. */
|
|
16
|
+
type CancelReason = "escape" | "pointercancel" | "released" | "external" | "tolerance";
|
|
17
|
+
/**
|
|
18
|
+
* What a sensor produces. The session consumes it, so the pointer sensor and the
|
|
19
|
+
* keyboard sensor feed one machine: "a keyboard drag is the same machine run".
|
|
20
|
+
*
|
|
21
|
+
* A press emits start before the movement threshold is crossed, which is what
|
|
22
|
+
* gives the machine its pending state; activate is the moment the item is really
|
|
23
|
+
* being dragged, and a cancel before it is indistinguishable from a click.
|
|
24
|
+
*/
|
|
25
|
+
type DragEvent = {
|
|
26
|
+
readonly type: "start";
|
|
27
|
+
readonly id: string;
|
|
28
|
+
readonly atTime: number;
|
|
29
|
+
} | {
|
|
30
|
+
readonly type: "activate";
|
|
31
|
+
} | {
|
|
32
|
+
readonly type: "move";
|
|
33
|
+
readonly by: Point;
|
|
34
|
+
readonly pointer?: Point;
|
|
35
|
+
} | {
|
|
36
|
+
readonly type: "drop";
|
|
37
|
+
} | {
|
|
38
|
+
readonly type: "cancel";
|
|
39
|
+
readonly reason: CancelReason;
|
|
40
|
+
} | {
|
|
41
|
+
readonly type: "settle";
|
|
42
|
+
};
|
|
43
|
+
/** A reducer step: the next state, and the drag events this event produced. */
|
|
44
|
+
interface SensorResult<S> {
|
|
45
|
+
readonly state: S;
|
|
46
|
+
readonly emit?: readonly DragEvent[];
|
|
47
|
+
}
|
|
48
|
+
/** A pure device-to-drag transducer, injected into the session. */
|
|
49
|
+
type SensorReducer<S, Event> = (state: S | undefined, event: Event) => SensorResult<S>;
|
|
50
|
+
/** Options for the pointer sensor; the defaults live here once. */
|
|
51
|
+
interface PointerSensorOptions {
|
|
52
|
+
/** How far the pointer must travel before a press becomes a drag. */
|
|
53
|
+
readonly activationDistance?: number;
|
|
54
|
+
/** How long a press must be held before it activates regardless of distance. */
|
|
55
|
+
readonly delayMs?: number;
|
|
56
|
+
/**
|
|
57
|
+
* How far a press waiting on `delayMs` may drift before the wait is abandoned.
|
|
58
|
+
*
|
|
59
|
+
* This is what keeps a touch long-press from stealing a scroll: the finger
|
|
60
|
+
* travels past the tolerance while the delay is still pending, the press is
|
|
61
|
+
* cancelled, and the browser's own scroll wins. Without it a held-then-scrolled
|
|
62
|
+
* touch activates mid-gesture, which is the bug the delay exists to prevent.
|
|
63
|
+
*/
|
|
64
|
+
readonly delayTolerance?: number;
|
|
65
|
+
/** Injected so a test can decide what "held long enough" means. */
|
|
66
|
+
readonly clock: Clock;
|
|
67
|
+
}
|
|
68
|
+
/** The pointer sensor has not seen a press. */
|
|
69
|
+
interface IdlePointerState {
|
|
70
|
+
readonly phase: "idle";
|
|
71
|
+
}
|
|
72
|
+
/** A press is being watched; the drag has not started yet. */
|
|
73
|
+
interface PressedPointerState {
|
|
74
|
+
readonly phase: "pressed";
|
|
75
|
+
readonly id: string;
|
|
76
|
+
readonly pointerId: number;
|
|
77
|
+
readonly origin: Point;
|
|
78
|
+
readonly pressedAt: number;
|
|
79
|
+
}
|
|
80
|
+
/** The pointer owns an active drag; every move is measured from the last one. */
|
|
81
|
+
interface ActivePointerState {
|
|
82
|
+
readonly phase: "active";
|
|
83
|
+
readonly id: string;
|
|
84
|
+
readonly pointerId: number;
|
|
85
|
+
readonly last: Point;
|
|
86
|
+
readonly startedAt: number;
|
|
87
|
+
}
|
|
88
|
+
/** Everything the pointer sensor remembers between events. */
|
|
89
|
+
type PointerSensorState = IdlePointerState | PressedPointerState | ActivePointerState;
|
|
90
|
+
/**
|
|
91
|
+
* Device events the pointer sensor accepts. Times are not carried: the sensor
|
|
92
|
+
* reads its injected clock at the moment of the event, which is the only place
|
|
93
|
+
* that knows what "now" means.
|
|
94
|
+
*/
|
|
95
|
+
type PointerSensorEvent = {
|
|
96
|
+
readonly type: "pointerdown";
|
|
97
|
+
readonly id: string;
|
|
98
|
+
readonly pointerId: number;
|
|
99
|
+
readonly at: Point;
|
|
100
|
+
} | {
|
|
101
|
+
readonly type: "pointermove";
|
|
102
|
+
readonly pointerId: number;
|
|
103
|
+
readonly at: Point;
|
|
104
|
+
} | {
|
|
105
|
+
readonly type: "pointerup";
|
|
106
|
+
readonly pointerId: number;
|
|
107
|
+
readonly at: Point;
|
|
108
|
+
} | {
|
|
109
|
+
readonly type: "pointercancel";
|
|
110
|
+
readonly pointerId: number;
|
|
111
|
+
readonly at: Point;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* A drag that starts after N pixels of travel or a long press.
|
|
115
|
+
*
|
|
116
|
+
* The threshold is deliberately checked on pointermove rather than on
|
|
117
|
+
* pointerdown: starting on the press would swallow every click, and the whole
|
|
118
|
+
* point of the pending state is that a click and a drag can share a press.
|
|
119
|
+
*
|
|
120
|
+
* Events from a pointer other than the one that pressed are ignored, because a
|
|
121
|
+
* second finger must not steer the drag the first one started.
|
|
122
|
+
*/
|
|
123
|
+
declare function createPointerSensor(options: PointerSensorOptions): SensorReducer<PointerSensorState, PointerSensorEvent>;
|
|
124
|
+
/** Options for the keyboard sensor; the defaults live here once. */
|
|
125
|
+
interface KeyboardSensorOptions {
|
|
126
|
+
/** Distance of one arrow press, or a pure measurement of the current layout. */
|
|
127
|
+
readonly step?: number | (() => number);
|
|
128
|
+
/** Distance of an arrow press with Shift held, or the same kind of measurement. */
|
|
129
|
+
readonly stepLarge?: number | (() => number);
|
|
130
|
+
/** Injected for the same reason as the pointer sensor's. */
|
|
131
|
+
readonly clock: Clock;
|
|
132
|
+
}
|
|
133
|
+
/** No key-driven drag is running. */
|
|
134
|
+
interface IdleKeyboardState {
|
|
135
|
+
readonly phase: "idle";
|
|
136
|
+
}
|
|
137
|
+
/** A key-driven drag is running; the session holds the accumulated delta. */
|
|
138
|
+
interface ActiveKeyboardState {
|
|
139
|
+
readonly phase: "active";
|
|
140
|
+
readonly id: string;
|
|
141
|
+
readonly startedAt: number;
|
|
142
|
+
}
|
|
143
|
+
/** Everything the keyboard sensor remembers between events. */
|
|
144
|
+
type KeyboardSensorState = IdleKeyboardState | ActiveKeyboardState;
|
|
145
|
+
/**
|
|
146
|
+
* A key event, already normalised by the binding.
|
|
147
|
+
*
|
|
148
|
+
* The id is only needed to begin a drag (it is the item that has focus); once a
|
|
149
|
+
* drag is running the session is the source of truth for what is active. key is
|
|
150
|
+
* a plain string so the binding can forward KeyboardEvent.key without this
|
|
151
|
+
* module importing the DOM.
|
|
152
|
+
*/
|
|
153
|
+
type KeyboardSensorEvent = {
|
|
154
|
+
readonly type: "keydown";
|
|
155
|
+
readonly key: string;
|
|
156
|
+
readonly id?: string;
|
|
157
|
+
readonly shiftKey?: boolean;
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* A keyboard drag: Space or Enter lifts the focused item, arrows move it, Space
|
|
161
|
+
* or Enter drops it and Escape cancels.
|
|
162
|
+
*
|
|
163
|
+
* The sensor emits exactly the events the pointer sensor emits, so the machine
|
|
164
|
+
* (and its sorting) runs unchanged for both input methods. Shift picks the large
|
|
165
|
+
* step, because the key set is arrows/space/enter/escape and there is
|
|
166
|
+
* no other way to ask for a bigger jump.
|
|
167
|
+
*/
|
|
168
|
+
declare function createKeyboardSensor(options: KeyboardSensorOptions): SensorReducer<KeyboardSensorState, KeyboardSensorEvent>;
|
|
169
|
+
/** The activation policy a composed input sensor reads for every press. */
|
|
170
|
+
interface DragInputActivation {
|
|
171
|
+
readonly distance: number;
|
|
172
|
+
readonly delayMs: number;
|
|
173
|
+
readonly delayTolerance?: number;
|
|
174
|
+
}
|
|
175
|
+
/** A device event names its channel so one reducer can hold both sensor states. */
|
|
176
|
+
type DragInputSensorEvent = {
|
|
177
|
+
readonly channel: "pointer";
|
|
178
|
+
readonly event: PointerSensorEvent;
|
|
179
|
+
} | {
|
|
180
|
+
readonly channel: "keyboard";
|
|
181
|
+
readonly event: KeyboardSensorEvent;
|
|
182
|
+
};
|
|
183
|
+
/** The independent pointer and keyboard states of a composed input sensor. */
|
|
184
|
+
interface DragInputSensorState {
|
|
185
|
+
readonly pointer?: PointerSensorState;
|
|
186
|
+
readonly keyboard?: KeyboardSensorState;
|
|
187
|
+
}
|
|
188
|
+
/** What a composed pointer/keyboard input sensor needs. */
|
|
189
|
+
interface DragInputSensorOptions {
|
|
190
|
+
/** Read synchronously for each event, so host options can change between drags. */
|
|
191
|
+
readonly activation: () => DragInputActivation;
|
|
192
|
+
/** One ordinary arrow step, usually measured from the current layout. */
|
|
193
|
+
readonly keyboardStep: () => number;
|
|
194
|
+
/** One shifted arrow step; defaults to twice the ordinary step. */
|
|
195
|
+
readonly keyboardStepLarge?: () => number;
|
|
196
|
+
}
|
|
197
|
+
/** A pure pointer/keyboard transducer plus the clock input its events carry. */
|
|
198
|
+
interface DragInputSensor {
|
|
199
|
+
readonly reducer: SensorReducer<DragInputSensorState, DragInputSensorEvent>;
|
|
200
|
+
/** Sets the time the next dispatched event happened at. */
|
|
201
|
+
readonly setTime: (atTime: number) => void;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Composes the pointer and keyboard sensors behind one reducer.
|
|
205
|
+
*
|
|
206
|
+
* The composition is small, but it is the part every strict drag primitive would
|
|
207
|
+
* otherwise copy: sensor state is channel-independent, device time is an input
|
|
208
|
+
* rather than a platform read, and activation options are re-read without
|
|
209
|
+
* resetting sensor state in the middle of a gesture.
|
|
210
|
+
*/
|
|
211
|
+
declare function createDragInputSensor(options: DragInputSensorOptions): DragInputSensor;
|
|
212
|
+
export { createPointerSensor as C, createKeyboardSensor as S, systemClock as T, PointerSensorState as _, DragInputActivation as a, SensorResult as b, DragInputSensorOptions as c, IdlePointerState as d, KeyboardSensorEvent as f, PointerSensorOptions as g, PointerSensorEvent as h, DragEvent as i, DragInputSensorState as l, KeyboardSensorState as m, ActivePointerState as n, DragInputSensor as o, KeyboardSensorOptions as p, CancelReason as r, DragInputSensorEvent as s, ActiveKeyboardState as t, IdleKeyboardState as u, PressedPointerState as v, Clock as w, createDragInputSensor as x, SensorReducer as y };
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { Point } from "@bevelo/runtime/geometry";
|
|
2
|
+
/** The only way this package reads the time. Injected, never imported as a global. */
|
|
3
|
+
interface Clock {
|
|
4
|
+
/** Milliseconds on an arbitrary origin; only differences are meaningful. */
|
|
5
|
+
now(): number;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* The real clock, for the DOM binding and for applications.
|
|
9
|
+
*
|
|
10
|
+
* performance.now() is preferred because it does not jump when the system clock
|
|
11
|
+
* is corrected mid-drag; Date.now() is the fallback for hosts (older SSR
|
|
12
|
+
* runtimes) that define neither. Nothing above the binding layer calls this.
|
|
13
|
+
*/
|
|
14
|
+
declare const systemClock: Clock;
|
|
15
|
+
/** Why a drag was cancelled; the binding forwards this to its announcements. */
|
|
16
|
+
type CancelReason = "escape" | "pointercancel" | "released" | "external" | "tolerance";
|
|
17
|
+
/**
|
|
18
|
+
* What a sensor produces. The session consumes it, so the pointer sensor and the
|
|
19
|
+
* keyboard sensor feed one machine: "a keyboard drag is the same machine run".
|
|
20
|
+
*
|
|
21
|
+
* A press emits start before the movement threshold is crossed, which is what
|
|
22
|
+
* gives the machine its pending state; activate is the moment the item is really
|
|
23
|
+
* being dragged, and a cancel before it is indistinguishable from a click.
|
|
24
|
+
*/
|
|
25
|
+
type DragEvent = {
|
|
26
|
+
readonly type: "start";
|
|
27
|
+
readonly id: string;
|
|
28
|
+
readonly atTime: number;
|
|
29
|
+
} | {
|
|
30
|
+
readonly type: "activate";
|
|
31
|
+
} | {
|
|
32
|
+
readonly type: "move";
|
|
33
|
+
readonly by: Point;
|
|
34
|
+
readonly pointer?: Point;
|
|
35
|
+
} | {
|
|
36
|
+
readonly type: "drop";
|
|
37
|
+
} | {
|
|
38
|
+
readonly type: "cancel";
|
|
39
|
+
readonly reason: CancelReason;
|
|
40
|
+
} | {
|
|
41
|
+
readonly type: "settle";
|
|
42
|
+
};
|
|
43
|
+
/** A reducer step: the next state, and the drag events this event produced. */
|
|
44
|
+
interface SensorResult<S> {
|
|
45
|
+
readonly state: S;
|
|
46
|
+
readonly emit?: readonly DragEvent[];
|
|
47
|
+
}
|
|
48
|
+
/** A pure device-to-drag transducer, injected into the session. */
|
|
49
|
+
type SensorReducer<S, Event> = (state: S | undefined, event: Event) => SensorResult<S>;
|
|
50
|
+
/** Options for the pointer sensor; the defaults live here once. */
|
|
51
|
+
interface PointerSensorOptions {
|
|
52
|
+
/** How far the pointer must travel before a press becomes a drag. */
|
|
53
|
+
readonly activationDistance?: number;
|
|
54
|
+
/** How long a press must be held before it activates regardless of distance. */
|
|
55
|
+
readonly delayMs?: number;
|
|
56
|
+
/**
|
|
57
|
+
* How far a press waiting on `delayMs` may drift before the wait is abandoned.
|
|
58
|
+
*
|
|
59
|
+
* This is what keeps a touch long-press from stealing a scroll: the finger
|
|
60
|
+
* travels past the tolerance while the delay is still pending, the press is
|
|
61
|
+
* cancelled, and the browser's own scroll wins. Without it a held-then-scrolled
|
|
62
|
+
* touch activates mid-gesture, which is the bug the delay exists to prevent.
|
|
63
|
+
*/
|
|
64
|
+
readonly delayTolerance?: number;
|
|
65
|
+
/** Injected so a test can decide what "held long enough" means. */
|
|
66
|
+
readonly clock: Clock;
|
|
67
|
+
}
|
|
68
|
+
/** The pointer sensor has not seen a press. */
|
|
69
|
+
interface IdlePointerState {
|
|
70
|
+
readonly phase: "idle";
|
|
71
|
+
}
|
|
72
|
+
/** A press is being watched; the drag has not started yet. */
|
|
73
|
+
interface PressedPointerState {
|
|
74
|
+
readonly phase: "pressed";
|
|
75
|
+
readonly id: string;
|
|
76
|
+
readonly pointerId: number;
|
|
77
|
+
readonly origin: Point;
|
|
78
|
+
readonly pressedAt: number;
|
|
79
|
+
}
|
|
80
|
+
/** The pointer owns an active drag; every move is measured from the last one. */
|
|
81
|
+
interface ActivePointerState {
|
|
82
|
+
readonly phase: "active";
|
|
83
|
+
readonly id: string;
|
|
84
|
+
readonly pointerId: number;
|
|
85
|
+
readonly last: Point;
|
|
86
|
+
readonly startedAt: number;
|
|
87
|
+
}
|
|
88
|
+
/** Everything the pointer sensor remembers between events. */
|
|
89
|
+
type PointerSensorState = IdlePointerState | PressedPointerState | ActivePointerState;
|
|
90
|
+
/**
|
|
91
|
+
* Device events the pointer sensor accepts. Times are not carried: the sensor
|
|
92
|
+
* reads its injected clock at the moment of the event, which is the only place
|
|
93
|
+
* that knows what "now" means.
|
|
94
|
+
*/
|
|
95
|
+
type PointerSensorEvent = {
|
|
96
|
+
readonly type: "pointerdown";
|
|
97
|
+
readonly id: string;
|
|
98
|
+
readonly pointerId: number;
|
|
99
|
+
readonly at: Point;
|
|
100
|
+
} | {
|
|
101
|
+
readonly type: "pointermove";
|
|
102
|
+
readonly pointerId: number;
|
|
103
|
+
readonly at: Point;
|
|
104
|
+
} | {
|
|
105
|
+
readonly type: "pointerup";
|
|
106
|
+
readonly pointerId: number;
|
|
107
|
+
readonly at: Point;
|
|
108
|
+
} | {
|
|
109
|
+
readonly type: "pointercancel";
|
|
110
|
+
readonly pointerId: number;
|
|
111
|
+
readonly at: Point;
|
|
112
|
+
};
|
|
113
|
+
/**
|
|
114
|
+
* A drag that starts after N pixels of travel or a long press.
|
|
115
|
+
*
|
|
116
|
+
* The threshold is deliberately checked on pointermove rather than on
|
|
117
|
+
* pointerdown: starting on the press would swallow every click, and the whole
|
|
118
|
+
* point of the pending state is that a click and a drag can share a press.
|
|
119
|
+
*
|
|
120
|
+
* Events from a pointer other than the one that pressed are ignored, because a
|
|
121
|
+
* second finger must not steer the drag the first one started.
|
|
122
|
+
*/
|
|
123
|
+
declare function createPointerSensor(options: PointerSensorOptions): SensorReducer<PointerSensorState, PointerSensorEvent>;
|
|
124
|
+
/** Options for the keyboard sensor; the defaults live here once. */
|
|
125
|
+
interface KeyboardSensorOptions {
|
|
126
|
+
/** Distance of one arrow press, or a pure measurement of the current layout. */
|
|
127
|
+
readonly step?: number | (() => number);
|
|
128
|
+
/** Distance of an arrow press with Shift held, or the same kind of measurement. */
|
|
129
|
+
readonly stepLarge?: number | (() => number);
|
|
130
|
+
/** Injected for the same reason as the pointer sensor's. */
|
|
131
|
+
readonly clock: Clock;
|
|
132
|
+
}
|
|
133
|
+
/** No key-driven drag is running. */
|
|
134
|
+
interface IdleKeyboardState {
|
|
135
|
+
readonly phase: "idle";
|
|
136
|
+
}
|
|
137
|
+
/** A key-driven drag is running; the session holds the accumulated delta. */
|
|
138
|
+
interface ActiveKeyboardState {
|
|
139
|
+
readonly phase: "active";
|
|
140
|
+
readonly id: string;
|
|
141
|
+
readonly startedAt: number;
|
|
142
|
+
}
|
|
143
|
+
/** Everything the keyboard sensor remembers between events. */
|
|
144
|
+
type KeyboardSensorState = IdleKeyboardState | ActiveKeyboardState;
|
|
145
|
+
/**
|
|
146
|
+
* A key event, already normalised by the binding.
|
|
147
|
+
*
|
|
148
|
+
* The id is only needed to begin a drag (it is the item that has focus); once a
|
|
149
|
+
* drag is running the session is the source of truth for what is active. key is
|
|
150
|
+
* a plain string so the binding can forward KeyboardEvent.key without this
|
|
151
|
+
* module importing the DOM.
|
|
152
|
+
*/
|
|
153
|
+
type KeyboardSensorEvent = {
|
|
154
|
+
readonly type: "keydown";
|
|
155
|
+
readonly key: string;
|
|
156
|
+
readonly id?: string;
|
|
157
|
+
readonly shiftKey?: boolean;
|
|
158
|
+
};
|
|
159
|
+
/**
|
|
160
|
+
* A keyboard drag: Space or Enter lifts the focused item, arrows move it, Space
|
|
161
|
+
* or Enter drops it and Escape cancels.
|
|
162
|
+
*
|
|
163
|
+
* The sensor emits exactly the events the pointer sensor emits, so the machine
|
|
164
|
+
* (and its sorting) runs unchanged for both input methods. Shift picks the large
|
|
165
|
+
* step, because the key set is arrows/space/enter/escape and there is
|
|
166
|
+
* no other way to ask for a bigger jump.
|
|
167
|
+
*/
|
|
168
|
+
declare function createKeyboardSensor(options: KeyboardSensorOptions): SensorReducer<KeyboardSensorState, KeyboardSensorEvent>;
|
|
169
|
+
/** The activation policy a composed input sensor reads for every press. */
|
|
170
|
+
interface DragInputActivation {
|
|
171
|
+
readonly distance: number;
|
|
172
|
+
readonly delayMs: number;
|
|
173
|
+
readonly delayTolerance?: number;
|
|
174
|
+
}
|
|
175
|
+
/** A device event names its channel so one reducer can hold both sensor states. */
|
|
176
|
+
type DragInputSensorEvent = {
|
|
177
|
+
readonly channel: "pointer";
|
|
178
|
+
readonly event: PointerSensorEvent;
|
|
179
|
+
} | {
|
|
180
|
+
readonly channel: "keyboard";
|
|
181
|
+
readonly event: KeyboardSensorEvent;
|
|
182
|
+
};
|
|
183
|
+
/** The independent pointer and keyboard states of a composed input sensor. */
|
|
184
|
+
interface DragInputSensorState {
|
|
185
|
+
readonly pointer?: PointerSensorState;
|
|
186
|
+
readonly keyboard?: KeyboardSensorState;
|
|
187
|
+
}
|
|
188
|
+
/** What a composed pointer/keyboard input sensor needs. */
|
|
189
|
+
interface DragInputSensorOptions {
|
|
190
|
+
/** Read synchronously for each event, so host options can change between drags. */
|
|
191
|
+
readonly activation: () => DragInputActivation;
|
|
192
|
+
/** One ordinary arrow step, usually measured from the current layout. */
|
|
193
|
+
readonly keyboardStep: () => number;
|
|
194
|
+
/** One shifted arrow step; defaults to twice the ordinary step. */
|
|
195
|
+
readonly keyboardStepLarge?: () => number;
|
|
196
|
+
}
|
|
197
|
+
/** A pure pointer/keyboard transducer plus the clock input its events carry. */
|
|
198
|
+
interface DragInputSensor {
|
|
199
|
+
readonly reducer: SensorReducer<DragInputSensorState, DragInputSensorEvent>;
|
|
200
|
+
/** Sets the time the next dispatched event happened at. */
|
|
201
|
+
readonly setTime: (atTime: number) => void;
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* Composes the pointer and keyboard sensors behind one reducer.
|
|
205
|
+
*
|
|
206
|
+
* The composition is small, but it is the part every strict drag primitive would
|
|
207
|
+
* otherwise copy: sensor state is channel-independent, device time is an input
|
|
208
|
+
* rather than a platform read, and activation options are re-read without
|
|
209
|
+
* resetting sensor state in the middle of a gesture.
|
|
210
|
+
*/
|
|
211
|
+
declare function createDragInputSensor(options: DragInputSensorOptions): DragInputSensor;
|
|
212
|
+
export { createPointerSensor as C, createKeyboardSensor as S, systemClock as T, PointerSensorState as _, DragInputActivation as a, SensorResult as b, DragInputSensorOptions as c, IdlePointerState as d, KeyboardSensorEvent as f, PointerSensorOptions as g, PointerSensorEvent as h, DragEvent as i, DragInputSensorState as l, KeyboardSensorState as m, ActivePointerState as n, DragInputSensor as o, KeyboardSensorOptions as p, CancelReason as r, DragInputSensorEvent as s, ActiveKeyboardState as t, IdleKeyboardState as u, PressedPointerState as v, Clock as w, createDragInputSensor as x, SensorReducer as y };
|
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
import { c as shiftOf, i as isDragged } from "./state-CYXS6GIb.js";
|
|
2
|
+
// src/live/writer.ts
|
|
3
|
+
/** The attributes the writer sets; `reset` removes them from every element it flagged. */
|
|
4
|
+
const FLAGS = [
|
|
5
|
+
"data-dragging",
|
|
6
|
+
"data-drop-target",
|
|
7
|
+
"data-drag-over",
|
|
8
|
+
"data-drag-operation"
|
|
9
|
+
];
|
|
10
|
+
/** A translate as a transform string. */
|
|
11
|
+
function translate(by) {
|
|
12
|
+
return "translate3d(" + by.x + "px, " + by.y + "px, 0)";
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Inline `transform` and `visibility`, each element's own value remembered at the first write
|
|
16
|
+
* and restored by `reset`. The default.
|
|
17
|
+
*/
|
|
18
|
+
const styleChannel = () => {
|
|
19
|
+
const originals = /* @__PURE__ */ new Map();
|
|
20
|
+
const put = (el, prop, value) => {
|
|
21
|
+
let own = originals.get(el);
|
|
22
|
+
if (own === void 0) {
|
|
23
|
+
if (value === null) return;
|
|
24
|
+
own = {
|
|
25
|
+
transform: el.style.transform,
|
|
26
|
+
transformOrigin: el.style.transformOrigin,
|
|
27
|
+
visibility: el.style.visibility
|
|
28
|
+
};
|
|
29
|
+
originals.set(el, own);
|
|
30
|
+
}
|
|
31
|
+
const next = value ?? own[prop];
|
|
32
|
+
if (el.style[prop] !== next) el.style[prop] = next;
|
|
33
|
+
};
|
|
34
|
+
return {
|
|
35
|
+
move: (el, by) => put(el, "transform", by && translate(by)),
|
|
36
|
+
hide: (el, hidden) => put(el, "visibility", hidden ? "hidden" : null),
|
|
37
|
+
inverse(el, transform) {
|
|
38
|
+
put(el, "transformOrigin", "0 0");
|
|
39
|
+
put(el, "transform", transform);
|
|
40
|
+
},
|
|
41
|
+
reset() {
|
|
42
|
+
for (const [el, own] of originals) Object.assign(el.style, own);
|
|
43
|
+
originals.clear();
|
|
44
|
+
}
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
/** The custom properties `varsChannel` writes. */
|
|
48
|
+
const DRAG_X = "--bevelo-drag-x";
|
|
49
|
+
const DRAG_Y = "--bevelo-drag-y";
|
|
50
|
+
/** The attribute `varsChannel` sets on a hidden move source. */
|
|
51
|
+
const DRAG_HIDDEN = "data-drag-hidden";
|
|
52
|
+
/**
|
|
53
|
+
* The engine's own channel: `--bevelo-drag-x` and `--bevelo-drag-y` (px) for every translate,
|
|
54
|
+
* and `data-drag-hidden` on a hidden move source. It never writes `transform` or `visibility`,
|
|
55
|
+
* so a part whose `style` a framework renders keeps it; the stylesheet consumes the channel:
|
|
56
|
+
*
|
|
57
|
+
* ```css
|
|
58
|
+
* @property --bevelo-drag-x { syntax: "<length>"; inherits: false; initial-value: 0px; }
|
|
59
|
+
* @property --bevelo-drag-y { syntax: "<length>"; inherits: false; initial-value: 0px; }
|
|
60
|
+
* [data-part] { translate: var(--bevelo-drag-x) var(--bevelo-drag-y); }
|
|
61
|
+
* [data-drag-hidden] { visibility: hidden; }
|
|
62
|
+
* ```
|
|
63
|
+
*
|
|
64
|
+
* A settle paints no inverse: `animateSettle` animates `transform` with the Web Animations
|
|
65
|
+
* API, which writes no inline style, and `springSettle` springs these same properties.
|
|
66
|
+
*/
|
|
67
|
+
const varsChannel = () => {
|
|
68
|
+
const touched = /* @__PURE__ */ new Set();
|
|
69
|
+
const clear = (el) => {
|
|
70
|
+
el.style.removeProperty(DRAG_X);
|
|
71
|
+
el.style.removeProperty(DRAG_Y);
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
move(el, by) {
|
|
75
|
+
if (by === null) return clear(el);
|
|
76
|
+
touched.add(el);
|
|
77
|
+
el.style.setProperty(DRAG_X, by.x + "px");
|
|
78
|
+
el.style.setProperty(DRAG_Y, by.y + "px");
|
|
79
|
+
},
|
|
80
|
+
hide(el, hidden) {
|
|
81
|
+
if (hidden) touched.add(el);
|
|
82
|
+
el.toggleAttribute(DRAG_HIDDEN, hidden);
|
|
83
|
+
},
|
|
84
|
+
painted(el) {
|
|
85
|
+
if (typeof el.getAnimations !== "function" || el.getAnimations().length === 0) return void 0;
|
|
86
|
+
const value = el.ownerDocument.defaultView?.getComputedStyle(el).translate ?? "";
|
|
87
|
+
if (value === "" || value === "none") return {
|
|
88
|
+
x: 0,
|
|
89
|
+
y: 0
|
|
90
|
+
};
|
|
91
|
+
const [x = "0", y = "0"] = value.split(" ");
|
|
92
|
+
return {
|
|
93
|
+
x: Number.parseFloat(x) || 0,
|
|
94
|
+
y: Number.parseFloat(y) || 0
|
|
95
|
+
};
|
|
96
|
+
},
|
|
97
|
+
reset() {
|
|
98
|
+
for (const el of touched) {
|
|
99
|
+
clear(el);
|
|
100
|
+
el.removeAttribute(DRAG_HIDDEN);
|
|
101
|
+
}
|
|
102
|
+
touched.clear();
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
};
|
|
106
|
+
function createWriter(base) {
|
|
107
|
+
const flagged = /* @__PURE__ */ new Set();
|
|
108
|
+
const offsets = /* @__PURE__ */ new Map();
|
|
109
|
+
const channel = {
|
|
110
|
+
...base,
|
|
111
|
+
move(el, by) {
|
|
112
|
+
if (by === null) offsets.delete(el);
|
|
113
|
+
else offsets.set(el, by);
|
|
114
|
+
base.move(el, by);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
function flag(el, name, value) {
|
|
118
|
+
if (value === null) el.removeAttribute(name);
|
|
119
|
+
else if (el.getAttribute(name) !== value) {
|
|
120
|
+
el.setAttribute(name, value);
|
|
121
|
+
flagged.add(el);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
return {
|
|
125
|
+
channel,
|
|
126
|
+
drag(state, items, zones, preview, full) {
|
|
127
|
+
if (state.phase !== "dragging") return;
|
|
128
|
+
if (full) {
|
|
129
|
+
const { operation, target } = state;
|
|
130
|
+
const marked = target?.kind === "item" ? target.key : null;
|
|
131
|
+
for (const [key, { el }] of items) {
|
|
132
|
+
const carried = isDragged(state, key);
|
|
133
|
+
if (el !== preview?.el) {
|
|
134
|
+
const shift = shiftOf(state, key);
|
|
135
|
+
channel.move(el, shift.x === 0 && shift.y === 0 ? null : shift);
|
|
136
|
+
channel.hide(el, carried && operation !== "copy");
|
|
137
|
+
}
|
|
138
|
+
flag(el, "data-dragging", carried ? "" : null);
|
|
139
|
+
flag(el, "data-drop-target", key === marked ? "" : null);
|
|
140
|
+
}
|
|
141
|
+
for (const [id, el] of zones) {
|
|
142
|
+
const over = state.hit?.zoneId === id;
|
|
143
|
+
flag(el, "data-drag-over", over ? "" : null);
|
|
144
|
+
flag(el, "data-drag-operation", over ? state.operation : null);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
if (preview !== null) channel.move(preview.el, preview.at);
|
|
148
|
+
},
|
|
149
|
+
rect(el) {
|
|
150
|
+
const { x, y, width, height } = el.getBoundingClientRect();
|
|
151
|
+
const by = base.painted?.(el) ?? offsets.get(el);
|
|
152
|
+
return by === void 0 ? {
|
|
153
|
+
x,
|
|
154
|
+
y,
|
|
155
|
+
width,
|
|
156
|
+
height
|
|
157
|
+
} : {
|
|
158
|
+
x: x - by.x,
|
|
159
|
+
y: y - by.y,
|
|
160
|
+
width,
|
|
161
|
+
height
|
|
162
|
+
};
|
|
163
|
+
},
|
|
164
|
+
reset() {
|
|
165
|
+
offsets.clear();
|
|
166
|
+
channel.reset();
|
|
167
|
+
for (const el of flagged) for (const name of FLAGS) el.removeAttribute(name);
|
|
168
|
+
flagged.clear();
|
|
169
|
+
}
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
// src/live/settle.ts
|
|
173
|
+
/** The transform that paints a move's element, laid out at `to`, back over `from`; its origin is the top-left corner. */
|
|
174
|
+
function inverseOf({ from, to }) {
|
|
175
|
+
const sx = to.width > 0 ? from.width / to.width : 1;
|
|
176
|
+
const sy = to.height > 0 ? from.height / to.height : 1;
|
|
177
|
+
return `${translate({
|
|
178
|
+
x: from.x - to.x,
|
|
179
|
+
y: from.y - to.y
|
|
180
|
+
})} scale(${sx}, ${sy})`;
|
|
181
|
+
}
|
|
182
|
+
const REDUCED_MOTION = "(prefers-reduced-motion: reduce)";
|
|
183
|
+
/**
|
|
184
|
+
* FLIP: shifted items and the carried ones (from the preview's painted rect, unless the drop
|
|
185
|
+
* copied) start where they were painted, and `animate` resolves once they are at rest.
|
|
186
|
+
* `animate` runs in the write pass, so it sees each element's own inline transform; the
|
|
187
|
+
* engine then paints each translate and scale inverse in that same pass, so no frame shows
|
|
188
|
+
* the new layout or a size jump, and restores the element's own transform when the drag ends.
|
|
189
|
+
*/
|
|
190
|
+
function flipSettle(animate) {
|
|
191
|
+
return (drop) => {
|
|
192
|
+
const { state, items, preview } = drop;
|
|
193
|
+
const from = /* @__PURE__ */ new Map();
|
|
194
|
+
for (const [key, { el }] of items) {
|
|
195
|
+
const shift = shiftOf(state, key);
|
|
196
|
+
if (shift.x !== 0 || shift.y !== 0) from.set(key, el.getBoundingClientRect());
|
|
197
|
+
}
|
|
198
|
+
if (drop.result?.operation !== "copy") {
|
|
199
|
+
const painted = preview.getBoundingClientRect();
|
|
200
|
+
for (const key of state.source.carried) from.set(key, painted);
|
|
201
|
+
}
|
|
202
|
+
const moves = [];
|
|
203
|
+
const abort = new AbortController();
|
|
204
|
+
return {
|
|
205
|
+
cancel: () => abort.abort(),
|
|
206
|
+
read() {
|
|
207
|
+
for (const [key, rect] of from) {
|
|
208
|
+
const el = items.get(key)?.el;
|
|
209
|
+
if (el?.isConnected) moves.push({
|
|
210
|
+
el,
|
|
211
|
+
from: rect,
|
|
212
|
+
to: drop.rect(el)
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
},
|
|
216
|
+
write() {
|
|
217
|
+
if (moves.length === 0) return void 0;
|
|
218
|
+
const view = preview.ownerDocument.defaultView;
|
|
219
|
+
const reducedMotion = typeof view?.matchMedia === "function" && view.matchMedia(REDUCED_MOTION).matches;
|
|
220
|
+
const { transform } = drop;
|
|
221
|
+
const done = animate({
|
|
222
|
+
moves,
|
|
223
|
+
reducedMotion,
|
|
224
|
+
frame: drop.frame,
|
|
225
|
+
signal: abort.signal,
|
|
226
|
+
translate: drop.translate,
|
|
227
|
+
inverse: transform !== void 0
|
|
228
|
+
});
|
|
229
|
+
if (transform !== void 0) for (const move of moves) transform(move.el, inverseOf(move));
|
|
230
|
+
return done;
|
|
231
|
+
}
|
|
232
|
+
};
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
export { DRAG_Y as a, varsChannel as c, DRAG_X as i, inverseOf as n, createWriter as o, DRAG_HIDDEN as r, styleChannel as s, flipSettle as t };
|