All files / src/controllers ColorStyleController.ts

100% Statements 48/48
100% Branches 2/2
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  5x 5x           5x       12x           12x             12x 1x 1x               12x 1x 1x             12x 1x 1x               12x 1x 1x                 12x 1x 1x                 12x 1x 1x                 12x 2x 2x 2x 1x   2x               12x 1x 1x      
import { EditorAPI, Id } from '../types/CommonTypes';
import { ColorType, ColorUpdate, DocumentColor } from '../types/ColorStyleTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
 
/**
 * The ColorStyleController is responsible for all communication regarding color styles.
 * Methods inside this controller can be called by `window.SDK.colorStyle.{method-name}`
 */
export class ColorStyleController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method returns the list of colors
     * @returns list of all colors
     */
    getAll = async () => {
        const res = await this.#editorAPI;
        return res.getColors().then((result) => getEditorResponseData<DocumentColor[]>(result));
    };
 
    /**
     * This method returns a color by id
     * @param id the id of a specific color
     * @returns color properties for given id
     */
    getById = async (id: string) => {
        const res = await this.#editorAPI;
        return res.getColorById(id).then((result) => getEditorResponseData<DocumentColor>(result));
    };
 
    /**
     * This method creates a new color
     * @returns the new created color id
     */
    create = async () => {
        const res = await this.#editorAPI;
        return res.createColor().then((result) => getEditorResponseData<Id>(result));
    };
 
    /**
     * This method duplicates a color by the id
     * @param id the id of a specific color
     * @returns id of the duplicated color
     */
    duplicate = async (id: string) => {
        const res = await this.#editorAPI;
        return res.duplicateColor(id).then((result) => getEditorResponseData<Id>(result));
    };
 
    /**
     * This method changes positions of colors
     * @param order the position of the colors
     * @param ids the list of color IDs
     * @returns
     */
    move = async (order: number, ids: string[]) => {
        const res = await this.#editorAPI;
        return res.moveColors(order, ids).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method renames a color
     * @param id the id of a specific color
     * @param newColorName the new name of the color
     * @returns
     */
    rename = async (id: string, newColorName: string) => {
        const res = await this.#editorAPI;
        return res.renameColor(id, newColorName).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method updates a color
     * @param id the id of a specific color
     * @param newColorProperties the new color properties
     * @returns
     */
    update = async (id: string, newColorProperties: ColorUpdate) => {
        const res = await this.#editorAPI;
        const properties = newColorProperties;
        if (properties.type === ColorType.spot) {
            properties.type = ColorType.spotCMYK;
        }
        return res.updateColor(id, JSON.stringify(properties)).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method removes a color
     * @param id the id of a specific color
     * @returns
     */
    remove = async (id: string) => {
        const res = await this.#editorAPI;
        return res.removeColor(id).then((result) => getEditorResponseData<null>(result));
    };
}