From 885be178558b2ded9d547fc05122d66c0544925a Mon Sep 17 00:00:00 2001 From: David Brochart Date: Tue, 25 Jun 2024 17:32:39 +0200 Subject: [PATCH] Support undo manager expand_scope --- python/pycrdt/_undo.py | 4 ++++ src/undo.rs | 12 ++++++++++++ tests/test_undo.py | 34 ++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+) diff --git a/python/pycrdt/_undo.py b/python/pycrdt/_undo.py index cb7c76c..9c3548e 100644 --- a/python/pycrdt/_undo.py +++ b/python/pycrdt/_undo.py @@ -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() diff --git a/src/undo.rs b/src/undo.rs index 7ce0e15..03b9dd1 100644 --- a/src/undo.rs +++ b/src/undo.rs @@ -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() } diff --git a/tests/test_undo.py b/tests/test_undo.py index a58902c..06cc70f 100644 --- a/tests/test_undo.py +++ b/tests/test_undo.py @@ -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"}