@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
|
@@ -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.53.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",
|
|
@@ -18,42 +18,74 @@
|
|
|
18
18
|
".": {
|
|
19
19
|
"import": "./lib/index.js",
|
|
20
20
|
"types": "./lib/types/index.d.ts"
|
|
21
|
+
},
|
|
22
|
+
"./native-plugin": {
|
|
23
|
+
"import": "./lib/native-plugin.js",
|
|
24
|
+
"types": "./lib/types/native-plugin.d.ts"
|
|
21
25
|
}
|
|
22
26
|
},
|
|
23
27
|
"files": [
|
|
24
28
|
"lib",
|
|
29
|
+
"!lib/analysis",
|
|
25
30
|
"README.md",
|
|
26
|
-
"LICENSE"
|
|
31
|
+
"LICENSE",
|
|
32
|
+
"native/swift",
|
|
33
|
+
"native/kotlin"
|
|
27
34
|
],
|
|
28
35
|
"scripts": {
|
|
29
36
|
"build": "vl_rolldown_build",
|
|
30
37
|
"dev": "vl_rolldown_build-watch",
|
|
31
|
-
"bench": "bun bench/dnd-wrapper-tax.ts",
|
|
38
|
+
"bench": "NODE_ENV=production bun bench/dnd-wrapper-tax.ts",
|
|
32
39
|
"test": "vitest run",
|
|
33
40
|
"test:browser": "vitest run --config ./vitest.browser.config.ts",
|
|
34
41
|
"typecheck": "tsc --noEmit",
|
|
35
42
|
"lint": "oxlint ."
|
|
36
43
|
},
|
|
37
44
|
"peerDependencies": {
|
|
38
|
-
"@pyreon/core": "^0.
|
|
39
|
-
"@pyreon/reactivity": "^0.
|
|
45
|
+
"@pyreon/core": "^0.53.0",
|
|
46
|
+
"@pyreon/reactivity": "^0.53.0",
|
|
47
|
+
"@pyreon/native-compiler": "^0.53.0"
|
|
48
|
+
},
|
|
49
|
+
"peerDependenciesMeta": {
|
|
50
|
+
"@pyreon/native-compiler": {
|
|
51
|
+
"optional": true
|
|
52
|
+
}
|
|
40
53
|
},
|
|
41
54
|
"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.
|
|
55
|
+
"@atlaskit/pragmatic-drag-and-drop": "^4.0.0",
|
|
56
|
+
"@atlaskit/pragmatic-drag-and-drop-auto-scroll": "^3.2.1",
|
|
57
|
+
"@atlaskit/pragmatic-drag-and-drop-hitbox": "^3.0.0",
|
|
58
|
+
"@pyreon/a11y": "^0.53.0"
|
|
46
59
|
},
|
|
47
60
|
"devDependencies": {
|
|
48
|
-
"@happy-dom/global-registrator": "^20.
|
|
61
|
+
"@happy-dom/global-registrator": "^20.14.5",
|
|
49
62
|
"@pyreon/manifest": "0.50.0",
|
|
50
|
-
"@pyreon/
|
|
51
|
-
"@pyreon/
|
|
52
|
-
"@pyreon/
|
|
63
|
+
"@pyreon/native-compiler": "0.53.0",
|
|
64
|
+
"@pyreon/test-utils": "^0.50.3",
|
|
65
|
+
"@pyreon/tsconfig": "0.50.3",
|
|
66
|
+
"@pyreon/typescript": "^0.53.0",
|
|
53
67
|
"@pyreon/vitest-config": "0.50.0",
|
|
54
|
-
"@vitest/browser-playwright": "^
|
|
68
|
+
"@vitest/browser-playwright": "^5.0.2"
|
|
55
69
|
},
|
|
56
70
|
"publishConfig": {
|
|
57
71
|
"access": "public"
|
|
72
|
+
},
|
|
73
|
+
"pyreon": {
|
|
74
|
+
"native": {
|
|
75
|
+
"swift": "native/swift",
|
|
76
|
+
"kotlin": "native/kotlin",
|
|
77
|
+
"kotlinServices": {
|
|
78
|
+
"PyreonSortable": [
|
|
79
|
+
"PyreonSortable.kt"
|
|
80
|
+
]
|
|
81
|
+
},
|
|
82
|
+
"kotlinSdkOnly": [
|
|
83
|
+
"PyreonSortableModifier.kt"
|
|
84
|
+
],
|
|
85
|
+
"plugin": "./lib/native-plugin.js",
|
|
86
|
+
"modules": [
|
|
87
|
+
"@pyreon/dnd"
|
|
88
|
+
]
|
|
89
|
+
}
|
|
58
90
|
}
|
|
59
91
|
}
|