All files / src/controllers CharacterStyleController.ts

100% Statements 44/44
100% Branches 0/0
100% Functions 25/25
100% Lines 31/31

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  5x             5x       11x           11x             11x 1x 1x               11x 1x 1x   1x             11x 1x 1x                 11x 1x 1x   1x               11x 1x 1x               11x 1x 1x                 11x 1x 1x   1x                 11x 1x 1x      
import { EditorAPI, Id } from '../types/CommonTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
import { CharacterStyle, CharacterStyleUpdate } from '../types/CharacterStyleTypes';
 
/**
 * The CharacterStyleController is responsible for all communication regarding character styles.
 * Methods inside this controller can be called by `window.SDK.characterStyle.{method-name}`
 */
export class CharacterStyleController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method returns the list of character styles
     * @returns list of all character styles
     */
    getAll = async () => {
        const res = await this.#editorAPI;
        return res.getCharacterStyles().then((result) => getEditorResponseData<CharacterStyle[]>(result));
    };
 
    /**
     * This method returns a character style by the id
     * @param characterStyleId the id of a specific character style
     * @returns character style for given id
     */
    getById = async (characterStyleId: Id) => {
        const res = await this.#editorAPI;
        return res
            .getCharacterStyleById(characterStyleId)
            .then((result) => getEditorResponseData<CharacterStyle>(result));
    };
 
    /**
     * This method creates a new character style
     * @returns the id of new character style
     */
    create = async () => {
        const res = await this.#editorAPI;
        return res.createCharacterStyle().then((result) => getEditorResponseData<Id>(result));
    };
 
    /**
     * This method updates a character style by the id and provided properties
     * @param characterStyleId the id of a specific character style
     * @param characterStyle the new character style properties
     * @returns
     */
    update = async (characterStyleId: Id, characterStyle: CharacterStyleUpdate) => {
        const res = await this.#editorAPI;
        return res
            .updateCharacterStyle(characterStyleId, JSON.stringify(characterStyle))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method removes a character style by the id
     * @param characterStyleId the id of a specific character style
     * @returns
     */
    remove = async (characterStyleId: Id) => {
        const res = await this.#editorAPI;
        return res.removeCharacterStyle(characterStyleId).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method duplicates a character style by the id
     * @param characterStyleId the id of a specific character style
     * @returns id of the duplicated character style
     */
    duplicate = async (characterStyleId: Id) => {
        const res = await this.#editorAPI;
        return res.duplicateCharacterStyle(characterStyleId).then((result) => getEditorResponseData<Id>(result));
    };
 
    /**
     * This method renames a character style by the id
     * @param characterStyleId the id of a specific character style
     * @param characterStyleName the new name of the character style
     * @returns
     */
    rename = async (characterStyleId: Id, characterStyleName: string) => {
        const res = await this.#editorAPI;
        return res
            .renameCharacterStyle(characterStyleId, characterStyleName)
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method changes positions of character styles
     * @param order the position of the character styles
     * @param ids the list of character style ids
     * @returns
     */
    move = async (order: number, ids: Id[]) => {
        const res = await this.#editorAPI;
        return res.moveCharacterStyles(order, ids).then((result) => getEditorResponseData<null>(result));
    };
}