craft-native 0.0.276 → 0.0.290
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 +16 -0
- package/dist/android/src/emulator.d.ts +4 -0
- package/dist/android/src/index.js +15 -6
- package/dist/android/templates/CraftNativeMutation.kt.template +35 -0
- package/dist/android/templates/CraftNativeShell.kt.template +12 -5
- package/dist/android/templates/CraftSnapshotFile.kt.template +26 -0
- package/dist/android/templates/MainActivityNative.kt.template +16 -0
- package/dist/api/index.d.ts +2 -2
- package/dist/api/mobile.d.ts +25 -0
- package/dist/browser/storage.d.ts +5 -3
- package/dist/browser.d.ts +1 -0
- package/dist/browser.js +31 -7
- package/dist/cli.js +131 -28
- package/dist/index.cjs +177 -27
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +181 -28
- package/dist/ios/src/index.js +7 -3
- package/dist/ios/templates/CraftApp.swift +44 -5
- package/dist/ios/templates/CraftHybrid.swift +21 -0
- package/dist/ios/templates/CraftNativeActions.swift +6 -0
- package/dist/ios/templates/CraftNativeFiles.swift +136 -0
- package/dist/ios/templates/CraftNativeMutation.swift +51 -0
- package/dist/ios/templates/CraftNativeScreen.swift +116 -8
- package/dist/ios/templates/CraftWatchApp.swift.template +303 -9
- package/dist/ios/templates/WatchExtension.Info.plist.template +1 -0
- package/dist/macos/templates/CraftNativeMutation.swift +51 -0
- package/dist/mobile.js +50 -0
- package/dist/package.d.ts +29 -3
- package/dist/types.d.ts +32 -0
- package/package.json +1 -1
|
@@ -115,6 +115,9 @@ final class CraftNativeMutationDocument {
|
|
|
115
115
|
// A child crossing parents needs the committed tree to be
|
|
116
116
|
// reconciled so the host can move its existing native view.
|
|
117
117
|
requiresFullRender = true
|
|
118
|
+
case "replaceChildren":
|
|
119
|
+
let parentId = try replaceChildren(operation, nodes: &candidateNodes)
|
|
120
|
+
appendUnique(parentId, to: &affectedNodeIds)
|
|
118
121
|
case "removeNode":
|
|
119
122
|
if let id = operation["id"] as? String, let parent = candidateNodes[id]?.parent {
|
|
120
123
|
appendUnique(parent, to: &affectedNodeIds)
|
|
@@ -287,6 +290,54 @@ final class CraftNativeMutationDocument {
|
|
|
287
290
|
return previousParentId
|
|
288
291
|
}
|
|
289
292
|
|
|
293
|
+
/// Replaces a container's ordered child snapshot in one atomic mutation.
|
|
294
|
+
///
|
|
295
|
+
/// The list protocol uses this operation when a keyed `:for` expansion
|
|
296
|
+
/// produces a new row set. Existing nodes may stay attached to the same
|
|
297
|
+
/// parent; removed children become detached and can be reused or removed
|
|
298
|
+
/// by later operations in the same batch.
|
|
299
|
+
private func replaceChildren(_ operation: [String: Any], nodes: inout [String: Node]) throws -> String {
|
|
300
|
+
let parentId = try requiredId(operation, field: "parentId", name: "replaceChildren")
|
|
301
|
+
guard var parent = nodes[parentId] else {
|
|
302
|
+
throw CraftNativeMutationFailure("UNKNOWN_PARENT", "node \(parentId) does not exist")
|
|
303
|
+
}
|
|
304
|
+
guard acceptsChildren(parent.type) else {
|
|
305
|
+
throw CraftNativeMutationFailure("INVALID_PARENT", "node \(parentId) cannot contain child nodes")
|
|
306
|
+
}
|
|
307
|
+
guard let values = operation["children"] as? [Any] else {
|
|
308
|
+
throw CraftNativeMutationFailure("INVALID_CHILDREN", "replaceChildren.children must be an array")
|
|
309
|
+
}
|
|
310
|
+
var childIds: [String] = []
|
|
311
|
+
for value in values {
|
|
312
|
+
guard let childId = value as? String, !childId.isEmpty else {
|
|
313
|
+
throw CraftNativeMutationFailure("INVALID_CHILDREN", "replaceChildren.children must contain non-empty ids")
|
|
314
|
+
}
|
|
315
|
+
guard !childIds.contains(childId) else {
|
|
316
|
+
throw CraftNativeMutationFailure("DUPLICATE_CHILD", "child \(childId) appears more than once")
|
|
317
|
+
}
|
|
318
|
+
guard let child = nodes[childId] else {
|
|
319
|
+
throw CraftNativeMutationFailure("UNKNOWN_NODE", "node \(childId) does not exist")
|
|
320
|
+
}
|
|
321
|
+
guard childId != parentId, !descendants(of: childId, nodes: nodes).contains(parentId) else {
|
|
322
|
+
throw CraftNativeMutationFailure("CYCLE", "attaching node \(childId) would create a cycle")
|
|
323
|
+
}
|
|
324
|
+
guard child.parent == nil || child.parent == parentId else {
|
|
325
|
+
throw CraftNativeMutationFailure("NODE_ATTACHED", "node \(childId) already has a parent")
|
|
326
|
+
}
|
|
327
|
+
childIds.append(childId)
|
|
328
|
+
}
|
|
329
|
+
let removedChildIds = parent.children.filter { !childIds.contains($0) }
|
|
330
|
+
for oldChildId in removedChildIds {
|
|
331
|
+
for descendant in descendants(of: oldChildId, nodes: nodes).union([oldChildId]) {
|
|
332
|
+
nodes.removeValue(forKey: descendant)
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
parent.children = childIds
|
|
336
|
+
nodes[parentId] = parent
|
|
337
|
+
for childId in childIds { nodes[childId]?.parent = parentId }
|
|
338
|
+
return parentId
|
|
339
|
+
}
|
|
340
|
+
|
|
290
341
|
private func removeNode(
|
|
291
342
|
_ operation: [String: Any],
|
|
292
343
|
nodes: inout [String: Node],
|
|
@@ -5101,6 +5101,17 @@ private final class CraftNativeTextView: UITextView {
|
|
|
5101
5101
|
|
|
5102
5102
|
override func layoutSubviews() {
|
|
5103
5103
|
super.layoutSubviews()
|
|
5104
|
+
// A multiline input grows with its text up to its `maxHeight`, then
|
|
5105
|
+
// scrolls inside itself. Never scrolling, the text past that height
|
|
5106
|
+
// was cut off and could not be reached (a long workout comment).
|
|
5107
|
+
// With scrolling off, contentSize is the view's own height: measure
|
|
5108
|
+
// the text itself.
|
|
5109
|
+
let needed = isEditable ? sizeThatFits(CGSize(width: bounds.width, height: .greatestFiniteMagnitude)).height : 0
|
|
5110
|
+
let overflows = isEditable && bounds.height > 0 && needed > bounds.height + 1
|
|
5111
|
+
if isScrollEnabled != overflows {
|
|
5112
|
+
isScrollEnabled = overflows
|
|
5113
|
+
if overflows, isFirstResponder { scrollRangeToVisible(selectedRange) }
|
|
5114
|
+
}
|
|
5104
5115
|
let inset = textContainerInset
|
|
5105
5116
|
let padding = textContainer.lineFragmentPadding
|
|
5106
5117
|
placeholderLabel.frame = CGRect(
|
|
@@ -5117,6 +5128,7 @@ private final class CraftNativeTextView: UITextView {
|
|
|
5117
5128
|
}
|
|
5118
5129
|
|
|
5119
5130
|
private func configurePlaceholder() {
|
|
5131
|
+
isScrollEnabled = false
|
|
5120
5132
|
placeholderLabel.numberOfLines = 0
|
|
5121
5133
|
placeholderLabel.font = font
|
|
5122
5134
|
placeholderLabel.textColor = .placeholderText
|
|
@@ -5623,6 +5635,7 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
5623
5635
|
// A tab prepared in a hidden stack "appears" to UIKit as well; the
|
|
5624
5636
|
// shell says when it really shows (screenVisibilityChanged).
|
|
5625
5637
|
if isShownOnScreen {
|
|
5638
|
+
hasAppeared = true
|
|
5626
5639
|
screenVisibilityChanged(true)
|
|
5627
5640
|
// A navigation breadcrumb for the crash reporter: the trail a
|
|
5628
5641
|
// crash report carries is the screens that led to it.
|
|
@@ -5694,6 +5707,14 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
5694
5707
|
// preview also restores a screen that never lost focus, so restore its
|
|
5695
5708
|
// chrome even when no new SCREEN_FOCUS event is needed.
|
|
5696
5709
|
if visible { restoreShellChrome() }
|
|
5710
|
+
// A prepared tab the shell now shows has appeared once its frame is
|
|
5711
|
+
// drawn; what its script asks for until then is there from the start.
|
|
5712
|
+
if visible, !hasAppeared {
|
|
5713
|
+
DispatchQueue.main.async { [weak self] in
|
|
5714
|
+
guard let self, self.isViewLoaded, self.isShownOnScreen else { return }
|
|
5715
|
+
self.hasAppeared = true
|
|
5716
|
+
}
|
|
5717
|
+
}
|
|
5697
5718
|
if visible, frameWanted { CraftNativeFrameClock.shared.request(self) { [weak self] in self?.fireFrame() } }
|
|
5698
5719
|
if !visible { CraftNativeFrameClock.shared.cancel(self) }
|
|
5699
5720
|
guard visible != reportedVisible else { return }
|
|
@@ -5780,6 +5801,27 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
5780
5801
|
super.viewDidLayoutSubviews()
|
|
5781
5802
|
positionModals()
|
|
5782
5803
|
emitLayoutEvents()
|
|
5804
|
+
sendViewportIfChanged()
|
|
5805
|
+
}
|
|
5806
|
+
|
|
5807
|
+
/// The width the bundle last heard, so a layout pass at the same size says nothing.
|
|
5808
|
+
private var reportedViewportWidth: CGFloat = -1
|
|
5809
|
+
|
|
5810
|
+
/// The screen's own width once laid out, else its window's: what a web
|
|
5811
|
+
/// page's media queries would measure.
|
|
5812
|
+
private func currentViewportWidth() -> CGFloat {
|
|
5813
|
+
let width = view.bounds.width > 0 ? view.bounds.width : (view.window ?? CraftPresenter.keyWindow())?.bounds.width ?? 0
|
|
5814
|
+
reportedViewportWidth = width
|
|
5815
|
+
return width
|
|
5816
|
+
}
|
|
5817
|
+
|
|
5818
|
+
/// A rotation, iPad split view or a resized Stage Manager window: the
|
|
5819
|
+
/// bundle's width classes (`md:`, `lg:`) follow the screen's new width.
|
|
5820
|
+
private func sendViewportIfChanged() {
|
|
5821
|
+
let width = view.bounds.width
|
|
5822
|
+
guard width > 0, abs(width - reportedViewportWidth) >= 0.5 else { return }
|
|
5823
|
+
reportedViewportWidth = width
|
|
5824
|
+
send(type: "VIEWPORT", payload: ["width": Double(width), "height": Double(view.bounds.height)])
|
|
5783
5825
|
}
|
|
5784
5826
|
|
|
5785
5827
|
/// Modals cover the screen; their content starts below its bars (the
|
|
@@ -5864,6 +5906,8 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
5864
5906
|
let appearanceJSON: String
|
|
5865
5907
|
let sessionTokenKey: String
|
|
5866
5908
|
let focused: Bool?
|
|
5909
|
+
/// The screen's width in points, for the bundle's `sm:`/`md:`/`lg:` classes.
|
|
5910
|
+
let viewportWidth: Double
|
|
5867
5911
|
}
|
|
5868
5912
|
|
|
5869
5913
|
private func scriptInputs() -> ScriptInputs {
|
|
@@ -5875,7 +5919,8 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
5875
5919
|
colorScheme: colorScheme,
|
|
5876
5920
|
appearanceJSON: appearanceJSON(),
|
|
5877
5921
|
sessionTokenKey: Self.jsonString(CraftSession.tokenKey(config)),
|
|
5878
|
-
focused: reportedVisible
|
|
5922
|
+
focused: reportedVisible,
|
|
5923
|
+
viewportWidth: Double(currentViewportWidth())
|
|
5879
5924
|
)
|
|
5880
5925
|
}
|
|
5881
5926
|
|
|
@@ -5977,6 +6022,7 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
5977
6022
|
capabilities: \(capabilityJSON),
|
|
5978
6023
|
initialAppState: "\(inputs.appState)",
|
|
5979
6024
|
colorScheme: "\(inputs.colorScheme)",
|
|
6025
|
+
viewportWidth: \(inputs.viewportWidth),
|
|
5980
6026
|
appearance: \(inputs.appearanceJSON),
|
|
5981
6027
|
appURL: \(appURL),
|
|
5982
6028
|
appVersion: \(appVersion),
|
|
@@ -6546,6 +6592,14 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
6546
6592
|
/// own horizontal pager or chart gesture.
|
|
6547
6593
|
private(set) var tabSwipeEnabled = true
|
|
6548
6594
|
private var prefersLargeTitle: Bool?
|
|
6595
|
+
/// Whether the screen has been on screen. Until then, a bar its script
|
|
6596
|
+
/// asks for is simply there in its first frame: a tab opened for the
|
|
6597
|
+
/// first time (HQ's Me) mounts in the window, and its bar and large title
|
|
6598
|
+
/// slid in from the side as the screen appeared.
|
|
6599
|
+
private var hasAppeared = false
|
|
6600
|
+
/// A bar shown or hidden after the screen has appeared animates; before,
|
|
6601
|
+
/// it is in place from the start.
|
|
6602
|
+
var animatesNavigationBarChanges: Bool { hasAppeared && view.window != nil }
|
|
6549
6603
|
/// Whether this screen opts into an opaque navigation bar filled with its
|
|
6550
6604
|
/// own background colour. The default keeps UIKit's material appearance.
|
|
6551
6605
|
private var navigationBarSolid = false
|
|
@@ -6624,7 +6678,7 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
6624
6678
|
if let hidden = options["hidden"] as? Bool {
|
|
6625
6679
|
navigationBarHidden = hidden
|
|
6626
6680
|
if navigationController?.topViewController === self {
|
|
6627
|
-
navigationController?.setNavigationBarHidden(hidden, animated:
|
|
6681
|
+
navigationController?.setNavigationBarHidden(hidden, animated: animatesNavigationBarChanges)
|
|
6628
6682
|
}
|
|
6629
6683
|
}
|
|
6630
6684
|
if options.keys.contains("leftButtons") {
|
|
@@ -6642,7 +6696,7 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
6642
6696
|
// Asked for after the bar was hidden for a bare root screen.
|
|
6643
6697
|
if navigationBarHidden == nil, wantsNavigationBar, navigationController?.topViewController === self,
|
|
6644
6698
|
navigationController?.isNavigationBarHidden == true {
|
|
6645
|
-
navigationController?.setNavigationBarHidden(false, animated:
|
|
6699
|
+
navigationController?.setNavigationBarHidden(false, animated: animatesNavigationBarChanges)
|
|
6646
6700
|
}
|
|
6647
6701
|
}
|
|
6648
6702
|
|
|
@@ -7104,13 +7158,39 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
7104
7158
|
/// `input`, scrolled into the visible part of the scroll view it is in.
|
|
7105
7159
|
/// Internal for XCTest.
|
|
7106
7160
|
func scrollIntoView(_ input: UIView, animated: Bool) {
|
|
7107
|
-
|
|
7108
|
-
|
|
7109
|
-
|
|
7110
|
-
|
|
7161
|
+
guard let scroll = enclosingVerticalScroll(of: input) else { return }
|
|
7162
|
+
var rect = input.convert(input.bounds, to: scroll).insetBy(dx: 0, dy: -16)
|
|
7163
|
+
// A field taller than the part the keyboard leaves (a long comment
|
|
7164
|
+
// with no max height) shows its caret rather than its top: scrolled
|
|
7165
|
+
// to its top, the caret at the end of the text stayed under the
|
|
7166
|
+
// keyboard with no way to scroll it out.
|
|
7167
|
+
let visible = scroll.bounds.inset(by: scroll.adjustedContentInset)
|
|
7168
|
+
if rect.height > visible.height, let caret = caretRect(of: input, in: scroll) { rect = caret }
|
|
7111
7169
|
scroll.scrollRectToVisible(rect, animated: animated)
|
|
7112
7170
|
}
|
|
7113
7171
|
|
|
7172
|
+
/// The vertical scroll view `view` sits in, if any.
|
|
7173
|
+
private func enclosingVerticalScroll(of view: UIView) -> CraftNativeScrollView? {
|
|
7174
|
+
var ancestor = view.superview
|
|
7175
|
+
while let current = ancestor, !(current is CraftNativeScrollView) { ancestor = current.superview }
|
|
7176
|
+
guard let scroll = ancestor as? CraftNativeScrollView, scroll.isVertical else { return nil }
|
|
7177
|
+
return scroll
|
|
7178
|
+
}
|
|
7179
|
+
|
|
7180
|
+
/// A focused input's caret in `scroll`'s coordinates, with a little room
|
|
7181
|
+
/// around it, kept within the input (one scrolling inside itself has its
|
|
7182
|
+
/// caret beyond its own frame until it scrolls to it).
|
|
7183
|
+
private func caretRect(of input: UIView, in scroll: UIScrollView) -> CGRect? {
|
|
7184
|
+
guard input.isFirstResponder, let text = input as? UITextInput,
|
|
7185
|
+
let end = text.selectedTextRange?.end else { return nil }
|
|
7186
|
+
let caret = text.caretRect(for: end)
|
|
7187
|
+
guard caret.minY.isFinite, caret.height > 0 else { return nil }
|
|
7188
|
+
let frame = input.convert(input.bounds, to: scroll)
|
|
7189
|
+
var rect = input.convert(caret, to: scroll)
|
|
7190
|
+
rect.origin.y = min(max(rect.minY, frame.minY), max(frame.minY, frame.maxY - rect.height))
|
|
7191
|
+
return rect.insetBy(dx: 0, dy: -16)
|
|
7192
|
+
}
|
|
7193
|
+
|
|
7114
7194
|
private func sendAppState(_ state: String) {
|
|
7115
7195
|
// A screen hears it while it is on top of its stack; the app script,
|
|
7116
7196
|
// which is on no stack, always.
|
|
@@ -8977,7 +9057,8 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
8977
9057
|
if textView.textContainerInset != padding { textView.textContainerInset = padding }
|
|
8978
9058
|
textView.textContainer.lineFragmentPadding = 0
|
|
8979
9059
|
}
|
|
8980
|
-
|
|
9060
|
+
// An input's own scrolling follows its height (CraftNativeTextView).
|
|
9061
|
+
if !(textView is CraftNativeTextView) { textView.isScrollEnabled = false }
|
|
8981
9062
|
let numberOfLines = Int(number(props["numberOfLines"]) ?? 0)
|
|
8982
9063
|
if numberOfLines > 0 {
|
|
8983
9064
|
textView.textContainer.maximumNumberOfLines = numberOfLines
|
|
@@ -9846,11 +9927,38 @@ final class CraftNativeScreenController: UIViewController, UIScrollViewDelegate,
|
|
|
9846
9927
|
let id = ObjectIdentifier(textView)
|
|
9847
9928
|
inputIdentities[id].map { inputDrafts[$0] = textView.text ?? "" }
|
|
9848
9929
|
(textView as? CraftNativeTextView)?.refreshPlaceholder()
|
|
9930
|
+
keepCaretVisible(in: textView)
|
|
9849
9931
|
guard let handler = handlers[id] else { return }
|
|
9850
9932
|
noteSentText(textView.text ?? "", for: textView)
|
|
9851
9933
|
send(type: "EVENT", payload: ["handlerName": handler, "nativeEvent": ["text": textView.text ?? ""]])
|
|
9852
9934
|
}
|
|
9853
9935
|
|
|
9936
|
+
func textViewDidChangeSelection(_ textView: UITextView) {
|
|
9937
|
+
guard textView.isFirstResponder else { return }
|
|
9938
|
+
keepCaretVisible(in: textView)
|
|
9939
|
+
}
|
|
9940
|
+
|
|
9941
|
+
/// The caret, kept where it can be seen as a multiline input grows or the
|
|
9942
|
+
/// caret moves: in the part of the screen's scroll view the keyboard
|
|
9943
|
+
/// leaves. Only the field was scrolled into view, when editing began, so
|
|
9944
|
+
/// a long comment typed at the bottom of a screen went under the keyboard.
|
|
9945
|
+
private func keepCaretVisible(in textView: UITextView) {
|
|
9946
|
+
DispatchQueue.main.async { [weak self, weak textView] in
|
|
9947
|
+
guard let self, let textView, textView.isFirstResponder else { return }
|
|
9948
|
+
// The input grows with its text first: a text view's new lines
|
|
9949
|
+
// are news to the flex layout around it, which sized the scroll
|
|
9950
|
+
// view's content to the old height.
|
|
9951
|
+
textView.invalidateIntrinsicContentSize()
|
|
9952
|
+
CraftNativeFlexLayout.invalidateAncestors(of: textView)
|
|
9953
|
+
self.view.layoutIfNeeded()
|
|
9954
|
+
guard let scroll = self.enclosingVerticalScroll(of: textView),
|
|
9955
|
+
let rect = self.caretRect(of: textView, in: scroll) else { return }
|
|
9956
|
+
let visible = scroll.bounds.inset(by: scroll.adjustedContentInset)
|
|
9957
|
+
guard !visible.contains(rect) else { return }
|
|
9958
|
+
scroll.scrollRectToVisible(rect, animated: false)
|
|
9959
|
+
}
|
|
9960
|
+
}
|
|
9961
|
+
|
|
9854
9962
|
func textViewDidBeginEditing(_ textView: UITextView) {
|
|
9855
9963
|
lastFocusedInput = textView
|
|
9856
9964
|
if keyboardScreenFrame != nil { DispatchQueue.main.async { self.scrollFocusedInputIntoView(animated: true) } }
|