forked from sudodoki/copy-to-clipboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·132 lines (116 loc) · 3.77 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
'use strict';
var deselectCurrent = require('toggle-selection');
var defaultMessage = 'Copy to clipboard: #{key}, Enter';
function format(message) {
var copyKey = (/mac os x/i.test(navigator.userAgent) ? '⌘' : 'Ctrl') + '+C';
return message.replace(/#{\s*key\s*}/g, copyKey);
}
function stripHtml(html)
{
var tmp = document.createElement("DIV");
tmp.innerHTML = html;
return tmp.textContent || tmp.innerText || "";
}
function createMarkElForText(text) {
var mark;
mark = document.createElement('span');
mark.textContent = text;
// reset user styles for span element
mark.style.all = 'unset';
// prevents scrolling to the end of the page
mark.style.position = 'fixed';
mark.style.top = 0;
mark.style.clip = 'rect(0, 0, 0, 0)';
// used to preserve spaces and line breaks
mark.style.whiteSpace = 'pre';
// do not inherit user-select (it may be `none`)
mark.style.webkitUserSelect = 'text';
mark.style.MozUserSelect = 'text';
mark.style.msUserSelect = 'text';
mark.style.userSelect = 'text';
return mark;
}
function createMarkElForHtml(html) {
var mark;
mark = document.createElement('div');
mark.style.fontSize = '12pt'; // Prevent zooming on iOS
// Reset box model
mark.style.border = '0';
mark.style.padding = '0';
mark.style.margin = '0';
// Move element out of screen
mark.style.position = 'fixed';
mark.style['right'] = '-9999px';
mark.style.top = (window.pageYOffset || document.documentElement.scrollTop) + 'px';
// more hiding
mark.setAttribute('readonly', '');
mark.style.opacity = 0;
mark.style.pointerEvents = 'none';
mark.style.zIndex = -1;
mark.setAttribute('tabindex', '0'); // so it can be focused
mark.innerHTML = html;
return mark;
}
function copy(textOrHtml, options) {
var debug, message, reselectPrevious, range, selection, mark, success = false;
if (!options) { options = {}; }
debug = options.debug || false;
options.onlyHtml = options.onlyHtml && options.asHtml;
options.canUsePrompt = options.canUsePrompt != undefined ? options.canUsePrompt : true;
try {
reselectPrevious = deselectCurrent();
range = document.createRange();
selection = document.getSelection();
var html, text;
if (options.asHtml) {
html = textOrHtml;
text = stripHtml(html);
} else {
text = textOrHtml;
}
mark = options.asHtml ? createMarkElForHtml(html) : createMarkElForText(text);
document.body.appendChild(mark);
//range.setStartBefore(mark.firstChild);
//range.setEndAfter(mark.lastChild);
range.selectNode(mark);
selection.removeAllRanges();
selection.addRange(range);
var successful = document.execCommand('copy');
if (!successful) {
throw new Error('copy command was unsuccessful');
}
success = true;
} catch (err) {
debug && console.error('unable to copy using execCommand: ', err);
if (!options.onlyHtml) {
debug && console.warn('trying IE specific stuff');
try {
//html is not supported in IE
//https://msdn.microsoft.com/en-us/library/ms536744(v=VS.85).aspx
window.clipboardData.setData('text', text);
success = true;
} catch (err) {
debug && console.error('unable to copy using clipboardData: ', err);
if (options.canUsePrompt) {
debug && console.error('falling back to prompt');
message = format('message' in options ? options.message : defaultMessage);
window.prompt(message, text);
}
}
}
} finally {
if (selection) {
if (typeof selection.removeRange == 'function') {
selection.removeRange(range);
} else {
selection.removeAllRanges();
}
}
if (mark) {
document.body.removeChild(mark);
}
reselectPrevious();
}
return success;
}
module.exports = copy;