Skip to content

Commit 0f29ebb

Browse files
authored
Merge pull request #7 from mwildehahn/mh-apply-delta
feat: add apply_delta
2 parents 0ca48e2 + fcbaa0d commit 0f29ebb

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

Sources/Loro/Loro.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ extension LoroDoc{
4444

4545

4646
class ClosureOnPush: OnPush {
47-
private let closure: (UndoOrRedo, CounterSpan) ->UndoItemMeta
47+
private let closure: (UndoOrRedo, CounterSpan, DiffEvent?) ->UndoItemMeta
4848

49-
public init(closure: @escaping (UndoOrRedo, CounterSpan) ->UndoItemMeta) {
49+
public init(closure: @escaping (UndoOrRedo, CounterSpan, DiffEvent?) ->UndoItemMeta) {
5050
self.closure = closure
5151
}
5252

53-
public func onPush(undoOrRedo: UndoOrRedo, span: CounterSpan) -> UndoItemMeta{
54-
closure(undoOrRedo, span)
53+
public func onPush(undoOrRedo: UndoOrRedo, span: CounterSpan, diffEvent: DiffEvent?) -> UndoItemMeta{
54+
closure(undoOrRedo, span, diffEvent)
5555
}
5656
}
5757

@@ -68,7 +68,7 @@ class ClosureOnPop: OnPop {
6868
}
6969

7070
extension UndoManager{
71-
public func setOnPush(callback: ((UndoOrRedo, CounterSpan) ->UndoItemMeta)?){
71+
public func setOnPush(callback: ((UndoOrRedo, CounterSpan, DiffEvent?) ->UndoItemMeta)?){
7272
if let onPush = callback{
7373
let closureOnPush = ClosureOnPush(closure: onPush)
7474
self.setOnPush(onPush: closureOnPush)

Sources/Loro/LoroFFI.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4894,6 +4894,11 @@ public func FfiConverterTypeLoroMovableList_lower(_ value: LoroMovableList) -> U
48944894

48954895
public protocol LoroTextProtocol : AnyObject {
48964896

4897+
/**
4898+
* Apply a [delta](https://quilljs.com/docs/delta/) to the text container.
4899+
*/
4900+
func applyDelta(delta: [TextDelta]) throws
4901+
48974902
/**
48984903
* Delete a range of text at the given unicode position with unicode length.
48994904
*/
@@ -5120,6 +5125,16 @@ public convenience init() {
51205125

51215126

51225127

5128+
/**
5129+
* Apply a [delta](https://quilljs.com/docs/delta/) to the text container.
5130+
*/
5131+
open func applyDelta(delta: [TextDelta])throws {try rustCallWithError(FfiConverterTypeLoroError.lift) {
5132+
uniffi_loro_fn_method_lorotext_apply_delta(self.uniffiClonePointer(),
5133+
FfiConverterSequenceTypeTextDelta.lower(delta),$0
5134+
)
5135+
}
5136+
}
5137+
51235138
/**
51245139
* Delete a range of text at the given unicode position with unicode length.
51255140
*/
@@ -13410,6 +13425,9 @@ private var initializationResult: InitializationResult = {
1341013425
if (uniffi_loro_checksum_method_loromovablelist_to_vec() != 28826) {
1341113426
return InitializationResult.apiChecksumMismatch
1341213427
}
13428+
if (uniffi_loro_checksum_method_lorotext_apply_delta() != 32084) {
13429+
return InitializationResult.apiChecksumMismatch
13430+
}
1341313431
if (uniffi_loro_checksum_method_lorotext_delete() != 47933) {
1341413432
return InitializationResult.apiChecksumMismatch
1341513433
}

Tests/LoroTests/LoroTests.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ final class LoroTests: XCTestCase {
4545
let text2 = doc2.getText(id: "text")
4646
try! text2.insert(pos: 0, s:"123")
4747
let _ = try! doc2.import(bytes: doc.export(mode:ExportMode.snapshot))
48-
try! doc2.importBatch(bytes: [doc.exportSnapshot(), doc.export(mode: ExportMode.updates(from: VersionVector()))])
48+
let _ = try! doc2.importBatch(bytes: [doc.exportSnapshot(), doc.export(mode: ExportMode.updates(from: VersionVector()))])
4949
XCTAssertEqual(text2.toString(), "bc123")
5050
}
5151

@@ -79,4 +79,13 @@ final class LoroTests: XCTestCase {
7979
XCTAssertEqual(text.toString(), "abc")
8080
XCTAssertEqual(n, 1)
8181
}
82+
83+
func testApplyDelta(){
84+
let doc = LoroDoc()
85+
let text = doc.getText(id: "text")
86+
try! text.insert(pos: 0, s: "abc")
87+
try! text.applyDelta(delta: [TextDelta.delete(delete: 1), TextDelta.retain(retain: 2, attributes: nil), TextDelta.insert(insert: "def", attributes: nil)])
88+
let s = text.toString()
89+
XCTAssertEqual(s, "bcdef")
90+
}
8291
}

loro-rs/src/loro.udl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,6 @@ interface LoroDoc{
257257
VersionVector? frontiers_to_vv([ByRef] Frontiers frontiers);
258258

259259
/// Minimize the frontiers by removing the unnecessary entries.
260-
// TODO:
261260
FrontiersOrID minimize_frontiers([ByRef] Frontiers frontiers);
262261

263262
/// Convert `VersionVector` into `Frontiers`
@@ -440,7 +439,6 @@ interface LoroDoc{
440439
///
441440
/// * `id` - The starting ID of the change range
442441
/// * `len` - The length of the change range to check
443-
// TODO:
444442
sequence<ContainerID> get_changed_containers_in(ID id, u32 len);
445443

446444
/// Check if the doc contains the full history.
@@ -519,10 +517,13 @@ interface LoroText{
519517
///
520518
/// This could take a long time for large texts (e.g. > 50_000 characters).
521519
/// In that case, you should use `updateByLine` instead.
522-
// TODO:
523520
[Throws=UpdateTimeoutError]
524521
void update([ByRef] string s, UpdateOptions options);
525522

523+
/// Apply a [delta](https://quilljs.com/docs/delta/) to the text container.
524+
[Throws=LoroError]
525+
void apply_delta(sequence<TextDelta> delta);
526+
526527
/// Update the current text based on the provided text.
527528
///
528529
/// This update calculation is line-based, which will be more efficient but less precise.

0 commit comments

Comments
 (0)