@pyreon/dnd 0.50.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 +33 -16
- package/lib/analysis/index.js.html +0 -5406
|
@@ -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
|
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
// PyreonSortable — the iOS-native port of @pyreon/dnd's `useSortable`.
|
|
2
|
+
//
|
|
3
|
+
// The web hook is pointer-driven (pragmatic-drag-and-drop over DOM events).
|
|
4
|
+
// That engine does NOT cross: a drag on iOS is a platform gesture, not a
|
|
5
|
+
// pointer-event protocol. What DOES cross is the part an author reasons about
|
|
6
|
+
// — the REORDER SEMANTICS: which item is lifted, which item it is over, which
|
|
7
|
+
// EDGE of that item the pointer is nearest, and where the moved element lands.
|
|
8
|
+
// That logic is pure, and `moveIndex` below is a verbatim port of the
|
|
9
|
+
// TypeScript `performReorder` arithmetic, so a drop at a given edge produces
|
|
10
|
+
// the SAME array on web, iOS and Android.
|
|
11
|
+
//
|
|
12
|
+
// The gesture layer on top is deliberately NOT a DOM re-implementation: it is
|
|
13
|
+
// SwiftUI's own `.draggable` / `.dropDestination`, so a drag reads as a normal
|
|
14
|
+
// iOS drag (lift, system preview, spring-loading). It is also the only option
|
|
15
|
+
// available: PMTC lowers `<For>` to a bare `ForEach`, never a `List`, so
|
|
16
|
+
// `ForEach.onMove` — which requires a `List` — cannot be reached from
|
|
17
|
+
// compiled Pyreon source.
|
|
18
|
+
//
|
|
19
|
+
// The drag PAYLOAD is the item's key as a `String`. `String` already conforms
|
|
20
|
+
// to `Transferable`, so a consumer's row type needs no conformance of its own.
|
|
21
|
+
//
|
|
22
|
+
// `PyreonSortableState` is pure logic plus `@Observable` — the SwiftUI view
|
|
23
|
+
// modifiers live below the fold, so the engine unit-tests headlessly.
|
|
24
|
+
|
|
25
|
+
import Foundation
|
|
26
|
+
import Observation
|
|
27
|
+
import SwiftUI
|
|
28
|
+
|
|
29
|
+
/// Which way the list runs. Mirrors the web `axis` option.
|
|
30
|
+
public enum PyreonSortAxis: String, Equatable {
|
|
31
|
+
case vertical
|
|
32
|
+
case horizontal
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/// The edge of the hovered item nearest the pointer — the insert side.
|
|
36
|
+
/// Mirrors the web `DropEdge`.
|
|
37
|
+
public enum PyreonDropEdge: String, Equatable {
|
|
38
|
+
case top
|
|
39
|
+
case bottom
|
|
40
|
+
case left
|
|
41
|
+
case right
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/// Sortable-list state: which key is lifted, which key it is over, and the
|
|
45
|
+
/// edge of that key. Mutating through the methods below drives a SwiftUI
|
|
46
|
+
/// re-render (`@Observable`).
|
|
47
|
+
///
|
|
48
|
+
/// The reactive item source is wired AFTER init via `bind` (from SwiftUI's
|
|
49
|
+
/// `.onAppear`), because a `@State` property initializer cannot capture the
|
|
50
|
+
/// view's own state — the same constraint `PyreonTableState.setData` documents.
|
|
51
|
+
@available(iOS 17.0, macOS 14.0, *)
|
|
52
|
+
@Observable
|
|
53
|
+
public final class PyreonSortableState<T> {
|
|
54
|
+
private var itemsProvider: () -> [T] = { [] }
|
|
55
|
+
private var keyOf: (T) -> String = { _ in "" }
|
|
56
|
+
private var onReorderFn: ([T]) -> Void = { _ in }
|
|
57
|
+
|
|
58
|
+
/// The key of the item being dragged, `nil` when idle.
|
|
59
|
+
public private(set) var activeKey: String?
|
|
60
|
+
/// The key of the item currently hovered, `nil` when none.
|
|
61
|
+
public private(set) var overKey: String?
|
|
62
|
+
/// The nearest edge of `overKey`, `nil` when none.
|
|
63
|
+
public private(set) var currentEdge: PyreonDropEdge?
|
|
64
|
+
|
|
65
|
+
public let axis: PyreonSortAxis
|
|
66
|
+
|
|
67
|
+
public init(axis: PyreonSortAxis = .vertical) {
|
|
68
|
+
self.axis = axis
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/// Wire the reactive item source + reorder sink. Called from `.onAppear`.
|
|
72
|
+
public func bind(
|
|
73
|
+
items: @escaping () -> [T],
|
|
74
|
+
by: @escaping (T) -> String,
|
|
75
|
+
onReorder: @escaping ([T]) -> Void
|
|
76
|
+
) {
|
|
77
|
+
self.itemsProvider = items
|
|
78
|
+
self.keyOf = by
|
|
79
|
+
self.onReorderFn = onReorder
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// ── the web result surface, name-for-name ────────────────────────────────
|
|
83
|
+
// `useSortable` returns `isActive` / `isOverKey` / `activeId` / `overId` /
|
|
84
|
+
// `overEdge`, so the native engine spells them IDENTICALLY and returns the
|
|
85
|
+
// same value shapes (edge as its raw string). That is what lets a row
|
|
86
|
+
// template — `<Text opacity={s.isActive(t.id) ? 0.5 : 1}>` — compile
|
|
87
|
+
// unchanged on all three targets with no compiler-side name mapping.
|
|
88
|
+
public func isActive(_ key: String) -> Bool { activeKey == key }
|
|
89
|
+
public func isOverKey(_ key: String) -> Bool { overKey == key }
|
|
90
|
+
public func activeId() -> String? { activeKey }
|
|
91
|
+
public func overId() -> String? { overKey }
|
|
92
|
+
public func overEdge() -> String? { currentEdge?.rawValue }
|
|
93
|
+
|
|
94
|
+
// ── drag lifecycle ───────────────────────────────────────────────────────
|
|
95
|
+
/// The drag lifted off `key`.
|
|
96
|
+
public func pickUp(_ key: String) { activeKey = key }
|
|
97
|
+
|
|
98
|
+
/// The pointer is over `key`, nearest `edge`.
|
|
99
|
+
public func dragOver(_ key: String, edge: PyreonDropEdge) {
|
|
100
|
+
overKey = key
|
|
101
|
+
currentEdge = edge
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/// The pointer left `key`. A leave for a STALE key must not clear the live
|
|
105
|
+
/// one — SwiftUI delivers the outgoing target's `isTargeted(false)` after
|
|
106
|
+
/// the incoming target's `true`, so an unguarded clear blanks the
|
|
107
|
+
/// highlight the user is actually over.
|
|
108
|
+
public func dragLeave(_ key: String) {
|
|
109
|
+
guard overKey == key else { return }
|
|
110
|
+
overKey = nil
|
|
111
|
+
currentEdge = nil
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/// The drag ended without a drop.
|
|
115
|
+
public func cancel() {
|
|
116
|
+
activeKey = nil
|
|
117
|
+
overKey = nil
|
|
118
|
+
currentEdge = nil
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/// Commit a drop of `source` onto `target` at `edge`. Returns `true` when
|
|
122
|
+
/// the list actually changed, so a drop destination can report handled.
|
|
123
|
+
@discardableResult
|
|
124
|
+
public func drop(source: String, on target: String, edge: PyreonDropEdge) -> Bool {
|
|
125
|
+
defer { cancel() }
|
|
126
|
+
guard let next = reordered(dragKey: source, dropKey: target, edge: edge) else {
|
|
127
|
+
return false
|
|
128
|
+
}
|
|
129
|
+
onReorderFn(next)
|
|
130
|
+
return true
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// ── the shared reorder algorithm ─────────────────────────────────────────
|
|
134
|
+
/// Move the element at `from` so that it sits at `to`, where `to` is an
|
|
135
|
+
/// index in the list WITHOUT the moved element. The single mutation
|
|
136
|
+
/// primitive both the edge path and any displacement path route through,
|
|
137
|
+
/// so every platform lands on identical arithmetic.
|
|
138
|
+
public static func moveIndex(_ list: [T], from: Int, to: Int) -> [T] {
|
|
139
|
+
var out = list
|
|
140
|
+
let moved = out.remove(at: from)
|
|
141
|
+
out.insert(moved, at: max(0, min(to, out.count)))
|
|
142
|
+
return out
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/// Pure reorder: move `dragKey` next to `dropKey`, inserting on `edge`'s
|
|
146
|
+
/// side. Returns `nil` when the move is a no-op (same key, or either key
|
|
147
|
+
/// missing) — matching the TypeScript `performReorder` guards exactly.
|
|
148
|
+
///
|
|
149
|
+
/// The insert-index arithmetic is a verbatim port of the web engine; that
|
|
150
|
+
/// is what makes a drop produce the same array on all three targets.
|
|
151
|
+
public func reordered(dragKey: String, dropKey: String, edge: PyreonDropEdge) -> [T]? {
|
|
152
|
+
if dragKey == dropKey { return nil }
|
|
153
|
+
let current = itemsProvider()
|
|
154
|
+
guard
|
|
155
|
+
let dragIndex = current.firstIndex(where: { keyOf($0) == dragKey }),
|
|
156
|
+
let dropIndex = current.firstIndex(where: { keyOf($0) == dropKey })
|
|
157
|
+
else { return nil }
|
|
158
|
+
|
|
159
|
+
let after = edge == .bottom || edge == .right
|
|
160
|
+
let rawInsert: Int
|
|
161
|
+
if after {
|
|
162
|
+
rawInsert = dropIndex >= dragIndex ? dropIndex : dropIndex + 1
|
|
163
|
+
} else {
|
|
164
|
+
rawInsert = dropIndex <= dragIndex ? dropIndex : dropIndex - 1
|
|
165
|
+
}
|
|
166
|
+
return PyreonSortableState.moveIndex(current, from: dragIndex, to: rawInsert)
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/// The edge nearest a drop `point` inside a view of `size`, on this
|
|
170
|
+
/// state's axis. Vertical lists split top/bottom at the midline;
|
|
171
|
+
/// horizontal lists split left/right.
|
|
172
|
+
public func edgeAt(_ point: CGPoint, in size: CGSize) -> PyreonDropEdge {
|
|
173
|
+
switch axis {
|
|
174
|
+
case .vertical:
|
|
175
|
+
return point.y < size.height / 2 ? .top : .bottom
|
|
176
|
+
case .horizontal:
|
|
177
|
+
return point.x < size.width / 2 ? .left : .right
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
// ─────────────────────────── SwiftUI binding ────────────────────────────────
|
|
183
|
+
|
|
184
|
+
/// The per-item drag/drop wiring. A `ViewModifier` (not a bare `View`
|
|
185
|
+
/// extension) because it needs its OWN `@State` for the measured item size:
|
|
186
|
+
/// the drop `location` arrives in the item's LOCAL space, and the edge is
|
|
187
|
+
/// meaningless without the item's height/width.
|
|
188
|
+
@available(iOS 17.0, macOS 14.0, *)
|
|
189
|
+
public struct PyreonSortableItemModifier<T>: ViewModifier {
|
|
190
|
+
private let state: PyreonSortableState<T>
|
|
191
|
+
private let key: String
|
|
192
|
+
@State private var measured: CGSize = .zero
|
|
193
|
+
|
|
194
|
+
public init(state: PyreonSortableState<T>, key: String) {
|
|
195
|
+
self.state = state
|
|
196
|
+
self.key = key
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
public func body(content: Content) -> some View {
|
|
200
|
+
content
|
|
201
|
+
.background(
|
|
202
|
+
GeometryReader { proxy in
|
|
203
|
+
Color.clear
|
|
204
|
+
.onAppear { measured = proxy.size }
|
|
205
|
+
.onChange(of: proxy.size) { _, next in measured = next }
|
|
206
|
+
}
|
|
207
|
+
)
|
|
208
|
+
// The lifted row dims — the standard iOS drag affordance.
|
|
209
|
+
.opacity(state.isActive(key) ? 0.5 : 1)
|
|
210
|
+
.draggable(key) {
|
|
211
|
+
// The drag preview. Returning `content` would re-enter this
|
|
212
|
+
// modifier; a neutral chip keeps the preview cheap.
|
|
213
|
+
Text(key).padding(6)
|
|
214
|
+
}
|
|
215
|
+
.dropDestination(for: String.self) { payload, location in
|
|
216
|
+
guard let source = payload.first else { return false }
|
|
217
|
+
return state.drop(
|
|
218
|
+
source: source,
|
|
219
|
+
on: key,
|
|
220
|
+
edge: state.edgeAt(location, in: measured)
|
|
221
|
+
)
|
|
222
|
+
} isTargeted: { targeted in
|
|
223
|
+
if targeted {
|
|
224
|
+
// The precise edge arrives with the drop location; on
|
|
225
|
+
// entry seed the leading edge so a hover highlight has
|
|
226
|
+
// something to read.
|
|
227
|
+
state.dragOver(key, edge: state.axis == .vertical ? .top : .left)
|
|
228
|
+
} else {
|
|
229
|
+
state.dragLeave(key)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
@available(iOS 17.0, macOS 14.0, *)
|
|
236
|
+
extension View {
|
|
237
|
+
/// Make this view a sortable item of `state`, identified by `key`.
|
|
238
|
+
/// Emitted by PMTC for `ref={s.itemRef(key)}`.
|
|
239
|
+
public func pyreonSortableItem<T>(
|
|
240
|
+
_ state: PyreonSortableState<T>,
|
|
241
|
+
key: String
|
|
242
|
+
) -> some View {
|
|
243
|
+
modifier(PyreonSortableItemModifier(state: state, key: key))
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/// Mark this view as the sortable CONTAINER of `state`. Emitted by PMTC
|
|
247
|
+
/// for `ref={s.containerRef}`. It groups the items for assistive
|
|
248
|
+
/// technology and carries the reorder hint; the per-item modifier owns the
|
|
249
|
+
/// drag itself.
|
|
250
|
+
public func pyreonSortableContainer<T>(
|
|
251
|
+
_ state: PyreonSortableState<T>
|
|
252
|
+
) -> some View {
|
|
253
|
+
accessibilityElement(children: .contain)
|
|
254
|
+
.accessibilityHint(Text("Drag an item to reorder the list"))
|
|
255
|
+
}
|
|
256
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pyreon/dnd",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.52.0",
|
|
4
4
|
"description": "Signal-driven drag and drop for Pyreon — wraps @atlaskit/pragmatic-drag-and-drop",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"homepage": "https://github.com/pyreon/pyreon/tree/main/packages/fundamentals/dnd#readme",
|
|
@@ -22,38 +22,55 @@
|
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
24
|
"lib",
|
|
25
|
+
"!lib/analysis",
|
|
25
26
|
"README.md",
|
|
26
|
-
"LICENSE"
|
|
27
|
+
"LICENSE",
|
|
28
|
+
"native/swift",
|
|
29
|
+
"native/kotlin"
|
|
27
30
|
],
|
|
28
31
|
"scripts": {
|
|
29
32
|
"build": "vl_rolldown_build",
|
|
30
33
|
"dev": "vl_rolldown_build-watch",
|
|
31
|
-
"bench": "bun bench/dnd-wrapper-tax.ts",
|
|
34
|
+
"bench": "NODE_ENV=production bun bench/dnd-wrapper-tax.ts",
|
|
32
35
|
"test": "vitest run",
|
|
33
36
|
"test:browser": "vitest run --config ./vitest.browser.config.ts",
|
|
34
37
|
"typecheck": "tsc --noEmit",
|
|
35
38
|
"lint": "oxlint ."
|
|
36
39
|
},
|
|
37
40
|
"peerDependencies": {
|
|
38
|
-
"@pyreon/core": "^0.
|
|
39
|
-
"@pyreon/reactivity": "^0.
|
|
41
|
+
"@pyreon/core": "^0.52.0",
|
|
42
|
+
"@pyreon/reactivity": "^0.52.0"
|
|
40
43
|
},
|
|
41
44
|
"dependencies": {
|
|
42
|
-
"@atlaskit/pragmatic-drag-and-drop": "^
|
|
43
|
-
"@atlaskit/pragmatic-drag-and-drop-auto-scroll": "^3.
|
|
44
|
-
"@atlaskit/pragmatic-drag-and-drop-hitbox": "^
|
|
45
|
-
"@pyreon/a11y": "^0.
|
|
45
|
+
"@atlaskit/pragmatic-drag-and-drop": "^4.0.0",
|
|
46
|
+
"@atlaskit/pragmatic-drag-and-drop-auto-scroll": "^3.2.1",
|
|
47
|
+
"@atlaskit/pragmatic-drag-and-drop-hitbox": "^3.0.0",
|
|
48
|
+
"@pyreon/a11y": "^0.52.0"
|
|
46
49
|
},
|
|
47
50
|
"devDependencies": {
|
|
48
|
-
"@happy-dom/global-registrator": "^20.
|
|
49
|
-
"@pyreon/manifest": "0.
|
|
50
|
-
"@pyreon/test-utils": "^0.
|
|
51
|
-
"@pyreon/tsconfig": "0.
|
|
52
|
-
"@pyreon/typescript": "^0.
|
|
53
|
-
"@pyreon/vitest-config": "0.
|
|
54
|
-
"@vitest/browser-playwright": "^
|
|
51
|
+
"@happy-dom/global-registrator": "^20.14.5",
|
|
52
|
+
"@pyreon/manifest": "0.50.0",
|
|
53
|
+
"@pyreon/test-utils": "^0.50.2",
|
|
54
|
+
"@pyreon/tsconfig": "0.50.2",
|
|
55
|
+
"@pyreon/typescript": "^0.52.0",
|
|
56
|
+
"@pyreon/vitest-config": "0.50.0",
|
|
57
|
+
"@vitest/browser-playwright": "^5.0.2"
|
|
55
58
|
},
|
|
56
59
|
"publishConfig": {
|
|
57
60
|
"access": "public"
|
|
61
|
+
},
|
|
62
|
+
"pyreon": {
|
|
63
|
+
"native": {
|
|
64
|
+
"swift": "native/swift",
|
|
65
|
+
"kotlin": "native/kotlin",
|
|
66
|
+
"kotlinServices": {
|
|
67
|
+
"PyreonSortable": [
|
|
68
|
+
"PyreonSortable.kt"
|
|
69
|
+
]
|
|
70
|
+
},
|
|
71
|
+
"kotlinSdkOnly": [
|
|
72
|
+
"PyreonSortableModifier.kt"
|
|
73
|
+
]
|
|
74
|
+
}
|
|
58
75
|
}
|
|
59
76
|
}
|