All files / src/controllers ToolController.ts

95.24% Statements 40/42
100% Branches 0/0
92% Functions 23/25
96.55% Lines 28/29

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  4x 4x           4x       12x           12x             12x 8x 8x           12x 1x 1x               12x 1x           12x             12x 1x           12x 1x           12x 1x           12x 1x           12x 1x           12x 1x           12x 1x      
import { EditorAPI } from '../types/CommonTypes';
import { ToolType } from '../utils/Enums';
import { getEditorResponseData } from '../utils/EditorResponseData';
 
/**
 * The ToolController is responsible for all communication regarding the tools.
 * Methods inside this controller can be called by `window.SDK.tool.{method-name}`
 */
export class ToolController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method sets the currently used tool
     * @param tool
     */
    setTool = async (tool: ToolType) => {
        const res = await this.#editorAPI;
        return res.setTool(tool).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method returns selected tool
     */
    getSelected = async () => {
        const res = await this.#editorAPI;
        return res.getSelectedTool().then((result) => getEditorResponseData<ToolType>(result));
    };
 
    /**
     * @deprecated
     * This method sets the used tool to the select tool.
     * This method is deprecated in favour of the setSelect tool setter.
     */
    setPointer = async () => {
        return this.setTool(ToolType.SELECT);
    };
 
    /**
     * This method sets the used tool to the select tool.
     */
    setSelect = async () => {
        return this.setTool(ToolType.SELECT);
    };
 
    /**
     * This method sets the used tool to the Move tool
     */
    setHand = async () => {
        return this.setTool(ToolType.HAND);
    };
 
    /**
     * This method sets the used tool to the Zoom tool
     */
    setZoom = async () => {
        return this.setTool(ToolType.ZOOM);
    };
 
    /**
     * This method sets the used tool to the TextFrame tool
     */
    setTextFrame = async () => {
        return this.setTool(ToolType.TEXT_FRAME);
    };
 
    /**
     * This method sets the used tool to the ImageFrame tool
     */
    setImageFrame = async () => {
        return this.setTool(ToolType.IMAGE_FRAME);
    };
 
    /**
     * This method sets the used tool to the ShapeRectangle tool
     */
    setShapeRect = async () => {
        return this.setTool(ToolType.SHAPE_RECT);
    };
 
    /**
     * This method sets the used tool to the ShapeEllipse tool
     */
    setShapeEllipse = async () => {
        return this.setTool(ToolType.SHAPE_ELLIPSE);
    };
 
    /**
     * This method sets the used tool to the ShapePolygon tool
     */
    setShapePolygon = async () => {
        return this.setTool(ToolType.SHAPE_POLYGON);
    };
}