Development Documentation (main branch) - For stable release docs, see docs.rs/eidetica

UndoManager

Struct UndoManager 

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§

§

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

pub fn new<T>(doc: &Doc, scope: &T) -> UndoManager<M>
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.

pub fn doc(&self) -> &Doc

pub fn with_options(doc: &Doc, options: Options<M>) -> UndoManager<M>

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.

pub fn with_scope_and_options<T>( doc: &Doc, scope: &T, options: Options<M>, ) -> UndoManager<M>
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.

pub fn observe_item_added<F>(&self, f: F) -> Arc<dyn Drop>
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.

pub fn observe_item_added_with<K, F>(&self, key: K, f: F)
where K: Into<Origin>, 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.

Provided key is used to identify the origin of the callback. It can be used to unregister the callback later on.

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

pub fn observe_item_updated<F>(&self, f: F) -> Arc<dyn Drop>
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.

pub fn observe_item_updated_with<K, F>(&self, key: K, f: F)
where K: Into<Origin>, 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.

Provided key is used to identify the origin of the callback. It can be used to unregister the callback later on.

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

pub fn observe_item_popped<F>(&self, f: F) -> Arc<dyn Drop>
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.

pub fn observe_item_popped_with<K, F>(&self, key: K, f: F)
where K: Into<Origin>, 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.

Provided key is used to identify the origin of the callback. It can be used to unregister the callback later on.

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

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.

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).

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.

pub fn clear(&mut self)

Clears all StackItems stored within current UndoManager, 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.

pub fn as_origin(&self) -> Origin

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_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(&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)

pub fn can_undo(&self) -> bool

Are there any undo steps available?

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

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

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

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

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.

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.

pub fn try_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.

See also: UndoManager::undo and UndoManager::undo_blocking.

§Errors

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, an error will be returned.

pub fn can_redo(&self) -> bool

Are there any redo steps available?

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.

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.

pub fn try_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 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§

§

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

§

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

Formats the value using the given formatter. Read more
§

impl<M> Drop for UndoManager<M>

§

fn drop(&mut self)

Executes the destructor for this type. 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
§

impl<T> CompatExt for T

§

fn compat(self) -> Compat<T>

Applies the [Compat] adapter by value. Read more
§

fn compat_ref(&self) -> Compat<&T>

Applies the [Compat] adapter by shared reference. Read more
§

fn compat_mut(&mut self) -> Compat<&mut T>

Applies the [Compat] adapter by mutable reference. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more