All files / src/controllers CanvasController.ts

100% Statements 26/26
100% Branches 2/2
100% Functions 13/13
100% Lines 19/19

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    5x           5x       11x           11x                           11x           3x 3x 2x   3x 3x             11x 1x 1x               11x 1x 1x               11x 1x 1x      
import { EditorAPI, Id } from '../types/CommonTypes';
import { ViewMode } from '../types/ViewModeTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
 
/**
 * The CanvasController is responsible for all Canvas-related functionality.
 * Methods inside this controller can be called by `window.SDK.canvas.{method-name}`
 */
export class CanvasController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method fits the page to the given rectangle or the viewport available
     * Optional parameters `left`, `top`, `width` and `height` are needed to define the rectangle to fit the page to
     * If any or all of them aren't provided the page will fit the whole viewport available
     * @param _id the id of a specific page
     * @param left
     * @param top
     * @param width
     * @param height
     * @returns
     */
    zoomToPage = async (
        _id?: Id | null,
        left?: number | null,
        top?: number | null,
        width?: number | null,
        height?: number | null,
    ) => {
        if (_id !== undefined) {
            console.warn('id has no effect and will be deprecated in the future');
        }
        const res = await this.#editorAPI;
        return res.zoomToPage(left, top, width, height).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method gets the scale factor of the canvas
     * @returns scale factor in percents
     */
    getZoomPercentage = async () => {
        const res = await this.#editorAPI;
        return res.getZoomPercentage().then((result) => getEditorResponseData<number>(result));
    };
 
    /**
     * This method sets the scale factor to the canvas and re-centers the page
     * @param scaleFactor scale factor in percents
     * @returns
     */
    setZoomPercentage = async (scaleFactor: number) => {
        const res = await this.#editorAPI;
        return res.setZoomPercentage(scaleFactor).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method sets a new view mode
     * @param viewMode view mode of the editor
     * @returns
     */
    setViewMode = async (viewMode: ViewMode) => {
        const res = await this.#editorAPI;
        return res.setViewMode(viewMode).then((result) => getEditorResponseData<null>(result));
    };
}