History
History provides undo (back()) and redo (forward()), plus stack state and events. Use editor.get(History) for the current instance.
Finish Getting started first. Below, editor is a Textbus instance that has already **render**ed.
Getting History
import { History } from '@textbus/core'
const history = editor.get(History)TextbusConfig: history-related options
Set these on new Textbus({ ... }) or on **Module**s merged with imports (merge order: Modules & extensions).
historyStackSize
Type: number | undefined
Meaning: max number of undo steps; default 500 if omitted. Oldest steps fall off the stack and can no longer be undone.
const editor = new Textbus({
historyStackSize: 200,
// ...
})After you undo and then make new edits, the redo list is cleared (only the current branch remains).
readonly
Type: boolean | undefined
Meaning: whether the editor starts read-only. The same flag is available at runtime as editor.readonly.
const editor = new Textbus({
readonly: true,
// ...
})
editor.readonly = falseHistory: methods and properties
Assume const history = editor.get(History).
listen(): void
Start history recording. The editor calls this once after render completes; do not call listen() from app code.
back(): void
Undo one step. No-op if canBack === false.
history.back()forward(): void
Redo one step. No-op if canForward === false.
history.forward()clear(): void
Clear undo and redo stacks; afterwards canBack / canForward are false until new undoable edits happen.
history.clear()destroy(): void
Tear down this History. Called when editor is destroyed; you rarely call it yourself.
history.destroy()canBack (read-only)
Type: boolean
Meaning: whether back() is allowed.
undoBtn.disabled = !history.canBackcanForward (read-only)
Type: boolean
Meaning: whether forward() is allowed.
redoBtn.disabled = !history.canForwardonChange
Type: Observable<void>
Meaning: fires when undo/redo stacks or canBack / canForward change (e.g. clear(), new step pushed, back() / forward() run).
history.onChange.subscribe(() => {
undoBtn.disabled = !history.canBack
redoBtn.disabled = !history.canForward
})onPush
Type: Observable<void>
Meaning: a new undo step was pushed to the stack.
history.onPush.subscribe(() => {})onBack
Type: Observable<void>
Meaning: a back() completed successfully.
history.onBack.subscribe(() => {})onForward
Type: Observable<void>
Meaning: a forward() completed successfully.
history.onForward.subscribe(() => {})Default shortcuts
With the default shortcut registration:
- Undo: Mod+Z
- Redo: Mod+Shift+Z or Mod+Y
Same as history.back() / history.forward(). Customization: Shortcuts & grammar.
Read-only and undo
When readonly === true, users usually cannot make new edits; history.back() / forward() do not check readonly. To block undo/redo in a read-only UI, disable the buttons or avoid calling these methods.
FAQ
- Redo disabled: after undo, new edits replace the old redo branch.
- Clear history after loading a new document: call
history.clear().
What's next
- Shortcuts: Shortcuts & grammar
- Writes & selection: Query & operations, Selection
- Collaboration: Collaboration
