|
| 1 | +import { default as EditorJS, OutputBlockData, OutputData } from '@editorjs/editorjs'; |
| 2 | +import { EditorJsReady } from './types'; |
| 3 | +import { Blocks, Caret } from '@editorjs/editorjs/types/api'; |
| 4 | +export type UndoConfig = { |
| 5 | + debounceTimer: number; |
| 6 | + shortcuts: { |
| 7 | + redo: string[]; |
| 8 | + undo: string[]; |
| 9 | + }; |
| 10 | +}; |
| 11 | +export type UndoSettings = { |
| 12 | + debounceTimer?: number; |
| 13 | + shortcuts?: { |
| 14 | + redo?: string[] | string; |
| 15 | + undo?: string[] | string; |
| 16 | + }; |
| 17 | +}; |
| 18 | +interface StackStated { |
| 19 | + caretIndex?: null | number; |
| 20 | + index: number; |
| 21 | + state: OutputBlockData[]; |
| 22 | +} |
| 23 | +export type UndoConstructor = { |
| 24 | + config?: UndoSettings; |
| 25 | + maxLength?: number; |
| 26 | + onUpdate?: () => void; |
| 27 | + editor: EditorJS; |
| 28 | +}; |
| 29 | +/** |
| 30 | + * Undo/Redo feature for Editor.js. |
| 31 | + * |
| 32 | + * @typedef {Object} Undo |
| 33 | + * @description Feature's initialization class. |
| 34 | + * @property {Object} editor — Editor.js instance object. |
| 35 | + * @property {Number} maxLength - Max amount of changes recorded by the history stack. |
| 36 | + * @property {Function} onUpdate - Callback called when the user performs an undo or redo action. |
| 37 | + * @property {Boolean} shouldSaveHistory - Defines if the plugin should save the change in the stack |
| 38 | + * @property {Object} initialItem - Initial data object. |
| 39 | + */ |
| 40 | +export default class Undo { |
| 41 | + blocks: Blocks; |
| 42 | + caret: Caret; |
| 43 | + config: UndoConfig; |
| 44 | + defaultBlock: string | undefined; |
| 45 | + editor: EditorJsReady; |
| 46 | + holder: HTMLElement | null | undefined; |
| 47 | + initialItem: null | StackStated; |
| 48 | + maxLength: number; |
| 49 | + onUpdate: () => void; |
| 50 | + position: number; |
| 51 | + readOnly: boolean | undefined; |
| 52 | + shouldSaveHistory: boolean; |
| 53 | + stack: StackStated[]; |
| 54 | + /** |
| 55 | + * @param options — Plugin custom options. |
| 56 | + */ |
| 57 | + constructor({ editor, config, onUpdate, maxLength }: UndoConstructor); |
| 58 | + /** |
| 59 | + * Notify core that read-only mode is suppoorted |
| 60 | + * |
| 61 | + * @returns {boolean} |
| 62 | + */ |
| 63 | + static get isReadOnlySupported(): boolean; |
| 64 | + /** |
| 65 | + * Truncates the history stack when it excedes the limit of changes. |
| 66 | + * |
| 67 | + * @param {Object} stack Changes history stack. |
| 68 | + * @param {Number} stack Limit of changes recorded by the history stack. |
| 69 | + */ |
| 70 | + truncate(stack: StackStated[], limit: number): void; |
| 71 | + /** |
| 72 | + * Initializes the stack when the user provides initial data. |
| 73 | + * |
| 74 | + * @param {Object} initialItem Initial data provided by the user. |
| 75 | + */ |
| 76 | + initialize(initialItem: OutputData | OutputBlockData[]): void; |
| 77 | + /** |
| 78 | + * Clears the history stack. |
| 79 | + */ |
| 80 | + clear(): void; |
| 81 | + /** |
| 82 | + * Returns true if readOnly was toggled to true |
| 83 | + * @returns {Node} Indirectly shows if readOnly was set to true or false |
| 84 | + */ |
| 85 | + setReadOnly(): void; |
| 86 | + /** |
| 87 | + * Registers the data returned by API's save method into the history stack. |
| 88 | + */ |
| 89 | + registerChange(): void; |
| 90 | + /** |
| 91 | + * Checks if the saved data has to be added to the history stack. |
| 92 | + * |
| 93 | + * @param {Object} newData New data to be saved in the history stack. |
| 94 | + * @returns {Boolean} |
| 95 | + */ |
| 96 | + editorDidUpdate(newData: OutputBlockData[]): boolean; |
| 97 | + /** |
| 98 | + * Adds the saved data in the history stack and updates current position. |
| 99 | + */ |
| 100 | + save(state: OutputBlockData[]): void; |
| 101 | + /** |
| 102 | + * Gets the caret position. |
| 103 | + * @param {Number} index is the block index |
| 104 | + * @returns The caret position |
| 105 | + */ |
| 106 | + getCaretIndex(index: number): number | null; |
| 107 | + /** |
| 108 | + * Decreases the current position and update the respective block in the editor. |
| 109 | + */ |
| 110 | + undo(): Promise<void>; |
| 111 | + /** |
| 112 | + * Sets the caret position. |
| 113 | + * @param {Number} index is the block index |
| 114 | + * @param {Number} caretIndex is the caret position |
| 115 | + * @param {Array} state is the current state according to this.position. |
| 116 | + */ |
| 117 | + setCaretIndex(index: number, caretIndex: number): void; |
| 118 | + /** |
| 119 | + * Inserts new block |
| 120 | + * @param {Array} state is the current state according to this.position. |
| 121 | + * @param {Number} index is the block index |
| 122 | + */ |
| 123 | + insertBlock(state: OutputBlockData[], index: number): void; |
| 124 | + /** |
| 125 | + * Updates the passed block or render the state when the content was copied. |
| 126 | + * @param {Array} state is the current state according to this.position. |
| 127 | + * @param {Number} index is the block index. |
| 128 | + */ |
| 129 | + updateModifiedBlock(state: OutputBlockData[], index: number): Promise<void | import('@editorjs/editorjs').BlockAPI>; |
| 130 | + /** |
| 131 | + * Increases the current position and update the respective block in the editor. |
| 132 | + */ |
| 133 | + redo(): Promise<void>; |
| 134 | + switchState(stateToApply: OutputBlockData[], stateToCompare: OutputBlockData[]): Promise<void>; |
| 135 | + /** |
| 136 | + * Checks if the history stack can perform an undo action. |
| 137 | + * |
| 138 | + * @returns {Boolean} |
| 139 | + */ |
| 140 | + canUndo(): boolean; |
| 141 | + /** |
| 142 | + * Checks if the history stack can perform a redo action. |
| 143 | + * |
| 144 | + * @returns {Boolean} |
| 145 | + */ |
| 146 | + canRedo(): boolean; |
| 147 | + /** |
| 148 | + * Returns the number of changes recorded in the history stack. |
| 149 | + * |
| 150 | + * @returns {Number} |
| 151 | + */ |
| 152 | + count(): number; |
| 153 | + /** |
| 154 | + * Parses the keys passed in the shortcut property to accept CMD,ALT and SHIFT |
| 155 | + * |
| 156 | + * @param {Array} keys are the keys passed in shortcuts in config |
| 157 | + * @returns {Array} |
| 158 | + */ |
| 159 | + parseKeys(keys: string[]): string[]; |
| 160 | + /** |
| 161 | + * Sets events listeners to allow keyboard actions support |
| 162 | + */ |
| 163 | + setEventListeners(): void; |
| 164 | +} |
| 165 | +export {}; |
0 commit comments