@pyreon/dnd 0.51.0 → 0.53.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 +36 -22
- package/lib/index.js +297 -134
- package/lib/index.js.map +1 -1
- package/lib/native-plugin.js +269 -0
- package/lib/native-plugin.js.map +1 -0
- package/lib/types/index.d.ts +88 -41
- package/lib/types/native-plugin.d.ts +13 -0
- 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 +46 -14
- package/lib/analysis/index.js.html +0 -5406
package/lib/types/index.d.ts
CHANGED
|
@@ -35,8 +35,14 @@ interface DragPreviewOptions {
|
|
|
35
35
|
offset?: 'pointer-outside' | 'center' | 'preserve-offset';
|
|
36
36
|
}
|
|
37
37
|
interface UseDraggableOptions<T extends DragData = DragData> {
|
|
38
|
-
/**
|
|
39
|
-
|
|
38
|
+
/**
|
|
39
|
+
* Element getter for the draggable element. Optional — attach the result's
|
|
40
|
+
* `ref` instead for an element that mounts later or may be swapped. Resolved
|
|
41
|
+
* on a microtask after the hook runs; a getter that reads a SIGNAL
|
|
42
|
+
* re-registers when the signal changes. A getter still returning `null` at
|
|
43
|
+
* that point (with no `ref` attached) warns in dev.
|
|
44
|
+
*/
|
|
45
|
+
element?: () => HTMLElement | null;
|
|
40
46
|
/** Data to transfer on drag. Can be a function for dynamic data. */
|
|
41
47
|
data: T | (() => T);
|
|
42
48
|
/** Optional drag handle element (subset of the draggable). */
|
|
@@ -57,20 +63,37 @@ interface UseDraggableOptions<T extends DragData = DragData> {
|
|
|
57
63
|
interface UseDraggableResult {
|
|
58
64
|
/** Whether this element is currently being dragged. */
|
|
59
65
|
isDragging: () => boolean;
|
|
66
|
+
/**
|
|
67
|
+
* Ref callback for the draggable element: registers on mount, moves the
|
|
68
|
+
* registration when the element is swapped, disposes on `null`.
|
|
69
|
+
*/
|
|
70
|
+
ref: (el: HTMLElement | null) => void;
|
|
60
71
|
}
|
|
61
|
-
interface UseDroppableOptions<T extends DragData = DragData> {
|
|
62
|
-
/**
|
|
63
|
-
|
|
64
|
-
|
|
72
|
+
interface UseDroppableOptions<T extends DragData = DragData, TSource extends DragData = DragData> {
|
|
73
|
+
/**
|
|
74
|
+
* Element getter for the drop target. Optional — attach the result's `ref`
|
|
75
|
+
* instead for an element that mounts later or may be swapped (same
|
|
76
|
+
* semantics as `UseDraggableOptions.element`).
|
|
77
|
+
*/
|
|
78
|
+
element?: () => HTMLElement | null;
|
|
79
|
+
/** Data to attach to the drop target (`T`). */
|
|
65
80
|
data?: T | (() => T);
|
|
66
|
-
/**
|
|
67
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Filter what can be dropped. Return false to reject. Receives the DRAG
|
|
83
|
+
* SOURCE's data — type it with the second generic, `TSource`.
|
|
84
|
+
*/
|
|
85
|
+
canDrop?: (sourceData: TSource) => boolean;
|
|
68
86
|
/** Called when a draggable enters this target. */
|
|
69
|
-
onDragEnter?: (sourceData:
|
|
87
|
+
onDragEnter?: (sourceData: TSource) => void;
|
|
70
88
|
/** Called when a draggable leaves this target. */
|
|
71
89
|
onDragLeave?: () => void;
|
|
72
|
-
/**
|
|
73
|
-
|
|
90
|
+
/**
|
|
91
|
+
* Called when an item is dropped on this target, with the source's data and
|
|
92
|
+
* WHERE it landed: `edge` (the closest configured edge, `null` without
|
|
93
|
+
* `edges`) and `data` (this target's own data). Both are captured before the
|
|
94
|
+
* `isOver` / `overEdge` signals reset.
|
|
95
|
+
*/
|
|
96
|
+
onDrop?: (sourceData: TSource, location: DropLocation) => void;
|
|
74
97
|
/**
|
|
75
98
|
* Opt into closest-edge detection (pdnd hitbox `attachClosestEdge` /
|
|
76
99
|
* `extractClosestEdge`). Pass the edges you care about (e.g.
|
|
@@ -94,6 +117,8 @@ interface UseDroppableResult {
|
|
|
94
117
|
* otherwise. Always `null` unless the `edges` option is set.
|
|
95
118
|
*/
|
|
96
119
|
overEdge: () => DropEdge | null;
|
|
120
|
+
/** Ref callback for the drop target — see `UseDroppableOptions.element`. */
|
|
121
|
+
ref: (el: HTMLElement | null) => void;
|
|
97
122
|
}
|
|
98
123
|
interface UseSortableOptions<T> {
|
|
99
124
|
/** Reactive list of items to sort. */
|
|
@@ -138,13 +163,18 @@ interface UseSortableOptions<T> {
|
|
|
138
163
|
* your keys are opaque ids.
|
|
139
164
|
*/
|
|
140
165
|
label?: (item: T) => string;
|
|
166
|
+
/**
|
|
167
|
+
* Disable reordering (pointer drag AND keyboard). Reactive when a function.
|
|
168
|
+
*/
|
|
169
|
+
disabled?: boolean | (() => boolean);
|
|
141
170
|
}
|
|
142
171
|
interface UseSortableResult {
|
|
143
172
|
/**
|
|
144
173
|
* Attach to the scroll container. Pyreon's runtime invokes refs with
|
|
145
|
-
* `T | null
|
|
146
|
-
*
|
|
147
|
-
*
|
|
174
|
+
* `T | null`: an element (re)registers the container (auto-scroll, the
|
|
175
|
+
* container drop target, the keyboard handler, `role="list"` on a non-list
|
|
176
|
+
* element), and `null` DISPOSES that registration — so a container behind
|
|
177
|
+
* `<Show>` can toggle without leaking.
|
|
148
178
|
*/
|
|
149
179
|
containerRef: (el: HTMLElement | null) => void;
|
|
150
180
|
/**
|
|
@@ -218,57 +248,72 @@ interface UseDragMonitorResult {
|
|
|
218
248
|
* </Show>
|
|
219
249
|
* ```
|
|
220
250
|
*/
|
|
221
|
-
declare function useDragMonitor(options?: UseDragMonitorOptions): UseDragMonitorResult;
|
|
251
|
+
export declare function useDragMonitor(options?: UseDragMonitorOptions): UseDragMonitorResult;
|
|
222
252
|
//#endregion
|
|
223
253
|
//#region src/use-draggable.d.ts
|
|
224
254
|
/**
|
|
225
255
|
* Make an element draggable with signal-driven state.
|
|
226
256
|
*
|
|
257
|
+
* Attach the returned `ref` to the element (works for elements that mount
|
|
258
|
+
* later or get swapped), or pass an `element` getter.
|
|
259
|
+
*
|
|
227
260
|
* @example
|
|
228
261
|
* ```tsx
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* const { isDragging } = useDraggable({
|
|
232
|
-
* element: () => cardEl,
|
|
262
|
+
* const { ref, isDragging } = useDraggable({
|
|
233
263
|
* data: { id: card.id, type: "card" },
|
|
234
264
|
* })
|
|
235
265
|
*
|
|
236
|
-
* <div ref={
|
|
266
|
+
* <div ref={ref} class={isDragging() ? "opacity-50" : ""}>
|
|
237
267
|
* {card.title}
|
|
238
268
|
* </div>
|
|
239
269
|
* ```
|
|
240
270
|
*/
|
|
241
|
-
declare function useDraggable<T extends DragData = DragData>(options: UseDraggableOptions<T>): UseDraggableResult;
|
|
271
|
+
export declare function useDraggable<T extends DragData = DragData>(options: UseDraggableOptions<T>): UseDraggableResult;
|
|
242
272
|
//#endregion
|
|
243
273
|
//#region src/use-droppable.d.ts
|
|
244
274
|
/**
|
|
245
275
|
* Make an element a drop target with signal-driven state.
|
|
246
276
|
*
|
|
277
|
+
* Attach the returned `ref` to the element (works for elements that mount
|
|
278
|
+
* later or get swapped), or pass an `element` getter.
|
|
279
|
+
*
|
|
247
280
|
* @example
|
|
248
281
|
* ```tsx
|
|
249
|
-
*
|
|
250
|
-
*
|
|
251
|
-
* const { isOver, overEdge } = useDroppable({
|
|
252
|
-
* element: () => zoneEl,
|
|
253
|
-
* onDrop: (data) => handleDrop(data),
|
|
282
|
+
* const { ref, isOver, overEdge } = useDroppable({
|
|
283
|
+
* onDrop: (data, { edge }) => handleDrop(data, edge),
|
|
254
284
|
* canDrop: (data) => data.type === "card",
|
|
255
285
|
* edges: ["top", "bottom"], // opt-in closest-edge detection
|
|
256
286
|
* })
|
|
257
287
|
*
|
|
258
|
-
* <div ref={
|
|
288
|
+
* <div ref={ref} class={isOver() ? "bg-blue-50" : ""}>
|
|
259
289
|
* Drop here
|
|
260
290
|
* </div>
|
|
261
291
|
* ```
|
|
262
292
|
*/
|
|
263
|
-
declare function useDroppable<T extends DragData = DragData>(options: UseDroppableOptions<T>): UseDroppableResult;
|
|
293
|
+
export declare function useDroppable<T extends DragData = DragData, TSource extends DragData = DragData>(options: UseDroppableOptions<T, TSource>): UseDroppableResult;
|
|
264
294
|
//#endregion
|
|
265
295
|
//#region src/use-file-drop.d.ts
|
|
296
|
+
/** Why a dropped file was not delivered to `onDrop`. */
|
|
297
|
+
type FileRejectReason = 'accept' | 'maxFiles';
|
|
266
298
|
interface UseFileDropOptions {
|
|
267
|
-
/**
|
|
268
|
-
|
|
269
|
-
|
|
299
|
+
/**
|
|
300
|
+
* Element getter for the drop zone. Optional — attach the returned `ref`
|
|
301
|
+
* instead for an element that mounts later or may be swapped. A getter that
|
|
302
|
+
* reads a signal re-registers when the signal changes.
|
|
303
|
+
*/
|
|
304
|
+
element?: () => HTMLElement | null;
|
|
305
|
+
/** Called when files are dropped (only the accepted ones). */
|
|
270
306
|
onDrop: (files: File[]) => void;
|
|
271
|
-
/**
|
|
307
|
+
/**
|
|
308
|
+
* Called with the dropped files that were NOT delivered, and why: `'accept'`
|
|
309
|
+
* (type / extension not in `accept`) or `'maxFiles'` (over the limit). Fired
|
|
310
|
+
* once per reason per drop. Without it a rejected drop is silent.
|
|
311
|
+
*/
|
|
312
|
+
onReject?: (rejected: File[], reason: FileRejectReason) => void;
|
|
313
|
+
/**
|
|
314
|
+
* Filter accepted file types — MIME types (`"image/png"`), MIME wildcards
|
|
315
|
+
* (`"image/*"`), extensions (`".pdf"`), or `"*"` / `"*\/*"` for any file.
|
|
316
|
+
*/
|
|
272
317
|
accept?: string[];
|
|
273
318
|
/** Maximum number of files. */
|
|
274
319
|
maxFiles?: number;
|
|
@@ -280,6 +325,8 @@ interface UseFileDropResult {
|
|
|
280
325
|
isOver: () => boolean;
|
|
281
326
|
/** Whether files are being dragged anywhere on the page. */
|
|
282
327
|
isDraggingFiles: () => boolean;
|
|
328
|
+
/** Ref callback for the drop zone — see `UseFileDropOptions.element`. */
|
|
329
|
+
ref: (el: HTMLElement | null) => void;
|
|
283
330
|
}
|
|
284
331
|
/**
|
|
285
332
|
* File drop zone with signal-driven state.
|
|
@@ -287,24 +334,22 @@ interface UseFileDropResult {
|
|
|
287
334
|
*
|
|
288
335
|
* @example
|
|
289
336
|
* ```tsx
|
|
290
|
-
*
|
|
291
|
-
*
|
|
292
|
-
* const { isOver, isDraggingFiles } = useFileDrop({
|
|
293
|
-
* element: () => dropZone,
|
|
337
|
+
* const { ref, isOver, isDraggingFiles } = useFileDrop({
|
|
294
338
|
* accept: ["image/*", ".pdf"],
|
|
295
339
|
* maxFiles: 5,
|
|
296
340
|
* onDrop: (files) => upload(files),
|
|
341
|
+
* onReject: (files, reason) => toast(`${files.length} file(s) rejected: ${reason}`),
|
|
297
342
|
* })
|
|
298
343
|
*
|
|
299
344
|
* <div
|
|
300
|
-
* ref={
|
|
345
|
+
* ref={ref}
|
|
301
346
|
* class={isOver() ? "drop-active" : isDraggingFiles() ? "drop-ready" : ""}
|
|
302
347
|
* >
|
|
303
348
|
* Drop files here
|
|
304
349
|
* </div>
|
|
305
350
|
* ```
|
|
306
351
|
*/
|
|
307
|
-
declare function useFileDrop(options: UseFileDropOptions): UseFileDropResult;
|
|
352
|
+
export declare function useFileDrop(options: UseFileDropOptions): UseFileDropResult;
|
|
308
353
|
//#endregion
|
|
309
354
|
//#region src/use-sortable.d.ts
|
|
310
355
|
/**
|
|
@@ -315,7 +360,9 @@ declare function useFileDrop(options: UseFileDropOptions): UseFileDropResult;
|
|
|
315
360
|
* - Auto-scroll when dragging near container edges
|
|
316
361
|
* - Closest-edge detection (drop above/below or left/right)
|
|
317
362
|
* - Axis constraint (vertical/horizontal)
|
|
318
|
-
* - Keyboard reordering
|
|
363
|
+
* - Keyboard reordering: Space/Enter picks an item up, arrows move it,
|
|
364
|
+
* Space/Enter drops, Escape cancels (restores the original position);
|
|
365
|
+
* Alt+Arrow moves directly. Every step is announced to screen readers.
|
|
319
366
|
* - Optional cross-list `groupId` for Trello/Notion/Linear board layouts
|
|
320
367
|
* (W18) — share the same `groupId` between two
|
|
321
368
|
* sortable instances and items can be dragged between them. The
|
|
@@ -357,6 +404,6 @@ declare function useFileDrop(options: UseFileDropOptions): UseFileDropResult;
|
|
|
357
404
|
* row to the id signal (O(N) notifies per change); the selectors notify
|
|
358
405
|
* only the two affected rows (O(2), `createSelector` semantics).
|
|
359
406
|
*/
|
|
360
|
-
declare function useSortable<T>(options: UseSortableOptions<T>): UseSortableResult;
|
|
407
|
+
export declare function useSortable<T>(options: UseSortableOptions<T>): UseSortableResult;
|
|
361
408
|
//#endregion
|
|
362
|
-
export {
|
|
409
|
+
export type { DragData, DragPreviewOptions, DropEdge, DropLocation, FileRejectReason, UseDragMonitorOptions, UseDragMonitorResult, UseDraggableOptions, UseDraggableResult, UseDroppableOptions, UseDroppableResult, UseFileDropOptions, UseFileDropResult, UseSortableOptions, UseSortableResult };
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { CompilerPlugin, StubAugmentation } from "@pyreon/native-compiler/plugin-api";
|
|
2
|
+
//#region src/native-plugin/plugin.d.ts
|
|
3
|
+
/**
|
|
4
|
+
* The `@pyreon/dnd` native plugin. Shipped by `@pyreon/dnd` itself and discovered from its manifest
|
|
5
|
+
* (`pyreon.native.plugin`) when a source file imports the package.
|
|
6
|
+
*/
|
|
7
|
+
export declare const dndPlugin: CompilerPlugin;
|
|
8
|
+
//#endregion
|
|
9
|
+
//#region src/native-plugin/stubs.d.ts
|
|
10
|
+
/** The `PyreonSortableState` compile-gate stubs, appended only to an emit that names the engine or its modifiers. */
|
|
11
|
+
export declare const dndStubs: StubAugmentation;
|
|
12
|
+
//#endregion
|
|
13
|
+
export { dndPlugin as default };
|
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
package com.pyreon.runtime
|
|
2
|
+
|
|
3
|
+
import androidx.compose.runtime.mutableStateOf
|
|
4
|
+
import kotlin.math.roundToInt
|
|
5
|
+
|
|
6
|
+
// PyreonSortable — the Android-native port of @pyreon/dnd's `useSortable`.
|
|
7
|
+
//
|
|
8
|
+
// The web hook is pointer-driven (pragmatic-drag-and-drop over DOM events).
|
|
9
|
+
// That engine does NOT cross: a drag on Android is a platform gesture. What
|
|
10
|
+
// DOES cross is the REORDER SEMANTICS — which item is lifted, which item it is
|
|
11
|
+
// over, which EDGE of that item is nearest, and where the moved element lands.
|
|
12
|
+
// `moveIndex` below is a verbatim port of the TypeScript `performReorder`
|
|
13
|
+
// arithmetic, and the Swift port routes through the identical primitive, so a
|
|
14
|
+
// reorder produces the SAME list on web, iOS and Android.
|
|
15
|
+
//
|
|
16
|
+
// This file is deliberately PURE: the only Compose dependency is
|
|
17
|
+
// `mutableStateOf` (so a mutation recomposes a reader), exactly like
|
|
18
|
+
// PyreonTableState.kt. There is no gesture code here — the long-press drag is
|
|
19
|
+
// EMITTED into the app's own composable by PMTC, following the same shape
|
|
20
|
+
// `<Press onSwipeLeft>` already uses (`pointerInput` +
|
|
21
|
+
// `detectDragGesturesAfterLongPress`). Keeping the gesture in the emit rather
|
|
22
|
+
// than the runtime is what lets `validateKotlin` type-check it against the
|
|
23
|
+
// mirrored Compose stubs, and lets this file be verified headlessly by
|
|
24
|
+
// `check-native-cosource` with no Compose-UI stub bundle at all.
|
|
25
|
+
//
|
|
26
|
+
// Android's drag model is DISPLACEMENT-based (long-press, then move by whole
|
|
27
|
+
// slots) where iOS is DROP-TARGET-based. Both funnel into `moveIndex`, so the
|
|
28
|
+
// gesture differs per platform — as it should — while the RESULT does not.
|
|
29
|
+
|
|
30
|
+
/** Which way the list runs. Mirrors the web `axis` option. */
|
|
31
|
+
enum class PyreonSortAxis { VERTICAL, HORIZONTAL }
|
|
32
|
+
|
|
33
|
+
/** The edge of the hovered item nearest the pointer — the insert side. */
|
|
34
|
+
enum class PyreonDropEdge { TOP, BOTTOM, LEFT, RIGHT }
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Sortable-list state: which key is lifted, which key it is over, and the edge
|
|
38
|
+
* of that key. Backed by `mutableStateOf` so a drag recomposes the rows that
|
|
39
|
+
* read `isActive` / `isOver`.
|
|
40
|
+
*/
|
|
41
|
+
class PyreonSortableState<T>(
|
|
42
|
+
val axis: PyreonSortAxis = PyreonSortAxis.VERTICAL,
|
|
43
|
+
) {
|
|
44
|
+
private var itemsProvider: () -> List<T> = { emptyList() }
|
|
45
|
+
private var keyOf: (T) -> String = { "" }
|
|
46
|
+
private var onReorderFn: (List<T>) -> Unit = {}
|
|
47
|
+
|
|
48
|
+
private val _activeKey = mutableStateOf<String?>(null)
|
|
49
|
+
/** The key of the item being dragged, `null` when idle. */
|
|
50
|
+
val activeKey: String?
|
|
51
|
+
get() = _activeKey.value
|
|
52
|
+
|
|
53
|
+
private val _overKey = mutableStateOf<String?>(null)
|
|
54
|
+
/** The key of the item currently hovered, `null` when none. */
|
|
55
|
+
val overKey: String?
|
|
56
|
+
get() = _overKey.value
|
|
57
|
+
|
|
58
|
+
private val _currentEdge = mutableStateOf<PyreonDropEdge?>(null)
|
|
59
|
+
/** The nearest edge of [overKey], `null` when none. */
|
|
60
|
+
val currentEdge: PyreonDropEdge?
|
|
61
|
+
get() = _currentEdge.value
|
|
62
|
+
|
|
63
|
+
// Accumulated drag distance along the axis, in pixels, since pick-up, and
|
|
64
|
+
// the dragged row's extent (height for a vertical list). Plain fields, not
|
|
65
|
+
// Compose state: they change on every pointer sample and nothing renders
|
|
66
|
+
// them directly — the derived slot delta lands in _overKey, which does.
|
|
67
|
+
private var dragAccum: Float = 0f
|
|
68
|
+
private var itemExtent: Float = 0f
|
|
69
|
+
|
|
70
|
+
/** Wire the reactive item source + reorder sink. */
|
|
71
|
+
fun bind(items: () -> List<T>, by: (T) -> String, onReorder: (List<T>) -> Unit) {
|
|
72
|
+
itemsProvider = items
|
|
73
|
+
keyOf = by
|
|
74
|
+
onReorderFn = onReorder
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// ── the web result surface, name-for-name ────────────────────────────────
|
|
78
|
+
// `useSortable` returns `isActive` / `isOverKey` / `activeId` / `overId` /
|
|
79
|
+
// `overEdge`, so the native engine spells them IDENTICALLY and returns the
|
|
80
|
+
// same value shapes (edge as its lowercase string). That is what lets a row
|
|
81
|
+
// template compile unchanged on all three targets with no compiler-side
|
|
82
|
+
// name mapping.
|
|
83
|
+
fun isActive(key: String): Boolean = activeKey == key
|
|
84
|
+
fun isOverKey(key: String): Boolean = overKey == key
|
|
85
|
+
fun activeId(): String? = activeKey
|
|
86
|
+
fun overId(): String? = overKey
|
|
87
|
+
fun overEdge(): String? = currentEdge?.name?.lowercase()
|
|
88
|
+
|
|
89
|
+
// ── drag lifecycle ───────────────────────────────────────────────────────
|
|
90
|
+
/** The long-press lifted `key`. */
|
|
91
|
+
fun pickUp(key: String) {
|
|
92
|
+
_activeKey.value = key
|
|
93
|
+
dragAccum = 0f
|
|
94
|
+
itemExtent = 0f
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Accumulate a drag sample. [delta] is this frame's movement along the
|
|
99
|
+
* axis and [extent] the dragged row's size on that axis (Compose hands
|
|
100
|
+
* both to the emitted gesture: the drag amount and `PointerInputScope.size`).
|
|
101
|
+
*
|
|
102
|
+
* A zero/negative extent is ignored rather than dividing — a row measured
|
|
103
|
+
* before layout reports 0, and a divide there would send the slot delta to
|
|
104
|
+
* infinity and reorder the list on the first pixel of movement.
|
|
105
|
+
*/
|
|
106
|
+
fun dragBy(delta: Float, extent: Float) {
|
|
107
|
+
if (extent > 0f) itemExtent = extent
|
|
108
|
+
dragAccum += delta
|
|
109
|
+
val active = activeKey ?: return
|
|
110
|
+
val slots = slotDelta()
|
|
111
|
+
if (slots == 0) {
|
|
112
|
+
_overKey.value = null
|
|
113
|
+
_currentEdge.value = null
|
|
114
|
+
return
|
|
115
|
+
}
|
|
116
|
+
val current = itemsProvider()
|
|
117
|
+
val from = current.indexOfFirst { keyOf(it) == active }
|
|
118
|
+
if (from < 0) return
|
|
119
|
+
val target = (from + slots).coerceIn(0, current.size - 1)
|
|
120
|
+
_overKey.value = keyOf(current[target])
|
|
121
|
+
_currentEdge.value = if (slots > 0) {
|
|
122
|
+
if (axis == PyreonSortAxis.VERTICAL) PyreonDropEdge.BOTTOM else PyreonDropEdge.RIGHT
|
|
123
|
+
} else {
|
|
124
|
+
if (axis == PyreonSortAxis.VERTICAL) PyreonDropEdge.TOP else PyreonDropEdge.LEFT
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** How many whole slots the accumulated drag covers. */
|
|
129
|
+
private fun slotDelta(): Int =
|
|
130
|
+
if (itemExtent <= 0f) 0 else (dragAccum / itemExtent).roundToInt()
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Commit the drag. Returns `true` when the list actually changed.
|
|
134
|
+
*/
|
|
135
|
+
fun drop(): Boolean {
|
|
136
|
+
val active = activeKey
|
|
137
|
+
val slots = slotDelta()
|
|
138
|
+
cancel()
|
|
139
|
+
if (active == null || slots == 0) return false
|
|
140
|
+
val current = itemsProvider()
|
|
141
|
+
val from = current.indexOfFirst { keyOf(it) == active }
|
|
142
|
+
if (from < 0) return false
|
|
143
|
+
val to = (from + slots).coerceIn(0, current.size - 1)
|
|
144
|
+
if (to == from) return false
|
|
145
|
+
onReorderFn(moveIndex(current, from.toLong(), to.toLong()))
|
|
146
|
+
return true
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** The drag ended without a drop. */
|
|
150
|
+
fun cancel() {
|
|
151
|
+
_activeKey.value = null
|
|
152
|
+
_overKey.value = null
|
|
153
|
+
_currentEdge.value = null
|
|
154
|
+
dragAccum = 0f
|
|
155
|
+
itemExtent = 0f
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// ── the shared reorder algorithm ─────────────────────────────────────────
|
|
159
|
+
/**
|
|
160
|
+
* Pure reorder: move `dragKey` next to `dropKey`, inserting on `edge`'s
|
|
161
|
+
* side. Returns `null` when the move is a no-op (same key, or either key
|
|
162
|
+
* missing) — matching the TypeScript `performReorder` guards exactly.
|
|
163
|
+
*
|
|
164
|
+
* Not on the Android gesture path (which is displacement-based) but kept
|
|
165
|
+
* as the CROSS-PLATFORM contract: it is the shape the web engine and the
|
|
166
|
+
* iOS drop destination both use, and the parity test asserts all three
|
|
167
|
+
* agree.
|
|
168
|
+
*/
|
|
169
|
+
fun reordered(dragKey: String, dropKey: String, edge: PyreonDropEdge): List<T>? {
|
|
170
|
+
if (dragKey == dropKey) return null
|
|
171
|
+
val current = itemsProvider()
|
|
172
|
+
val dragIndex = current.indexOfFirst { keyOf(it) == dragKey }
|
|
173
|
+
val dropIndex = current.indexOfFirst { keyOf(it) == dropKey }
|
|
174
|
+
if (dragIndex < 0 || dropIndex < 0) return null
|
|
175
|
+
|
|
176
|
+
val after = edge == PyreonDropEdge.BOTTOM || edge == PyreonDropEdge.RIGHT
|
|
177
|
+
val rawInsert = if (after) {
|
|
178
|
+
if (dropIndex >= dragIndex) dropIndex else dropIndex + 1
|
|
179
|
+
} else {
|
|
180
|
+
if (dropIndex <= dragIndex) dropIndex else dropIndex - 1
|
|
181
|
+
}
|
|
182
|
+
return moveIndex(current, dragIndex.toLong(), rawInsert.toLong())
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
companion object {
|
|
186
|
+
/**
|
|
187
|
+
* Move the element at [from] so that it sits at [to], where [to] is an
|
|
188
|
+
* index in the list WITHOUT the moved element. The single mutation
|
|
189
|
+
* primitive every platform routes through.
|
|
190
|
+
*/
|
|
191
|
+
fun <T> moveIndex(list: List<T>, from: Long, to: Long): List<T> {
|
|
192
|
+
val out = list.toMutableList()
|
|
193
|
+
val moved = out.removeAt(from.toInt())
|
|
194
|
+
out.add(to.coerceIn(0L, out.size.toLong()).toInt(), moved)
|
|
195
|
+
return out
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
package com.pyreon.runtime
|
|
2
|
+
|
|
3
|
+
import androidx.compose.foundation.gestures.detectDragGesturesAfterLongPress
|
|
4
|
+
import androidx.compose.ui.Modifier
|
|
5
|
+
import androidx.compose.ui.input.pointer.pointerInput
|
|
6
|
+
|
|
7
|
+
// The Compose gesture half of @pyreon/dnd's `useSortable`, split out of
|
|
8
|
+
// PyreonSortable.kt so the state engine stays dependency-free and headlessly
|
|
9
|
+
// verifiable (see that file's header). This file is declared
|
|
10
|
+
// `pyreon.native.kotlinSdkOnly` — the co-source stub harness models no Compose
|
|
11
|
+
// gesture surface, so it is verified by the Android device gate and by the
|
|
12
|
+
// emit-level `validateKotlin` stubs, not by `check-native-cosource`.
|
|
13
|
+
//
|
|
14
|
+
// LONG-PRESS-THEN-DRAG is the Android reorder idiom (what every reorderable
|
|
15
|
+
// list uses, and what Material specifies), so this is the platform's own
|
|
16
|
+
// gesture rather than a port of the web's pointer protocol. The DISPLACEMENT
|
|
17
|
+
// model it implies — drag N row-heights, move N slots — differs from iOS's
|
|
18
|
+
// drop-target model on purpose; both funnel into `PyreonSortableState`, so the
|
|
19
|
+
// resulting list is identical on every target.
|
|
20
|
+
//
|
|
21
|
+
// `PointerInputScope.size` is the dragged row's own measured size, which is
|
|
22
|
+
// what makes a global bounds registry unnecessary: a slot is one row-extent.
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Make this composable a sortable item of [state], identified by [key].
|
|
26
|
+
* Emitted by PMTC for `ref={s.itemRef(key)}`.
|
|
27
|
+
*/
|
|
28
|
+
fun <T> Modifier.pyreonSortableItem(state: PyreonSortableState<T>, key: String): Modifier =
|
|
29
|
+
this.pointerInput(key) {
|
|
30
|
+
val extent =
|
|
31
|
+
if (state.axis == PyreonSortAxis.VERTICAL) size.height.toFloat()
|
|
32
|
+
else size.width.toFloat()
|
|
33
|
+
detectDragGesturesAfterLongPress(
|
|
34
|
+
onDragStart = { state.pickUp(key) },
|
|
35
|
+
onDragEnd = { state.drop() },
|
|
36
|
+
onDragCancel = { state.cancel() },
|
|
37
|
+
onDrag = { change, dragAmount ->
|
|
38
|
+
change.consume()
|
|
39
|
+
val delta =
|
|
40
|
+
if (state.axis == PyreonSortAxis.VERTICAL) dragAmount.y else dragAmount.x
|
|
41
|
+
state.dragBy(delta, extent)
|
|
42
|
+
},
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Mark this composable as the sortable CONTAINER of [state]. Emitted by PMTC
|
|
48
|
+
* for `ref={s.containerRef}`.
|
|
49
|
+
*
|
|
50
|
+
* Deliberately a no-op on Android: the displacement gesture is entirely
|
|
51
|
+
* per-item (a slot is one row extent, read from the item's own
|
|
52
|
+
* `PointerInputScope.size`), so unlike SwiftUI — where the container supplies
|
|
53
|
+
* the accessibility grouping the flattened view tree would otherwise lose —
|
|
54
|
+
* there is nothing for a Compose container to carry. It exists so ONE source
|
|
55
|
+
* emits on both targets without the compiler needing a per-target branch, and
|
|
56
|
+
* so the seam is already here if container-level wiring is ever needed.
|
|
57
|
+
*/
|
|
58
|
+
@Suppress("UNUSED_PARAMETER")
|
|
59
|
+
fun <T> Modifier.pyreonSortableContainer(state: PyreonSortableState<T>): Modifier = this
|