-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
136 lines (108 loc) · 4.04 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
133
134
135
136
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _utils = require('./lib/utils');
var _observer = require('./lib/observer');
var _observer2 = _interopRequireDefault(_observer);
var _fakeInput = require('./lib/fakeInput');
var _fakeInput2 = _interopRequireDefault(_fakeInput);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var VERSION = '<@VERSION@>';
var _clipboardList = [];
var _triggers = [];
(0, _utils.inheritPrototype)(Clipboard, _observer2.default);
function exec(e) {
if ((0, _utils.contain)(_triggers, e.target)) {
(function () {
var clipboard = _clipboardList[_triggers.indexOf(e.target)];
var prop = clipboard.trigger.innerHTML ? 'innerHTML' : 'value';
var originalText = clipboard.trigger[prop];
if (clipboard.type === 1) {
clipboard.target.select();
try {
document.execCommand(clipboard.action);
clipboard.target.blur();
clipboard.notify('success', clipboard);
clipboard.trigger[prop] = 'OK!️';
clipboard.trigger.disabled = true;
setTimeout(function () {
clipboard.trigger[prop] = originalText;
clipboard.trigger.disabled = false;
}, 1500);
} catch (e) {
clipboard.notify('error', clipboard);
}
} else if (clipboard.type === 2) {
var text = clipboard.target.innerHTML;
_fakeInput2.default.value = text;
_fakeInput2.default.select();
try {
document.execCommand(clipboard.action);
_fakeInput2.default.blur();
clipboard.notify('success', clipboard);
clipboard.trigger[prop] = 'OK!️';
clipboard.trigger.disabled = true;
setTimeout(function () {
clipboard.trigger[prop] = originalText;
clipboard.trigger.disabled = false;
}, 1500);
} catch (e) {
_fakeInput2.default.blur();
alert('error');
clipboard.notify('error', clipboard);
}
}
})();
}
}
function Clipboard(ele) {
// 实现继承
_observer2.default.call(this);
var trigger = (0, _utils.isString)(ele) ? document.querySelector(ele) : ele;
// 按钮已注册过时
if ((0, _utils.getData)(trigger, 'registered')) {
return _clipboardList[_triggers.indexOf(trigger)];
}
var target = document.querySelector((0, _utils.getData)(trigger, 'clipboard-target'));
var action = void 0,
type = void 0;
if (/input|textarea/g.test(target.nodeName.toLowerCase())) {
action = (0, _utils.getData)(trigger, 'clipboard-action') === 'cut' ? 'cut' : 'copy';
type = 1;
} else {
action = 'copy';
type = 2;
}
this.trigger = trigger;
this.target = target;
this.action = action;
this.type = type;
_clipboardList.push(this);
_triggers.push(trigger);
(0, _utils.setData)(trigger, 'registered', 'true');
return this;
}
Clipboard.ver = Clipboard.version = VERSION;
Clipboard.init = function (className) {
var btns = (0, _utils.toArray)(document.querySelectorAll('.' + className));
for (var i = 0, len = btns.length; i < len; i++) {
/* eslint-disable no-new */
new Clipboard(btns[i]);
/* eslint-enable no-new */
}
return _clipboardList;
};
Clipboard.prototype.destroy = function () {
var that = this;
var index = _clipboardList.indexOf(that);
if (index > -1) {
_clipboardList.splice(index, 1);
_triggers.splice(index, 1);
return true;
}
return false;
};
window.addEventListener('click', exec, false);
exports.default = Clipboard;
module.exports = exports['default'];