Class UndoManager

UndoManager is responsible for handling undo and redo operations.

By default, the maxUndoSteps is set to 100, mergeInterval is set to 1000 ms.

Each commit made by the current peer is recorded as an undo step in the UndoManager. Undo steps can be merged if they occur within a specified merge interval.

Note that undo operations are local and cannot revert changes made by other peers. To undo changes made by other peers, consider using the time travel feature.

Once the peerId is bound to the UndoManager in the document, it cannot be changed. Otherwise, the UndoManager may not function correctly.

Constructors

  • UndoManager is responsible for handling undo and redo operations.

    PeerID cannot be changed during the lifetime of the UndoManager.

    Note that undo operations are local and cannot revert changes made by other peers. To undo changes made by other peers, consider using the time travel feature.

    Each commit made by the current peer is recorded as an undo step in the UndoManager. Undo steps can be merged if they occur within a specified merge interval.

    • mergeInterval: Optional. The interval in milliseconds within which undo steps can be merged. Default is 1000 ms.
    • maxUndoSteps: Optional. The maximum number of undo steps to retain. Default is 100.
    • excludeOriginPrefixes: Optional. An array of string prefixes. Events with origins matching these prefixes will be excluded from undo steps.
    • onPush: Optional. A callback function that is called when an undo/redo step is pushed. The function can return a meta data value that will be attached to the given stack item.
    • onPop: Optional. A callback function that is called when an undo/redo step is popped. The function will have a meta data value that was attached to the given stack item when onPush was called.

    Parameters

    Returns UndoManager

Methods

  • If a local event's origin matches the given prefix, it will not be recorded in the undo stack.

    Parameters

    • prefix: string

    Returns void

  • Can redo the last operation.

    Returns boolean

  • Can undo the last operation.

    Returns boolean

  • Returns void

  • Returns void

  • Redo the last undone operation.

    Returns boolean

  • The number of max undo steps. If the number of undo steps exceeds this number, the oldest undo step will be removed.

    Parameters

    • steps: number

    Returns void

  • Set the merge interval (in ms).

    If the interval is set to 0, the undo steps will not be merged. Otherwise, the undo steps will be merged if the interval between the two steps is less than the given interval.

    Parameters

    • interval: number

    Returns void

  • Set the callback function that is called when an undo/redo step is popped. The function will have a meta data value that was attached to the given stack item when onPush was called.

    Parameters

    • Optionallistener: (
          isUndo: boolean,
          value: { cursors: Cursor[]; value: Value },
          counterRange: { end: number; start: number },
      ) => void

      The callback function.

    Returns void

  • Set the callback function that is called when an undo/redo step is pushed. The function can return a meta data value that will be attached to the given stack item.

    Parameters

    • Optionallistener: (
          isUndo: boolean,
          counterRange: { end: number; start: number },
          event?: LoroEventBatch,
      ) => { cursors: Cursor[]; value: Value }

      The callback function.

    Returns void

  • Undo the last operation.

    Returns boolean