Skip to content

Commit

Permalink
fix: on push wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Leeeon233 committed Jan 6, 2025
1 parent c3427dd commit fcbaa0d
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 9 deletions.
10 changes: 5 additions & 5 deletions Sources/Loro/Loro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand All @@ -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)
Expand Down
18 changes: 18 additions & 0 deletions Sources/Loro/LoroFFI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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
}
Expand Down
11 changes: 10 additions & 1 deletion Tests/LoroTests/LoroTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}

Expand Down Expand Up @@ -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")
}
}
3 changes: 0 additions & 3 deletions loro-rs/src/loro.udl
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down Expand Up @@ -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<ContainerID> get_changed_containers_in(ID id, u32 len);

/// Check if the doc contains the full history.
Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit fcbaa0d

Please sign in to comment.