Skip to content

Commit

Permalink
feat: undo manager
Browse files Browse the repository at this point in the history
  • Loading branch information
Leeeon233 committed Aug 7, 2024
1 parent fb1fd22 commit 0369c92
Show file tree
Hide file tree
Showing 7 changed files with 1,092 additions and 27 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ let subId = doc.subscribeRoot{ diffEvent in
doc.unsubscribe(subId: id)

// export updates or snapshot

let doc2 = LoroDoc()
let snapshot = doc.exportSnapshot()
let updates = doc.exportFrom(vv: VersionVector())
Expand Down
45 changes: 45 additions & 0 deletions Sources/Loro/Loro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,48 @@


import Foundation


class ClosureOnPush: OnPush {
private let closure: (UndoOrRedo, CounterSpan) ->UndoItemMeta

public init(closure: @escaping (UndoOrRedo, CounterSpan) ->UndoItemMeta) {
self.closure = closure
}

public func onPush(undoOrRedo: UndoOrRedo, span: CounterSpan) -> UndoItemMeta{
closure(undoOrRedo, span)
}
}

class ClosureOnPop: OnPop {
private let closure: (UndoOrRedo, CounterSpan,UndoItemMeta)->Void

public init(closure: @escaping (UndoOrRedo, CounterSpan,UndoItemMeta)->Void) {
self.closure = closure
}

public func onPop(undoOrRedo: UndoOrRedo, span: CounterSpan, undoMeta: UndoItemMeta) {
closure(undoOrRedo, span, undoMeta)
}
}

extension UndoManager{
public func setOnPush(callback: ((UndoOrRedo, CounterSpan) ->UndoItemMeta)?){
if let onPush = callback{
let closureOnPush = ClosureOnPush(closure: onPush)
self.setOnPush(onPush: closureOnPush)
}else{
self.setOnPush(onPush: nil)
}
}

public func setOnPop(callback: ( (UndoOrRedo, CounterSpan,UndoItemMeta)->Void)?){
if let onPop = callback{
let closureOnPop = ClosureOnPop(closure: onPop)
self.setOnPop(onPop: closureOnPop)
}else{
self.setOnPop(onPop: nil)
}
}
}
Loading

0 comments on commit 0369c92

Please sign in to comment.