From fcbaa0d9a98208dbf5ab520d302a70c8a9d95f18 Mon Sep 17 00:00:00 2001 From: Leon Zhao Date: Mon, 6 Jan 2025 16:43:19 +0800 Subject: [PATCH] fix: on push wrapper --- Sources/Loro/Loro.swift | 10 +++++----- Sources/Loro/LoroFFI.swift | 18 ++++++++++++++++++ Tests/LoroTests/LoroTests.swift | 11 ++++++++++- loro-rs/src/loro.udl | 3 --- 4 files changed, 33 insertions(+), 9 deletions(-) diff --git a/Sources/Loro/Loro.swift b/Sources/Loro/Loro.swift index 6a7e8e3..1b47118 100644 --- a/Sources/Loro/Loro.swift +++ b/Sources/Loro/Loro.swift @@ -44,14 +44,14 @@ extension LoroDoc{ class ClosureOnPush: OnPush { - private let closure: (UndoOrRedo, CounterSpan) ->UndoItemMeta + private let closure: (UndoOrRedo, CounterSpan, DiffEvent?) ->UndoItemMeta - public init(closure: @escaping (UndoOrRedo, CounterSpan) ->UndoItemMeta) { + public init(closure: @escaping (UndoOrRedo, CounterSpan, DiffEvent?) ->UndoItemMeta) { self.closure = closure } - public func onPush(undoOrRedo: UndoOrRedo, span: CounterSpan) -> UndoItemMeta{ - closure(undoOrRedo, span) + public func onPush(undoOrRedo: UndoOrRedo, span: CounterSpan, diffEvent: DiffEvent?) -> UndoItemMeta{ + closure(undoOrRedo, span, diffEvent) } } @@ -68,7 +68,7 @@ class ClosureOnPop: OnPop { } extension UndoManager{ - public func setOnPush(callback: ((UndoOrRedo, CounterSpan) ->UndoItemMeta)?){ + public func setOnPush(callback: ((UndoOrRedo, CounterSpan, DiffEvent?) ->UndoItemMeta)?){ if let onPush = callback{ let closureOnPush = ClosureOnPush(closure: onPush) self.setOnPush(onPush: closureOnPush) diff --git a/Sources/Loro/LoroFFI.swift b/Sources/Loro/LoroFFI.swift index 137efd9..797b6ce 100644 --- a/Sources/Loro/LoroFFI.swift +++ b/Sources/Loro/LoroFFI.swift @@ -4894,6 +4894,11 @@ public func FfiConverterTypeLoroMovableList_lower(_ value: LoroMovableList) -> U public protocol LoroTextProtocol : AnyObject { + /** + * Apply a [delta](https://quilljs.com/docs/delta/) to the text container. + */ + func applyDelta(delta: [TextDelta]) throws + /** * Delete a range of text at the given unicode position with unicode length. */ @@ -5120,6 +5125,16 @@ public convenience init() { + /** + * Apply a [delta](https://quilljs.com/docs/delta/) to the text container. + */ +open func applyDelta(delta: [TextDelta])throws {try rustCallWithError(FfiConverterTypeLoroError.lift) { + uniffi_loro_fn_method_lorotext_apply_delta(self.uniffiClonePointer(), + FfiConverterSequenceTypeTextDelta.lower(delta),$0 + ) +} +} + /** * Delete a range of text at the given unicode position with unicode length. */ @@ -13410,6 +13425,9 @@ private var initializationResult: InitializationResult = { if (uniffi_loro_checksum_method_loromovablelist_to_vec() != 28826) { return InitializationResult.apiChecksumMismatch } + if (uniffi_loro_checksum_method_lorotext_apply_delta() != 32084) { + return InitializationResult.apiChecksumMismatch + } if (uniffi_loro_checksum_method_lorotext_delete() != 47933) { return InitializationResult.apiChecksumMismatch } diff --git a/Tests/LoroTests/LoroTests.swift b/Tests/LoroTests/LoroTests.swift index afa68d2..80eb741 100644 --- a/Tests/LoroTests/LoroTests.swift +++ b/Tests/LoroTests/LoroTests.swift @@ -45,7 +45,7 @@ final class LoroTests: XCTestCase { let text2 = doc2.getText(id: "text") try! text2.insert(pos: 0, s:"123") let _ = try! doc2.import(bytes: doc.export(mode:ExportMode.snapshot)) - try! doc2.importBatch(bytes: [doc.exportSnapshot(), doc.export(mode: ExportMode.updates(from: VersionVector()))]) + let _ = try! doc2.importBatch(bytes: [doc.exportSnapshot(), doc.export(mode: ExportMode.updates(from: VersionVector()))]) XCTAssertEqual(text2.toString(), "bc123") } @@ -79,4 +79,13 @@ final class LoroTests: XCTestCase { XCTAssertEqual(text.toString(), "abc") XCTAssertEqual(n, 1) } + + func testApplyDelta(){ + let doc = LoroDoc() + let text = doc.getText(id: "text") + try! text.insert(pos: 0, s: "abc") + try! text.applyDelta(delta: [TextDelta.delete(delete: 1), TextDelta.retain(retain: 2, attributes: nil), TextDelta.insert(insert: "def", attributes: nil)]) + let s = text.toString() + XCTAssertEqual(s, "bcdef") + } } diff --git a/loro-rs/src/loro.udl b/loro-rs/src/loro.udl index fae6182..df151cd 100644 --- a/loro-rs/src/loro.udl +++ b/loro-rs/src/loro.udl @@ -257,7 +257,6 @@ interface LoroDoc{ VersionVector? frontiers_to_vv([ByRef] Frontiers frontiers); /// Minimize the frontiers by removing the unnecessary entries. - // TODO: FrontiersOrID minimize_frontiers([ByRef] Frontiers frontiers); /// Convert `VersionVector` into `Frontiers` @@ -440,7 +439,6 @@ interface LoroDoc{ /// /// * `id` - The starting ID of the change range /// * `len` - The length of the change range to check - // TODO: sequence get_changed_containers_in(ID id, u32 len); /// Check if the doc contains the full history. @@ -519,7 +517,6 @@ interface LoroText{ /// /// This could take a long time for large texts (e.g. > 50_000 characters). /// In that case, you should use `updateByLine` instead. - // TODO: [Throws=UpdateTimeoutError] void update([ByRef] string s, UpdateOptions options);