-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
ClipboardService.ts
90 lines (75 loc) · 2.33 KB
/
ClipboardService.ts
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
const isSupported =
typeof document !== 'undefined' &&
document.queryCommandSupported &&
document.queryCommandSupported('copy');
export class ClipboardService {
static isSupported(): boolean {
return isSupported;
}
static selectElement(element: any): void {
let range;
let selection;
if ((document.body as any).createTextRange) {
range = (document.body as any).createTextRange();
range.moveToElementText(element);
range.select();
} else if (document.createRange && window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(element);
selection.removeAllRanges();
selection.addRange(range);
}
}
static deselect(): void {
if ((document as any).selection) {
(document as any).selection.empty();
} else if (window.getSelection) {
const selection = window.getSelection();
if (selection) {
selection.removeAllRanges();
}
}
}
static copySelected(): boolean {
let result;
try {
result = document.execCommand('copy');
} catch (err) {
result = false;
}
return result;
}
static copyElement(element: any): boolean {
ClipboardService.selectElement(element);
const res = ClipboardService.copySelected();
if (res) {
ClipboardService.deselect();
}
return res;
}
static copyCustom(text: string): boolean {
const textArea = document.createElement('textarea');
textArea.style.position = 'fixed';
textArea.style.top = '0';
textArea.style.left = '0';
// Ensure it has a small width and height. Setting to 1px / 1em
// doesn't work as this gives a negative w/h on some browsers.
textArea.style.width = '2em';
textArea.style.height = '2em';
// We don't need padding, reducing the size if it does flash render.
textArea.style.padding = '0';
// Clean up any borders.
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
// Avoid flash of white box if rendered for any reason.
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
const res = ClipboardService.copySelected();
document.body.removeChild(textArea);
return res;
}
}