Struct yrs::undo::UndoManager

source ·
pub struct UndoManager<M>(/* private fields */);
Expand description

Undo manager is a structure used to perform undo/redo operations over the associated shared type(s).

Undo-/redo-able actions (a.k.a. StackItems) are not equivalent to TransactionMut unit of work, but rather a series of updates batched within specified time intervals (see: Options::capture_timeout_millis) and their corresponding origins (see: Doc::transact_mut_with and UndoManager::include_origin).

Individual stack item boundaries can be also specified explicitly by calling UndoManager::reset, which denotes the end of the batch.

In order to revert an operation, call UndoManager::undo, then UndoManager::redo to bring it back.

Users can also subscribe to change notifications observed by undo manager:

Implementations§

source§

impl<M> UndoManager<M>
where M: Default + 'static,

source

pub fn new<T>(doc: &Doc, scope: &T) -> Self
where T: AsRef<Branch>,

Creates a new instance of the UndoManager working in a scope of a particular shared type and document. While it’s possible for undo manager to observe multiple shared types (see: UndoManager::expand_scope), it can only work with a single document at the same time.

source

pub fn doc(&self) -> &Doc

source

pub fn with_options<T>(doc: &Doc, scope: &T, options: Options) -> Self
where T: AsRef<Branch>,

Creates a new instance of the UndoManager working in a scope of a particular shared type and document. While it’s possible for undo manager to observe multiple shared types (see: UndoManager::expand_scope), it can only work with a single document at the same time.

source

pub fn observe_item_added<F>(&self, f: F) -> Subscription
where F: Fn(&TransactionMut<'_>, &mut Event<M>) + 'static,

Registers a callback function to be called every time a new StackItem is created. This usually happens when a new update over an tracked shared type happened after capture timeout threshold from the previous stack item occurence has been reached or UndoManager::reset has been called.

Returns a subscription object which - when dropped - will unregister provided callback.

source

pub fn observe_item_updated<F>(&self, f: F) -> Subscription
where F: Fn(&TransactionMut<'_>, &mut Event<M>) + 'static,

Registers a callback function to be called every time an existing StackItem has been extended as a result of updates from tracked types which happened before a capture timeout has passed.

Returns a subscription object which - when dropped - will unregister provided callback.

source

pub fn observe_item_popped<F>(&self, f: F) -> Subscription
where F: Fn(&TransactionMut<'_>, &mut Event<M>) + 'static,

Registers a callback function to be called every time an existing StackItem has been removed as a result of UndoManager::undo or UndoManager::redo method.

Returns a subscription object which - when dropped - will unregister provided callback.

source

pub fn expand_scope<T>(&mut self, scope: &T)
where T: AsRef<Branch>,

Extends a list of shared types tracked by current undo manager by a given scope.

source

pub fn include_origin<O>(&mut self, origin: O)
where O: Into<Origin>,

Extends a list of origins tracked by current undo manager by given origin. Origin markers can be assigned to updates executing in a scope of a particular transaction (see: Doc::transact_mut_with).

source

pub fn exclude_origin<O>(&mut self, origin: O)
where O: Into<Origin>,

Removes an origin from the list of origins tracked by a current undo manager.

source

pub fn clear(&mut self) -> Result<(), TransactionAcqError>

Clears all StackItems stored within current UndoManager, effectively resetting its state.

source

pub fn as_origin(&self) -> Origin

source

pub fn reset(&mut self)

UndoManager merges undo stack items if they were created withing the time gap smaller than Options::capture_timeout_millis. You can call this method so that the next stack item won’t be merged.

Example:

use yrs::{Doc, GetString, Text, Transact, UndoManager};
let doc = Doc::new();

// without UndoManager::stop
let txt = doc.get_or_insert_text("no-stop");
let mut mgr = UndoManager::new(&doc, &txt);
txt.insert(&mut doc.transact_mut(), 0, "a");
txt.insert(&mut doc.transact_mut(), 1, "b");
mgr.undo().unwrap();
txt.get_string(&doc.transact()); // => "" (note that 'ab' was removed)

// with UndoManager::stop
let txt = doc.get_or_insert_text("with-stop");
let mut mgr = UndoManager::new(&doc, &txt);
txt.insert(&mut doc.transact_mut(), 0, "a");
mgr.reset();
txt.insert(&mut doc.transact_mut(), 1, "b");
mgr.undo().unwrap();
txt.get_string(&doc.transact()); // => "a" (note that only 'b' was removed)
source

pub fn can_undo(&self) -> bool

Are there any undo steps available?

source

pub fn undo(&mut self) -> Result<bool, TransactionAcqError>

Undo last action tracked by current undo manager. Actions (a.k.a. StackItems) are groups of updates performed in a given time range - they also can be separated explicitly by calling UndoManager::reset.

Successful execution returns a boolean value telling if an undo call has performed any changes.

§Errors

This method requires an exclusive access to underlying document store. This means that no other transaction on that same document can be active while calling this method. Otherwise an error will be returned.

source

pub fn can_redo(&self) -> bool

Are there any redo steps available?

source

pub fn redo(&mut self) -> Result<bool, TransactionAcqError>

Redo’es last action previously undo’ed by current undo manager. Actions (a.k.a. StackItems) are groups of updates performed in a given time range - they also can be separated explicitly by calling UndoManager::reset.

Successful execution returns a boolean value telling if an undo call has performed any changes.

§Errors

This method requires an exclusive access to underlying document store. This means that no other transaction on that same document can be active while calling this method. Otherwise an error will be returned.

Trait Implementations§

source§

impl<M: Debug> Debug for UndoManager<M>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<M> Freeze for UndoManager<M>

§

impl<M> !RefUnwindSafe for UndoManager<M>

§

impl<M> !Send for UndoManager<M>

§

impl<M> !Sync for UndoManager<M>

§

impl<M> Unpin for UndoManager<M>

§

impl<M> !UnwindSafe for UndoManager<M>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.