-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
91 lines (78 loc) · 2.08 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
/**
* How to use it?
*
* import nanoClipboard from 'nano-clipboard';
* var success = nanoClipboard('Hello, world');
*
* @param text
* @param usePrompt, default false.
* @returns {boolean}
*/
!function (root, factory) {
if (typeof module === 'object' && module.exports) {
module.exports = factory(root); // nodejs support
module.exports['default'] = module.exports; // es6 support
}
else
root.nanoClipboard = factory(root);
}(typeof window !== 'undefined' ? window : this, function () {
/**
* use command to copy. https://github.com/sindresorhus/copy-text-to-clipboard
* @param input
* @returns {boolean}
*/
function copyWithCommand(input) {
var el = document.createElement('textarea');
el.value = input;
// Prevent keyboard from showing on mobile
el.setAttribute('readonly', '');
el.style.contain = 'strict';
el.style.position = 'absolute';
el.style.left = '-9999px';
el.style.fontSize = '12pt'; // Prevent zooming on iOS
const selection = document.getSelection();
var originalRange = false;
if (selection.rangeCount > 0) {
originalRange = selection.getRangeAt(0);
}
document.body.appendChild(el);
el.select();
// Explicit selection workaround for iOS
el.selectionStart = 0;
el.selectionEnd = input.length;
var success = false;
try {
success = document.execCommand('copy');
} catch (err) {
success = false;
}
document.body.removeChild(el);
if (originalRange) {
selection.removeAllRanges();
selection.addRange(originalRange);
}
return success;
}
/**
* backup copy way
* @param text
* @returns {boolean}
*/
function backupCopy(text) {
if (window.prompt) {
window.prompt('Copy to clipboard by ctrl / ⌘ + c', text);
return true;
}
return false;
}
/**
* main entry
*/
return function(text, usePrompt) {
var success = copyWithCommand(text);
// if not success and usePrompt, then use backup way.
return (usePrompt && !success) ?
backupCopy(text) :
success;
}
});