All files / src/controllers ParagraphStyleController.ts

100% Statements 44/44
100% Branches 0/0
100% Functions 25/25
100% Lines 29/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 105    5x           5x       11x           11x             11x 1x 1x               11x 1x 1x             11x 1x 1x               11x 1x 1x                 11x 1x 1x   1x                 11x 1x 1x               11x 1x 1x                 11x 1x 1x      
import { EditorAPI, Id } from '../types/CommonTypes';
import { ParagraphStyle, ParagraphStyleUpdate } from '../types/ParagraphStyleTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
 
/**
 * The ParagraphStyleController is responsible for all communication regarding paragraph styles.
 * Methods inside this controller can be called by `window.SDK.paragraphStyle.{method-name}`
 */
export class ParagraphStyleController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method returns the list of paragraph styles
     * @returns
     */
    getAll = async () => {
        const res = await this.#editorAPI;
        return res.getParagraphStyles().then((result) => getEditorResponseData<ParagraphStyle[]>(result));
    };
 
    /**
     * This method returns a paragraph style by id
     * @param id the id of a specific paragraph style
     * @returns
     */
    getById = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.getParagraphStyleById(id).then((result) => getEditorResponseData<ParagraphStyle>(result));
    };
 
    /**
     * This method creates a new paragraph style
     * @returns the id of new paragraph style
     */
    create = async () => {
        const res = await this.#editorAPI;
        return res.createParagraphStyle().then((result) => getEditorResponseData<Id>(result));
    };
 
    /**
     * This method duplicates a paragraph style
     * @param id the id of a specific paragraph style
     * @returns the new paragraph style id
     */
    duplicate = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.duplicateParagraphStyle(id).then((result) => getEditorResponseData<Id>(result));
    };
 
    /**
     * This method updates a paragraph style
     * @param id the id of a specific paragraph style
     * @param properties The new paragraph style properties
     * @returns
     */
    update = async (id: Id, properties: ParagraphStyleUpdate) => {
        const res = await this.#editorAPI;
        return res
            .updateParagraphStyle(id, JSON.stringify(properties))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method renames a paragraph style
     * @param id the id of a specific paragraph style
     * @param name the new name of the paragraph style
     * @returns
     */
    rename = async (id: Id, name: string) => {
        const res = await this.#editorAPI;
        return res.renameParagraphStyle(id, name).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method removes a paragraph style
     * @param id the id of a specific paragraph style
     * @returns
     */
    remove = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.removeParagraphStyle(id).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method changes positions of paragraph styles
     * @param order the position of the paragraph styles
     * @param ids the list of paragraph style ids
     * @returns
     */
    move = async (order: number, ids: Id[]) => {
        const res = await this.#editorAPI;
        return res.moveParagraphStyles(order, ids).then((result) => getEditorResponseData<null>(result));
    };
}