Skip to content

Commit

Permalink
Support undo manager expand_scope
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrochart committed Jun 25, 2024
1 parent 611b481 commit 885be17
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
4 changes: 4 additions & 0 deletions python/pycrdt/_undo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ def __init__(self, scope: BaseType, capture_timeout_millis: int = 500) -> None:
method = getattr(undo_manager, f"from_{scope.type_name}")
self._undo_manager = method(scope.doc._doc, scope._integrated, capture_timeout_millis)

def expand_scope(self, scope: BaseType) -> None:
method = getattr(self._undo_manager, f"expand_scope_{scope.type_name}")
method(scope._integrated)

def can_undo(self) -> bool:
return self._undo_manager.can_undo()

Expand Down
12 changes: 12 additions & 0 deletions src/undo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ impl UndoManager {
UndoManager { undo_manager: Some(undo_manager) }
}

pub fn expand_scope_text(&mut self, scope: &Text) {
self.undo_manager.as_mut().unwrap().expand_scope(&scope.text);
}

pub fn expand_scope_array(&mut self, scope: &Array) {
self.undo_manager.as_mut().unwrap().expand_scope(&scope.array);
}

pub fn expand_scope_map(&mut self, scope: &Map) {
self.undo_manager.as_mut().unwrap().expand_scope(&scope.map);
}

pub fn can_undo(&mut self) -> bool {
self.undo_manager.as_ref().unwrap().can_undo()
}
Expand Down
34 changes: 34 additions & 0 deletions tests/test_undo.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,37 @@ def test_map_undo():
data.update(val2)
assert data.to_py() == val3
undo_redo(data, undo_manager, val0, val1, val3)


def test_scopes():
doc = Doc()
doc["text"] = text = Text()
doc["array"] = array = Array()
doc["map"] = map = Map()
undo_manager = UndoManager(text, capture_timeout_millis=0)

text += "Hello"
text += ", World!"
assert str(text) == "Hello, World!"
undo_manager.undo()
assert str(text) == "Hello"

array.append(0)
assert array.to_py() == [0]
undo_manager.undo()
assert array.to_py() == [0]
undo_manager.expand_scope(array)
array.append(1)
assert array.to_py() == [0, 1]
undo_manager.undo()
assert array.to_py() == [0]

map["key0"] = "val0"
assert map.to_py() == {"key0": "val0"}
undo_manager.undo()
assert map.to_py() == {"key0": "val0"}
undo_manager.expand_scope(map)
map["key1"] = "val1"
assert map.to_py() == {"key0": "val0", "key1": "val1"}
undo_manager.undo()
assert map.to_py() == {"key0": "val0"}

0 comments on commit 885be17

Please sign in to comment.