All files / src/controllers ClipboardController.ts

100% Statements 30/30
50% Branches 2/4
100% Functions 9/9
100% Lines 26/26

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    5x           5x       7x           7x               7x 1x 1x   1x 1x     1x                 7x 1x 1x   1x 1x     1x             7x 1x   1x 1x   1x             7x 1x   1x 1x   1x      
import { ClipboardContentType } from '../types/ClipboardTypes';
import { EditorAPI, Id } from '../types/CommonTypes';
import { getEditorResponseData } from '../utils/EditorResponseData';
 
/**
 * The ClipboardController is responsible for all communication regarding clipboard.
 * Methods inside this controller can be called by `window.SDK.clipboard.{method-name}`
 */
export class ClipboardController {
    /**
     * @ignore
     */
    #editorAPI: EditorAPI;
 
    /**
     * @ignore
     */
    constructor(editorAPI: EditorAPI) {
        this.#editorAPI = editorAPI;
    }
 
    /**
     * This method will copy the frames to the OS clipboard as a json frame
     * @param ids An array of all frame ids you want to copy
     * @returns
     */
    copyFrames = async (ids: Id[]) => {
        const res = await this.#editorAPI;
        const frameData = await res.copyFrames(ids);
 
        Eif (frameData.data) {
            await navigator.clipboard.writeText(frameData.data);
        }
 
        return getEditorResponseData<null>(frameData);
    };
 
    /**
     * This method will remove the frames and store them to the OS clipboard as
     * a json frame
     * @param ids An array of all frame ids you want to cut
     * @returns
     */
    cutFrames = async (ids: Id[]) => {
        const res = await this.#editorAPI;
        const frameData = await res.cutFrames(ids);
 
        Eif (frameData.data) {
            await navigator.clipboard.writeText(frameData.data);
        }
 
        return getEditorResponseData<null>(frameData);
    };
 
    /**
     * This method will paste the OS clipboard content to the editor as frames
     * @returns
     */
    pasteFrames = async () => {
        const res = await this.#editorAPI;
 
        const clipboardData = await navigator.clipboard.readText();
        const result = await res.pasteFrames(clipboardData);
 
        return getEditorResponseData<null>(result);
    };
 
    /**
     * This method will get the content type of the OS clipboard
     * @returns content type
     */
    getContentType = async () => {
        const res = await this.#editorAPI;
 
        const clipboardData = await navigator.clipboard.readText();
        const result = await res.getClipboardContentType(clipboardData);
 
        return getEditorResponseData<ClipboardContentType>(result);
    };
}