History Manager
A class for managing the history of changes in the editor.
HistoryManager Methods
| Function | Signature | Description |
|---|---|---|
| scheduleSave | scheduleSave(): void | Schedules saving the state with a delay. |
| save | save(): void | Immediately saves the current state of the editor. |
| undo | undo(): boolean | Undoes the last action. Returns true if the undo was successful. |
| redo | redo(): boolean | Redoes the last undone action. Returns true if the redo was successful. |
| canUndo | canUndo(): boolean | Checks if undoing an action is available. |
| canRedo | canRedo(): boolean | Checks if redoing an action is available. |
| clear | clear(): void | Clears all history. |
| setSaveInterval | setSaveInterval(interval: number): void | Sets 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;
}