-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
78 lines (68 loc) · 2.29 KB
/
index.js
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
/**
* Copy a string to clipboard
* @param {String} string The string to be copied to clipboard
* @return {Boolean} returns a boolean correspondent to the success of the copy operation.
* @author https://github.com/victornpb/
*/
export function copyToClipboard(string) {
let textarea;
let result;
try {
textarea = document.createElement('textarea');
// prevent scroll from jumping to the bottom when focus is set.
textarea.style.cssText = 'position:fixed; top:0px; z-index: 999;';
textarea.readOnly = true;
textarea.value = string;
document.body.appendChild(textarea);
// Try selecting text using setSelectionRange (Works on iOS Mobile Safari)
textarea.setSelectionRange(0, string.length);
const selection = window.getSelection().toString();
if (selection !== string) {
// test whats selected (setSelectionRange doesn't work on chrome for some reason)
// Select using old school method (this also works on iOS, but it makes zoom jump to selection)
textarea.focus();
textarea.select();
}
result = document.execCommand('copy');
} catch (err) {
console.error(err);
result = null;
} finally {
// setTimeout(() => { //debugging
document.body.removeChild(textarea);
// }, 1000 * 10);
}
return result;
}
/**
* @function copyRichTextToClipboard
* @param {String} html The string to be copied to clipboard
* @return {Boolean} returns a boolean correspondent to the success of the copy operation.
* @author Victor B. www.victorborges.com
*/
export function copyRichTextToClipboard(html) {
let el;
let result;
try {
el = document.createElement('div');
// prevent scroll from jumping to the bottom when focus is set.
el.style.cssText = 'position:fixed; top:0px;';
el.innerHTML = html;
document.body.appendChild(el);
// html selection (works on chrome and iOS)
const range = document.createRange();
range.selectNodeContents(el);
const sel = window.getSelection();
sel.removeAllRanges();
sel.addRange(range);
result = document.execCommand('copy');
} catch (err) {
console.error(err);
result = null;
} finally {
// setTimeout(() => { //debugging
document.body.removeChild(el);
// }, 1000 * 10);
}
return result;
}