Skip to main content

UndoManager

Struct 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: Meta + 'static,

Source

pub fn new() -> Self

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 with_options(options: Options<M>) -> Self

Creates a new instance of the UndoManager working in a context of a given document, but without any pre-initialize scope. 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 expand_scope<T>(&mut self, doc: &Doc, scope: &T)
where T: AsRef<Branch>,

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

Source

pub fn docs(&self) -> impl Iterator<Item = &Doc>

Source

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

Registers a callback to be called every time a new StackItem is created.

Source

pub fn observe_item_added_with<K, F>(&mut self, key: K, f: F)
where K: Into<Origin>, F: FnMut(&TransactionMut<'_>, &mut Event<M>) + 'static,

Source

pub fn unobserve_item_added<K>(&mut self, key: K) -> bool
where K: Into<Origin>,

Source

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

Registers a callback to be called every time an existing StackItem is extended.

Source

pub fn observe_item_updated_with<K, F>(&mut self, key: K, f: F)
where K: Into<Origin>, F: FnMut(&TransactionMut<'_>, &mut Event<M>) + 'static,

Source

pub fn unobserve_item_updated<K>(&mut self, key: K) -> bool
where K: Into<Origin>,

Source

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

Registers a callback to be called every time a StackItem is popped via UndoManager::undo or UndoManager::redo.

Source

pub fn observe_item_popped_with<K, F>(&mut self, key: K, f: F)
where K: Into<Origin>, F: FnMut(&TransactionMut<'_>, &mut Event<M>) + 'static,

Source

pub fn unobserve_item_popped<K>(&mut self, key: K) -> bool
where K: Into<Origin>,

Source

pub fn observe_stack_cleared<F>(&mut self, f: F) -> Subscription
where F: FnMut(&StackClearedEvent) + 'static,

Registers a callback to be called every time undo/redo stacks are cleared. The callback receives two booleans: (undo_stack_cleared, redo_stack_cleared).

Source

pub fn observe_stack_cleared_with<K, F>(&mut self, key: K, f: F)
where K: Into<Origin>, F: FnMut(&StackClearedEvent) + 'static,

Source

pub fn unobserve_stack_cleared<K>(&mut self, key: K) -> bool
where K: Into<Origin>,

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_all(&mut self)

Clears all StackItems stored within current UndoManager undo AND redo stacks, effectively resetting its state.

§Deadlocks

In order to perform its function, this method must guarantee that underlying document store is not being modified by another running TransactionMut. It does so by acquiring a read-only transaction itself. If transaction couldn’t be acquired (because another read-write transaction is in progress), it will hold current thread until, acquisition is available.

Source

pub fn clear_undo(&mut self)

Clears all StackItems stored within current UndoManager undo stack alone, leaving redo stack untouched.

§Deadlocks

In order to perform its function, this method must guarantee that underlying document store is not being modified by another running TransactionMut. It does so by acquiring a read-only transaction itself. If transaction couldn’t be acquired (because another read-write transaction is in progress), it will hold current thread until, acquisition is available.

Source

pub fn clear_redo(&mut self)

Clears all StackItems stored within current UndoManager redo stack alone, leaving undo stack untouched.

§Deadlocks

In order to perform its function, this method must guarantee that underlying document store is not being modified by another running TransactionMut. It does so by acquiring a read-only transaction itself. If transaction couldn’t be acquired (because another read-write transaction is in progress), it will hold current thread until, acquisition is available.

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();
mgr.expand_scope(&doc, &txt);
txt.insert(&mut doc.transact_mut(), 0, "a");
txt.insert(&mut doc.transact_mut(), 1, "b");
mgr.undo_blocking();
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();
mgr.expand_scope(&doc, &txt);
txt.insert(&mut doc.transact_mut(), 0, "a");
mgr.reset();
txt.insert(&mut doc.transact_mut(), 1, "b");
mgr.undo_blocking();
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_stack(&self) -> &[StackItem<M>]

Returns a list of StackItems stored within current undo manager responsible for performing potential undo operations.

Source

pub fn redo_stack(&self) -> &[StackItem<M>]

Returns a list of StackItems stored within current undo manager responsible for performing potential redo operations.

Source

pub async fn undo(&mut self) -> bool

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.

§Deadlocks

This method requires exclusive access to underlying document store. This means that no other transaction on that same document can be active while calling this method. Otherwise, it may cause a deadlock.

See also: [UndoManager::try_undo] and UndoManager::undo_blocking.

Source

pub fn undo_blocking(&mut self) -> bool

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.

§Deadlocks

This method requires exclusive access to underlying document store. This means that no other transaction on that same document can be active while calling this method. Otherwise, it may cause a deadlock.

See also: [UndoManager::try_undo] and UndoManager::undo.

Source

pub fn can_redo(&self) -> bool

Are there any redo steps available?

Source

pub async fn redo(&mut self) -> bool

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.

§Deadlocks

This method requires exclusive access to underlying document store. This means that no other transaction on that same document can be active while calling this method. Otherwise, it may cause a deadlock.

See also: [UndoManager::try_redo] and UndoManager::redo_blocking.

Source

pub fn redo_blocking(&mut self) -> bool

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.

§Deadlocks

This method requires exclusive access to underlying document store. This means that no other transaction on that same document can be active while calling this method. Otherwise, it may cause a deadlock.

See also: [UndoManager::try_redo] and UndoManager::redo_blocking.

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
Source§

impl<M> Drop for UndoManager<M>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

Auto Trait Implementations§

§

impl<M> !RefUnwindSafe for UndoManager<M>

§

impl<M> !Send for UndoManager<M>

§

impl<M> !Sync for UndoManager<M>

§

impl<M> !UnwindSafe for UndoManager<M>

§

impl<M> Freeze for UndoManager<M>

§

impl<M> Unpin for UndoManager<M>

§

impl<M> UnsafeUnpin 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>,

Source§

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>,

Source§

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.