craft-native 0.0.394 → 0.0.396

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.
@@ -35,6 +35,9 @@ final class CraftNativeMutationDocument {
35
35
  var style: [String: Any]
36
36
  var events: [String: Any]
37
37
  var text: [String]
38
+ // Legacy snapshots may interleave bare text and native children.
39
+ // Keep each text fragment's slot among the child nodes on import.
40
+ var textPositions: [Int]? = nil
38
41
  var children: [String]
39
42
  var parent: String?
40
43
  }
@@ -43,6 +46,11 @@ final class CraftNativeMutationDocument {
43
46
  private var rootId: String?
44
47
  private(set) var revision = 0
45
48
 
49
+ /// The normalized committed tree, whose root need not be named "root".
50
+ var document: [String: Any]? {
51
+ rootId.flatMap { materialize($0, nodes: nodes) }
52
+ }
53
+
46
54
  func replace(with document: [String: Any]) {
47
55
  var replacement: [String: Node] = [:]
48
56
  let root = importNode(document, preferredId: "root", parent: nil, nodes: &replacement)
@@ -217,6 +225,7 @@ final class CraftNativeMutationDocument {
217
225
  if patch.keys.contains("events") { mergePatch(patch["events"], into: &node.events) }
218
226
  if patch.keys.contains("children") || patch.keys.contains("text") {
219
227
  node.text = try textChildren(patch["children"] ?? patch["text"])
228
+ node.textPositions = nil
220
229
  }
221
230
  nodes[id] = node
222
231
  }
@@ -422,9 +431,18 @@ final class CraftNativeMutationDocument {
422
431
  "events": node.events,
423
432
  ]
424
433
  if let key = node.key { value["key"] = key }
425
- let text: [Any] = node.text
426
434
  let children: [Any] = node.children.compactMap { materialize($0, nodes: nodes) }
427
- value["children"] = text + children
435
+ var content: [Any] = []
436
+ var textIndex = 0
437
+ for index in 0...children.count {
438
+ while textIndex < node.text.count,
439
+ min(node.textPositions?[textIndex] ?? 0, children.count) == index {
440
+ content.append(node.text[textIndex])
441
+ textIndex += 1
442
+ }
443
+ if index < children.count { content.append(children[index]) }
444
+ }
445
+ value["children"] = content
428
446
  return value
429
447
  }
430
448
 
@@ -440,6 +458,12 @@ final class CraftNativeMutationDocument {
440
458
  var id = explicit ?? key.map { "\(parent ?? "root")/key:\($0)" } ?? preferredId
441
459
  if nodes[id] != nil { id = "\(preferredId)#\(nodes.count)" }
442
460
  let children = value["children"] as? [Any] ?? []
461
+ var textPositions: [Int] = []
462
+ var childCount = 0
463
+ for child in children {
464
+ if child is String { textPositions.append(childCount) }
465
+ else if child is [String: Any] { childCount += 1 }
466
+ }
443
467
  nodes[id] = Node(
444
468
  id: id,
445
469
  key: value["key"] as? String,
@@ -448,6 +472,7 @@ final class CraftNativeMutationDocument {
448
472
  style: dictionary(value["style"]),
449
473
  events: dictionary(value["events"]),
450
474
  text: children.compactMap { $0 as? String },
475
+ textPositions: textPositions,
451
476
  children: [],
452
477
  parent: parent
453
478
  )
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "craft-native",
3
- "version": "0.0.394",
3
+ "version": "0.0.396",
4
4
  "type": "module",
5
5
  "description": "Build desktop apps with web languages - TypeScript SDK for Craft",
6
6
  "author": "Chris Breuer <chris@stacksjs.org>",