All files / src/controllers FontController.ts

100% Statements 69/69
100% Branches 0/0
100% Functions 40/40
100% Lines 45/45

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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161    5x           5x       16x           16x                 16x 1x 1x   1x               16x 1x 1x                 16x 1x 1x   1x               16x 1x 1x             16x 1x 1x               16x 1x 1x               16x 1x 1x               16x 1x 1x                   16x 1x 1x                 16x 1x 1x               16x 1x 1x               16x 1x 1x                 16x 1x 1x      
import { EditorAPI, Id } from '../types/CommonTypes';
import { AddDocumentFontFamily, AddDocumentFontStyle, DocumentFontFamily, DocumentFontStyle } from '../types/FontTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
 
/**
 * The FontController is responsible for all communication regarding font styles.
 * Methods inside this controller can be called by `window.SDK.font.{method-name}`
 */
export class FontController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method adds a font family
     * @param connectorId unique id of the font connector
     * @param fontFamily the font family object
     * @returns id generated on the engine side `CONNECTOR_ID::FONT_FAMILY_ID`
     */
    addFontFamily = async (connectorId: Id, fontFamily: AddDocumentFontFamily) => {
        const res = await this.#editorAPI;
        return res
            .addFontFamily(connectorId, JSON.stringify(fontFamily))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method removes a font family
     * @param id the id of a specific font family
     * @returns
     */
    removeFontFamily = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.removeFontFamily(id).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method adds a font style
     * @param connectorId unique id of the font connector
     * @param fontStyle the font object
     * @returns id generated on the engine side `CONNECTOR_ID::FONT_FAMILY_ID::FONT_STYLE_ID`
     */
    addFontStyle = async (connectorId: Id, fontStyle: AddDocumentFontStyle) => {
        const res = await this.#editorAPI;
        return res
            .addFontStyle(connectorId, JSON.stringify(fontStyle))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method removes a font style
     * @param id the id of a specific font style
     * @returns
     */
    removeFontStyle = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.removeFontStyle(id).then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method returns the list of font families
     * @returns DocumentFontFamily[]
     */
    getFontFamilies = async () => {
        const res = await this.#editorAPI;
        return res.getFontFamilies().then((result) => getEditorResponseData<DocumentFontFamily[]>(result));
    };
 
    /**
     * This method returns the list of font styles for a specific family
     * @param id the id of a specific font style
     * @returns DocumentFontStyle[]
     */
    getFontStyles = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.getFontStyles(id).then((result) => getEditorResponseData<DocumentFontStyle[]>(result));
    };
 
    /**
     * This method returns a font family by id
     * @param id the id of a specific font family
     * @returns DocumentFontFamily properties
     */
    getFontFamilyById = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.getFontFamilyById(id).then((result) => getEditorResponseData<DocumentFontFamily>(result));
    };
 
    /**
     * This method returns a font style by id
     * @param id the id of a specific font style
     * @returns DocumentFontStyle properties
     */
    getFontStyleById = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.getFontStyleById(id).then((result) => getEditorResponseData<DocumentFontStyle>(result));
    };
 
    /**
     * This method returns the default font.
     * Be aware that the default font will not change during the entire lifetime of the SDK session.
     * It is not necessary to call this more than once in an integration, this value can be safely stored during the lifetime of this SDK session.
     * @returns a default DocumentFontStyle
     */
 
    getDefaultFontStyle = async () => {
        const res = await this.#editorAPI;
        return res.getDefaultFontStyle().then((result) => getEditorResponseData<DocumentFontStyle>(result));
    };
 
    /**
     * This method returns the default font family.
     * Be aware that the default font family will not change during the entire lifetime of the SDK session.
     * It is not necessary to call this more than once in an integration, this value can be safely stored during the lifetime of this SDK session.
     * @returns a default DocumentFontFamily
     */
    getDefaultFontFamily = async () => {
        const res = await this.#editorAPI;
        return res.getDefaultFontFamily().then((result) => getEditorResponseData<DocumentFontFamily>(result));
    };
 
    /**
     * Check if the font family is used anywhere in the document
     * @param id the id of the font family to check
     * @returns boolean
     */
    isFontFamilyUsed = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.isFontFamilyUsed(id).then((result) => getEditorResponseData<boolean>(result));
    };
 
    /**
     * Check if the font style is used anywhere in the document
     * @param id the id of the font style to check
     * @returns boolean
     */
    isFontStyleUsed = async (id: Id) => {
        const res = await this.#editorAPI;
        return res.isFontStyleUsed(id).then((result) => getEditorResponseData<boolean>(result));
    };
 
    /**
     * This method changes positions of font families
     * @param order the position of the font family
     * @param ids the list of font family  IDs
     * @returns
     */
    moveFontFamilies = async (order: number, ids: string[]) => {
        const res = await this.#editorAPI;
        return res.moveFontFamilies(order, ids).then((result) => getEditorResponseData<null>(result));
    };
}