@pyreon/dnd 0.51.0 → 0.52.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/README.md +32 -22
- package/lib/index.js +297 -134
- package/lib/index.js.map +1 -1
- package/lib/types/index.d.ts +88 -41
- package/native/kotlin/com/pyreon/runtime/PyreonSortable.kt +198 -0
- package/native/kotlin/com/pyreon/runtime/PyreonSortableModifier.kt +59 -0
- package/native/swift/PyreonSortable.swift +256 -0
- package/package.json +31 -14
- package/lib/analysis/index.js.html +0 -5406
package/lib/index.js
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
|
-
import { batch, createSelector, isServer, onCleanup, registerSingleton, signal } from "@pyreon/reactivity";
|
|
2
|
-
import { draggable, dropTargetForElements, monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/element
|
|
3
|
-
import { centerUnderPointer } from "@atlaskit/pragmatic-drag-and-drop/
|
|
4
|
-
import { pointerOutsideOfPreview } from "@atlaskit/pragmatic-drag-and-drop/
|
|
5
|
-
import { preserveOffsetOnSource } from "@atlaskit/pragmatic-drag-and-drop/
|
|
6
|
-
import { setCustomNativeDragPreview } from "@atlaskit/pragmatic-drag-and-drop/
|
|
1
|
+
import { batch, createSelector, effect, isServer, onCleanup, registerSingleton, signal, untrack } from "@pyreon/reactivity";
|
|
2
|
+
import { draggable, dropTargetForElements, monitorForElements } from "@atlaskit/pragmatic-drag-and-drop/adapter/element-adapter";
|
|
3
|
+
import { centerUnderPointer } from "@atlaskit/pragmatic-drag-and-drop/utils/center-under-pointer";
|
|
4
|
+
import { pointerOutsideOfPreview } from "@atlaskit/pragmatic-drag-and-drop/utils/pointer-outside-of-preview";
|
|
5
|
+
import { preserveOffsetOnSource } from "@atlaskit/pragmatic-drag-and-drop/utils/preserve-offset-on-source";
|
|
6
|
+
import { setCustomNativeDragPreview } from "@atlaskit/pragmatic-drag-and-drop/utils/set-custom-native-drag-preview";
|
|
7
7
|
import { attachClosestEdge, extractClosestEdge } from "@atlaskit/pragmatic-drag-and-drop-hitbox/closest-edge";
|
|
8
|
-
import { dropTargetForExternal
|
|
9
|
-
import {
|
|
10
|
-
import {
|
|
8
|
+
import { dropTargetForExternal } from "@atlaskit/pragmatic-drag-and-drop/adapter/drop-target-for-external";
|
|
9
|
+
import { monitorForExternal } from "@atlaskit/pragmatic-drag-and-drop/adapter/monitor-for-external";
|
|
10
|
+
import { containsFiles } from "@atlaskit/pragmatic-drag-and-drop/utils/contains-files";
|
|
11
|
+
import { getFiles } from "@atlaskit/pragmatic-drag-and-drop/utils/get-files";
|
|
12
|
+
import { combine } from "@atlaskit/pragmatic-drag-and-drop/utils/combine";
|
|
11
13
|
import { autoScrollForElements } from "@atlaskit/pragmatic-drag-and-drop-auto-scroll/element";
|
|
12
14
|
import { announce } from "@pyreon/a11y";
|
|
13
15
|
|
|
14
16
|
//#region package.json
|
|
15
17
|
var name = "@pyreon/dnd";
|
|
16
|
-
var version = "0.
|
|
18
|
+
var version = "0.52.0";
|
|
17
19
|
|
|
18
20
|
//#endregion
|
|
19
21
|
//#region src/use-drag-monitor.ts
|
|
@@ -45,7 +47,7 @@ function useDragMonitor(options) {
|
|
|
45
47
|
const dragData = signal(null);
|
|
46
48
|
/* v8 ignore next 4 — opt-in canMonitor predicate; both arms structurally exercised but counted as one */
|
|
47
49
|
const canMonitorFn = options?.canMonitor ? ({ source }) => options.canMonitor?.(source.data) ?? true : null;
|
|
48
|
-
|
|
50
|
+
const cleanup = monitorForElements({
|
|
49
51
|
...canMonitorFn ? { canMonitor: canMonitorFn } : {},
|
|
50
52
|
onDragStart: ({ source }) => {
|
|
51
53
|
batch(() => {
|
|
@@ -62,13 +64,78 @@ function useDragMonitor(options) {
|
|
|
62
64
|
const targetData = location.current.dropTargets[0]?.data ?? {};
|
|
63
65
|
options?.onDrop?.(source.data, targetData);
|
|
64
66
|
}
|
|
65
|
-
})
|
|
67
|
+
});
|
|
68
|
+
onCleanup(cleanup);
|
|
66
69
|
return {
|
|
67
70
|
isDragging,
|
|
68
71
|
dragData
|
|
69
72
|
};
|
|
70
73
|
}
|
|
71
74
|
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/element-binding.ts
|
|
77
|
+
/**
|
|
78
|
+
* Bind a pdnd registration to an element that may mount LATER or be SWAPPED.
|
|
79
|
+
*
|
|
80
|
+
* The hooks used to read `options.element()` exactly once, on a microtask
|
|
81
|
+
* after the hook ran. An element that mounted after that (behind `<Show>`, a
|
|
82
|
+
* lazy branch, a data-dependent render) was never registered, and a swapped
|
|
83
|
+
* element kept the registration on the detached old node — both silently.
|
|
84
|
+
*
|
|
85
|
+
* Two ways in, both re-resolving:
|
|
86
|
+
* - `ref` — attach it as the element's `ref`; every (el | null) call moves
|
|
87
|
+
* the registration, so mount order no longer matters.
|
|
88
|
+
* - the `element` getter — still resolved on a microtask (so a plain
|
|
89
|
+
* `let el` assigned by a ref is populated), but inside an effect, so a
|
|
90
|
+
* getter that reads a SIGNAL re-registers when that signal changes. A
|
|
91
|
+
* getter still `null` at that point (and no ref) gets a dev warning naming
|
|
92
|
+
* the fix, instead of a drop zone that never works.
|
|
93
|
+
*
|
|
94
|
+
* Cleanup contract: identity-based — one live registration, disposed on
|
|
95
|
+
* re-register, on `ref(null)`, and when the owning scope is cleaned up.
|
|
96
|
+
*/
|
|
97
|
+
function bindElement(hookName, getter, register) {
|
|
98
|
+
let current = null;
|
|
99
|
+
let cleanup;
|
|
100
|
+
let disposed = false;
|
|
101
|
+
let refUsed = false;
|
|
102
|
+
let stopGetter;
|
|
103
|
+
const attach = (el) => {
|
|
104
|
+
if (disposed || el === current) return;
|
|
105
|
+
if (cleanup) {
|
|
106
|
+
cleanup();
|
|
107
|
+
cleanup = void 0;
|
|
108
|
+
}
|
|
109
|
+
current = el;
|
|
110
|
+
if (el) cleanup = register(el);
|
|
111
|
+
};
|
|
112
|
+
const ref = (el) => {
|
|
113
|
+
if (el) refUsed = true;
|
|
114
|
+
attach(el);
|
|
115
|
+
};
|
|
116
|
+
if (getter) queueMicrotask(() => {
|
|
117
|
+
if (disposed || refUsed) return;
|
|
118
|
+
let first = true;
|
|
119
|
+
const e = effect(() => {
|
|
120
|
+
const el = getter();
|
|
121
|
+
untrack(() => {
|
|
122
|
+
if (first && !el && !refUsed && process.env.NODE_ENV !== "production") console.warn(`[Pyreon] ${hookName}: element() returned null when the deferred setup ran, so nothing was registered. If the element mounts later (behind <Show>, a lazy branch), attach the returned \`ref\` to it instead (<div ref={ref}>), or read the element from a signal so the getter re-resolves.`);
|
|
123
|
+
first = false;
|
|
124
|
+
if (!refUsed) attach(el);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
stopGetter = () => e.dispose();
|
|
128
|
+
});
|
|
129
|
+
onCleanup(() => {
|
|
130
|
+
disposed = true;
|
|
131
|
+
stopGetter?.();
|
|
132
|
+
if (cleanup) cleanup();
|
|
133
|
+
cleanup = void 0;
|
|
134
|
+
current = null;
|
|
135
|
+
});
|
|
136
|
+
return { ref };
|
|
137
|
+
}
|
|
138
|
+
|
|
72
139
|
//#endregion
|
|
73
140
|
//#region src/use-draggable.ts
|
|
74
141
|
/**
|
|
@@ -90,36 +157,31 @@ function resolvePreviewOffset(offset, source, input) {
|
|
|
90
157
|
/**
|
|
91
158
|
* Make an element draggable with signal-driven state.
|
|
92
159
|
*
|
|
160
|
+
* Attach the returned `ref` to the element (works for elements that mount
|
|
161
|
+
* later or get swapped), or pass an `element` getter.
|
|
162
|
+
*
|
|
93
163
|
* @example
|
|
94
164
|
* ```tsx
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* const { isDragging } = useDraggable({
|
|
98
|
-
* element: () => cardEl,
|
|
165
|
+
* const { ref, isDragging } = useDraggable({
|
|
99
166
|
* data: { id: card.id, type: "card" },
|
|
100
167
|
* })
|
|
101
168
|
*
|
|
102
|
-
* <div ref={
|
|
169
|
+
* <div ref={ref} class={isDragging() ? "opacity-50" : ""}>
|
|
103
170
|
* {card.title}
|
|
104
171
|
* </div>
|
|
105
172
|
* ```
|
|
106
173
|
*/
|
|
107
174
|
function useDraggable(options) {
|
|
108
|
-
if (isServer) return {
|
|
175
|
+
if (isServer) return {
|
|
176
|
+
isDragging: () => false,
|
|
177
|
+
ref: () => {}
|
|
178
|
+
};
|
|
109
179
|
const isDragging = signal(false);
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
function setup() {
|
|
113
|
-
/* v8 ignore next — defensive disposed-during-setup guard; tested implicitly via lifecycle */
|
|
114
|
-
if (disposed) return;
|
|
115
|
-
/* v8 ignore next — defensive re-setup teardown; unreachable with single-shot queueMicrotask */
|
|
116
|
-
if (cleanup) cleanup();
|
|
117
|
-
const el = options.element();
|
|
118
|
-
if (!el) return;
|
|
119
|
-
const resolveData = () => typeof options.data === "function" ? options.data() : options.data;
|
|
180
|
+
const resolveData = () => typeof options.data === "function" ? options.data() : options.data;
|
|
181
|
+
function register(el) {
|
|
120
182
|
const handle = options.handle?.();
|
|
121
183
|
const preview = options.preview;
|
|
122
|
-
|
|
184
|
+
return draggable({
|
|
123
185
|
element: el,
|
|
124
186
|
...handle ? { dragHandle: handle } : {},
|
|
125
187
|
...preview ? { onGenerateDragPreview: ({ nativeSetDragImage, source, location }) => {
|
|
@@ -146,12 +208,11 @@ function useDraggable(options) {
|
|
|
146
208
|
}
|
|
147
209
|
});
|
|
148
210
|
}
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
}
|
|
154
|
-
return { isDragging };
|
|
211
|
+
const { ref } = bindElement("useDraggable", options.element, register);
|
|
212
|
+
return {
|
|
213
|
+
isDragging,
|
|
214
|
+
ref
|
|
215
|
+
};
|
|
155
216
|
}
|
|
156
217
|
|
|
157
218
|
//#endregion
|
|
@@ -159,18 +220,18 @@ function useDraggable(options) {
|
|
|
159
220
|
/**
|
|
160
221
|
* Make an element a drop target with signal-driven state.
|
|
161
222
|
*
|
|
223
|
+
* Attach the returned `ref` to the element (works for elements that mount
|
|
224
|
+
* later or get swapped), or pass an `element` getter.
|
|
225
|
+
*
|
|
162
226
|
* @example
|
|
163
227
|
* ```tsx
|
|
164
|
-
*
|
|
165
|
-
*
|
|
166
|
-
* const { isOver, overEdge } = useDroppable({
|
|
167
|
-
* element: () => zoneEl,
|
|
168
|
-
* onDrop: (data) => handleDrop(data),
|
|
228
|
+
* const { ref, isOver, overEdge } = useDroppable({
|
|
229
|
+
* onDrop: (data, { edge }) => handleDrop(data, edge),
|
|
169
230
|
* canDrop: (data) => data.type === "card",
|
|
170
231
|
* edges: ["top", "bottom"], // opt-in closest-edge detection
|
|
171
232
|
* })
|
|
172
233
|
*
|
|
173
|
-
* <div ref={
|
|
234
|
+
* <div ref={ref} class={isOver() ? "bg-blue-50" : ""}>
|
|
174
235
|
* Drop here
|
|
175
236
|
* </div>
|
|
176
237
|
* ```
|
|
@@ -178,29 +239,23 @@ function useDraggable(options) {
|
|
|
178
239
|
function useDroppable(options) {
|
|
179
240
|
if (isServer) return {
|
|
180
241
|
isOver: () => false,
|
|
181
|
-
overEdge: () => null
|
|
242
|
+
overEdge: () => null,
|
|
243
|
+
ref: () => {}
|
|
182
244
|
};
|
|
183
245
|
const isOver = signal(false);
|
|
184
246
|
const overEdge = signal(null);
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
if (!el) return;
|
|
194
|
-
const edges = options.edges;
|
|
195
|
-
const resolveData = () => {
|
|
196
|
-
if (!options.data) return {};
|
|
197
|
-
return typeof options.data === "function" ? options.data() : options.data;
|
|
198
|
-
};
|
|
199
|
-
cleanup = dropTargetForElements({
|
|
247
|
+
const edges = options.edges;
|
|
248
|
+
const hasEdges = !!edges && edges.length > 0;
|
|
249
|
+
const resolveData = () => {
|
|
250
|
+
if (!options.data) return {};
|
|
251
|
+
return typeof options.data === "function" ? options.data() : options.data;
|
|
252
|
+
};
|
|
253
|
+
function register(el) {
|
|
254
|
+
return dropTargetForElements({
|
|
200
255
|
element: el,
|
|
201
256
|
getData: ({ input, element }) => {
|
|
202
257
|
const data = resolveData();
|
|
203
|
-
if (
|
|
258
|
+
if (hasEdges) return attachClosestEdge(data, {
|
|
204
259
|
input,
|
|
205
260
|
element,
|
|
206
261
|
allowedEdges: edges
|
|
@@ -215,11 +270,11 @@ function useDroppable(options) {
|
|
|
215
270
|
onDragEnter: ({ source, self }) => {
|
|
216
271
|
batch(() => {
|
|
217
272
|
isOver.set(true);
|
|
218
|
-
if (
|
|
273
|
+
if (hasEdges) overEdge.set(extractClosestEdge(self.data));
|
|
219
274
|
});
|
|
220
275
|
options.onDragEnter?.(source.data);
|
|
221
276
|
},
|
|
222
|
-
...
|
|
277
|
+
...hasEdges ? { onDrag: ({ self }) => {
|
|
223
278
|
overEdge.set(extractClosestEdge(self.data));
|
|
224
279
|
} } : {},
|
|
225
280
|
onDragLeave: () => {
|
|
@@ -229,45 +284,52 @@ function useDroppable(options) {
|
|
|
229
284
|
});
|
|
230
285
|
options.onDragLeave?.();
|
|
231
286
|
},
|
|
232
|
-
onDrop: ({ source }) => {
|
|
287
|
+
onDrop: ({ source, self }) => {
|
|
288
|
+
const location = {
|
|
289
|
+
edge: hasEdges ? extractClosestEdge(self.data) ?? overEdge.peek() : null,
|
|
290
|
+
data: resolveData()
|
|
291
|
+
};
|
|
233
292
|
batch(() => {
|
|
234
293
|
isOver.set(false);
|
|
235
294
|
overEdge.set(null);
|
|
236
295
|
});
|
|
237
|
-
options.onDrop?.(source.data);
|
|
296
|
+
options.onDrop?.(source.data, location);
|
|
238
297
|
}
|
|
239
298
|
});
|
|
240
299
|
}
|
|
241
|
-
|
|
242
|
-
onCleanup(() => {
|
|
243
|
-
disposed = true;
|
|
244
|
-
if (cleanup) cleanup();
|
|
245
|
-
});
|
|
300
|
+
const { ref } = bindElement("useDroppable", options.element, register);
|
|
246
301
|
return {
|
|
247
302
|
isOver,
|
|
248
|
-
overEdge
|
|
303
|
+
overEdge,
|
|
304
|
+
ref
|
|
249
305
|
};
|
|
250
306
|
}
|
|
251
307
|
|
|
252
308
|
//#endregion
|
|
253
309
|
//#region src/use-file-drop.ts
|
|
310
|
+
function matchesAccept(file, accept) {
|
|
311
|
+
return accept.some((pattern) => {
|
|
312
|
+
if (pattern === "*" || pattern === "*/*") return true;
|
|
313
|
+
if (pattern.startsWith(".")) return file.name.toLowerCase().endsWith(pattern.toLowerCase());
|
|
314
|
+
if (pattern.endsWith("/*")) return file.type.startsWith(pattern.slice(0, -1));
|
|
315
|
+
return file.type === pattern;
|
|
316
|
+
});
|
|
317
|
+
}
|
|
254
318
|
/**
|
|
255
319
|
* File drop zone with signal-driven state.
|
|
256
320
|
* Uses the native file drag events via pragmatic-drag-and-drop.
|
|
257
321
|
*
|
|
258
322
|
* @example
|
|
259
323
|
* ```tsx
|
|
260
|
-
*
|
|
261
|
-
*
|
|
262
|
-
* const { isOver, isDraggingFiles } = useFileDrop({
|
|
263
|
-
* element: () => dropZone,
|
|
324
|
+
* const { ref, isOver, isDraggingFiles } = useFileDrop({
|
|
264
325
|
* accept: ["image/*", ".pdf"],
|
|
265
326
|
* maxFiles: 5,
|
|
266
327
|
* onDrop: (files) => upload(files),
|
|
328
|
+
* onReject: (files, reason) => toast(`${files.length} file(s) rejected: ${reason}`),
|
|
267
329
|
* })
|
|
268
330
|
*
|
|
269
331
|
* <div
|
|
270
|
-
* ref={
|
|
332
|
+
* ref={ref}
|
|
271
333
|
* class={isOver() ? "drop-active" : isDraggingFiles() ? "drop-ready" : ""}
|
|
272
334
|
* >
|
|
273
335
|
* Drop files here
|
|
@@ -277,25 +339,12 @@ function useDroppable(options) {
|
|
|
277
339
|
function useFileDrop(options) {
|
|
278
340
|
if (isServer) return {
|
|
279
341
|
isOver: () => false,
|
|
280
|
-
isDraggingFiles: () => false
|
|
342
|
+
isDraggingFiles: () => false,
|
|
343
|
+
ref: () => {}
|
|
281
344
|
};
|
|
282
345
|
const isOver = signal(false);
|
|
283
346
|
const isDraggingFiles = signal(false);
|
|
284
|
-
|
|
285
|
-
let disposed = false;
|
|
286
|
-
function matchesAccept(file, accept) {
|
|
287
|
-
return accept.some((pattern) => {
|
|
288
|
-
if (pattern.startsWith(".")) return file.name.toLowerCase().endsWith(pattern.toLowerCase());
|
|
289
|
-
if (pattern.endsWith("/*")) return file.type.startsWith(pattern.slice(0, -1));
|
|
290
|
-
return file.type === pattern;
|
|
291
|
-
});
|
|
292
|
-
}
|
|
293
|
-
function setup() {
|
|
294
|
-
/* v8 ignore next — defensive disposed-during-setup guard */
|
|
295
|
-
if (disposed) return;
|
|
296
|
-
if (cleanup) cleanup();
|
|
297
|
-
const el = options.element();
|
|
298
|
-
if (!el) return;
|
|
347
|
+
function register(el) {
|
|
299
348
|
const cleanups = [];
|
|
300
349
|
cleanups.push(monitorForExternal({
|
|
301
350
|
canMonitor: ({ source }) => containsFiles({ source }),
|
|
@@ -317,23 +366,30 @@ function useFileDrop(options) {
|
|
|
317
366
|
isDraggingFiles.set(false);
|
|
318
367
|
});
|
|
319
368
|
let files = getFiles({ source });
|
|
320
|
-
|
|
321
|
-
if (
|
|
369
|
+
const accept = options.accept;
|
|
370
|
+
if (accept && accept.length > 0) {
|
|
371
|
+
const rejected = files.filter((f) => !matchesAccept(f, accept));
|
|
372
|
+
if (rejected.length > 0) {
|
|
373
|
+
files = files.filter((f) => matchesAccept(f, accept));
|
|
374
|
+
options.onReject?.(rejected, "accept");
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
if (options.maxFiles && files.length > options.maxFiles) {
|
|
378
|
+
options.onReject?.(files.slice(options.maxFiles), "maxFiles");
|
|
379
|
+
files = files.slice(0, options.maxFiles);
|
|
380
|
+
}
|
|
322
381
|
if (files.length > 0) options.onDrop(files);
|
|
323
382
|
}
|
|
324
383
|
}));
|
|
325
|
-
|
|
384
|
+
return () => {
|
|
326
385
|
for (const fn of cleanups) fn();
|
|
327
386
|
};
|
|
328
387
|
}
|
|
329
|
-
|
|
330
|
-
onCleanup(() => {
|
|
331
|
-
disposed = true;
|
|
332
|
-
if (cleanup) cleanup();
|
|
333
|
-
});
|
|
388
|
+
const { ref } = bindElement("useFileDrop", options.element, register);
|
|
334
389
|
return {
|
|
335
390
|
isOver,
|
|
336
|
-
isDraggingFiles
|
|
391
|
+
isDraggingFiles,
|
|
392
|
+
ref
|
|
337
393
|
};
|
|
338
394
|
}
|
|
339
395
|
|
|
@@ -345,6 +401,31 @@ const SORT_GROUP = "__pyreon_sortable_group";
|
|
|
345
401
|
const SORT_PAYLOAD = "__pyreon_sortable_payload";
|
|
346
402
|
const SR_ONLY = "position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);white-space:nowrap;border:0";
|
|
347
403
|
let _sortableCounter = 0;
|
|
404
|
+
const INSTRUCTIONS = "To reorder, press Space or Enter to pick up an item, use the arrow keys to move it, then Space or Enter to drop it, or Escape to cancel. Alt plus an arrow key moves an item directly.";
|
|
405
|
+
let _instructionsHost = null;
|
|
406
|
+
function mountInstructions(id) {
|
|
407
|
+
/* v8 ignore next — callers run client-side only (containerRef is a DOM ref) */
|
|
408
|
+
if (isServer) return () => {};
|
|
409
|
+
if (!_instructionsHost || !_instructionsHost.isConnected) {
|
|
410
|
+
_instructionsHost = document.createElement("div");
|
|
411
|
+
_instructionsHost.setAttribute("data-pyreon-sortable-instructions-host", "");
|
|
412
|
+
_instructionsHost.style.cssText = SR_ONLY;
|
|
413
|
+
document.body.appendChild(_instructionsHost);
|
|
414
|
+
}
|
|
415
|
+
const host = _instructionsHost;
|
|
416
|
+
const node = document.createElement("div");
|
|
417
|
+
node.id = id;
|
|
418
|
+
node.setAttribute("data-pyreon-sortable-instructions", "");
|
|
419
|
+
node.textContent = INSTRUCTIONS;
|
|
420
|
+
host.appendChild(node);
|
|
421
|
+
return () => {
|
|
422
|
+
node.remove();
|
|
423
|
+
if (host.childElementCount === 0) {
|
|
424
|
+
host.remove();
|
|
425
|
+
if (_instructionsHost === host) _instructionsHost = null;
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
}
|
|
348
429
|
const _sortableRegistry = /* @__PURE__ */ new Map();
|
|
349
430
|
/**
|
|
350
431
|
* Sortable list with signal-driven state, auto-scroll, and edge detection.
|
|
@@ -354,7 +435,9 @@ const _sortableRegistry = /* @__PURE__ */ new Map();
|
|
|
354
435
|
* - Auto-scroll when dragging near container edges
|
|
355
436
|
* - Closest-edge detection (drop above/below or left/right)
|
|
356
437
|
* - Axis constraint (vertical/horizontal)
|
|
357
|
-
* - Keyboard reordering
|
|
438
|
+
* - Keyboard reordering: Space/Enter picks an item up, arrows move it,
|
|
439
|
+
* Space/Enter drops, Escape cancels (restores the original position);
|
|
440
|
+
* Alt+Arrow moves directly. Every step is announced to screen readers.
|
|
358
441
|
* - Optional cross-list `groupId` for Trello/Notion/Linear board layouts
|
|
359
442
|
* (W18) — share the same `groupId` between two
|
|
360
443
|
* sortable instances and items can be dragged between them. The
|
|
@@ -419,6 +502,10 @@ function useSortable(options) {
|
|
|
419
502
|
const isOverSelector = createSelector(overId);
|
|
420
503
|
const axis = options.axis ?? "vertical";
|
|
421
504
|
const groupId = options.groupId;
|
|
505
|
+
const isDisabled = () => {
|
|
506
|
+
const d = options.disabled;
|
|
507
|
+
return typeof d === "function" ? d() : !!d;
|
|
508
|
+
};
|
|
422
509
|
/** Resolve the announcement label for an item: `label(item)` else the key. */
|
|
423
510
|
function labelOf(item, key) {
|
|
424
511
|
if (item !== void 0 && options.label) return options.label(item);
|
|
@@ -426,7 +513,7 @@ function useSortable(options) {
|
|
|
426
513
|
}
|
|
427
514
|
/** Resolve the announcement label for a key by looking the item up. */
|
|
428
515
|
function labelFor(key) {
|
|
429
|
-
return labelOf(options.items().find((i) => options.by(i) === key), key);
|
|
516
|
+
return labelOf(options.items().find((i) => options.by(i) === key || String(options.by(i)) === key), key);
|
|
430
517
|
}
|
|
431
518
|
_sortableRegistry.set(sortableId, {
|
|
432
519
|
groupId,
|
|
@@ -460,7 +547,8 @@ function useSortable(options) {
|
|
|
460
547
|
/** Announce a cross-list receive on THIS (destination) sortable. */
|
|
461
548
|
function announceCrossListReceive(item, insertAt) {
|
|
462
549
|
const count = Math.max(options.items().length, insertAt + 1);
|
|
463
|
-
|
|
550
|
+
const key = options.by(item);
|
|
551
|
+
announce(`Dropped ${labelOf(item, key)} at position ${insertAt + 1} of ${count} in the receiving list`);
|
|
464
552
|
}
|
|
465
553
|
/** Returns true when `source` belongs to this sortable or to a sibling
|
|
466
554
|
* in the same `groupId`. */
|
|
@@ -476,13 +564,13 @@ function useSortable(options) {
|
|
|
476
564
|
}
|
|
477
565
|
if (!el) return;
|
|
478
566
|
const containerCleanups = [];
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
567
|
+
containerCleanups.push(mountInstructions(instructionsId));
|
|
568
|
+
if (!el.hasAttribute("role") && el.tagName !== "UL" && el.tagName !== "OL") {
|
|
569
|
+
el.setAttribute("role", "list");
|
|
570
|
+
containerCleanups.push(() => {
|
|
571
|
+
if (el.getAttribute("role") === "list") el.removeAttribute("role");
|
|
572
|
+
});
|
|
573
|
+
}
|
|
486
574
|
containerCleanups.push(autoScrollForElements({
|
|
487
575
|
element: el,
|
|
488
576
|
canScroll: ({ source }) => acceptsSource(source)
|
|
@@ -512,38 +600,97 @@ function useSortable(options) {
|
|
|
512
600
|
});
|
|
513
601
|
}
|
|
514
602
|
}));
|
|
603
|
+
let picked = null;
|
|
604
|
+
let focusFrame = 0;
|
|
605
|
+
const indexOf = (key) => options.items().findIndex((item) => String(options.by(item)) === key);
|
|
606
|
+
const restoreFocus = (key) => {
|
|
607
|
+
if (focusFrame) cancelAnimationFrame(focusFrame);
|
|
608
|
+
focusFrame = requestAnimationFrame(() => {
|
|
609
|
+
focusFrame = 0;
|
|
610
|
+
const items = el.querySelectorAll("[data-pyreon-sort-key]");
|
|
611
|
+
for (const item of items) if (item.dataset.pyreonSortKey === key) {
|
|
612
|
+
item.focus();
|
|
613
|
+
break;
|
|
614
|
+
}
|
|
615
|
+
});
|
|
616
|
+
};
|
|
617
|
+
/** Move the item with `key` from its index to `to`; returns false when out of range. */
|
|
618
|
+
const moveTo = (key, to) => {
|
|
619
|
+
const currentItems = options.items();
|
|
620
|
+
const from = indexOf(key);
|
|
621
|
+
/* v8 ignore next — defensive findIndex guard; the key comes from a live item */
|
|
622
|
+
if (from === -1) return false;
|
|
623
|
+
if (to < 0 || to >= currentItems.length || to === from) return false;
|
|
624
|
+
const reordered = [...currentItems];
|
|
625
|
+
const [moved] = reordered.splice(from, 1);
|
|
626
|
+
reordered.splice(to, 0, moved);
|
|
627
|
+
options.onReorder(reordered);
|
|
628
|
+
restoreFocus(key);
|
|
629
|
+
return true;
|
|
630
|
+
};
|
|
631
|
+
const endPickup = () => {
|
|
632
|
+
picked = null;
|
|
633
|
+
activeId.set(null);
|
|
634
|
+
};
|
|
515
635
|
const keyHandler = (e) => {
|
|
516
|
-
if (!e.altKey) return;
|
|
517
|
-
const isUp = axis === "vertical" ? e.key === "ArrowUp" : e.key === "ArrowLeft";
|
|
518
|
-
const isDown = axis === "vertical" ? e.key === "ArrowDown" : e.key === "ArrowRight";
|
|
519
|
-
if (!isUp && !isDown) return;
|
|
520
636
|
const focused = document.activeElement;
|
|
521
637
|
if (!focused || !el.contains(focused)) return;
|
|
522
638
|
const focusedKey = focused.dataset.pyreonSortKey;
|
|
523
639
|
if (!focusedKey) return;
|
|
524
|
-
e.
|
|
525
|
-
const
|
|
526
|
-
const
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
reordered[targetIndex] = temp;
|
|
535
|
-
options.onReorder(reordered);
|
|
536
|
-
announce(`Moved ${labelOf(temp, focusedKey)} to position ${targetIndex + 1} of ${currentItems.length}`);
|
|
537
|
-
requestAnimationFrame(() => {
|
|
538
|
-
const items = el.querySelectorAll("[data-pyreon-sort-key]");
|
|
539
|
-
for (const item of items) if (item.dataset.pyreonSortKey === focusedKey) {
|
|
540
|
-
item.focus();
|
|
541
|
-
break;
|
|
640
|
+
const isSpaceOrEnter = e.key === " " || e.key === "Enter";
|
|
641
|
+
const isUp = axis === "vertical" ? e.key === "ArrowUp" : e.key === "ArrowLeft";
|
|
642
|
+
const isDown = axis === "vertical" ? e.key === "ArrowDown" : e.key === "ArrowRight";
|
|
643
|
+
if (picked) {
|
|
644
|
+
if (isUp || isDown) {
|
|
645
|
+
e.preventDefault();
|
|
646
|
+
const from = indexOf(picked.key);
|
|
647
|
+
const to = isUp ? from - 1 : from + 1;
|
|
648
|
+
if (moveTo(picked.key, to)) announce(`Moved ${labelFor(picked.key)} to position ${to + 1} of ${options.items().length}`);
|
|
649
|
+
return;
|
|
542
650
|
}
|
|
543
|
-
|
|
651
|
+
if (isSpaceOrEnter) {
|
|
652
|
+
e.preventDefault();
|
|
653
|
+
const key = picked.key;
|
|
654
|
+
endPickup();
|
|
655
|
+
announce(`Dropped ${labelFor(key)} at position ${indexOf(key) + 1} of ${options.items().length}`);
|
|
656
|
+
return;
|
|
657
|
+
}
|
|
658
|
+
if (e.key === "Escape") {
|
|
659
|
+
e.preventDefault();
|
|
660
|
+
const { key, origin } = picked;
|
|
661
|
+
moveTo(key, origin);
|
|
662
|
+
endPickup();
|
|
663
|
+
announce(`Reorder cancelled. ${labelFor(key)} returned to position ${origin + 1} of ${options.items().length}`);
|
|
664
|
+
}
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
if (isDisabled()) return;
|
|
668
|
+
if (isSpaceOrEnter && e.target === focused && !e.altKey && !e.ctrlKey && !e.metaKey && !e.shiftKey) {
|
|
669
|
+
e.preventDefault();
|
|
670
|
+
const origin = indexOf(focusedKey);
|
|
671
|
+
/* v8 ignore next — defensive findIndex guard; the key comes from a live item */
|
|
672
|
+
if (origin === -1) return;
|
|
673
|
+
picked = {
|
|
674
|
+
key: focusedKey,
|
|
675
|
+
origin
|
|
676
|
+
};
|
|
677
|
+
activeId.set(options.by(options.items()[origin]));
|
|
678
|
+
announce(`Picked up ${labelFor(focusedKey)}. Current position ${origin + 1} of ${options.items().length}. Use the arrow keys to move, Space or Enter to drop, Escape to cancel.`);
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
if (!e.altKey || !isUp && !isDown) return;
|
|
682
|
+
e.preventDefault();
|
|
683
|
+
const from = indexOf(focusedKey);
|
|
684
|
+
const to = isUp ? from - 1 : from + 1;
|
|
685
|
+
if (moveTo(focusedKey, to)) announce(`Moved ${labelFor(focusedKey)} to position ${to + 1} of ${options.items().length}`);
|
|
544
686
|
};
|
|
545
687
|
el.addEventListener("keydown", keyHandler);
|
|
546
688
|
containerCleanups.push(() => el.removeEventListener("keydown", keyHandler));
|
|
689
|
+
containerCleanups.push(() => {
|
|
690
|
+
if (focusFrame) cancelAnimationFrame(focusFrame);
|
|
691
|
+
focusFrame = 0;
|
|
692
|
+
if (picked) endPickup();
|
|
693
|
+
});
|
|
547
694
|
containerCleanup = () => {
|
|
548
695
|
for (const fn of containerCleanups) fn();
|
|
549
696
|
};
|
|
@@ -551,7 +698,13 @@ function useSortable(options) {
|
|
|
551
698
|
const itemEls = /* @__PURE__ */ new Map();
|
|
552
699
|
const itemHandles = /* @__PURE__ */ new Map();
|
|
553
700
|
function itemRef(key) {
|
|
701
|
+
let mine = null;
|
|
554
702
|
return (el) => {
|
|
703
|
+
if (!el) {
|
|
704
|
+
const stale = mine !== null && itemEls.get(key) !== mine;
|
|
705
|
+
mine = null;
|
|
706
|
+
if (stale) return;
|
|
707
|
+
}
|
|
555
708
|
const prev = itemCleanups.get(key);
|
|
556
709
|
if (prev) {
|
|
557
710
|
prev();
|
|
@@ -561,6 +714,7 @@ function useSortable(options) {
|
|
|
561
714
|
itemEls.delete(key);
|
|
562
715
|
return;
|
|
563
716
|
}
|
|
717
|
+
mine = el;
|
|
564
718
|
itemEls.set(key, el);
|
|
565
719
|
registerItem(key, el);
|
|
566
720
|
};
|
|
@@ -572,9 +726,17 @@ function useSortable(options) {
|
|
|
572
726
|
* effect — pdnd captures the handle at registration time.
|
|
573
727
|
*/
|
|
574
728
|
function itemHandleRef(key) {
|
|
729
|
+
let mine = null;
|
|
575
730
|
return (el) => {
|
|
576
|
-
if (el)
|
|
577
|
-
|
|
731
|
+
if (el) {
|
|
732
|
+
mine = el;
|
|
733
|
+
itemHandles.set(key, el);
|
|
734
|
+
} else {
|
|
735
|
+
const stale = mine !== null && itemHandles.get(key) !== mine;
|
|
736
|
+
mine = null;
|
|
737
|
+
if (stale) return;
|
|
738
|
+
itemHandles.delete(key);
|
|
739
|
+
}
|
|
578
740
|
const itemEl = itemEls.get(key);
|
|
579
741
|
if (!itemEl) return;
|
|
580
742
|
/* v8 ignore next — defensive optional call; a live itemEl implies a live cleanup */
|
|
@@ -586,7 +748,7 @@ function useSortable(options) {
|
|
|
586
748
|
function registerItem(key, el) {
|
|
587
749
|
el.dataset.pyreonSortKey = String(key);
|
|
588
750
|
if (!el.hasAttribute("tabindex")) el.setAttribute("tabindex", "0");
|
|
589
|
-
el.setAttribute("role", "listitem");
|
|
751
|
+
if (!el.hasAttribute("role")) el.setAttribute("role", "listitem");
|
|
590
752
|
el.setAttribute("aria-roledescription", "sortable item");
|
|
591
753
|
if (!el.hasAttribute("aria-describedby")) el.setAttribute("aria-describedby", instructionsId);
|
|
592
754
|
const allowedEdges = axis === "vertical" ? ["top", "bottom"] : ["left", "right"];
|
|
@@ -594,6 +756,7 @@ function useSortable(options) {
|
|
|
594
756
|
const cleanup = combine(draggable({
|
|
595
757
|
element: el,
|
|
596
758
|
...handle ? { dragHandle: handle } : {},
|
|
759
|
+
canDrag: () => !isDisabled(),
|
|
597
760
|
getInitialData: () => {
|
|
598
761
|
const item = options.items().find((i) => options.by(i) === key);
|
|
599
762
|
return {
|