ElementorexCms.Editor.History (ElementorexCms v0.1.0)

Copy Markdown View Source

Undo and redo for the editor: snapshots of the document with a label.

Edits of the same field that follow each other quickly (typing in an input) are merged into one step, so undo goes back a whole word and not a letter.

Summary

Functions

Whether there is something to redo.

Whether there is something to undo.

An empty history.

Records previous (the document before a change) under label. key identifies the edited field: consecutive changes with the same key within 1500 ms are merged.

Goes one step forward. Returns {:ok, document, history} or :error.

Jumps to step index of past (0 is the most recent), keeping the steps in between as redo steps.

Goes one step back. Returns {:ok, document, history} or :error.

Types

entry()

@type entry() :: %{doc: map(), label: String.t(), at: integer()}

t()

@type t() :: %ElementorexCms.Editor.History{
  future: [entry()],
  last: term(),
  limit: pos_integer(),
  past: [entry()]
}

Functions

can_redo?(history)

Whether there is something to redo.

can_undo?(history)

Whether there is something to undo.

new(opts \\ [])

@spec new(keyword()) :: t()

An empty history.

push(history, previous, label, key \\ nil)

@spec push(t(), map(), String.t(), term()) :: t()

Records previous (the document before a change) under label. key identifies the edited field: consecutive changes with the same key within 1500 ms are merged.

redo(history, current)

@spec redo(t(), map()) :: {:ok, map(), t()} | :error

Goes one step forward. Returns {:ok, document, history} or :error.

restore(history, current, index)

@spec restore(t(), map(), non_neg_integer()) :: {:ok, map(), t()} | :error

Jumps to step index of past (0 is the most recent), keeping the steps in between as redo steps.

undo(history, current)

@spec undo(t(), map()) :: {:ok, map(), t()} | :error

Goes one step back. Returns {:ok, document, history} or :error.