All files / src/controllers ExperimentController.ts

100% Statements 46/46
100% Branches 4/4
100% Functions 25/25
100% Lines 32/32

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  5x 5x                 5x       12x           12x                 12x 1x 1x 1x   1x                 12x 1x 1x               12x 1x 1x             12x 1x 1x                     12x 1x 1x                 12x 1x 1x                   12x 1x 1x                       12x 2x 2x   2x      
import { EditorAPI, Id } from '../types/CommonTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
import { ImageSourceTypeEnum, ImageFrameVariableSource } from '../types/FrameTypes';
import { TextType } from '../types/TextTypes';
 
/**
 * The ExperimentController contains all SDK functions that are considered for addition,
 * or functions we want to keep open for changing definitions. This is a sneak peak
 * into future versions of the SDK. Never build production code relying on functions in
 * this controller.
 */
export class ExperimentController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method will assign an image from a variable to the correct ImageFrame
     * @param imageFrameId the id of the imageFrame where an image needs to be assigned to
     * @param variableId the id of the variable which contains the image
     * @returns
     */
    insertImageVariableToFrame = async (imageFrameId: Id, variableId: Id) => {
        const res = await this.#editorAPI;
        const src: ImageFrameVariableSource = { id: variableId, type: ImageSourceTypeEnum.variable };
        return res
            .setImageSource(imageFrameId, JSON.stringify(src))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method will insert a text variable in the selected frame. Calling this method
     * requires that the selected frame is in text editing mode.
     * @param id the id of the variable to be inserted.
     * @returns
     */
    insertTextVariable = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.insertTextVariable(id).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method will enter text editing mode on the provided frame.
     * @param id the id frame to enter text edit mode on.
     * @returns
     */
    enterTextEditMode = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.enterTextEditMode(id).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method will exit text editing mode.
     * @returns
     */
    exitTextEditMode = async () => {
        const res = await this.#editorAPI;
        return res.exitTextEditMode().then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method gets the text content of a text frame.
     * The variables contained in the text will return their values only if you
     * are in text editing mode.
     * @param frameId The ID of a text frame
     * @param textType The type of the text
     * @returns the text content
     */
    getText = async (frameId: string, textType: TextType) => {
        const res = await this.#editorAPI;
        return res.getTextByFrameId(frameId, textType).then((result) => getEditorResponseData<string>(result));
    };
 
    /**
     * This method sets the text content of a text frame
     * @param frameId The ID of a text frame
     * @param text The new text content
     * @returns
     */
    setText = async (frameId: string, text: string) => {
        const res = await this.#editorAPI;
        return res.setTextByFrameId(frameId, text).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method selects the text content of a text frame
     * @param frameId The ID of a text frame
     * @param startIndex The index where the selection starts
     * @param length The length of selection from startIndex
     * @returns
     */
    selectText = async (frameId: string, startIndex: number, length: number) => {
        const res = await this.#editorAPI;
        return res.selectTextById(frameId, startIndex, length).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
     * @param skipEvent Whether to skip the event that is triggered when the custom data is changed, defaults to false
     * @returns
     */
    addCustomUndoData = async (key: string, value: string, skipEvent?: boolean) => {
        const res = await this.#editorAPI;
        return res
            .setCustomUndoData(key, value, skipEvent ?? false)
            .then((result) => getEditorResponseData<null>(result));
    };
}