• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

worktile / slate-angular / 2241d398-cf7c-4f79-b1b3-923ba1e05d17

11 Jul 2025 07:46AM UTC coverage: 48.44%. Remained the same
2241d398-cf7c-4f79-b1b3-923ba1e05d17

push

circleci

pubuzhixing8
build: release 19.1.0-next.3

364 of 925 branches covered (39.35%)

Branch coverage included in aggregate %.

940 of 1767 relevant lines covered (53.2%)

44.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

8.77
/packages/src/utils/clipboard/clipboard.ts
1
import { Element } from 'slate';
2
import { ClipboardData } from '../../types/clipboard';
3
import { isClipboardReadSupported, isClipboardWriteSupported, isClipboardWriteTextSupported } from './common';
4
import { getDataTransferClipboard, setDataTransferClipboard, setDataTransferClipboardText } from './data-transfer';
5
import { getNavigatorClipboard, setNavigatorClipboard } from './navigator-clipboard';
6
import { getSlateFragmentAttribute, SlateFragmentAttributeKey } from '../dom';
7

8
export const buildHTMLText = (wrapper: HTMLElement, attach: HTMLElement, data: Element[]) => {
1✔
9
    const stringObj = JSON.stringify(data);
×
10
    const encoded = window.btoa(encodeURIComponent(stringObj));
×
11
    attach.setAttribute(SlateFragmentAttributeKey, encoded);
×
12
    return wrapper.innerHTML;
×
13
};
14

15
export const getClipboardFromHTMLText = (html: string): ClipboardData => {
1✔
16
    const fragmentAttribute = getSlateFragmentAttribute(html);
×
17
    if (fragmentAttribute) {
×
18
        try {
×
19
            const decoded = decodeURIComponent(window.atob(fragmentAttribute));
×
20
            const result = JSON.parse(decoded);
×
21
            if (result && Array.isArray(result) && result.length > 0) {
×
22
                return {
×
23
                    elements: result
24
                };
25
            }
26
        } catch (error) {
27
            console.error(error);
×
28
            return null;
×
29
        }
30
    }
31
    return null;
×
32
};
33

34
export const createClipboardData = (html: string, elements: Element[], text: string, files: File[]): ClipboardData => {
1✔
35
    const data = { elements, text, html, files };
×
36
    return data;
×
37
};
38

39
export const getClipboardData = async (dataTransfer?: DataTransfer): Promise<ClipboardData> => {
1✔
40
    let clipboardData = null;
×
41
    if (dataTransfer) {
×
42
        let filesData = {};
×
43
        if (dataTransfer.files.length) {
×
44
            filesData = { ...filesData, files: Array.from(dataTransfer.files) };
×
45
        }
46
        clipboardData = getDataTransferClipboard(dataTransfer);
×
47
        return { ...clipboardData, ...filesData };
×
48
    }
49
    if (isClipboardReadSupported()) {
×
50
        return await getNavigatorClipboard();
×
51
    }
52
    return clipboardData;
×
53
};
54

55
/**
56
 * @param wrapper get wrapper.innerHTML string which will be written in clipboard
57
 * @param attach attach must be child element of wrapper which will be attached json data
58
 * @returns void
59
 */
60
export const setClipboardData = async (
1✔
61
    clipboardData: ClipboardData,
62
    wrapper: HTMLElement,
63
    attach: HTMLElement,
64
    dataTransfer?: Pick<DataTransfer, 'getData' | 'setData'>
65
) => {
66
    if (!clipboardData) {
×
67
        return;
×
68
    }
69
    const { elements, text } = clipboardData;
×
70
    if (isClipboardWriteSupported()) {
×
71
        const htmlText = buildHTMLText(wrapper, attach, elements);
×
72
        // TODO
73
        // maybe fail to write when copy some cell in table
74
        return await setNavigatorClipboard(htmlText, elements, text);
×
75
    }
76

77
    if (dataTransfer) {
×
78
        const htmlText = buildHTMLText(wrapper, attach, elements);
×
79
        setDataTransferClipboard(dataTransfer, htmlText);
×
80
        setDataTransferClipboardText(dataTransfer, text);
×
81
        return;
×
82
    }
83

84
    // Compatible with situations where navigator.clipboard.write is not supported and dataTransfer is empty
85
    // Such as contextmenu copy in Firefox.
86
    if (isClipboardWriteTextSupported()) {
×
87
        const htmlText = buildHTMLText(wrapper, attach, elements);
×
88
        return await navigator.clipboard.writeText(htmlText);
×
89
    }
90
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc