All files / src/controllers DocumentController.ts

100% Statements 21/21
100% Branches 3/3
100% Functions 10/10
100% Lines 16/16

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        5x           5x       7x           7x             7x 1x 1x                             7x 2x   2x   2x   2x               7x 1x   1x   1x      
import { EditorAPI } from '../types/CommonTypes';
import type { ChiliDocument, DocumentLoadOptions } from '../types/DocumentTypes';
import { LayoutPreset } from '../types/LayoutTypes';
 
import { getEditorResponseData } from '../utils/EditorResponseData';
/**
 * The DocumentController is responsible for all communication regarding the Document.
 * Methods inside this controller can be called by `window.SDK.document.{method-name}`
 */
 
export class DocumentController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method retrieves the current document state from the editor
     * @returns the JSON document in the form of a string
     */
    getCurrentState = async () => {
        const res = await this.#editorAPI;
        return res.getCurrentDocumentState().then((result) => getEditorResponseData<ChiliDocument>(result));
    };
 
    /**
     * This method will load a provided document in the ChiliDocument format and
     * clean old document states.
     * If you set `DocumentLoadOptions.keepConnectors` to true, `load` will keep
     * your old connectors states e.g. options, authentication..
     * By default `keepConnectors` is false and your connectors states are reset.
     * In this case, you would need to configure your connectors appropriately
     * beforehand.
     * @param doc the document to load in
     * @param options the options object for setting up the document load
     * @returns the document loaded inside of the canvas
     */
    load = async (doc: ChiliDocument | string, options: DocumentLoadOptions = { keepConnectors: false }) => {
        const res = await this.#editorAPI;
 
        const parsedDoc = typeof doc !== 'string' ? JSON.stringify(doc) : doc;
 
        return res
            .loadDocument(parsedDoc, JSON.stringify(options))
            .then((result) => getEditorResponseData<null>(result));
    };
 
    /**
     * This method will create a new document, based on a document preset
     * @param preset the preset where we will create a document of.
     * @returns the new document created and loaded inside of the canvas
     */
    createAndLoad = async (preset: LayoutPreset) => {
        const res = await this.#editorAPI;
 
        const parsedDoc = JSON.stringify(preset);
 
        return res.createAndLoadDocument(parsedDoc).then((result) => getEditorResponseData<null>(result));
    };
}