Passed
Push — trunk ( 0037c7...daa0f1 )
by Christian
15:43 queued 12s
created

dom.utils.ts ➔ copyStringToClipboard   A

Complexity

Conditions 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
/**
2
 * @package admin
3
 *
4
 * @module core/service/utils/dom
5
 */
6
7
import { warn } from './debug.utils';
8
9
/**
10
 * Returns the scrollbar height of an HTML element.
11
 */
12
function getScrollbarHeight(element: HTMLElement): number {
13
    if (!(element instanceof HTMLElement)) {
14
        warn('DOM Utilities', 'The provided element needs to be an instance of "HTMLElement".', element);
15
        return 0;
16
    }
17
    return (element.offsetHeight - element.clientHeight);
18
}
19
20
/**
21
 * Returns the scrollbar width of an HTML element.
22
 */
23
function getScrollbarWidth(element: HTMLElement): number {
24
    if (!(element instanceof HTMLElement)) {
25
        warn('DOM Utilities', 'The provided element needs to be an instance of "HTMLElement".', element);
26
        return 0;
27
    }
28
    return (element.offsetWidth - element.clientWidth);
29
}
30
31
/**
32
 * uses the browser's copy function to copy a string
33
 * @deprecated tag:v6.6.0 - The document.execCommand() API is deprecated, use copyStringToClipBoard instead
34
 */
35
function copyToClipboard(stringToCopy: string): void {
36
    const tempTextArea = document.createElement('textarea');
37
    tempTextArea.value = stringToCopy;
38
    document.body.appendChild(tempTextArea);
39
    tempTextArea.select();
40
    document.execCommand('copy');
41
    document.body.removeChild(tempTextArea);
42
}
43
44
async function copyStringToClipboard(stringToCopy: string): Promise<void> {
45
    await navigator.clipboard.writeText(stringToCopy);
46
}
47
48
// eslint-disable-next-line sw-deprecation-rules/private-feature-declarations
49
export default {
50
    getScrollbarHeight,
51
    getScrollbarWidth,
52
    copyToClipboard,
53
    copyStringToClipboard,
54
};
55