Skip to content

History Manager

A class for managing the history of changes in the editor.

HistoryManager Methods

FunctionSignatureDescription
scheduleSavescheduleSave(): voidSchedules saving the state with a delay.
savesave(): voidImmediately saves the current state of the editor.
undoundo(): booleanUndoes the last action. Returns true if the undo was successful.
redoredo(): booleanRedoes the last undone action. Returns true if the redo was successful.
canUndocanUndo(): booleanChecks if undoing an action is available.
canRedocanRedo(): booleanChecks if redoing an action is available.
clearclear(): voidClears all history.
setSaveIntervalsetSaveInterval(interval: number): voidSets the delay interval for saving the state (in milliseconds).

Example of Using HistoryManager

javascript
import Texditor from "texditor";

const editor = new Texditor({
  handle: "my-editor"
});

// Immediate saving of the state
editor.historyManager.save();

// Undo the last action
if (editor.historyManager.canUndo()) {
  editor.historyManager.undo();
}

// Redo the undone action
if (editor.historyManager.canRedo()) {
  editor.historyManager.redo();
}

// Schedule saving of the state
editor.historyManager.scheduleSave();

// Clear history
editor.historyManager.clear();

Types and Interfaces of HistoryManager

typescript
/**
 * Interface for selection data to be saved in history.
 * Describes the cursor position and selected block at the time of saving the state.
 * @property start - Start position of the selection in the text
 * @property end - End position of the selection in the text
 * @property index - Index of the current block
 * @property itemIndex - Index of the item within the block (for editable lists, etc.)
 */
export interface HistoryStateSelectionData {
 start: number;
 end: number;
 index: number;
 itemIndex?: number;
}

/**
 * Interface for history state.
 * Stores a full snapshot of the editor's state at a specific point in time.
 * @property content - Content of all editor blocks
 * @property selection - Current selection data
 * @property timestamp - Timestamp of the state creation (Unix timestamp)
 */
export interface HistoryState {
 content: BlockSchema[];
 selection: HistoryStateSelectionData;
 timestamp: number;
}

/**
 * Interface for the history manager.
 */
export interface HistoryManager {
 scheduleSave(): void;
 save(): void;
 undo(): boolean;
 redo(): boolean;
 canUndo(): boolean;
 canRedo(): boolean;
 clear(): void;
 setSaveInterval(interval: number): void;
}

dev@priveted.com | priveted.com