@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,840 @@
|
|
|
1
|
+
import { announcement, defaultMessages, keyboardInstructions } from "./pure/a11y.js";
|
|
2
|
+
import { d as carrySelection, o as placeholderOf, r as idle, t as dragReducer } from "./state-CYXS6GIb.js";
|
|
3
|
+
import { o as createWriter, s as styleChannel } from "./settle-BiwZ0ZDt.js";
|
|
4
|
+
import { createPointerTracker, createStore, frameLoopOf, intersection, observeSize, scrollParents, startAutoscroll, trackFromRects } from "@bevelo/runtime";
|
|
5
|
+
// src/live/input.ts
|
|
6
|
+
/** Whether `key` lifts an item from its handle. */
|
|
7
|
+
function isLiftKey(key) {
|
|
8
|
+
return key === " " || key === "Enter";
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* The keyboard map. Along the zone axis the arrows step between gaps; across it they move
|
|
12
|
+
* between zones, or indent and outdent in a tree (mirrored in RTL). Space and Enter drop,
|
|
13
|
+
* Escape cancels; any other key is not the drag's.
|
|
14
|
+
*/
|
|
15
|
+
function keyboardIntent(key, axis, tree, rtl) {
|
|
16
|
+
if (isLiftKey(key)) return { type: "drop" };
|
|
17
|
+
if (key === "Escape") return { type: "cancel" };
|
|
18
|
+
const left = rtl ? "ArrowRight" : "ArrowLeft";
|
|
19
|
+
const right = rtl ? "ArrowLeft" : "ArrowRight";
|
|
20
|
+
const [back, forward, crossBack, crossForward] = axis === "y" ? [
|
|
21
|
+
"ArrowUp",
|
|
22
|
+
"ArrowDown",
|
|
23
|
+
left,
|
|
24
|
+
right
|
|
25
|
+
] : [
|
|
26
|
+
left,
|
|
27
|
+
right,
|
|
28
|
+
"ArrowUp",
|
|
29
|
+
"ArrowDown"
|
|
30
|
+
];
|
|
31
|
+
const step = (direction) => ({
|
|
32
|
+
type: "step",
|
|
33
|
+
direction
|
|
34
|
+
});
|
|
35
|
+
if (key === back) return step("prev");
|
|
36
|
+
if (key === forward) return step("next");
|
|
37
|
+
if (key === crossBack) return step(tree ? "outdent" : "zone-prev");
|
|
38
|
+
if (key === crossForward) return step(tree ? "indent" : "zone-next");
|
|
39
|
+
return null;
|
|
40
|
+
}
|
|
41
|
+
/** The modifier keys an event carries. */
|
|
42
|
+
function modifiersOf(event) {
|
|
43
|
+
return {
|
|
44
|
+
alt: event.altKey,
|
|
45
|
+
shift: event.shiftKey,
|
|
46
|
+
ctrl: event.ctrlKey,
|
|
47
|
+
meta: event.metaKey
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
// src/live/live-region.ts
|
|
51
|
+
/** The `id` of the instructions node handles point `aria-describedby` at. */
|
|
52
|
+
const INSTRUCTIONS_ID = "bevelo-dnd-instructions";
|
|
53
|
+
const HIDDEN = "position:absolute;width:1px;height:1px;margin:-1px;padding:0;overflow:hidden;clip:rect(0 0 0 0);white-space:nowrap;border:0";
|
|
54
|
+
const shared = /* @__PURE__ */ new WeakMap();
|
|
55
|
+
function hiddenNode(doc) {
|
|
56
|
+
const node = doc.createElement("div");
|
|
57
|
+
node.setAttribute("style", HIDDEN);
|
|
58
|
+
doc.body.append(node);
|
|
59
|
+
return node;
|
|
60
|
+
}
|
|
61
|
+
/** Joins the document's region, creating it with the first user. */
|
|
62
|
+
function joinLiveRegion(doc) {
|
|
63
|
+
let entry = shared.get(doc);
|
|
64
|
+
if (entry === void 0) {
|
|
65
|
+
const region = hiddenNode(doc);
|
|
66
|
+
region.setAttribute("aria-live", "polite");
|
|
67
|
+
region.setAttribute("aria-atomic", "true");
|
|
68
|
+
region.setAttribute("data-dnd-live", "");
|
|
69
|
+
const instructions = hiddenNode(doc);
|
|
70
|
+
instructions.id = INSTRUCTIONS_ID;
|
|
71
|
+
instructions.textContent = keyboardInstructions;
|
|
72
|
+
entry = {
|
|
73
|
+
region,
|
|
74
|
+
instructions,
|
|
75
|
+
users: 0
|
|
76
|
+
};
|
|
77
|
+
shared.set(doc, entry);
|
|
78
|
+
}
|
|
79
|
+
entry.users += 1;
|
|
80
|
+
const current = entry;
|
|
81
|
+
let live = true;
|
|
82
|
+
return {
|
|
83
|
+
say(message) {
|
|
84
|
+
current.region.textContent = current.region.textContent === message ? message + "" : message;
|
|
85
|
+
},
|
|
86
|
+
release() {
|
|
87
|
+
if (!live) return;
|
|
88
|
+
live = false;
|
|
89
|
+
current.users -= 1;
|
|
90
|
+
if (current.users > 0) return;
|
|
91
|
+
current.region.remove();
|
|
92
|
+
current.instructions.remove();
|
|
93
|
+
shared.delete(doc);
|
|
94
|
+
}
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
// src/live/preview.ts
|
|
98
|
+
/** Computed styles a clone keeps, since it leaves the selectors that styled it. */
|
|
99
|
+
const KEPT = [
|
|
100
|
+
"color",
|
|
101
|
+
"font",
|
|
102
|
+
"line-height",
|
|
103
|
+
"letter-spacing",
|
|
104
|
+
"text-align",
|
|
105
|
+
"background-color",
|
|
106
|
+
"border-radius",
|
|
107
|
+
"padding"
|
|
108
|
+
];
|
|
109
|
+
function clone(item, rect) {
|
|
110
|
+
const copy = item.cloneNode(true);
|
|
111
|
+
for (const node of [copy, ...copy.querySelectorAll("[id],[tabindex],[name]")]) {
|
|
112
|
+
node.removeAttribute("id");
|
|
113
|
+
node.removeAttribute("tabindex");
|
|
114
|
+
node.removeAttribute("name");
|
|
115
|
+
}
|
|
116
|
+
const computed = item.ownerDocument.defaultView?.getComputedStyle(item);
|
|
117
|
+
copy.removeAttribute("style");
|
|
118
|
+
if (computed !== void 0) for (const name of KEPT) copy.style.setProperty(name, computed.getPropertyValue(name));
|
|
119
|
+
Object.assign(copy.style, {
|
|
120
|
+
position: "fixed",
|
|
121
|
+
left: rect.x + "px",
|
|
122
|
+
top: rect.y + "px",
|
|
123
|
+
width: rect.width + "px",
|
|
124
|
+
height: rect.height + "px",
|
|
125
|
+
margin: "0",
|
|
126
|
+
boxSizing: "border-box",
|
|
127
|
+
pointerEvents: "none",
|
|
128
|
+
zIndex: "2147483647",
|
|
129
|
+
translate: "var(--bevelo-drag-x, 0px) var(--bevelo-drag-y, 0px)"
|
|
130
|
+
});
|
|
131
|
+
copy.setAttribute("aria-hidden", "true");
|
|
132
|
+
copy.setAttribute("data-drag-preview", "");
|
|
133
|
+
copy.inert = true;
|
|
134
|
+
item.ownerDocument.body.append(copy);
|
|
135
|
+
return copy;
|
|
136
|
+
}
|
|
137
|
+
const inPlace = (el) => ({
|
|
138
|
+
el,
|
|
139
|
+
owned: false
|
|
140
|
+
});
|
|
141
|
+
/** The item itself follows the pointer. The default. */
|
|
142
|
+
const inPlacePreview = (item) => inPlace(item);
|
|
143
|
+
/** A fixed clone of the item, which no clipping column can hide. */
|
|
144
|
+
const clonePreview = (item, _overlay, rect) => ({
|
|
145
|
+
el: clone(item, rect),
|
|
146
|
+
owned: true,
|
|
147
|
+
base: () => rect
|
|
148
|
+
});
|
|
149
|
+
/** The element registered with `overlay`; a clone while none is registered. */
|
|
150
|
+
const overlayPreview = (item, overlay, rect) => overlay === null ? clonePreview(item, overlay, rect) : inPlace(overlay);
|
|
151
|
+
// src/live/zones.ts
|
|
152
|
+
let versions = 0;
|
|
153
|
+
/** The record of the zone with `id`. */
|
|
154
|
+
function recordIn(zones, id) {
|
|
155
|
+
for (const record of zones) if (record.spec.id === id) return record;
|
|
156
|
+
}
|
|
157
|
+
/** The zone element's own scroller (when it scrolls) and every scrollable ancestor. */
|
|
158
|
+
function scrollersOf(record) {
|
|
159
|
+
const { el } = record;
|
|
160
|
+
return el === null ? [] : record.scrollers ??= scrollParents(el, true);
|
|
161
|
+
}
|
|
162
|
+
/** The registry; `layoutRect` measures an item's layout rect in the viewport. */
|
|
163
|
+
function createRegistry(layoutRect) {
|
|
164
|
+
const zones = /* @__PURE__ */ new Set();
|
|
165
|
+
const items = /* @__PURE__ */ new Map();
|
|
166
|
+
function read(record, reader) {
|
|
167
|
+
const { spec, el } = record;
|
|
168
|
+
if (spec.disabled === true || el === null) return null;
|
|
169
|
+
const axis = spec.axis ?? "y";
|
|
170
|
+
const measured = reader.measure?.(spec);
|
|
171
|
+
let origin;
|
|
172
|
+
let track;
|
|
173
|
+
if (measured !== void 0) ({origin, track} = measured);
|
|
174
|
+
else {
|
|
175
|
+
const box = el.getBoundingClientRect();
|
|
176
|
+
const at = {
|
|
177
|
+
x: box.left + el.clientLeft - el.scrollLeft,
|
|
178
|
+
y: box.top + el.clientTop - el.scrollTop
|
|
179
|
+
};
|
|
180
|
+
if (record.dirty || record.track === null) {
|
|
181
|
+
const rectOf = (key) => {
|
|
182
|
+
const item = items.get(key)?.el;
|
|
183
|
+
if (item === void 0 || !item.isConnected) return void 0;
|
|
184
|
+
const { x, y, width, height } = layoutRect(item);
|
|
185
|
+
return {
|
|
186
|
+
x: x - at.x,
|
|
187
|
+
y: y - at.y,
|
|
188
|
+
width,
|
|
189
|
+
height
|
|
190
|
+
};
|
|
191
|
+
};
|
|
192
|
+
record.track = trackFromRects(spec.items ?? [], rectOf, axis, versions += 1);
|
|
193
|
+
record.dirty = false;
|
|
194
|
+
}
|
|
195
|
+
origin = at;
|
|
196
|
+
track = record.track;
|
|
197
|
+
}
|
|
198
|
+
let clip = el.getBoundingClientRect();
|
|
199
|
+
for (const scroller of scrollersOf(record)) clip = clip && intersection(clip, scroller.viewport());
|
|
200
|
+
const { x, y, width, height } = clip ?? {
|
|
201
|
+
...origin,
|
|
202
|
+
width: 0,
|
|
203
|
+
height: 0
|
|
204
|
+
};
|
|
205
|
+
const inputs = [
|
|
206
|
+
spec.id,
|
|
207
|
+
axis,
|
|
208
|
+
track,
|
|
209
|
+
origin.x,
|
|
210
|
+
origin.y,
|
|
211
|
+
x,
|
|
212
|
+
y,
|
|
213
|
+
width,
|
|
214
|
+
height
|
|
215
|
+
];
|
|
216
|
+
if (record.zone !== null && inputs.every((input, i) => input === record.inputs[i])) return record.zone;
|
|
217
|
+
record.inputs = inputs;
|
|
218
|
+
const base = {
|
|
219
|
+
id: spec.id,
|
|
220
|
+
axis,
|
|
221
|
+
frame: {
|
|
222
|
+
x,
|
|
223
|
+
y,
|
|
224
|
+
width,
|
|
225
|
+
height
|
|
226
|
+
},
|
|
227
|
+
origin,
|
|
228
|
+
track
|
|
229
|
+
};
|
|
230
|
+
return record.zone = reader.build?.(base, () => record.spec) ?? {
|
|
231
|
+
kind: "list",
|
|
232
|
+
...base
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
zones,
|
|
237
|
+
items,
|
|
238
|
+
zoneElements() {
|
|
239
|
+
const out = /* @__PURE__ */ new Map();
|
|
240
|
+
for (const record of zones) if (record.el !== null) out.set(record.spec.id, record.el);
|
|
241
|
+
return out;
|
|
242
|
+
},
|
|
243
|
+
layout(previous, reader) {
|
|
244
|
+
const next = [];
|
|
245
|
+
for (const record of zones) {
|
|
246
|
+
const zone = read(record, reader);
|
|
247
|
+
if (zone !== null) next.push(zone);
|
|
248
|
+
}
|
|
249
|
+
return previous !== null && previous.zones.length === next.length && next.every((zone, i) => previous.zones[i] === zone) ? previous : { zones: next };
|
|
250
|
+
},
|
|
251
|
+
attach(record, el, onStale, reader) {
|
|
252
|
+
record.el = el;
|
|
253
|
+
record.dirty = true;
|
|
254
|
+
record.scrollers = null;
|
|
255
|
+
record.stopSize?.();
|
|
256
|
+
record.stopSize = reader.observe?.(el, () => {
|
|
257
|
+
record.dirty = true;
|
|
258
|
+
onStale();
|
|
259
|
+
});
|
|
260
|
+
return () => {
|
|
261
|
+
if (record.el !== el) return;
|
|
262
|
+
record.stopSize?.();
|
|
263
|
+
record.stopSize = void 0;
|
|
264
|
+
record.el = null;
|
|
265
|
+
record.zone = null;
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
};
|
|
269
|
+
}
|
|
270
|
+
// src/live/dnd.ts
|
|
271
|
+
/** After the drop: waiting for the app's commit, committed, measured, settling. */
|
|
272
|
+
const WAITING = 0;
|
|
273
|
+
const COMMITTED = 1;
|
|
274
|
+
const MEASURED = 2;
|
|
275
|
+
const SETTLING = 3;
|
|
276
|
+
const sameItems = (a, b) => a === b || a.length === b.length && a.every((key, i) => key === b[i]);
|
|
277
|
+
/** Adds a listener and returns its removal. */
|
|
278
|
+
function listen(target, type, listener, capture = false) {
|
|
279
|
+
target.addEventListener(type, listener, capture);
|
|
280
|
+
return () => target.removeEventListener(type, listener, capture);
|
|
281
|
+
}
|
|
282
|
+
/**
|
|
283
|
+
* @experimental
|
|
284
|
+
* Creates the drag-and-drop coordinator: register zones and items, and it runs pointer and
|
|
285
|
+
* keyboard drags through `dragReducer`, writes sibling shifts and the preview, announces,
|
|
286
|
+
* and calls `onDrop` with a key-based result. One list zone and moves out of the box;
|
|
287
|
+
* `plugins` add the rest.
|
|
288
|
+
*/
|
|
289
|
+
function createDnd(options) {
|
|
290
|
+
let opts = options;
|
|
291
|
+
let plugins = [];
|
|
292
|
+
let hooks = {};
|
|
293
|
+
let reduce = dragReducer(opts);
|
|
294
|
+
const doc = options.document ?? (typeof document === "undefined" ? void 0 : document);
|
|
295
|
+
const frame = options.frame ?? frameLoopOf(doc);
|
|
296
|
+
const view = doc?.defaultView ?? globalThis;
|
|
297
|
+
const store = createStore((state, input) => reduce(state, input), idle);
|
|
298
|
+
const writer = createWriter((options.channel ?? styleChannel)());
|
|
299
|
+
const registry = createRegistry(writer.rect);
|
|
300
|
+
const handles = /* @__PURE__ */ new Map();
|
|
301
|
+
/** The key each bound element starts drags of. */
|
|
302
|
+
const bound = /* @__PURE__ */ new Map();
|
|
303
|
+
let overlayEl = null;
|
|
304
|
+
let tracker = null;
|
|
305
|
+
let region = null;
|
|
306
|
+
let session = null;
|
|
307
|
+
let pressed = null;
|
|
308
|
+
let previous = idle;
|
|
309
|
+
let disposed = false;
|
|
310
|
+
function configure(next) {
|
|
311
|
+
opts = next;
|
|
312
|
+
plugins = next.plugins ?? [];
|
|
313
|
+
hooks = Object.assign({}, ...plugins);
|
|
314
|
+
reduce = dragReducer({
|
|
315
|
+
...next,
|
|
316
|
+
plugins
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
configure(options);
|
|
320
|
+
const dispatch = (event) => store.dispatch(event);
|
|
321
|
+
const dragging = () => {
|
|
322
|
+
const state = store.get();
|
|
323
|
+
return state.phase === "dragging" ? state : null;
|
|
324
|
+
};
|
|
325
|
+
const recordOf = (zoneId) => recordIn(registry.zones, zoneId);
|
|
326
|
+
/** A zone's commit signal: what its plugin says, else its items. */
|
|
327
|
+
const commitSignal = (record) => hooks.signal?.(record.spec) ?? record.spec.items;
|
|
328
|
+
const changed = (s, state) => {
|
|
329
|
+
for (const plugin of plugins) plugin.change?.(s, state);
|
|
330
|
+
};
|
|
331
|
+
store.subscribe(() => {
|
|
332
|
+
const next = store.get();
|
|
333
|
+
const s = session;
|
|
334
|
+
if (s === null) return;
|
|
335
|
+
const before = previous;
|
|
336
|
+
previous = next;
|
|
337
|
+
s.full ||= before.phase !== next.phase || before.phase === "dragging" && next.phase === "dragging" && (before.target !== next.target || before.operation !== next.operation || before.layout !== next.layout);
|
|
338
|
+
frame.request();
|
|
339
|
+
const messages = opts.messages ?? defaultMessages;
|
|
340
|
+
if (messages !== false) s.message = announcement(before, next, messages, opts.label ?? String) ?? s.message;
|
|
341
|
+
if (before.phase === "dragging" && next.phase === "dropping" && !s.dropped) {
|
|
342
|
+
s.dropped = true;
|
|
343
|
+
commit();
|
|
344
|
+
}
|
|
345
|
+
changed(s, next.phase === "dragging" ? next : null);
|
|
346
|
+
});
|
|
347
|
+
function canLift(key, el) {
|
|
348
|
+
const item = registry.items.get(key);
|
|
349
|
+
const handle = handles.get(key);
|
|
350
|
+
return dragging() === null && item !== void 0 && !item.disabled && (handle === void 0 || handle === el);
|
|
351
|
+
}
|
|
352
|
+
/** Makes `el` start drags of `key` by pointer and keyboard. Returns the detach. */
|
|
353
|
+
function bind(el, key) {
|
|
354
|
+
if (opts.messages !== false) region ??= joinLiveRegion(el.ownerDocument);
|
|
355
|
+
tracker ??= createPointerTracker({
|
|
356
|
+
document: el.ownerDocument,
|
|
357
|
+
activation(event) {
|
|
358
|
+
const handle = event.currentTarget;
|
|
359
|
+
const key = bound.get(handle);
|
|
360
|
+
if (key === void 0 || !canLift(key, handle)) return null;
|
|
361
|
+
pressed = {
|
|
362
|
+
key,
|
|
363
|
+
modifiers: modifiersOf(event),
|
|
364
|
+
at: {
|
|
365
|
+
x: event.clientX,
|
|
366
|
+
y: event.clientY
|
|
367
|
+
}
|
|
368
|
+
};
|
|
369
|
+
const { activation } = opts;
|
|
370
|
+
return typeof activation === "function" ? activation(event) : activation;
|
|
371
|
+
},
|
|
372
|
+
onSignal(signal) {
|
|
373
|
+
const at = {
|
|
374
|
+
x: signal.x,
|
|
375
|
+
y: signal.y
|
|
376
|
+
};
|
|
377
|
+
if (signal.phase === "activate") {
|
|
378
|
+
if (pressed === null) return;
|
|
379
|
+
lift(pressed.key, pressed.at, pressed.modifiers);
|
|
380
|
+
if (session !== null && (at.x !== pressed.at.x || at.y !== pressed.at.y)) {
|
|
381
|
+
session.pointer = at;
|
|
382
|
+
frame.request();
|
|
383
|
+
}
|
|
384
|
+
} else if (signal.phase === "move") {
|
|
385
|
+
if (session === null) return;
|
|
386
|
+
session.pointer = at;
|
|
387
|
+
frame.request();
|
|
388
|
+
} else if (signal.phase !== "press") end(signal.phase === "release" ? "drop" : "cancel");
|
|
389
|
+
}
|
|
390
|
+
});
|
|
391
|
+
bound.set(el, key);
|
|
392
|
+
const offKey = listen(el, "keydown", (event) => {
|
|
393
|
+
if (!isLiftKey(event.key) || !canLift(key, el)) return;
|
|
394
|
+
event.preventDefault();
|
|
395
|
+
lift(key, null, modifiersOf(event));
|
|
396
|
+
});
|
|
397
|
+
const detach = tracker.attach(el);
|
|
398
|
+
return () => {
|
|
399
|
+
if (bound.get(el) === key) bound.delete(el);
|
|
400
|
+
offKey();
|
|
401
|
+
detach();
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
/** Starts a drag of `key`: at a pointer position, or from the keyboard when `at` is null. */
|
|
405
|
+
function lift(key, at, modifiers) {
|
|
406
|
+
if (disposed || dragging() !== null) return;
|
|
407
|
+
if (session !== null) finish();
|
|
408
|
+
const item = registry.items.get(key);
|
|
409
|
+
if (item === void 0) return;
|
|
410
|
+
for (const record of registry.zones) {
|
|
411
|
+
record.dirty = true;
|
|
412
|
+
record.scrollers = null;
|
|
413
|
+
}
|
|
414
|
+
const layout = registry.layout(null, hooks);
|
|
415
|
+
const rect = writer.rect(item.el);
|
|
416
|
+
const preview = (opts.preview ?? inPlacePreview)(item.el, overlayEl, rect);
|
|
417
|
+
const s = {
|
|
418
|
+
frame,
|
|
419
|
+
state: dragging,
|
|
420
|
+
zones: registry.zones,
|
|
421
|
+
memo: {},
|
|
422
|
+
preview,
|
|
423
|
+
primary: key,
|
|
424
|
+
stops: [],
|
|
425
|
+
base: preview.base?.() ?? rect,
|
|
426
|
+
full: true,
|
|
427
|
+
stage: WAITING,
|
|
428
|
+
snapshot: []
|
|
429
|
+
};
|
|
430
|
+
session = s;
|
|
431
|
+
dispatch({
|
|
432
|
+
type: "start",
|
|
433
|
+
ids: hooks.ids === void 0 ? [key] : [...hooks.ids()],
|
|
434
|
+
primary: key,
|
|
435
|
+
input: at === null ? "keyboard" : "pointer",
|
|
436
|
+
pointer: at ?? {
|
|
437
|
+
x: rect.x + rect.width / 2,
|
|
438
|
+
y: rect.y + rect.height / 2
|
|
439
|
+
},
|
|
440
|
+
layout,
|
|
441
|
+
modifiers
|
|
442
|
+
});
|
|
443
|
+
if (dragging() === null) return finish();
|
|
444
|
+
const target = item.el.ownerDocument;
|
|
445
|
+
s.stops.push(frame.job({
|
|
446
|
+
kind: "read",
|
|
447
|
+
beat: "input",
|
|
448
|
+
name: "dnd-read",
|
|
449
|
+
run: read
|
|
450
|
+
}), frame.job({
|
|
451
|
+
kind: "compute",
|
|
452
|
+
beat: "intent",
|
|
453
|
+
name: "dnd-intent",
|
|
454
|
+
run: compute
|
|
455
|
+
}), frame.job({
|
|
456
|
+
kind: "write",
|
|
457
|
+
beat: "write",
|
|
458
|
+
name: "dnd-write",
|
|
459
|
+
run: write
|
|
460
|
+
}), frame.keepAlive(), listen(target, "keydown", keyboard, true), listen(target, "keyup", keyboard, true), listen(target.defaultView ?? view, "blur", () => end("cancel")), listen(target.defaultView ?? view, "resize", touched));
|
|
461
|
+
for (const plugin of plugins) {
|
|
462
|
+
const stop = plugin.lift?.(s, at !== null);
|
|
463
|
+
if (stop !== void 0) s.stops.push(stop);
|
|
464
|
+
}
|
|
465
|
+
frame.request();
|
|
466
|
+
}
|
|
467
|
+
function keyboard(event) {
|
|
468
|
+
const state = dragging();
|
|
469
|
+
if (state === null) return;
|
|
470
|
+
dispatch({
|
|
471
|
+
type: "modifiers",
|
|
472
|
+
modifiers: modifiersOf(event)
|
|
473
|
+
});
|
|
474
|
+
if (event.type !== "keydown" || state.input !== "keyboard") return;
|
|
475
|
+
const zone = state.layout.zones.find((z) => z.id === (state.hit?.zoneId ?? state.source.zoneId));
|
|
476
|
+
const intent = keyboardIntent(event.key, zone?.axis ?? "y", zone?.kind === "tree", opts.rtl === true);
|
|
477
|
+
if (intent === null) return;
|
|
478
|
+
event.preventDefault();
|
|
479
|
+
event.stopPropagation();
|
|
480
|
+
if (intent.type !== "step") return end(intent.type);
|
|
481
|
+
dispatch({
|
|
482
|
+
type: "step",
|
|
483
|
+
direction: intent.direction
|
|
484
|
+
});
|
|
485
|
+
if (session !== null) session.reveal = true;
|
|
486
|
+
}
|
|
487
|
+
/** Read pass: zone geometry, the preview base, a reveal's scroll; after a drop, the commit and the settle's reads. */
|
|
488
|
+
function read() {
|
|
489
|
+
const s = session;
|
|
490
|
+
if (s === null) return;
|
|
491
|
+
const state = store.get();
|
|
492
|
+
if (state.phase === "dragging") {
|
|
493
|
+
s.layout = registry.layout(state.layout, hooks);
|
|
494
|
+
s.base = s.preview.base?.() ?? writer.rect(s.preview.el);
|
|
495
|
+
if (s.reveal) reveal(s, state);
|
|
496
|
+
return;
|
|
497
|
+
}
|
|
498
|
+
for (const [record, before] of s.snapshot) {
|
|
499
|
+
const now = commitSignal(record);
|
|
500
|
+
if (now !== before && !(Array.isArray(now) && sameItems(now, before))) commit();
|
|
501
|
+
}
|
|
502
|
+
if (s.stage !== COMMITTED) return;
|
|
503
|
+
s.stage = MEASURED;
|
|
504
|
+
s.settle?.read();
|
|
505
|
+
}
|
|
506
|
+
/** Read pass part of a keyboard step: the scroll that brings the target into view, run in the write pass. */
|
|
507
|
+
function reveal(s, state) {
|
|
508
|
+
const { hit } = state;
|
|
509
|
+
const record = recordOf(hit?.zoneId);
|
|
510
|
+
const plugged = record && hit?.item && hooks.reveal?.(record.spec, hit.item.index);
|
|
511
|
+
if (plugged) {
|
|
512
|
+
s.reveal = false;
|
|
513
|
+
s.scroll = plugged;
|
|
514
|
+
return;
|
|
515
|
+
}
|
|
516
|
+
const zone = state.layout.zones.find((z) => z.id === hit?.zoneId);
|
|
517
|
+
const placeholder = placeholderOf(state);
|
|
518
|
+
const scroller = record === void 0 ? void 0 : scrollersOf(record)[0];
|
|
519
|
+
if (zone === void 0 || placeholder === null || scroller === void 0) return;
|
|
520
|
+
s.reveal = false;
|
|
521
|
+
const y = zone.axis === "y";
|
|
522
|
+
const box = scroller.viewport();
|
|
523
|
+
const start = (y ? zone.origin.y : zone.origin.x) + placeholder.start;
|
|
524
|
+
const end = start + placeholder.size;
|
|
525
|
+
const low = y ? box.y : box.x;
|
|
526
|
+
const high = low + (y ? box.height : box.width);
|
|
527
|
+
const by = start < low ? start - low : end > high ? end - high : 0;
|
|
528
|
+
if (by !== 0) s.scroll = () => scroller.scrollBy(y ? {
|
|
529
|
+
x: 0,
|
|
530
|
+
y: by
|
|
531
|
+
} : {
|
|
532
|
+
x: by,
|
|
533
|
+
y: 0
|
|
534
|
+
});
|
|
535
|
+
}
|
|
536
|
+
function compute() {
|
|
537
|
+
const s = session;
|
|
538
|
+
if (s === null) return;
|
|
539
|
+
if (s.layout && dragging() !== null) dispatch({
|
|
540
|
+
type: "layout",
|
|
541
|
+
layout: s.layout
|
|
542
|
+
});
|
|
543
|
+
s.layout = null;
|
|
544
|
+
if (s.pointer) dispatch({
|
|
545
|
+
type: "move",
|
|
546
|
+
pointer: s.pointer
|
|
547
|
+
});
|
|
548
|
+
s.pointer = null;
|
|
549
|
+
}
|
|
550
|
+
/** Write pass: shifts and preview while dragging; once measured after a drop, the reset and the settle. */
|
|
551
|
+
function write() {
|
|
552
|
+
const s = session;
|
|
553
|
+
if (s === null) return;
|
|
554
|
+
const state = store.get();
|
|
555
|
+
if (state.phase === "dragging") {
|
|
556
|
+
const { pointer, source } = state;
|
|
557
|
+
const at = {
|
|
558
|
+
x: pointer.x - source.grab.x - s.base.x,
|
|
559
|
+
y: pointer.y - source.grab.y - s.base.y
|
|
560
|
+
};
|
|
561
|
+
writer.drag(state, registry.items, registry.zoneElements(), {
|
|
562
|
+
el: s.preview.el,
|
|
563
|
+
at
|
|
564
|
+
}, s.full);
|
|
565
|
+
s.full = false;
|
|
566
|
+
s.scroll?.();
|
|
567
|
+
s.scroll = null;
|
|
568
|
+
} else if (s.stage === MEASURED) {
|
|
569
|
+
s.stage = SETTLING;
|
|
570
|
+
writer.reset();
|
|
571
|
+
if (s.preview.owned) s.preview.el.remove();
|
|
572
|
+
else if (s.preview.el === overlayEl) writer.channel.hide(s.preview.el, true);
|
|
573
|
+
const done = s.settle?.write();
|
|
574
|
+
const settled = () => {
|
|
575
|
+
if (session === s) finish();
|
|
576
|
+
};
|
|
577
|
+
if (done === void 0) finish();
|
|
578
|
+
else done.then(settled, settled);
|
|
579
|
+
}
|
|
580
|
+
if (s.message) region?.say(s.message);
|
|
581
|
+
s.message = null;
|
|
582
|
+
}
|
|
583
|
+
/** Ends the drag: `drop` for a result, otherwise a cancel. The settle strategy reads painted rects first. */
|
|
584
|
+
function end(type) {
|
|
585
|
+
const s = session;
|
|
586
|
+
if (s === null || dragging() === null) return;
|
|
587
|
+
if (s.pointer && type === "drop") dispatch({
|
|
588
|
+
type: "move",
|
|
589
|
+
pointer: s.pointer
|
|
590
|
+
});
|
|
591
|
+
s.pointer = null;
|
|
592
|
+
const state = dragging();
|
|
593
|
+
s.dropped = true;
|
|
594
|
+
dispatch({ type });
|
|
595
|
+
const after = store.get();
|
|
596
|
+
const result = after.phase === "dropping" ? after.result : null;
|
|
597
|
+
if (state.input === "pointer") suppressClick(s.preview.el.ownerDocument);
|
|
598
|
+
s.settle = opts.settle?.({
|
|
599
|
+
state,
|
|
600
|
+
result,
|
|
601
|
+
items: registry.items,
|
|
602
|
+
preview: s.preview.el,
|
|
603
|
+
transform: writer.channel.inverse,
|
|
604
|
+
translate: (el, by) => writer.channel.move(el, by),
|
|
605
|
+
rect: writer.rect,
|
|
606
|
+
frame
|
|
607
|
+
});
|
|
608
|
+
if (result === null) return commit();
|
|
609
|
+
s.snapshot = [...registry.zones].map((record) => [record, commitSignal(record)]);
|
|
610
|
+
opts.onDrop(result);
|
|
611
|
+
s.timer = view.setTimeout(commit, opts.commitTimeoutMs ?? 250);
|
|
612
|
+
}
|
|
613
|
+
function commit() {
|
|
614
|
+
const s = session;
|
|
615
|
+
if (s === null || s.stage !== WAITING) return;
|
|
616
|
+
view.clearTimeout(s.timer);
|
|
617
|
+
s.stage = COMMITTED;
|
|
618
|
+
s.snapshot = [];
|
|
619
|
+
frame.request();
|
|
620
|
+
}
|
|
621
|
+
/** Tears the drag down at once: restores styles, removes the clone, undoes plugin lifts, settles. */
|
|
622
|
+
function finish() {
|
|
623
|
+
const s = session;
|
|
624
|
+
if (s === null) return;
|
|
625
|
+
session = null;
|
|
626
|
+
s.settle?.cancel?.();
|
|
627
|
+
view.clearTimeout(s.timer);
|
|
628
|
+
changed(s, null);
|
|
629
|
+
for (const stop of s.stops.splice(0)) stop();
|
|
630
|
+
writer.reset();
|
|
631
|
+
if (s.preview.owned) s.preview.el.remove();
|
|
632
|
+
if (dragging() !== null) dispatch({ type: "cancel" });
|
|
633
|
+
dispatch({ type: "settled" });
|
|
634
|
+
previous = store.get();
|
|
635
|
+
const doc = s.preview.el.ownerDocument;
|
|
636
|
+
if (doc.activeElement === null || doc.activeElement === doc.body) (handles.get(s.primary) ?? registry.items.get(s.primary)?.el)?.focus({ preventScroll: true });
|
|
637
|
+
}
|
|
638
|
+
/** A pointer drag ends with a click on the element it started from; that click is not the host's. */
|
|
639
|
+
function suppressClick(target) {
|
|
640
|
+
const off = listen(target, "click", (event) => {
|
|
641
|
+
event.stopPropagation();
|
|
642
|
+
event.preventDefault();
|
|
643
|
+
off();
|
|
644
|
+
}, true);
|
|
645
|
+
view.setTimeout(off, 0);
|
|
646
|
+
}
|
|
647
|
+
/** Detaches now, or when the drag ends if `key` is being dragged (the tracker owns its press). */
|
|
648
|
+
function deferWhileDragging(key, detach) {
|
|
649
|
+
if (session?.primary === key) session.stops.push(detach);
|
|
650
|
+
else detach();
|
|
651
|
+
}
|
|
652
|
+
function touched() {
|
|
653
|
+
if (session === null) return;
|
|
654
|
+
for (const record of registry.zones) record.dirty = true;
|
|
655
|
+
session.full = true;
|
|
656
|
+
frame.request();
|
|
657
|
+
}
|
|
658
|
+
return {
|
|
659
|
+
state: {
|
|
660
|
+
get: store.get,
|
|
661
|
+
subscribe: store.subscribe
|
|
662
|
+
},
|
|
663
|
+
instructionsId: INSTRUCTIONS_ID,
|
|
664
|
+
zone(spec) {
|
|
665
|
+
if (registry.zones.size > 0 && hooks.zoneAt === void 0) throw new Error("createDnd: a second zone needs boardZones()");
|
|
666
|
+
const record = {
|
|
667
|
+
spec,
|
|
668
|
+
el: null,
|
|
669
|
+
dirty: true,
|
|
670
|
+
track: null,
|
|
671
|
+
zone: null,
|
|
672
|
+
inputs: [],
|
|
673
|
+
scrollers: null
|
|
674
|
+
};
|
|
675
|
+
registry.zones.add(record);
|
|
676
|
+
let detach = null;
|
|
677
|
+
return {
|
|
678
|
+
attach(el) {
|
|
679
|
+
detach?.();
|
|
680
|
+
const off = registry.attach(record, el, () => session !== null && frame.request(), hooks);
|
|
681
|
+
detach = off;
|
|
682
|
+
touched();
|
|
683
|
+
return () => {
|
|
684
|
+
off();
|
|
685
|
+
if (detach === off) detach = null;
|
|
686
|
+
touched();
|
|
687
|
+
};
|
|
688
|
+
},
|
|
689
|
+
update(next) {
|
|
690
|
+
const before = record.spec.items;
|
|
691
|
+
const items = next.items;
|
|
692
|
+
if (before === void 0 || items === void 0 || !sameItems(before, items)) record.dirty = true;
|
|
693
|
+
record.spec = next;
|
|
694
|
+
touched();
|
|
695
|
+
},
|
|
696
|
+
dispose() {
|
|
697
|
+
detach?.();
|
|
698
|
+
registry.zones.delete(record);
|
|
699
|
+
touched();
|
|
700
|
+
}
|
|
701
|
+
};
|
|
702
|
+
},
|
|
703
|
+
item(key, el, itemOptions) {
|
|
704
|
+
const record = {
|
|
705
|
+
el,
|
|
706
|
+
disabled: itemOptions?.disabled === true
|
|
707
|
+
};
|
|
708
|
+
registry.items.set(key, record);
|
|
709
|
+
const unbind = bind(el, key);
|
|
710
|
+
touched();
|
|
711
|
+
return () => {
|
|
712
|
+
if (registry.items.get(key) === record) registry.items.delete(key);
|
|
713
|
+
deferWhileDragging(key, unbind);
|
|
714
|
+
touched();
|
|
715
|
+
};
|
|
716
|
+
},
|
|
717
|
+
handle(key, el) {
|
|
718
|
+
handles.set(key, el);
|
|
719
|
+
const unbind = bind(el, key);
|
|
720
|
+
return () => {
|
|
721
|
+
if (handles.get(key) === el) handles.delete(key);
|
|
722
|
+
deferWhileDragging(key, unbind);
|
|
723
|
+
};
|
|
724
|
+
},
|
|
725
|
+
overlay(el) {
|
|
726
|
+
overlayEl = el;
|
|
727
|
+
return () => {
|
|
728
|
+
if (overlayEl === el) overlayEl = null;
|
|
729
|
+
};
|
|
730
|
+
},
|
|
731
|
+
update: configure,
|
|
732
|
+
cancel: () => end("cancel"),
|
|
733
|
+
dispose() {
|
|
734
|
+
if (disposed) return;
|
|
735
|
+
disposed = true;
|
|
736
|
+
finish();
|
|
737
|
+
tracker?.dispose();
|
|
738
|
+
region?.release();
|
|
739
|
+
registry.zones.clear();
|
|
740
|
+
registry.items.clear();
|
|
741
|
+
}
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
// src/live/plugins.ts
|
|
745
|
+
/**
|
|
746
|
+
* Edge autoscroll of the zone under the pointer, or the zone the drag started in, during a
|
|
747
|
+
* pointer drag: the nearest scroller on each axis moves, faster closer to its edge. Keyboard
|
|
748
|
+
* drags scroll their target into view without it.
|
|
749
|
+
*/
|
|
750
|
+
function autoscroll(policy) {
|
|
751
|
+
return { lift: (drag, pointer) => pointer ? startAutoscroll({
|
|
752
|
+
frame: drag.frame,
|
|
753
|
+
...policy === void 0 ? {} : { policy },
|
|
754
|
+
pointer: () => drag.state()?.pointer ?? null,
|
|
755
|
+
scrollers: () => {
|
|
756
|
+
const state = drag.state();
|
|
757
|
+
const record = recordIn(drag.zones, state?.hit?.zoneId ?? state?.source.zoneId);
|
|
758
|
+
return record === void 0 ? [] : scrollersOf(record);
|
|
759
|
+
}
|
|
760
|
+
}) : void 0 };
|
|
761
|
+
}
|
|
762
|
+
/**
|
|
763
|
+
* Re-measures a zone whenever its element resizes, through the shared size observer: content
|
|
764
|
+
* that reflows mid-drag without a render (an image loading, a font swapping) moves the gaps
|
|
765
|
+
* with it. Without it a zone re-measures at lift, on every render and on a window resize.
|
|
766
|
+
*/
|
|
767
|
+
function observeZones() {
|
|
768
|
+
return { observe: (el, stale) => observeSize(el, stale) };
|
|
769
|
+
}
|
|
770
|
+
/**
|
|
771
|
+
* Zones over a virtualizer: a spec's `source` replaces `items`. Unmounted rows are targets, the
|
|
772
|
+
* carried rows stay mounted for the drag (`retain`), a keyboard step reveals its target row,
|
|
773
|
+
* and the source's track version is the commit signal.
|
|
774
|
+
*/
|
|
775
|
+
function virtualZones() {
|
|
776
|
+
return {
|
|
777
|
+
measure: ({ source }) => source && {
|
|
778
|
+
origin: source.origin(),
|
|
779
|
+
track: source.track()
|
|
780
|
+
},
|
|
781
|
+
signal: ({ source }) => source?.track().version,
|
|
782
|
+
reveal: ({ source }, index) => source && (() => source.reveal(index)),
|
|
783
|
+
lift(drag) {
|
|
784
|
+
const carried = drag.state()?.source.carried ?? [];
|
|
785
|
+
const stops = [];
|
|
786
|
+
for (const { spec: { source } } of drag.zones) {
|
|
787
|
+
const track = source?.track();
|
|
788
|
+
const keys = carried.filter((key) => track !== void 0 && track.indexOf(key) >= 0);
|
|
789
|
+
if (keys.length > 0) stops.push(source.retain(keys));
|
|
790
|
+
}
|
|
791
|
+
return () => stops.forEach((stop) => stop());
|
|
792
|
+
}
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
/**
|
|
796
|
+
* Tree zones: a spec's `tree` makes the zone an outline, with `outline` from
|
|
797
|
+
* `@bevelo/dnd/tree` (or a host's own) doing the projection. A lift carries the row's visible
|
|
798
|
+
* descendants, horizontal travel and the cross-axis arrows set the depth, and `tree.expand`
|
|
799
|
+
* hears the row the preview rests on.
|
|
800
|
+
*/
|
|
801
|
+
function treeZones(outline) {
|
|
802
|
+
return {
|
|
803
|
+
build(base, spec) {
|
|
804
|
+
const tree = spec().tree;
|
|
805
|
+
return tree && outline.zone(base, () => spec().tree ?? tree);
|
|
806
|
+
},
|
|
807
|
+
change(drag, state) {
|
|
808
|
+
const memo = drag.memo;
|
|
809
|
+
const key = state?.hit?.item?.key ?? null;
|
|
810
|
+
if (key === memo.key) return;
|
|
811
|
+
memo.expand?.(null);
|
|
812
|
+
memo.key = key;
|
|
813
|
+
memo.expand = recordIn(drag.zones, state?.hit?.zoneId)?.spec.tree?.expand;
|
|
814
|
+
if (key !== null) memo.expand?.(key);
|
|
815
|
+
}
|
|
816
|
+
};
|
|
817
|
+
}
|
|
818
|
+
/**
|
|
819
|
+
* Selection: `selected` is read at lift, and a primary key in it carries the whole set, in zone
|
|
820
|
+
* then track order. A pressed key outside it lifts alone.
|
|
821
|
+
*/
|
|
822
|
+
function selection(selected) {
|
|
823
|
+
return {
|
|
824
|
+
ids: selected,
|
|
825
|
+
carry: carrySelection
|
|
826
|
+
};
|
|
827
|
+
}
|
|
828
|
+
// src/live/expand.ts
|
|
829
|
+
/**
|
|
830
|
+
* A tree zone's `expand`: calls `onExpand` once the preview has rested on the same row for
|
|
831
|
+
* `afterMs`. Any other row, or none, restarts the wait.
|
|
832
|
+
*/
|
|
833
|
+
function expandAfter(afterMs, onExpand) {
|
|
834
|
+
let timer;
|
|
835
|
+
return (key) => {
|
|
836
|
+
clearTimeout(timer);
|
|
837
|
+
if (key !== null) timer = setTimeout(() => onExpand(key), afterMs);
|
|
838
|
+
};
|
|
839
|
+
}
|
|
840
|
+
export { treeZones as a, clonePreview as c, selection as i, inPlacePreview as l, autoscroll as n, virtualZones as o, observeZones as r, createDnd as s, expandAfter as t, overlayPreview as u };
|