Skip to content

Commit 0369c92

Browse files
committed
feat: undo manager
1 parent fb1fd22 commit 0369c92

File tree

7 files changed

+1092
-27
lines changed

7 files changed

+1092
-27
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ let subId = doc.subscribeRoot{ diffEvent in
7272
doc.unsubscribe(subId: id)
7373

7474
// export updates or snapshot
75-
7675
let doc2 = LoroDoc()
7776
let snapshot = doc.exportSnapshot()
7877
let updates = doc.exportFrom(vv: VersionVector())

Sources/Loro/Loro.swift

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,48 @@
88

99

1010
import Foundation
11+
12+
13+
class ClosureOnPush: OnPush {
14+
private let closure: (UndoOrRedo, CounterSpan) ->UndoItemMeta
15+
16+
public init(closure: @escaping (UndoOrRedo, CounterSpan) ->UndoItemMeta) {
17+
self.closure = closure
18+
}
19+
20+
public func onPush(undoOrRedo: UndoOrRedo, span: CounterSpan) -> UndoItemMeta{
21+
closure(undoOrRedo, span)
22+
}
23+
}
24+
25+
class ClosureOnPop: OnPop {
26+
private let closure: (UndoOrRedo, CounterSpan,UndoItemMeta)->Void
27+
28+
public init(closure: @escaping (UndoOrRedo, CounterSpan,UndoItemMeta)->Void) {
29+
self.closure = closure
30+
}
31+
32+
public func onPop(undoOrRedo: UndoOrRedo, span: CounterSpan, undoMeta: UndoItemMeta) {
33+
closure(undoOrRedo, span, undoMeta)
34+
}
35+
}
36+
37+
extension UndoManager{
38+
public func setOnPush(callback: ((UndoOrRedo, CounterSpan) ->UndoItemMeta)?){
39+
if let onPush = callback{
40+
let closureOnPush = ClosureOnPush(closure: onPush)
41+
self.setOnPush(onPush: closureOnPush)
42+
}else{
43+
self.setOnPush(onPush: nil)
44+
}
45+
}
46+
47+
public func setOnPop(callback: ( (UndoOrRedo, CounterSpan,UndoItemMeta)->Void)?){
48+
if let onPop = callback{
49+
let closureOnPop = ClosureOnPop(closure: onPop)
50+
self.setOnPop(onPop: closureOnPop)
51+
}else{
52+
self.setOnPop(onPop: nil)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)