@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,323 @@
|
|
|
1
|
+
import { Axis, ItemTrack, Point, Rect, Size } from "@bevelo/runtime/geometry";
|
|
2
|
+
/**
|
|
3
|
+
* Several zones: a lift may land in any registered zone. Entering a zone needs `hysteresis` px
|
|
4
|
+
* of penetration while the previous zone is within `hysteresis` px, and the cross-axis arrow
|
|
5
|
+
* keys step a keyboard drag to the neighbour zone, in registration order.
|
|
6
|
+
*/
|
|
7
|
+
declare function boardZones(): DragPlugin;
|
|
8
|
+
/**
|
|
9
|
+
* The operation the platform modifiers ask for, when `operations` allows it, else the first
|
|
10
|
+
* operation. Alt with Ctrl or Meta, or Ctrl with Shift, links; Alt or Ctrl copies.
|
|
11
|
+
*/
|
|
12
|
+
declare function operationFor<Op extends Operation>(modifiers: Modifiers, operations: readonly Op[]): Op | null;
|
|
13
|
+
/**
|
|
14
|
+
* The operations a drop may make, `move` included only when listed. Move and copy target the
|
|
15
|
+
* gap; swap and link target the item under the preview. The default policy is
|
|
16
|
+
* `operationFor(modifiers, list)`; a swap needs a single dragged id.
|
|
17
|
+
*/
|
|
18
|
+
declare function operations<const Op extends Operation>(list: readonly Op[]): DragPlugin<Op>;
|
|
19
|
+
/**
|
|
20
|
+
* The carry of a selection: every chosen key that is in some zone, in zone then track order.
|
|
21
|
+
* Each root brings its subtree, and a root inside an earlier root's subtree travels with that
|
|
22
|
+
* root instead of becoming one. A primary outside `ids` lifts alone.
|
|
23
|
+
*/
|
|
24
|
+
declare function carrySelection(layout: DragLayout, ids: readonly string[], primary: string): Carry;
|
|
25
|
+
type Operation = "move" | "copy" | "swap" | "link";
|
|
26
|
+
interface Modifiers {
|
|
27
|
+
readonly alt: boolean;
|
|
28
|
+
readonly shift: boolean;
|
|
29
|
+
readonly ctrl: boolean;
|
|
30
|
+
readonly meta: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare const noModifiers: Modifiers;
|
|
33
|
+
/**
|
|
34
|
+
* Where an item sits, by key. `parentId` is the zone id for a list, the parent node id (null =
|
|
35
|
+
* root) for a tree. `before` is the next sibling, null = last. Never names a dragged key.
|
|
36
|
+
*/
|
|
37
|
+
interface Placement {
|
|
38
|
+
readonly parentId: string | null;
|
|
39
|
+
readonly before: string | null;
|
|
40
|
+
}
|
|
41
|
+
interface TreeRules {
|
|
42
|
+
/** Pixels of cross-axis travel per level. */
|
|
43
|
+
readonly indent: number;
|
|
44
|
+
depthOf(key: string): number;
|
|
45
|
+
/** Deepest allowed depth; root children are depth 0. */
|
|
46
|
+
readonly maxDepth?: number | undefined;
|
|
47
|
+
/** May `ids` become children of `parentId`? Called during projection; keep it O(1). */
|
|
48
|
+
canNest?(parentId: string | null, ids: readonly string[]): boolean;
|
|
49
|
+
}
|
|
50
|
+
/** What every zone has: its id, axis, hit frame, content origin and track. */
|
|
51
|
+
interface ZoneBase {
|
|
52
|
+
readonly id: string;
|
|
53
|
+
readonly axis: Axis;
|
|
54
|
+
/** Where hits count: the zone box clipped by its scrollers, in viewport coordinates. */
|
|
55
|
+
readonly frame: Rect;
|
|
56
|
+
/** Viewport position of track offset 0; the only thing scrolling changes. */
|
|
57
|
+
readonly origin: Point;
|
|
58
|
+
readonly track: ItemTrack;
|
|
59
|
+
}
|
|
60
|
+
interface ListZone extends ZoneBase {
|
|
61
|
+
readonly kind: "list";
|
|
62
|
+
}
|
|
63
|
+
interface TreeZone extends ZoneBase, TreeRules {
|
|
64
|
+
readonly kind: "tree";
|
|
65
|
+
/** The tree maths: `outline` from `@bevelo/dnd/tree`. */
|
|
66
|
+
readonly outline: Outline;
|
|
67
|
+
}
|
|
68
|
+
/** Tree maths behind a tree zone; `outline` from `@bevelo/dnd/tree` implements it. */
|
|
69
|
+
interface Outline {
|
|
70
|
+
/** The tree zone over `base`. `rules` reads the host's latest rules on every call. */
|
|
71
|
+
zone(base: ZoneBase, rules: () => TreeRules): TreeZone;
|
|
72
|
+
/** The row at `index` with its visible descendants: one past the last of them, and how many levels they span below it. */
|
|
73
|
+
subtree(zone: TreeZone, index: number): {
|
|
74
|
+
readonly end: number;
|
|
75
|
+
readonly height: number;
|
|
76
|
+
};
|
|
77
|
+
/** Where the row at `index` sits by key, skipping carried keys. */
|
|
78
|
+
placement(zone: TreeZone, index: number, carried: ReadonlySet<string>): Placement;
|
|
79
|
+
/** 1-based position of track index `at` among non-carried siblings at `depth`, and their count. */
|
|
80
|
+
siblings(zone: TreeZone, at: number, depth: number, carried: ReadonlySet<string>): {
|
|
81
|
+
position: number;
|
|
82
|
+
total: number;
|
|
83
|
+
};
|
|
84
|
+
/** The gap target at row gap `gap` for depth intent `want`; null when no depth is allowed there. */
|
|
85
|
+
gap(zone: TreeZone, gap: number, want: number, source: DragSource, carried: ReadonlySet<string>): GapTarget | null;
|
|
86
|
+
/** A pointer's depth intent: the source depth plus its cross-axis travel over the indent, mirrored in RTL. */
|
|
87
|
+
depth(zone: TreeZone, pointer: Point, source: DragSource, rtl: boolean): number;
|
|
88
|
+
}
|
|
89
|
+
type Zone = ListZone | TreeZone;
|
|
90
|
+
/** The geometry snapshot the DOM layer builds. A new object per change. */
|
|
91
|
+
interface DragLayout {
|
|
92
|
+
readonly zones: readonly Zone[];
|
|
93
|
+
}
|
|
94
|
+
interface Hit {
|
|
95
|
+
readonly zoneId: string;
|
|
96
|
+
/** [0, rows] over the track minus carried keys, by the midpoint rule on the preview centre. */
|
|
97
|
+
readonly gap: number;
|
|
98
|
+
readonly item: {
|
|
99
|
+
readonly key: string;
|
|
100
|
+
readonly index: number;
|
|
101
|
+
readonly part: "before" | "inside" | "after";
|
|
102
|
+
} | null;
|
|
103
|
+
}
|
|
104
|
+
interface GapTarget {
|
|
105
|
+
readonly kind: "gap";
|
|
106
|
+
readonly zoneId: string;
|
|
107
|
+
readonly placement: Placement;
|
|
108
|
+
readonly index: number;
|
|
109
|
+
readonly depth: number;
|
|
110
|
+
}
|
|
111
|
+
/** The item under the preview, as a policy sees it. */
|
|
112
|
+
interface ItemHit {
|
|
113
|
+
readonly kind: "item";
|
|
114
|
+
readonly zoneId: string;
|
|
115
|
+
readonly key: string;
|
|
116
|
+
readonly index: number;
|
|
117
|
+
}
|
|
118
|
+
/** An item chosen as the target: the operation that chose it built its drop and its paint. */
|
|
119
|
+
interface ItemTarget extends ItemHit {
|
|
120
|
+
/** The drop it makes; null when it would change nothing. */
|
|
121
|
+
readonly result: DropResult | null;
|
|
122
|
+
/** The translate the item paints meanwhile: a swap ghosts it to the primary's slot. */
|
|
123
|
+
readonly shift: Point;
|
|
124
|
+
}
|
|
125
|
+
type DropTarget = GapTarget | ItemTarget;
|
|
126
|
+
interface DragSource {
|
|
127
|
+
/** What the result names: the primary plus selection roots, in zone then track order. */
|
|
128
|
+
readonly ids: readonly string[];
|
|
129
|
+
/** What moves on screen: ids plus their visible tree descendants. */
|
|
130
|
+
readonly carried: readonly string[];
|
|
131
|
+
readonly primary: string;
|
|
132
|
+
/** Parallel to `ids`. */
|
|
133
|
+
readonly from: readonly Placement[];
|
|
134
|
+
/** Zone of the primary. */
|
|
135
|
+
readonly zoneId: string;
|
|
136
|
+
/** Primary slot size: the gap size and the preview size. */
|
|
137
|
+
readonly size: Size;
|
|
138
|
+
/** Pointer offset inside the primary at start. */
|
|
139
|
+
readonly grab: Point;
|
|
140
|
+
/** Primary depth in a tree zone, else 0. */
|
|
141
|
+
readonly depth: number;
|
|
142
|
+
/** Deepest carried depth below its root. */
|
|
143
|
+
readonly height: number;
|
|
144
|
+
}
|
|
145
|
+
type InputKind = "pointer" | "keyboard";
|
|
146
|
+
type DragState<Op extends Operation = Operation> = {
|
|
147
|
+
readonly phase: "idle";
|
|
148
|
+
} | {
|
|
149
|
+
readonly phase: "dragging";
|
|
150
|
+
readonly input: InputKind;
|
|
151
|
+
readonly source: DragSource;
|
|
152
|
+
/** Keyboard: the target slot's centre, so the preview and scroll-into-view follow it. */
|
|
153
|
+
readonly pointer: Point;
|
|
154
|
+
readonly modifiers: Modifiers;
|
|
155
|
+
readonly layout: DragLayout;
|
|
156
|
+
/** The last hit; hysteresis compares against it. */
|
|
157
|
+
readonly hit: Hit | null;
|
|
158
|
+
/** Tree depth intent: pointer travel over indent, or the keyboard's last projected depth. */
|
|
159
|
+
readonly depth: number;
|
|
160
|
+
readonly operation: Op | null;
|
|
161
|
+
/** Null: dropping now cancels. */
|
|
162
|
+
readonly target: DropTarget | null;
|
|
163
|
+
} | {
|
|
164
|
+
readonly phase: "dropping";
|
|
165
|
+
readonly source: DragSource;
|
|
166
|
+
readonly result: DropResult<Op> | null;
|
|
167
|
+
};
|
|
168
|
+
declare const idle: DragState<never>;
|
|
169
|
+
/** Distributes over Op. A move to the source's own placement yields null (no onDrop). */
|
|
170
|
+
type DropResult<Op extends Operation = Operation> = Op extends "move" | "copy" ? {
|
|
171
|
+
readonly operation: Op;
|
|
172
|
+
readonly zone: string;
|
|
173
|
+
readonly ids: readonly string[];
|
|
174
|
+
readonly from: readonly Placement[];
|
|
175
|
+
readonly to: Placement;
|
|
176
|
+
readonly depth: number;
|
|
177
|
+
} : Op extends "swap" ? {
|
|
178
|
+
readonly operation: "swap";
|
|
179
|
+
readonly zone: string;
|
|
180
|
+
readonly ids: readonly [string];
|
|
181
|
+
readonly from: readonly [Placement];
|
|
182
|
+
readonly with: string;
|
|
183
|
+
readonly withFrom: Placement;
|
|
184
|
+
} : {
|
|
185
|
+
readonly operation: "link";
|
|
186
|
+
readonly zone: string;
|
|
187
|
+
readonly ids: readonly string[];
|
|
188
|
+
readonly from: readonly Placement[];
|
|
189
|
+
readonly onto: string;
|
|
190
|
+
};
|
|
191
|
+
type StepDirection = "prev" | "next" | "zone-prev" | "zone-next" | "indent" | "outdent";
|
|
192
|
+
/** Everything that advances a drag. The DOM layer produces these; nothing else changes state. */
|
|
193
|
+
type DragInput = {
|
|
194
|
+
readonly type: "start";
|
|
195
|
+
readonly ids: readonly string[];
|
|
196
|
+
readonly primary: string;
|
|
197
|
+
readonly input: InputKind;
|
|
198
|
+
readonly pointer: Point;
|
|
199
|
+
readonly layout: DragLayout;
|
|
200
|
+
readonly modifiers: Modifiers;
|
|
201
|
+
} | {
|
|
202
|
+
readonly type: "move";
|
|
203
|
+
readonly pointer: Point;
|
|
204
|
+
} | {
|
|
205
|
+
readonly type: "layout";
|
|
206
|
+
readonly layout: DragLayout;
|
|
207
|
+
} | {
|
|
208
|
+
readonly type: "modifiers";
|
|
209
|
+
readonly modifiers: Modifiers;
|
|
210
|
+
} | {
|
|
211
|
+
readonly type: "step";
|
|
212
|
+
readonly direction: StepDirection;
|
|
213
|
+
} | {
|
|
214
|
+
readonly type: "drop";
|
|
215
|
+
} | {
|
|
216
|
+
readonly type: "cancel";
|
|
217
|
+
} | {
|
|
218
|
+
readonly type: "settled";
|
|
219
|
+
};
|
|
220
|
+
interface Proposal {
|
|
221
|
+
readonly modifiers: Modifiers;
|
|
222
|
+
readonly input: InputKind;
|
|
223
|
+
readonly source: DragSource;
|
|
224
|
+
/** The gap under the preview, for move and copy. */
|
|
225
|
+
readonly gap: GapTarget | null;
|
|
226
|
+
/** The item under the preview, for swap and link; null unless `operations()` is installed. */
|
|
227
|
+
readonly item: ItemHit | null;
|
|
228
|
+
}
|
|
229
|
+
/** Host policy: the operation, or null for "no drop here". The one accept/reject hook. */
|
|
230
|
+
type DropPolicy<Op extends Operation> = (proposal: Proposal) => Op | null;
|
|
231
|
+
interface DragConfig<Op extends Operation> {
|
|
232
|
+
/**
|
|
233
|
+
* Accepts or refuses each proposal. Without `operations()` it may only answer "move" or
|
|
234
|
+
* null; with it, the default is `operationFor(modifiers, operations)`.
|
|
235
|
+
*/
|
|
236
|
+
readonly policy?: DropPolicy<Op>;
|
|
237
|
+
/** Pixels a pointer must pass a midpoint by, or penetrate a new zone; default 8. */
|
|
238
|
+
readonly hysteresis?: number;
|
|
239
|
+
/** Mirrors the indent direction. */
|
|
240
|
+
readonly rtl?: boolean;
|
|
241
|
+
/** Reducer plugins: `boardZones()`, `operations()`, the carry of `selection()`. */
|
|
242
|
+
readonly plugins?: readonly AnyPlugin[];
|
|
243
|
+
}
|
|
244
|
+
/** One lifted key: the zone and slot it was lifted from. */
|
|
245
|
+
interface Root {
|
|
246
|
+
readonly key: string;
|
|
247
|
+
readonly zone: Zone;
|
|
248
|
+
readonly index: number;
|
|
249
|
+
}
|
|
250
|
+
/** What a lift carries: the roots the result names, every key that moves, the subtree height. */
|
|
251
|
+
interface Carry {
|
|
252
|
+
readonly roots: readonly Root[];
|
|
253
|
+
readonly carried: readonly string[];
|
|
254
|
+
readonly height: number;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* What a plugin adds to the reducer. Each hook has one owner, and the reducer falls back to a
|
|
258
|
+
* single list when a hook is absent.
|
|
259
|
+
*/
|
|
260
|
+
interface DragPlugin<Op extends Operation = never, Shape = never, Extra = never> {
|
|
261
|
+
/**
|
|
262
|
+
* Type only, never set: the operations this plugin allows, the zone spec shape it adds as an
|
|
263
|
+
* alternative to `items`, and the fields it adds to every zone spec.
|
|
264
|
+
*/
|
|
265
|
+
readonly types?: {
|
|
266
|
+
readonly op: Op;
|
|
267
|
+
readonly shape: Shape;
|
|
268
|
+
readonly extra: Extra;
|
|
269
|
+
};
|
|
270
|
+
/** `selection()`: what a lift of `primary` carries when `ids` were chosen. */
|
|
271
|
+
readonly carry?: (layout: DragLayout, ids: readonly string[], primary: string) => Carry;
|
|
272
|
+
/** `boardZones()`: the zone under `point`, sticky against the previous hit's zone by `hysteresis`. */
|
|
273
|
+
readonly zoneAt?: (layout: DragLayout, point: Point, previous: Hit | null, hysteresis: number) => Zone | undefined;
|
|
274
|
+
/** `boardZones()`: the zone `by` steps from `zone`, for a cross-axis keyboard step. */
|
|
275
|
+
readonly across?: (layout: DragLayout, zone: Zone, by: number) => Zone | undefined;
|
|
276
|
+
/** `operations()`: the operation and the target for a hit, from the gap there and the policy. */
|
|
277
|
+
readonly choose?: (config: DragConfig<Operation>, state: Dragging<Operation>, gap: GapTarget | null, hit: Hit, zone: Zone) => readonly [Operation | null, DropTarget | null];
|
|
278
|
+
}
|
|
279
|
+
/** A plugin of any operations and spec, as the reducer holds them. */
|
|
280
|
+
type AnyPlugin = DragPlugin<any, any, any>;
|
|
281
|
+
type Dragging<Op extends Operation = Operation> = Extract<DragState<Op>, {
|
|
282
|
+
readonly phase: "dragging";
|
|
283
|
+
}>;
|
|
284
|
+
/**
|
|
285
|
+
* Pure and total. An input that changes nothing returns the same reference.
|
|
286
|
+
* Pointer input re-hit-tests the preview centre; keyboard steps move over track slots and
|
|
287
|
+
* place the pointer on the target slot. `start` while dragging, `drop` or `cancel` outside
|
|
288
|
+
* dragging, and `settled` outside dropping are ignored.
|
|
289
|
+
*/
|
|
290
|
+
declare function dragReducer<Op extends Operation>(config: DragConfig<Op>): (state: DragState<Op>, input: DragInput) => DragState<Op>;
|
|
291
|
+
/**
|
|
292
|
+
* The zone and gap under `point` (the preview centre). A gap changes only once the point is
|
|
293
|
+
* `hysteresis` px past the midpoint that separates it from the previous gap. `zoneAt` picks
|
|
294
|
+
* the zone: the one under the point, or `boardZones()`'s sticky choice. `carried` keys are
|
|
295
|
+
* not counted as rows.
|
|
296
|
+
*/
|
|
297
|
+
declare function hitTest(layout: DragLayout, point: Point, previous: Hit | null, hysteresis: number, carried?: ReadonlySet<string>, zoneAt?: AnyPlugin["zoneAt"]): Hit | null;
|
|
298
|
+
/** The drop the current state would make, or null when it would change nothing. */
|
|
299
|
+
declare function resultOf<Op extends Operation>(state: DragState<Op>): DropResult<Op> | null;
|
|
300
|
+
/**
|
|
301
|
+
* Translate for `key` now. Siblings at or after the gap open by the source size; a move
|
|
302
|
+
* closes the carried slots. An item target paints its own shift. Zero outside dragging.
|
|
303
|
+
*/
|
|
304
|
+
declare function shiftOf(state: DragState, key: string): Point;
|
|
305
|
+
interface Placeholder {
|
|
306
|
+
readonly zoneId: string;
|
|
307
|
+
readonly index: number;
|
|
308
|
+
readonly depth: number;
|
|
309
|
+
/** Content offset of the opened gap along the zone axis, after a move closes its origin. */
|
|
310
|
+
readonly start: number;
|
|
311
|
+
readonly size: number;
|
|
312
|
+
}
|
|
313
|
+
/** The opened gap of a move or copy, or null when the target is not a gap. */
|
|
314
|
+
declare function placeholderOf(state: DragState): Placeholder | null;
|
|
315
|
+
/** Whether `key` travels with the drag (dragging or settling). */
|
|
316
|
+
declare function isDragged(state: DragState, key: string): boolean;
|
|
317
|
+
/** What a zone shows: the preview is over it, the drag started in it, and the operation it would get. */
|
|
318
|
+
declare function zoneFlags(state: DragState, zoneId: string): {
|
|
319
|
+
readonly over: boolean;
|
|
320
|
+
readonly source: boolean;
|
|
321
|
+
readonly operation: Operation | null;
|
|
322
|
+
};
|
|
323
|
+
export { ZoneBase as A, boardZones as B, Placement as C, TreeRules as D, StepDirection as E, noModifiers as F, operationFor as H, placeholderOf as I, resultOf as L, hitTest as M, idle as N, TreeZone as O, isDragged as P, shiftOf as R, Placeholder as S, Root as T, operations as U, carrySelection as V, ItemTarget as _, DragLayout as a, Operation as b, DragState as c, DropResult as d, DropTarget as f, ItemHit as g, InputKind as h, DragInput as i, dragReducer as j, Zone as k, Dragging as l, Hit as m, Carry as n, DragPlugin as o, GapTarget as p, DragConfig as r, DragSource as s, AnyPlugin as t, DropPolicy as u, ListZone as v, Proposal as w, Outline as x, Modifiers as y, zoneFlags as z };
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
import { Axis, ItemTrack, Point, Rect, Size } from "@bevelo/runtime/geometry";
|
|
2
|
+
/**
|
|
3
|
+
* Several zones: a lift may land in any registered zone. Entering a zone needs `hysteresis` px
|
|
4
|
+
* of penetration while the previous zone is within `hysteresis` px, and the cross-axis arrow
|
|
5
|
+
* keys step a keyboard drag to the neighbour zone, in registration order.
|
|
6
|
+
*/
|
|
7
|
+
declare function boardZones(): DragPlugin;
|
|
8
|
+
/**
|
|
9
|
+
* The operation the platform modifiers ask for, when `operations` allows it, else the first
|
|
10
|
+
* operation. Alt with Ctrl or Meta, or Ctrl with Shift, links; Alt or Ctrl copies.
|
|
11
|
+
*/
|
|
12
|
+
declare function operationFor<Op extends Operation>(modifiers: Modifiers, operations: readonly Op[]): Op | null;
|
|
13
|
+
/**
|
|
14
|
+
* The operations a drop may make, `move` included only when listed. Move and copy target the
|
|
15
|
+
* gap; swap and link target the item under the preview. The default policy is
|
|
16
|
+
* `operationFor(modifiers, list)`; a swap needs a single dragged id.
|
|
17
|
+
*/
|
|
18
|
+
declare function operations<const Op extends Operation>(list: readonly Op[]): DragPlugin<Op>;
|
|
19
|
+
/**
|
|
20
|
+
* The carry of a selection: every chosen key that is in some zone, in zone then track order.
|
|
21
|
+
* Each root brings its subtree, and a root inside an earlier root's subtree travels with that
|
|
22
|
+
* root instead of becoming one. A primary outside `ids` lifts alone.
|
|
23
|
+
*/
|
|
24
|
+
declare function carrySelection(layout: DragLayout, ids: readonly string[], primary: string): Carry;
|
|
25
|
+
type Operation = "move" | "copy" | "swap" | "link";
|
|
26
|
+
interface Modifiers {
|
|
27
|
+
readonly alt: boolean;
|
|
28
|
+
readonly shift: boolean;
|
|
29
|
+
readonly ctrl: boolean;
|
|
30
|
+
readonly meta: boolean;
|
|
31
|
+
}
|
|
32
|
+
declare const noModifiers: Modifiers;
|
|
33
|
+
/**
|
|
34
|
+
* Where an item sits, by key. `parentId` is the zone id for a list, the parent node id (null =
|
|
35
|
+
* root) for a tree. `before` is the next sibling, null = last. Never names a dragged key.
|
|
36
|
+
*/
|
|
37
|
+
interface Placement {
|
|
38
|
+
readonly parentId: string | null;
|
|
39
|
+
readonly before: string | null;
|
|
40
|
+
}
|
|
41
|
+
interface TreeRules {
|
|
42
|
+
/** Pixels of cross-axis travel per level. */
|
|
43
|
+
readonly indent: number;
|
|
44
|
+
depthOf(key: string): number;
|
|
45
|
+
/** Deepest allowed depth; root children are depth 0. */
|
|
46
|
+
readonly maxDepth?: number | undefined;
|
|
47
|
+
/** May `ids` become children of `parentId`? Called during projection; keep it O(1). */
|
|
48
|
+
canNest?(parentId: string | null, ids: readonly string[]): boolean;
|
|
49
|
+
}
|
|
50
|
+
/** What every zone has: its id, axis, hit frame, content origin and track. */
|
|
51
|
+
interface ZoneBase {
|
|
52
|
+
readonly id: string;
|
|
53
|
+
readonly axis: Axis;
|
|
54
|
+
/** Where hits count: the zone box clipped by its scrollers, in viewport coordinates. */
|
|
55
|
+
readonly frame: Rect;
|
|
56
|
+
/** Viewport position of track offset 0; the only thing scrolling changes. */
|
|
57
|
+
readonly origin: Point;
|
|
58
|
+
readonly track: ItemTrack;
|
|
59
|
+
}
|
|
60
|
+
interface ListZone extends ZoneBase {
|
|
61
|
+
readonly kind: "list";
|
|
62
|
+
}
|
|
63
|
+
interface TreeZone extends ZoneBase, TreeRules {
|
|
64
|
+
readonly kind: "tree";
|
|
65
|
+
/** The tree maths: `outline` from `@bevelo/dnd/tree`. */
|
|
66
|
+
readonly outline: Outline;
|
|
67
|
+
}
|
|
68
|
+
/** Tree maths behind a tree zone; `outline` from `@bevelo/dnd/tree` implements it. */
|
|
69
|
+
interface Outline {
|
|
70
|
+
/** The tree zone over `base`. `rules` reads the host's latest rules on every call. */
|
|
71
|
+
zone(base: ZoneBase, rules: () => TreeRules): TreeZone;
|
|
72
|
+
/** The row at `index` with its visible descendants: one past the last of them, and how many levels they span below it. */
|
|
73
|
+
subtree(zone: TreeZone, index: number): {
|
|
74
|
+
readonly end: number;
|
|
75
|
+
readonly height: number;
|
|
76
|
+
};
|
|
77
|
+
/** Where the row at `index` sits by key, skipping carried keys. */
|
|
78
|
+
placement(zone: TreeZone, index: number, carried: ReadonlySet<string>): Placement;
|
|
79
|
+
/** 1-based position of track index `at` among non-carried siblings at `depth`, and their count. */
|
|
80
|
+
siblings(zone: TreeZone, at: number, depth: number, carried: ReadonlySet<string>): {
|
|
81
|
+
position: number;
|
|
82
|
+
total: number;
|
|
83
|
+
};
|
|
84
|
+
/** The gap target at row gap `gap` for depth intent `want`; null when no depth is allowed there. */
|
|
85
|
+
gap(zone: TreeZone, gap: number, want: number, source: DragSource, carried: ReadonlySet<string>): GapTarget | null;
|
|
86
|
+
/** A pointer's depth intent: the source depth plus its cross-axis travel over the indent, mirrored in RTL. */
|
|
87
|
+
depth(zone: TreeZone, pointer: Point, source: DragSource, rtl: boolean): number;
|
|
88
|
+
}
|
|
89
|
+
type Zone = ListZone | TreeZone;
|
|
90
|
+
/** The geometry snapshot the DOM layer builds. A new object per change. */
|
|
91
|
+
interface DragLayout {
|
|
92
|
+
readonly zones: readonly Zone[];
|
|
93
|
+
}
|
|
94
|
+
interface Hit {
|
|
95
|
+
readonly zoneId: string;
|
|
96
|
+
/** [0, rows] over the track minus carried keys, by the midpoint rule on the preview centre. */
|
|
97
|
+
readonly gap: number;
|
|
98
|
+
readonly item: {
|
|
99
|
+
readonly key: string;
|
|
100
|
+
readonly index: number;
|
|
101
|
+
readonly part: "before" | "inside" | "after";
|
|
102
|
+
} | null;
|
|
103
|
+
}
|
|
104
|
+
interface GapTarget {
|
|
105
|
+
readonly kind: "gap";
|
|
106
|
+
readonly zoneId: string;
|
|
107
|
+
readonly placement: Placement;
|
|
108
|
+
readonly index: number;
|
|
109
|
+
readonly depth: number;
|
|
110
|
+
}
|
|
111
|
+
/** The item under the preview, as a policy sees it. */
|
|
112
|
+
interface ItemHit {
|
|
113
|
+
readonly kind: "item";
|
|
114
|
+
readonly zoneId: string;
|
|
115
|
+
readonly key: string;
|
|
116
|
+
readonly index: number;
|
|
117
|
+
}
|
|
118
|
+
/** An item chosen as the target: the operation that chose it built its drop and its paint. */
|
|
119
|
+
interface ItemTarget extends ItemHit {
|
|
120
|
+
/** The drop it makes; null when it would change nothing. */
|
|
121
|
+
readonly result: DropResult | null;
|
|
122
|
+
/** The translate the item paints meanwhile: a swap ghosts it to the primary's slot. */
|
|
123
|
+
readonly shift: Point;
|
|
124
|
+
}
|
|
125
|
+
type DropTarget = GapTarget | ItemTarget;
|
|
126
|
+
interface DragSource {
|
|
127
|
+
/** What the result names: the primary plus selection roots, in zone then track order. */
|
|
128
|
+
readonly ids: readonly string[];
|
|
129
|
+
/** What moves on screen: ids plus their visible tree descendants. */
|
|
130
|
+
readonly carried: readonly string[];
|
|
131
|
+
readonly primary: string;
|
|
132
|
+
/** Parallel to `ids`. */
|
|
133
|
+
readonly from: readonly Placement[];
|
|
134
|
+
/** Zone of the primary. */
|
|
135
|
+
readonly zoneId: string;
|
|
136
|
+
/** Primary slot size: the gap size and the preview size. */
|
|
137
|
+
readonly size: Size;
|
|
138
|
+
/** Pointer offset inside the primary at start. */
|
|
139
|
+
readonly grab: Point;
|
|
140
|
+
/** Primary depth in a tree zone, else 0. */
|
|
141
|
+
readonly depth: number;
|
|
142
|
+
/** Deepest carried depth below its root. */
|
|
143
|
+
readonly height: number;
|
|
144
|
+
}
|
|
145
|
+
type InputKind = "pointer" | "keyboard";
|
|
146
|
+
type DragState<Op extends Operation = Operation> = {
|
|
147
|
+
readonly phase: "idle";
|
|
148
|
+
} | {
|
|
149
|
+
readonly phase: "dragging";
|
|
150
|
+
readonly input: InputKind;
|
|
151
|
+
readonly source: DragSource;
|
|
152
|
+
/** Keyboard: the target slot's centre, so the preview and scroll-into-view follow it. */
|
|
153
|
+
readonly pointer: Point;
|
|
154
|
+
readonly modifiers: Modifiers;
|
|
155
|
+
readonly layout: DragLayout;
|
|
156
|
+
/** The last hit; hysteresis compares against it. */
|
|
157
|
+
readonly hit: Hit | null;
|
|
158
|
+
/** Tree depth intent: pointer travel over indent, or the keyboard's last projected depth. */
|
|
159
|
+
readonly depth: number;
|
|
160
|
+
readonly operation: Op | null;
|
|
161
|
+
/** Null: dropping now cancels. */
|
|
162
|
+
readonly target: DropTarget | null;
|
|
163
|
+
} | {
|
|
164
|
+
readonly phase: "dropping";
|
|
165
|
+
readonly source: DragSource;
|
|
166
|
+
readonly result: DropResult<Op> | null;
|
|
167
|
+
};
|
|
168
|
+
declare const idle: DragState<never>;
|
|
169
|
+
/** Distributes over Op. A move to the source's own placement yields null (no onDrop). */
|
|
170
|
+
type DropResult<Op extends Operation = Operation> = Op extends "move" | "copy" ? {
|
|
171
|
+
readonly operation: Op;
|
|
172
|
+
readonly zone: string;
|
|
173
|
+
readonly ids: readonly string[];
|
|
174
|
+
readonly from: readonly Placement[];
|
|
175
|
+
readonly to: Placement;
|
|
176
|
+
readonly depth: number;
|
|
177
|
+
} : Op extends "swap" ? {
|
|
178
|
+
readonly operation: "swap";
|
|
179
|
+
readonly zone: string;
|
|
180
|
+
readonly ids: readonly [string];
|
|
181
|
+
readonly from: readonly [Placement];
|
|
182
|
+
readonly with: string;
|
|
183
|
+
readonly withFrom: Placement;
|
|
184
|
+
} : {
|
|
185
|
+
readonly operation: "link";
|
|
186
|
+
readonly zone: string;
|
|
187
|
+
readonly ids: readonly string[];
|
|
188
|
+
readonly from: readonly Placement[];
|
|
189
|
+
readonly onto: string;
|
|
190
|
+
};
|
|
191
|
+
type StepDirection = "prev" | "next" | "zone-prev" | "zone-next" | "indent" | "outdent";
|
|
192
|
+
/** Everything that advances a drag. The DOM layer produces these; nothing else changes state. */
|
|
193
|
+
type DragInput = {
|
|
194
|
+
readonly type: "start";
|
|
195
|
+
readonly ids: readonly string[];
|
|
196
|
+
readonly primary: string;
|
|
197
|
+
readonly input: InputKind;
|
|
198
|
+
readonly pointer: Point;
|
|
199
|
+
readonly layout: DragLayout;
|
|
200
|
+
readonly modifiers: Modifiers;
|
|
201
|
+
} | {
|
|
202
|
+
readonly type: "move";
|
|
203
|
+
readonly pointer: Point;
|
|
204
|
+
} | {
|
|
205
|
+
readonly type: "layout";
|
|
206
|
+
readonly layout: DragLayout;
|
|
207
|
+
} | {
|
|
208
|
+
readonly type: "modifiers";
|
|
209
|
+
readonly modifiers: Modifiers;
|
|
210
|
+
} | {
|
|
211
|
+
readonly type: "step";
|
|
212
|
+
readonly direction: StepDirection;
|
|
213
|
+
} | {
|
|
214
|
+
readonly type: "drop";
|
|
215
|
+
} | {
|
|
216
|
+
readonly type: "cancel";
|
|
217
|
+
} | {
|
|
218
|
+
readonly type: "settled";
|
|
219
|
+
};
|
|
220
|
+
interface Proposal {
|
|
221
|
+
readonly modifiers: Modifiers;
|
|
222
|
+
readonly input: InputKind;
|
|
223
|
+
readonly source: DragSource;
|
|
224
|
+
/** The gap under the preview, for move and copy. */
|
|
225
|
+
readonly gap: GapTarget | null;
|
|
226
|
+
/** The item under the preview, for swap and link; null unless `operations()` is installed. */
|
|
227
|
+
readonly item: ItemHit | null;
|
|
228
|
+
}
|
|
229
|
+
/** Host policy: the operation, or null for "no drop here". The one accept/reject hook. */
|
|
230
|
+
type DropPolicy<Op extends Operation> = (proposal: Proposal) => Op | null;
|
|
231
|
+
interface DragConfig<Op extends Operation> {
|
|
232
|
+
/**
|
|
233
|
+
* Accepts or refuses each proposal. Without `operations()` it may only answer "move" or
|
|
234
|
+
* null; with it, the default is `operationFor(modifiers, operations)`.
|
|
235
|
+
*/
|
|
236
|
+
readonly policy?: DropPolicy<Op>;
|
|
237
|
+
/** Pixels a pointer must pass a midpoint by, or penetrate a new zone; default 8. */
|
|
238
|
+
readonly hysteresis?: number;
|
|
239
|
+
/** Mirrors the indent direction. */
|
|
240
|
+
readonly rtl?: boolean;
|
|
241
|
+
/** Reducer plugins: `boardZones()`, `operations()`, the carry of `selection()`. */
|
|
242
|
+
readonly plugins?: readonly AnyPlugin[];
|
|
243
|
+
}
|
|
244
|
+
/** One lifted key: the zone and slot it was lifted from. */
|
|
245
|
+
interface Root {
|
|
246
|
+
readonly key: string;
|
|
247
|
+
readonly zone: Zone;
|
|
248
|
+
readonly index: number;
|
|
249
|
+
}
|
|
250
|
+
/** What a lift carries: the roots the result names, every key that moves, the subtree height. */
|
|
251
|
+
interface Carry {
|
|
252
|
+
readonly roots: readonly Root[];
|
|
253
|
+
readonly carried: readonly string[];
|
|
254
|
+
readonly height: number;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* What a plugin adds to the reducer. Each hook has one owner, and the reducer falls back to a
|
|
258
|
+
* single list when a hook is absent.
|
|
259
|
+
*/
|
|
260
|
+
interface DragPlugin<Op extends Operation = never, Shape = never, Extra = never> {
|
|
261
|
+
/**
|
|
262
|
+
* Type only, never set: the operations this plugin allows, the zone spec shape it adds as an
|
|
263
|
+
* alternative to `items`, and the fields it adds to every zone spec.
|
|
264
|
+
*/
|
|
265
|
+
readonly types?: {
|
|
266
|
+
readonly op: Op;
|
|
267
|
+
readonly shape: Shape;
|
|
268
|
+
readonly extra: Extra;
|
|
269
|
+
};
|
|
270
|
+
/** `selection()`: what a lift of `primary` carries when `ids` were chosen. */
|
|
271
|
+
readonly carry?: (layout: DragLayout, ids: readonly string[], primary: string) => Carry;
|
|
272
|
+
/** `boardZones()`: the zone under `point`, sticky against the previous hit's zone by `hysteresis`. */
|
|
273
|
+
readonly zoneAt?: (layout: DragLayout, point: Point, previous: Hit | null, hysteresis: number) => Zone | undefined;
|
|
274
|
+
/** `boardZones()`: the zone `by` steps from `zone`, for a cross-axis keyboard step. */
|
|
275
|
+
readonly across?: (layout: DragLayout, zone: Zone, by: number) => Zone | undefined;
|
|
276
|
+
/** `operations()`: the operation and the target for a hit, from the gap there and the policy. */
|
|
277
|
+
readonly choose?: (config: DragConfig<Operation>, state: Dragging<Operation>, gap: GapTarget | null, hit: Hit, zone: Zone) => readonly [Operation | null, DropTarget | null];
|
|
278
|
+
}
|
|
279
|
+
/** A plugin of any operations and spec, as the reducer holds them. */
|
|
280
|
+
type AnyPlugin = DragPlugin<any, any, any>;
|
|
281
|
+
type Dragging<Op extends Operation = Operation> = Extract<DragState<Op>, {
|
|
282
|
+
readonly phase: "dragging";
|
|
283
|
+
}>;
|
|
284
|
+
/**
|
|
285
|
+
* Pure and total. An input that changes nothing returns the same reference.
|
|
286
|
+
* Pointer input re-hit-tests the preview centre; keyboard steps move over track slots and
|
|
287
|
+
* place the pointer on the target slot. `start` while dragging, `drop` or `cancel` outside
|
|
288
|
+
* dragging, and `settled` outside dropping are ignored.
|
|
289
|
+
*/
|
|
290
|
+
declare function dragReducer<Op extends Operation>(config: DragConfig<Op>): (state: DragState<Op>, input: DragInput) => DragState<Op>;
|
|
291
|
+
/**
|
|
292
|
+
* The zone and gap under `point` (the preview centre). A gap changes only once the point is
|
|
293
|
+
* `hysteresis` px past the midpoint that separates it from the previous gap. `zoneAt` picks
|
|
294
|
+
* the zone: the one under the point, or `boardZones()`'s sticky choice. `carried` keys are
|
|
295
|
+
* not counted as rows.
|
|
296
|
+
*/
|
|
297
|
+
declare function hitTest(layout: DragLayout, point: Point, previous: Hit | null, hysteresis: number, carried?: ReadonlySet<string>, zoneAt?: AnyPlugin["zoneAt"]): Hit | null;
|
|
298
|
+
/** The drop the current state would make, or null when it would change nothing. */
|
|
299
|
+
declare function resultOf<Op extends Operation>(state: DragState<Op>): DropResult<Op> | null;
|
|
300
|
+
/**
|
|
301
|
+
* Translate for `key` now. Siblings at or after the gap open by the source size; a move
|
|
302
|
+
* closes the carried slots. An item target paints its own shift. Zero outside dragging.
|
|
303
|
+
*/
|
|
304
|
+
declare function shiftOf(state: DragState, key: string): Point;
|
|
305
|
+
interface Placeholder {
|
|
306
|
+
readonly zoneId: string;
|
|
307
|
+
readonly index: number;
|
|
308
|
+
readonly depth: number;
|
|
309
|
+
/** Content offset of the opened gap along the zone axis, after a move closes its origin. */
|
|
310
|
+
readonly start: number;
|
|
311
|
+
readonly size: number;
|
|
312
|
+
}
|
|
313
|
+
/** The opened gap of a move or copy, or null when the target is not a gap. */
|
|
314
|
+
declare function placeholderOf(state: DragState): Placeholder | null;
|
|
315
|
+
/** Whether `key` travels with the drag (dragging or settling). */
|
|
316
|
+
declare function isDragged(state: DragState, key: string): boolean;
|
|
317
|
+
/** What a zone shows: the preview is over it, the drag started in it, and the operation it would get. */
|
|
318
|
+
declare function zoneFlags(state: DragState, zoneId: string): {
|
|
319
|
+
readonly over: boolean;
|
|
320
|
+
readonly source: boolean;
|
|
321
|
+
readonly operation: Operation | null;
|
|
322
|
+
};
|
|
323
|
+
export { ZoneBase as A, boardZones as B, Placement as C, TreeRules as D, StepDirection as E, noModifiers as F, operationFor as H, placeholderOf as I, resultOf as L, hitTest as M, idle as N, TreeZone as O, isDragged as P, shiftOf as R, Placeholder as S, Root as T, operations as U, carrySelection as V, ItemTarget as _, DragLayout as a, Operation as b, DragState as c, DropResult as d, DropTarget as f, ItemHit as g, InputKind as h, DragInput as i, dragReducer as j, Zone as k, Dragging as l, Hit as m, Carry as n, DragPlugin as o, GapTarget as p, DragConfig as r, DragSource as s, AnyPlugin as t, DropPolicy as u, ListZone as v, Proposal as w, Outline as x, Modifiers as y, zoneFlags as z };
|