All files / src/controllers UndoManagerController.ts

91.38% Statements 53/58
100% Branches 0/0
89.29% Functions 25/28
92.68% Lines 38/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137    4x           4x       11x 11x 11x           11x 11x 11x             11x 1x 1x             11x 1x 1x                     11x 1x 1x                 11x 1x 1x   1x       1x               11x 2x 2x             11x           4x       19x           19x               19x 1x 1x               19x 2x 2x               19x 1x 1x      
import SDK from '..';
import { EditorAPI } from '../types/CommonTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
 
/**
 * The UndoManagerController is responsible for all communication regarding the Undo-Manager.
 * Methods inside this controller can be called by `window.SDK.undoManager.{method-name}`
 */
export class UndoManagerController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
    #advanced: AdvancedUndoManagerController;
    #sdk: SDK;
 
    /**
     * @ignore
     */
    constructor(children: EditorAPI, sdk: SDK) {
        this.#editorAPI = children;
        this.#sdk = sdk;
        this.#advanced = new AdvancedUndoManagerController(children);
    }
 
    /**
     * This method undoes the last operation
     * @returns
     */
    undo = async () => {
        const res = await this.#editorAPI;
        return res.undo().then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method redoes the last operation
     * @returns
     */
    redo = async () => {
        const res = await this.#editorAPI;
        return res.redo().then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method adds custom data that will be saved and restored when undoing and redoing.
     * Duplicate values are overwritten. The data is exposed via the onCustomUndoDataChanged event.
     *
     * @param key The key of the custom data
     * @param value The value of the custom data
     * @returns
     */
    addCustomData = async (key: string, value: string) => {
        const res = await this.#editorAPI;
        return res.setCustomUndoData(key, value, false).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * Record any operations in the current scope. This will automatically begin
     * the undo operation. Once you leave the record scope, it will end the undo operation.
     * Even if you throw an exception inside the record scope it will still end it properly.
     * @returns
     */
    record = async (operationName: string, undoOperationCallback: (sdk: SDK) => void) => {
        try {
            await this.#advanced.beginIfNoneActive(operationName);
 
            await undoOperationCallback(this.#sdk);
        } catch (error) {
            throw error;
        } finally {
            await this.#advanced.end();
        }
    };
 
    /**
     * This method pauses the undo manager
     * @returns
     */
    pause = async () => {
        const res = await this.#editorAPI;
        return res.pause().then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method resumes the undo manager
     * @returns
     */
    resume = async () => {
        const res = await this.#editorAPI;
        return res.resume().then((result) => getEditorResponseData<null>(result));
    };
}
 
export class AdvancedUndoManagerController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(children: EditorAPI) {
        this.#editorAPI = children;
    }
 
    /**
     * This will start a new undo operation.
     * This will throw an exception when there is already an undo operation recording.
     * @returns
     */
    begin = async (operationName: string) => {
        const res = await this.#editorAPI;
        return res.begin(operationName).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This will start a new undo operation if there is no other undo operation recording.
     * This does not throw.
     * @returns
     */
    beginIfNoneActive = async (operationName: string) => {
        const res = await this.#editorAPI;
        return res.beginIfNoneActive(operationName).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * Ends the currently active recording operation.
     * If there is no recording operation currently running this will throw an exception.
     * @returns
     */
    end = async () => {
        const res = await this.#editorAPI;
        return res.end().then((result) => getEditorResponseData<null>(result));
    };
}