|
| 1 | +use pyo3::prelude::*; |
| 2 | +use pyo3::exceptions::PyRuntimeError; |
| 3 | +use yrs::{ |
| 4 | + UndoManager as _UndoManager, |
| 5 | +}; |
| 6 | +use yrs::undo::Options; |
| 7 | +use crate::doc::Doc; |
| 8 | +use crate::text::Text; |
| 9 | +use crate::array::Array; |
| 10 | +use crate::map::Map; |
| 11 | + |
| 12 | + |
| 13 | +#[pyclass(unsendable)] |
| 14 | +pub struct UndoManager { |
| 15 | + undo_manager: Option<_UndoManager>, |
| 16 | +} |
| 17 | + |
| 18 | +impl UndoManager { |
| 19 | + fn get_options(&self, capture_timeout_millis: &u64) -> Options { |
| 20 | + let mut options = Options::default(); |
| 21 | + options.capture_timeout_millis = *capture_timeout_millis; |
| 22 | + options |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +#[pymethods] |
| 27 | +impl UndoManager { |
| 28 | + #[new] |
| 29 | + fn new() -> Self { |
| 30 | + UndoManager { undo_manager: None } |
| 31 | + } |
| 32 | + |
| 33 | + pub fn from_text(&self, doc: &Doc, scope: &Text, capture_timeout_millis: u64) -> Self { |
| 34 | + let options = self.get_options(&capture_timeout_millis); |
| 35 | + let undo_manager = _UndoManager::with_options(&doc.doc, &scope.text, options); |
| 36 | + UndoManager { undo_manager: Some(undo_manager) } |
| 37 | + } |
| 38 | + |
| 39 | + pub fn from_array(&self, doc: &Doc, scope: &Array, capture_timeout_millis: u64) -> Self { |
| 40 | + let options = self.get_options(&capture_timeout_millis); |
| 41 | + let undo_manager = _UndoManager::with_options(&doc.doc, &scope.array, options); |
| 42 | + UndoManager { undo_manager: Some(undo_manager) } |
| 43 | + } |
| 44 | + |
| 45 | + pub fn from_map(&self, doc: &Doc, scope: &Map, capture_timeout_millis: u64) -> Self { |
| 46 | + let options = self.get_options(&capture_timeout_millis); |
| 47 | + let undo_manager = _UndoManager::with_options(&doc.doc, &scope.map, options); |
| 48 | + UndoManager { undo_manager: Some(undo_manager) } |
| 49 | + } |
| 50 | + |
| 51 | + pub fn undo(&mut self) -> PyResult<bool> { |
| 52 | + let Ok(res) = self.undo_manager.as_mut().unwrap().undo() else { return Err(PyRuntimeError::new_err("Cannot undo")) }; |
| 53 | + Ok(res) |
| 54 | + } |
| 55 | +} |
0 commit comments