@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,672 @@
|
|
|
1
|
+
const require_rows = require("./rows-B2C7Yf94.cjs");
|
|
2
|
+
let _bevelo_runtime_geometry = require("@bevelo/runtime/geometry");
|
|
3
|
+
// src/pure/layout.ts
|
|
4
|
+
const zero = {
|
|
5
|
+
x: 0,
|
|
6
|
+
y: 0
|
|
7
|
+
};
|
|
8
|
+
const mainOf = (point, axis) => axis === "x" ? point.x : point.y;
|
|
9
|
+
const crossOf = (point, axis) => axis === "x" ? point.y : point.x;
|
|
10
|
+
const along = (axis, main, cross) => axis === "x" ? {
|
|
11
|
+
x: main,
|
|
12
|
+
y: cross
|
|
13
|
+
} : {
|
|
14
|
+
x: cross,
|
|
15
|
+
y: main
|
|
16
|
+
};
|
|
17
|
+
/** The frame's cross-axis start and size. */
|
|
18
|
+
const crossSpan = (zone) => zone.axis === "x" ? [zone.frame.y, zone.frame.height] : [zone.frame.x, zone.frame.width];
|
|
19
|
+
const lanesOf = (zone) => Math.max(1, zone.track.lanes);
|
|
20
|
+
const sizeAlong = (size, axis) => axis === "x" ? size.width : size.height;
|
|
21
|
+
/** Deep equality over the plain data a state holds: hits, targets, points, modifiers. */
|
|
22
|
+
function same(a, b) {
|
|
23
|
+
if (a === b) return true;
|
|
24
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false;
|
|
25
|
+
const keys = Object.keys(a);
|
|
26
|
+
return keys.length === Object.keys(b).length && keys.every((key) => same(a[key], b[key]));
|
|
27
|
+
}
|
|
28
|
+
/** The rows below `index` that travel with it: its visible tree descendants, none in a list. */
|
|
29
|
+
function subtreeOf(zone, index) {
|
|
30
|
+
return zone.kind === "tree" ? zone.outline.subtree(zone, index) : {
|
|
31
|
+
end: index + 1,
|
|
32
|
+
height: 0
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
/** Where `index` sits by key, skipping carried keys. */
|
|
36
|
+
function placementOf(zone, index, carried) {
|
|
37
|
+
if (zone.kind === "tree") return zone.outline.placement(zone, index, carried);
|
|
38
|
+
const { track } = zone;
|
|
39
|
+
let next = index + 1;
|
|
40
|
+
while (next < track.count && carried.has(track.keyAt(next))) next += 1;
|
|
41
|
+
return {
|
|
42
|
+
parentId: zone.id,
|
|
43
|
+
before: next < track.count ? track.keyAt(next) : null
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
/** The offset that moves `index`'s slot in `zone` onto slot `to` of `toZone`. */
|
|
47
|
+
function slotDelta(zone, index, toZone, to) {
|
|
48
|
+
const a = slotPosition(toZone, to);
|
|
49
|
+
const b = slotPosition(zone, index);
|
|
50
|
+
return {
|
|
51
|
+
x: a.x - b.x,
|
|
52
|
+
y: a.y - b.y
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
/** The slot's viewport position: main from the track, cross from its lane. */
|
|
56
|
+
function slotPosition(zone, index) {
|
|
57
|
+
const { axis, origin, track } = zone;
|
|
58
|
+
return along(axis, mainOf(origin, axis) + track.startOf(index), crossOf(origin, axis) + index % lanesOf(zone) * (crossSpan(zone)[1] / lanesOf(zone)));
|
|
59
|
+
}
|
|
60
|
+
function zoneById(layout, id) {
|
|
61
|
+
return layout.zones.find((zone) => zone.id === id);
|
|
62
|
+
}
|
|
63
|
+
function locate(layout, key) {
|
|
64
|
+
for (const zone of layout.zones) {
|
|
65
|
+
const index = zone.track.indexOf(key);
|
|
66
|
+
if (index >= 0) return {
|
|
67
|
+
zone,
|
|
68
|
+
index
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// src/pure/plugins.ts
|
|
73
|
+
/**
|
|
74
|
+
* Several zones: a lift may land in any registered zone. Entering a zone needs `hysteresis` px
|
|
75
|
+
* of penetration while the previous zone is within `hysteresis` px, and the cross-axis arrow
|
|
76
|
+
* keys step a keyboard drag to the neighbour zone, in registration order.
|
|
77
|
+
*/
|
|
78
|
+
function boardZones() {
|
|
79
|
+
return {
|
|
80
|
+
zoneAt(layout, point, previous, hysteresis) {
|
|
81
|
+
const under = layout.zones.find((zone) => (0, _bevelo_runtime_geometry.rectContains)(zone.frame, point));
|
|
82
|
+
const prior = zoneById(layout, previous?.zoneId);
|
|
83
|
+
if (prior === void 0 || under === prior) return under;
|
|
84
|
+
if (under !== void 0 && (0, _bevelo_runtime_geometry.rectContains)((0, _bevelo_runtime_geometry.expandRect)(under.frame, -hysteresis), point)) return under;
|
|
85
|
+
return (0, _bevelo_runtime_geometry.rectContains)((0, _bevelo_runtime_geometry.expandRect)(prior.frame, hysteresis), point) ? prior : under;
|
|
86
|
+
},
|
|
87
|
+
across: (layout, zone, by) => layout.zones[layout.zones.indexOf(zone) + by]
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* The operation the platform modifiers ask for, when `operations` allows it, else the first
|
|
92
|
+
* operation. Alt with Ctrl or Meta, or Ctrl with Shift, links; Alt or Ctrl copies.
|
|
93
|
+
*/
|
|
94
|
+
function operationFor(modifiers, operations) {
|
|
95
|
+
const { alt, ctrl, meta, shift } = modifiers;
|
|
96
|
+
const wanted = alt && (ctrl || meta) || ctrl && shift ? "link" : alt || ctrl ? "copy" : null;
|
|
97
|
+
return operations.find((op) => op === wanted) ?? operations[0] ?? null;
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* The operations a drop may make, `move` included only when listed. Move and copy target the
|
|
101
|
+
* gap; swap and link target the item under the preview. The default policy is
|
|
102
|
+
* `operationFor(modifiers, list)`; a swap needs a single dragged id.
|
|
103
|
+
*/
|
|
104
|
+
function operations(list) {
|
|
105
|
+
return { choose(config, state, gap, hit, zone) {
|
|
106
|
+
const { source, modifiers, input } = state;
|
|
107
|
+
const carried = new Set(source.carried);
|
|
108
|
+
const item = hit.item === null || carried.has(hit.item.key) ? null : {
|
|
109
|
+
kind: "item",
|
|
110
|
+
zoneId: zone.id,
|
|
111
|
+
key: hit.item.key,
|
|
112
|
+
index: hit.item.index
|
|
113
|
+
};
|
|
114
|
+
const { policy } = config;
|
|
115
|
+
const operation = policy === void 0 ? operationFor(modifiers, list) : policy({
|
|
116
|
+
modifiers,
|
|
117
|
+
input,
|
|
118
|
+
source,
|
|
119
|
+
gap,
|
|
120
|
+
item
|
|
121
|
+
});
|
|
122
|
+
if (operation === null || !list.includes(operation) || operation === "swap" && source.ids.length > 1) return [null, null];
|
|
123
|
+
if (operation === "move" || operation === "copy") return [operation, gap];
|
|
124
|
+
if (item === null) return [operation, null];
|
|
125
|
+
const { ids, from, primary } = source;
|
|
126
|
+
if (operation === "link") return [operation, {
|
|
127
|
+
...item,
|
|
128
|
+
result: {
|
|
129
|
+
operation,
|
|
130
|
+
zone: zone.id,
|
|
131
|
+
ids,
|
|
132
|
+
from,
|
|
133
|
+
onto: item.key
|
|
134
|
+
},
|
|
135
|
+
shift: zero
|
|
136
|
+
}];
|
|
137
|
+
const home = locate(state.layout, primary);
|
|
138
|
+
const result = {
|
|
139
|
+
operation,
|
|
140
|
+
zone: zone.id,
|
|
141
|
+
ids: [primary],
|
|
142
|
+
from: [from[0]],
|
|
143
|
+
with: item.key,
|
|
144
|
+
withFrom: placementOf(zone, item.index, carried)
|
|
145
|
+
};
|
|
146
|
+
const shift = home === void 0 ? zero : slotDelta(zone, item.index, home.zone, home.index);
|
|
147
|
+
return [operation, {
|
|
148
|
+
...item,
|
|
149
|
+
result,
|
|
150
|
+
shift
|
|
151
|
+
}];
|
|
152
|
+
} };
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The carry of a selection: every chosen key that is in some zone, in zone then track order.
|
|
156
|
+
* Each root brings its subtree, and a root inside an earlier root's subtree travels with that
|
|
157
|
+
* root instead of becoming one. A primary outside `ids` lifts alone.
|
|
158
|
+
*/
|
|
159
|
+
function carrySelection(layout, ids, primary) {
|
|
160
|
+
const chosen = new Set(ids.includes(primary) ? ids : [primary]);
|
|
161
|
+
const placed = [];
|
|
162
|
+
layout.zones.forEach((zone, order) => {
|
|
163
|
+
for (const key of chosen) {
|
|
164
|
+
const index = zone.track.indexOf(key);
|
|
165
|
+
if (index >= 0) {
|
|
166
|
+
chosen.delete(key);
|
|
167
|
+
placed.push({
|
|
168
|
+
key,
|
|
169
|
+
zone,
|
|
170
|
+
order,
|
|
171
|
+
index
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
placed.sort((a, b) => a.order - b.order || a.index - b.index);
|
|
177
|
+
const roots = [];
|
|
178
|
+
const carried = [];
|
|
179
|
+
let height = 0;
|
|
180
|
+
let covered;
|
|
181
|
+
let coveredEnd = -1;
|
|
182
|
+
for (const root of placed) {
|
|
183
|
+
const { zone, index } = root;
|
|
184
|
+
if (zone === covered && index < coveredEnd) continue;
|
|
185
|
+
roots.push(root);
|
|
186
|
+
const subtree = subtreeOf(zone, index);
|
|
187
|
+
covered = zone;
|
|
188
|
+
coveredEnd = subtree.end;
|
|
189
|
+
height = Math.max(height, subtree.height);
|
|
190
|
+
for (let at = index; at < coveredEnd; at += 1) carried.push(zone.track.keyAt(at));
|
|
191
|
+
}
|
|
192
|
+
return {
|
|
193
|
+
roots,
|
|
194
|
+
carried,
|
|
195
|
+
height
|
|
196
|
+
};
|
|
197
|
+
}
|
|
198
|
+
// src/pure/state.ts
|
|
199
|
+
const noModifiers = {
|
|
200
|
+
alt: false,
|
|
201
|
+
shift: false,
|
|
202
|
+
ctrl: false,
|
|
203
|
+
meta: false
|
|
204
|
+
};
|
|
205
|
+
const idle = { phase: "idle" };
|
|
206
|
+
const DEFAULT_HYSTERESIS = 8;
|
|
207
|
+
const noKeys = /* @__PURE__ */ new Set();
|
|
208
|
+
/** `previous` when `next` says the same thing, so selectors keep their reference. */
|
|
209
|
+
const keep = (previous, next) => same(previous, next) ? previous : next;
|
|
210
|
+
/** `state` when every field of `next` is the very same value, so an input that changes nothing returns it. */
|
|
211
|
+
const unless = (state, next) => Object.keys(next).every((key) => next[key] === state[key]) ? state : next;
|
|
212
|
+
/**
|
|
213
|
+
* Pure and total. An input that changes nothing returns the same reference.
|
|
214
|
+
* Pointer input re-hit-tests the preview centre; keyboard steps move over track slots and
|
|
215
|
+
* place the pointer on the target slot. `start` while dragging, `drop` or `cancel` outside
|
|
216
|
+
* dragging, and `settled` outside dropping are ignored.
|
|
217
|
+
*/
|
|
218
|
+
function dragReducer(config) {
|
|
219
|
+
const hooks = Object.assign({}, ...config.plugins ?? []);
|
|
220
|
+
return (state, input) => {
|
|
221
|
+
const { type } = input;
|
|
222
|
+
if (type === "start") return state.phase === "dragging" ? state : begin(config, hooks, input) ?? state;
|
|
223
|
+
if (type === "settled") return state.phase === "dropping" ? idle : state;
|
|
224
|
+
if (state.phase !== "dragging") return state;
|
|
225
|
+
if (type === "drop" || type === "cancel") return {
|
|
226
|
+
phase: "dropping",
|
|
227
|
+
source: state.source,
|
|
228
|
+
result: type === "drop" ? resultOf(state) : null
|
|
229
|
+
};
|
|
230
|
+
if (type === "move") return state.input !== "pointer" || same(state.pointer, input.pointer) ? state : byPointer(config, hooks, state, input.pointer, state.layout);
|
|
231
|
+
if (type === "modifiers") return same(state.modifiers, input.modifiers) ? state : resolve(config, hooks, {
|
|
232
|
+
...state,
|
|
233
|
+
modifiers: input.modifiers
|
|
234
|
+
}, state.hit, state.depth);
|
|
235
|
+
if (type === "step") return state.input === "keyboard" ? step(config, hooks, state, input.direction) : state;
|
|
236
|
+
const { layout } = input;
|
|
237
|
+
if (layout === state.layout) return state;
|
|
238
|
+
const home = locate(layout, state.source.primary);
|
|
239
|
+
if (home === void 0) return {
|
|
240
|
+
phase: "dropping",
|
|
241
|
+
source: state.source,
|
|
242
|
+
result: null
|
|
243
|
+
};
|
|
244
|
+
if (state.input === "pointer") return byPointer(config, hooks, state, state.pointer, layout);
|
|
245
|
+
const zone = zoneById(layout, state.hit?.zoneId) ?? home.zone;
|
|
246
|
+
return byKeyboard(config, hooks, {
|
|
247
|
+
...state,
|
|
248
|
+
layout
|
|
249
|
+
}, zone, state.hit?.gap ?? 0, state.depth);
|
|
250
|
+
};
|
|
251
|
+
}
|
|
252
|
+
/** The start input made into a dragging state, or null when the primary is in no zone. */
|
|
253
|
+
function begin(config, hooks, input) {
|
|
254
|
+
const { layout, primary } = input;
|
|
255
|
+
const home = locate(layout, primary);
|
|
256
|
+
if (home === void 0) return null;
|
|
257
|
+
const { roots, carried, height } = hooks.carry?.(layout, input.ids, primary) ?? carryOne({
|
|
258
|
+
key: primary,
|
|
259
|
+
...home
|
|
260
|
+
});
|
|
261
|
+
const carriedSet = new Set(carried);
|
|
262
|
+
const { zone, index } = home;
|
|
263
|
+
const topLeft = slotPosition(zone, index);
|
|
264
|
+
const lane = crossSpan(zone)[1] / lanesOf(zone);
|
|
265
|
+
const main = zone.track.sizeOf(index);
|
|
266
|
+
const depth = zone.kind === "tree" ? zone.depthOf(primary) : 0;
|
|
267
|
+
const source = {
|
|
268
|
+
ids: roots.map((root) => root.key),
|
|
269
|
+
carried,
|
|
270
|
+
primary,
|
|
271
|
+
from: roots.map((root) => placementOf(root.zone, root.index, carriedSet)),
|
|
272
|
+
zoneId: zone.id,
|
|
273
|
+
size: zone.axis === "x" ? {
|
|
274
|
+
width: main,
|
|
275
|
+
height: lane
|
|
276
|
+
} : {
|
|
277
|
+
width: lane,
|
|
278
|
+
height: main
|
|
279
|
+
},
|
|
280
|
+
grab: {
|
|
281
|
+
x: input.pointer.x - topLeft.x,
|
|
282
|
+
y: input.pointer.y - topLeft.y
|
|
283
|
+
},
|
|
284
|
+
depth,
|
|
285
|
+
height
|
|
286
|
+
};
|
|
287
|
+
return byPointer(config, hooks, {
|
|
288
|
+
phase: "dragging",
|
|
289
|
+
input: input.input,
|
|
290
|
+
source,
|
|
291
|
+
pointer: input.pointer,
|
|
292
|
+
modifiers: input.modifiers,
|
|
293
|
+
layout,
|
|
294
|
+
hit: null,
|
|
295
|
+
depth,
|
|
296
|
+
operation: null,
|
|
297
|
+
target: null
|
|
298
|
+
}, input.pointer, layout);
|
|
299
|
+
}
|
|
300
|
+
/** What a lift of one root carries: the root and its subtree. */
|
|
301
|
+
function carryOne(root) {
|
|
302
|
+
const { zone, index } = root;
|
|
303
|
+
const { end, height } = subtreeOf(zone, index);
|
|
304
|
+
const carried = [];
|
|
305
|
+
for (let at = index; at < end; at += 1) carried.push(zone.track.keyAt(at));
|
|
306
|
+
return {
|
|
307
|
+
roots: [root],
|
|
308
|
+
carried,
|
|
309
|
+
height
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
function byPointer(config, hooks, state, pointer, layout) {
|
|
313
|
+
const { source } = state;
|
|
314
|
+
const hit = hitTestRaw(layout, {
|
|
315
|
+
x: pointer.x - source.grab.x + source.size.width / 2,
|
|
316
|
+
y: pointer.y - source.grab.y + source.size.height / 2
|
|
317
|
+
}, state.hit, config.hysteresis ?? DEFAULT_HYSTERESIS, source.carried, hooks.zoneAt);
|
|
318
|
+
const zone = zoneById(layout, hit?.zoneId);
|
|
319
|
+
const depth = zone?.kind === "tree" ? zone.outline.depth(zone, pointer, source, config.rtl === true) : source.depth;
|
|
320
|
+
return resolve(config, hooks, {
|
|
321
|
+
...state,
|
|
322
|
+
pointer,
|
|
323
|
+
layout
|
|
324
|
+
}, hit, depth);
|
|
325
|
+
}
|
|
326
|
+
/** Along the track: prev and next. Across it: a neighbour zone (`boardZones()`), a level of a tree. */
|
|
327
|
+
const STEPS = {
|
|
328
|
+
prev: [
|
|
329
|
+
-1,
|
|
330
|
+
0,
|
|
331
|
+
0
|
|
332
|
+
],
|
|
333
|
+
next: [
|
|
334
|
+
1,
|
|
335
|
+
0,
|
|
336
|
+
0
|
|
337
|
+
],
|
|
338
|
+
"zone-prev": [
|
|
339
|
+
0,
|
|
340
|
+
-1,
|
|
341
|
+
0
|
|
342
|
+
],
|
|
343
|
+
"zone-next": [
|
|
344
|
+
0,
|
|
345
|
+
1,
|
|
346
|
+
0
|
|
347
|
+
],
|
|
348
|
+
outdent: [
|
|
349
|
+
0,
|
|
350
|
+
0,
|
|
351
|
+
-1
|
|
352
|
+
],
|
|
353
|
+
indent: [
|
|
354
|
+
0,
|
|
355
|
+
0,
|
|
356
|
+
1
|
|
357
|
+
]
|
|
358
|
+
};
|
|
359
|
+
function step(config, hooks, state, direction) {
|
|
360
|
+
const { layout, hit } = state;
|
|
361
|
+
const current = zoneById(layout, hit?.zoneId) ?? locate(layout, state.source.primary)?.zone;
|
|
362
|
+
if (current === void 0) return state;
|
|
363
|
+
const [by, zoneBy, depthBy] = STEPS[direction];
|
|
364
|
+
const zone = zoneBy === 0 ? current : hooks.across?.(layout, current, zoneBy);
|
|
365
|
+
if (zone === void 0 || depthBy !== 0 && zone.kind !== "tree") return state;
|
|
366
|
+
return byKeyboard(config, hooks, state, zone, (hit?.gap ?? 0) + by, state.depth + depthBy);
|
|
367
|
+
}
|
|
368
|
+
/** A keyboard hit at `gap` in `zone`: the target is resolved, then the pointer moves onto it. */
|
|
369
|
+
function byKeyboard(config, hooks, state, zone, gap, want) {
|
|
370
|
+
const rows = require_rows.rowsFor(zone.track, state.source.carried);
|
|
371
|
+
const at = Math.min(Math.max(0, gap), rows.count);
|
|
372
|
+
const row = Math.min(at, rows.count - 1);
|
|
373
|
+
const item = row < 0 ? null : {
|
|
374
|
+
key: rows.key(row),
|
|
375
|
+
index: rows.index(row),
|
|
376
|
+
part: at < rows.count ? "before" : "after"
|
|
377
|
+
};
|
|
378
|
+
const resolved = resolve(config, hooks, state, {
|
|
379
|
+
zoneId: zone.id,
|
|
380
|
+
gap: at,
|
|
381
|
+
item
|
|
382
|
+
}, want);
|
|
383
|
+
const depth = resolved.target?.kind === "gap" ? resolved.target.depth : resolved.depth;
|
|
384
|
+
const pointer = keep(state.pointer, keyboardPointer(config, {
|
|
385
|
+
...resolved,
|
|
386
|
+
depth
|
|
387
|
+
}, zone) ?? state.pointer);
|
|
388
|
+
return unless(state, {
|
|
389
|
+
...resolved,
|
|
390
|
+
depth,
|
|
391
|
+
pointer
|
|
392
|
+
});
|
|
393
|
+
}
|
|
394
|
+
/** The pointer that puts the preview on the target: the placeholder slot, or the target item. */
|
|
395
|
+
function keyboardPointer(config, state, zone) {
|
|
396
|
+
const { source, target } = state;
|
|
397
|
+
if (target === null) return null;
|
|
398
|
+
let main;
|
|
399
|
+
let crossShift = 0;
|
|
400
|
+
if (target.kind === "gap") {
|
|
401
|
+
const placeholder = placeholderOf(state);
|
|
402
|
+
if (placeholder === null) return null;
|
|
403
|
+
main = placeholder.start;
|
|
404
|
+
if (zone.kind === "tree") crossShift = (target.depth - source.depth) * zone.indent * (config.rtl === true ? -1 : 1);
|
|
405
|
+
} else main = zone.track.startOf(target.index);
|
|
406
|
+
const { axis, origin } = zone;
|
|
407
|
+
return along(axis, mainOf(origin, axis) + main + mainOf(source.grab, axis), crossOf(origin, axis) + crossOf(source.grab, axis) + crossShift);
|
|
408
|
+
}
|
|
409
|
+
/** Applies a hit and depth intent: the gap there, then the operation and the chosen target. */
|
|
410
|
+
function resolve(config, hooks, state, nextHit, depth) {
|
|
411
|
+
const hit = keep(state.hit, nextHit);
|
|
412
|
+
const { source, layout } = state;
|
|
413
|
+
const zone = zoneById(layout, hit?.zoneId);
|
|
414
|
+
let operation = null;
|
|
415
|
+
let chosen = null;
|
|
416
|
+
if (hit !== null && zone !== void 0) {
|
|
417
|
+
const rows = require_rows.rowsFor(zone.track, source.carried);
|
|
418
|
+
const gap = zone.kind === "tree" ? zone.outline.gap(zone, hit.gap, depth, source, new Set(source.carried)) : {
|
|
419
|
+
kind: "gap",
|
|
420
|
+
zoneId: zone.id,
|
|
421
|
+
placement: {
|
|
422
|
+
parentId: zone.id,
|
|
423
|
+
before: hit.gap < rows.count ? rows.key(hit.gap) : null
|
|
424
|
+
},
|
|
425
|
+
index: hit.gap,
|
|
426
|
+
depth: 0
|
|
427
|
+
};
|
|
428
|
+
if (hooks.choose !== void 0) [operation, chosen] = hooks.choose(config, state, gap, hit, zone);
|
|
429
|
+
else {
|
|
430
|
+
const { modifiers, input } = state;
|
|
431
|
+
operation = config.policy === void 0 ? "move" : config.policy({
|
|
432
|
+
modifiers,
|
|
433
|
+
input,
|
|
434
|
+
source,
|
|
435
|
+
gap,
|
|
436
|
+
item: null
|
|
437
|
+
});
|
|
438
|
+
if (operation !== "move") operation = null;
|
|
439
|
+
chosen = operation && gap;
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
return unless(state, {
|
|
443
|
+
...state,
|
|
444
|
+
hit,
|
|
445
|
+
depth,
|
|
446
|
+
operation,
|
|
447
|
+
target: keep(state.target, chosen)
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
function hitTestRaw(layout, point, previous, hysteresis, carried, zoneAt = zoneUnder) {
|
|
451
|
+
const zone = zoneAt(layout, point, previous, hysteresis);
|
|
452
|
+
if (zone === void 0) return null;
|
|
453
|
+
const { axis, track } = zone;
|
|
454
|
+
const rows = Array.isArray(carried) ? require_rows.rowsFor(track, carried) : require_rows.rowsOf(track, carried);
|
|
455
|
+
const [crossStart, span] = crossSpan(zone);
|
|
456
|
+
const main = mainOf(point, axis) - mainOf(zone.origin, axis);
|
|
457
|
+
const cross = crossOf(point, axis) - crossStart;
|
|
458
|
+
const invSpan = span > 0 ? 1 / span : 0;
|
|
459
|
+
const raw = slotHit(track, rows, main, cross * invSpan);
|
|
460
|
+
let gap = raw.gap;
|
|
461
|
+
if (previous?.zoneId === zone.id && previous.gap !== gap && hysteresis > 0) {
|
|
462
|
+
const g0 = slotHit(track, rows, main - hysteresis, cross * invSpan, false).gap;
|
|
463
|
+
const g1 = slotHit(track, rows, main + hysteresis, cross * invSpan, false).gap;
|
|
464
|
+
const g2 = slotHit(track, rows, main, (cross - hysteresis) * invSpan, false).gap;
|
|
465
|
+
const g3 = slotHit(track, rows, main, (cross + hysteresis) * invSpan, false).gap;
|
|
466
|
+
if (previous.gap >= Math.min(g0, g1, g2, g3) && previous.gap <= Math.max(g0, g1, g2, g3)) gap = previous.gap;
|
|
467
|
+
}
|
|
468
|
+
return keep(previous, {
|
|
469
|
+
zoneId: zone.id,
|
|
470
|
+
gap,
|
|
471
|
+
item: raw.item
|
|
472
|
+
});
|
|
473
|
+
}
|
|
474
|
+
/**
|
|
475
|
+
* The zone and gap under `point` (the preview centre). A gap changes only once the point is
|
|
476
|
+
* `hysteresis` px past the midpoint that separates it from the previous gap. `zoneAt` picks
|
|
477
|
+
* the zone: the one under the point, or `boardZones()`'s sticky choice. `carried` keys are
|
|
478
|
+
* not counted as rows.
|
|
479
|
+
*/
|
|
480
|
+
function hitTest(layout, point, previous, hysteresis, carried = noKeys, zoneAt = zoneUnder) {
|
|
481
|
+
return hitTestRaw(layout, point, previous, hysteresis, carried, zoneAt);
|
|
482
|
+
}
|
|
483
|
+
const zoneUnder = (layout, point) => layout.zones.find((zone) => (0, _bevelo_runtime_geometry.rectContains)(zone.frame, point));
|
|
484
|
+
/** The gap and item at a content offset. One lane decides by the main axis, lanes by the cross axis. */
|
|
485
|
+
function slotHit(track, rows, main, crossFraction, withItem = true) {
|
|
486
|
+
if (track.count === 0) return {
|
|
487
|
+
gap: 0,
|
|
488
|
+
item: null
|
|
489
|
+
};
|
|
490
|
+
const slot = (0, _bevelo_runtime_geometry.slotAt)(track, main, crossFraction);
|
|
491
|
+
const size = track.sizeOf(slot);
|
|
492
|
+
const position = track.lanes > 1 ? crossFraction * track.lanes - slot % track.lanes : size > 0 ? (main - track.startOf(slot)) / size : .5;
|
|
493
|
+
const rank = rows.rankOf(slot);
|
|
494
|
+
return {
|
|
495
|
+
gap: rows.hasCarried(slot) || position <= .5 ? rank : rank + 1,
|
|
496
|
+
item: withItem ? {
|
|
497
|
+
key: track.keyAt(slot),
|
|
498
|
+
index: slot,
|
|
499
|
+
part: position < 1 / 3 ? "before" : position > 2 / 3 ? "after" : "inside"
|
|
500
|
+
} : null
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
/** The drop the current state would make, or null when it would change nothing. */
|
|
504
|
+
function resultOf(state) {
|
|
505
|
+
if (state.phase !== "dragging") return state.phase === "idle" ? null : state.result;
|
|
506
|
+
const { source, target, operation } = state;
|
|
507
|
+
if (target === null || operation === null) return null;
|
|
508
|
+
if (target.kind === "item") return target.result;
|
|
509
|
+
const { ids, from } = source;
|
|
510
|
+
const zone = target.zoneId;
|
|
511
|
+
const to = target.placement;
|
|
512
|
+
return operation === "move" && zone === source.zoneId && from.every((placement) => same(placement, to)) ? null : {
|
|
513
|
+
operation,
|
|
514
|
+
zone,
|
|
515
|
+
ids,
|
|
516
|
+
from,
|
|
517
|
+
to,
|
|
518
|
+
depth: target.depth
|
|
519
|
+
};
|
|
520
|
+
}
|
|
521
|
+
/**
|
|
522
|
+
* Translate for `key` now. Siblings at or after the gap open by the source size; a move
|
|
523
|
+
* closes the carried slots. An item target paints its own shift. Zero outside dragging.
|
|
524
|
+
*/
|
|
525
|
+
function shiftOf(state, key) {
|
|
526
|
+
if (state.phase !== "dragging") return zero;
|
|
527
|
+
const { source, target, operation, layout } = state;
|
|
528
|
+
if (target === null) return zero;
|
|
529
|
+
const home = locate(layout, key);
|
|
530
|
+
if (home === void 0) return zero;
|
|
531
|
+
const { zone, index } = home;
|
|
532
|
+
if (target.kind === "item") return target.key === key && !source.carried.includes(key) ? target.shift : zero;
|
|
533
|
+
const { track, axis } = zone;
|
|
534
|
+
const rows = require_rows.rowsFor(track, source.carried);
|
|
535
|
+
if (rows.hasCarried(index)) return zero;
|
|
536
|
+
const rank = rows.rankOf(index);
|
|
537
|
+
const closes = operation === "move";
|
|
538
|
+
const opens = zone.id === target.zoneId && rank >= target.index;
|
|
539
|
+
if (track.lanes > 1) {
|
|
540
|
+
const visual = (closes ? rank : index) + (opens ? 1 : 0);
|
|
541
|
+
return visual === index || visual >= track.count ? zero : slotDelta(zone, index, zone, visual);
|
|
542
|
+
}
|
|
543
|
+
let shift = opens ? sizeAlong(source.size, axis) : 0;
|
|
544
|
+
if (closes) for (let i = 0; i < rows.carried.length; i += 1) {
|
|
545
|
+
const slot = rows.carried[i];
|
|
546
|
+
if (slot < index) shift -= track.sizeOf(slot);
|
|
547
|
+
else break;
|
|
548
|
+
}
|
|
549
|
+
return shift === 0 ? zero : along(axis, shift, 0);
|
|
550
|
+
}
|
|
551
|
+
/** The opened gap of a move or copy, or null when the target is not a gap. */
|
|
552
|
+
function placeholderOf(state) {
|
|
553
|
+
if (state.phase !== "dragging") return null;
|
|
554
|
+
const { source, target, operation, layout } = state;
|
|
555
|
+
if (target?.kind !== "gap" || operation !== "move" && operation !== "copy") return null;
|
|
556
|
+
const zone = zoneById(layout, target.zoneId);
|
|
557
|
+
if (zone === void 0) return null;
|
|
558
|
+
const { track } = zone;
|
|
559
|
+
const { count } = track;
|
|
560
|
+
const rows = require_rows.rowsFor(track, source.carried);
|
|
561
|
+
const closes = operation === "move";
|
|
562
|
+
const at = target.index < rows.count ? rows.index(target.index) : count;
|
|
563
|
+
let start;
|
|
564
|
+
if (track.lanes > 1) {
|
|
565
|
+
const visual = closes ? target.index : at;
|
|
566
|
+
start = visual < count ? track.startOf(visual) : track.total;
|
|
567
|
+
} else {
|
|
568
|
+
start = at < count ? track.startOf(at) : count > 0 ? track.startOf(count - 1) + track.sizeOf(count - 1) : 0;
|
|
569
|
+
if (closes) {
|
|
570
|
+
for (const slot of rows.carried) if (slot < at) start -= track.sizeOf(slot);
|
|
571
|
+
}
|
|
572
|
+
}
|
|
573
|
+
return {
|
|
574
|
+
zoneId: zone.id,
|
|
575
|
+
index: target.index,
|
|
576
|
+
depth: target.depth,
|
|
577
|
+
start,
|
|
578
|
+
size: sizeAlong(source.size, zone.axis)
|
|
579
|
+
};
|
|
580
|
+
}
|
|
581
|
+
/** Whether `key` travels with the drag (dragging or settling). */
|
|
582
|
+
function isDragged(state, key) {
|
|
583
|
+
return state.phase !== "idle" && state.source.carried.includes(key);
|
|
584
|
+
}
|
|
585
|
+
/** What a zone shows: the preview is over it, the drag started in it, and the operation it would get. */
|
|
586
|
+
function zoneFlags(state, zoneId) {
|
|
587
|
+
const source = state.phase !== "idle" && state.source.zoneId === zoneId;
|
|
588
|
+
const over = state.phase === "dragging" && state.hit?.zoneId === zoneId;
|
|
589
|
+
return {
|
|
590
|
+
over,
|
|
591
|
+
source,
|
|
592
|
+
operation: over ? state.operation : null
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
Object.defineProperty(exports, "boardZones", {
|
|
596
|
+
enumerable: true,
|
|
597
|
+
get: function() {
|
|
598
|
+
return boardZones;
|
|
599
|
+
}
|
|
600
|
+
});
|
|
601
|
+
Object.defineProperty(exports, "carrySelection", {
|
|
602
|
+
enumerable: true,
|
|
603
|
+
get: function() {
|
|
604
|
+
return carrySelection;
|
|
605
|
+
}
|
|
606
|
+
});
|
|
607
|
+
Object.defineProperty(exports, "dragReducer", {
|
|
608
|
+
enumerable: true,
|
|
609
|
+
get: function() {
|
|
610
|
+
return dragReducer;
|
|
611
|
+
}
|
|
612
|
+
});
|
|
613
|
+
Object.defineProperty(exports, "hitTest", {
|
|
614
|
+
enumerable: true,
|
|
615
|
+
get: function() {
|
|
616
|
+
return hitTest;
|
|
617
|
+
}
|
|
618
|
+
});
|
|
619
|
+
Object.defineProperty(exports, "idle", {
|
|
620
|
+
enumerable: true,
|
|
621
|
+
get: function() {
|
|
622
|
+
return idle;
|
|
623
|
+
}
|
|
624
|
+
});
|
|
625
|
+
Object.defineProperty(exports, "isDragged", {
|
|
626
|
+
enumerable: true,
|
|
627
|
+
get: function() {
|
|
628
|
+
return isDragged;
|
|
629
|
+
}
|
|
630
|
+
});
|
|
631
|
+
Object.defineProperty(exports, "noModifiers", {
|
|
632
|
+
enumerable: true,
|
|
633
|
+
get: function() {
|
|
634
|
+
return noModifiers;
|
|
635
|
+
}
|
|
636
|
+
});
|
|
637
|
+
Object.defineProperty(exports, "operationFor", {
|
|
638
|
+
enumerable: true,
|
|
639
|
+
get: function() {
|
|
640
|
+
return operationFor;
|
|
641
|
+
}
|
|
642
|
+
});
|
|
643
|
+
Object.defineProperty(exports, "operations", {
|
|
644
|
+
enumerable: true,
|
|
645
|
+
get: function() {
|
|
646
|
+
return operations;
|
|
647
|
+
}
|
|
648
|
+
});
|
|
649
|
+
Object.defineProperty(exports, "placeholderOf", {
|
|
650
|
+
enumerable: true,
|
|
651
|
+
get: function() {
|
|
652
|
+
return placeholderOf;
|
|
653
|
+
}
|
|
654
|
+
});
|
|
655
|
+
Object.defineProperty(exports, "resultOf", {
|
|
656
|
+
enumerable: true,
|
|
657
|
+
get: function() {
|
|
658
|
+
return resultOf;
|
|
659
|
+
}
|
|
660
|
+
});
|
|
661
|
+
Object.defineProperty(exports, "shiftOf", {
|
|
662
|
+
enumerable: true,
|
|
663
|
+
get: function() {
|
|
664
|
+
return shiftOf;
|
|
665
|
+
}
|
|
666
|
+
});
|
|
667
|
+
Object.defineProperty(exports, "zoneFlags", {
|
|
668
|
+
enumerable: true,
|
|
669
|
+
get: function() {
|
|
670
|
+
return zoneFlags;
|
|
671
|
+
}
|
|
672
|
+
});
|