| 1 | import Foundation |
| 2 | |
| 3 | struct CRDTProjection: Decodable, Sendable, Equatable { |
| 4 | var children: [CRDTNode] |
| 5 | var canUndo: Bool |
| 6 | var canRedo: Bool |
| 7 | } |
| 8 | /// The engine's `project_delta`: edits to the previous projection's list of |
| 9 | /// top-level blocks. Only blocks a transaction touched arrive re-projected; |
| 10 | /// every other block is the previous value, unchanged. |
| 11 | struct CRDTProjectionDelta: Decodable, Sendable { |
| 12 | enum Operation: Decodable, Sendable { |
| 13 | case keep(Int), remove(Int), insert([CRDTNode]) |
| 14 | private enum Key: String, CodingKey { case keep, remove, insert } |
| 15 | init(from decoder: any Decoder) throws { |
| 16 | let container = try decoder.container(keyedBy: Key.self) |
| 17 | if let count = try container.decodeIfPresent(Int.self, forKey: .keep) { self = .keep(count) } |
| 18 | else if let count = try container.decodeIfPresent(Int.self, forKey: .remove) { self = .remove(count) } |
| 19 | else { self = .insert(try container.decode([CRDTNode].self, forKey: .insert)) } |
| 20 | } |
| 21 | } |
| 22 | let base: Int |
| 23 | let count: Int |
| 24 | let ops: [Operation] |
| 25 | let canUndo: Bool |
| 26 | let canRedo: Bool |
| 27 | /// The top-level block ids this delta inserts or removes from |
| 28 | /// `previous` (an edit in place is both, under one id). |
| 29 | func touched(in previous: CRDTProjection) -> Set<String> { |
| 30 | var ids = Set<String>(), at = 0 |
| 31 | for operation in ops { |
| 32 | switch operation { |
| 33 | case .keep(let n): at += max(0, n) |
| 34 | case .remove(let n): |
| 35 | let end = min(previous.children.count, at + max(0, n)) |
| 36 | if at < end { ids.formUnion(previous.children[at..<end].map(\.id)) } |
| 37 | at += max(0, n) |
| 38 | case .insert(let nodes): ids.formUnion(nodes.map(\.id)) |
| 39 | } |
| 40 | } |
| 41 | return ids |
| 42 | } |
| 43 | /// nil unless it applies exactly to `previous`. |
| 44 | func applied(to previous: CRDTProjection) -> CRDTProjection? { |
| 45 | guard previous.children.count == base else { return nil } |
| 46 | var children: [CRDTNode] = [] |
| 47 | children.reserveCapacity(count) |
| 48 | var at = 0 |
| 49 | for operation in ops { |
| 50 | switch operation { |
| 51 | case .keep(let n): |
| 52 | guard n >= 0, at + n <= base else { return nil } |
| 53 | children.append(contentsOf: previous.children[at..<(at + n)]); at += n |
| 54 | case .remove(let n): |
| 55 | guard n >= 0, at + n <= base else { return nil } |
| 56 | at += n |
| 57 | case .insert(let nodes): children.append(contentsOf: nodes) |
| 58 | } |
| 59 | } |
| 60 | guard at == base, children.count == count else { return nil } |
| 61 | return CRDTProjection(children: children, canUndo: canUndo, canRedo: canRedo) |
| 62 | } |
| 63 | } |
| 64 | /// Equatable by structure, so a full comparison can tell which blocks of a |
| 65 | /// new projection differ from the ones drawn (spec §7 Design 5). |
| 66 | struct CRDTNode: Decodable, Identifiable, Sendable, Equatable { |
| 67 | let id: String |
| 68 | let type: String |
| 69 | var attrs: [String: String]? |
| 70 | var children: [CRDTNode]? |
| 71 | var runs: [CRDTRun]? |
| 72 | var plainText: String { runs?.map(\.insert).joined() ?? children?.map(\.plainText).joined() ?? "" } |
| 73 | } |
| 74 | struct CRDTRun: Decodable, Sendable, Equatable { |
| 75 | var insert: String |
| 76 | var marks: [String: JSONValue]? |
| 77 | } |
| 78 | indirect enum JSONValue: Codable, Sendable, Equatable { |
| 79 | case object([String: JSONValue]), array([JSONValue]), string(String), number(Double), bool(Bool), null |
| 80 | init(from decoder: any Decoder) throws { |
| 81 | let c = try decoder.singleValueContainer() |
| 82 | if c.decodeNil() { self = .null } |
| 83 | else if let v = try? c.decode(Bool.self) { self = .bool(v) } |
| 84 | else if let v = try? c.decode(String.self) { self = .string(v) } |
| 85 | else if let v = try? c.decode(Double.self) { self = .number(v) } |
| 86 | else if let v = try? c.decode([String: JSONValue].self) { self = .object(v) } |
| 87 | else { self = .array(try c.decode([JSONValue].self)) } |
| 88 | } |
| 89 | func encode(to encoder: any Encoder) throws { |
| 90 | var c = encoder.singleValueContainer() |
| 91 | switch self { |
| 92 | case .object(let v): try c.encode(v) |
| 93 | case .array(let v): try c.encode(v) |
| 94 | case .string(let v): try c.encode(v) |
| 95 | case .number(let v): try c.encode(v) |
| 96 | case .bool(let v): try c.encode(v) |
| 97 | case .null: try c.encodeNil() |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |