-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathalpine-clipboard.js
55 lines (44 loc) · 1.33 KB
/
alpine-clipboard.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
(function (factory) {
typeof define === 'function' && define.amd ? define(factory) :
factory();
}((function () { 'use strict';
let onCopy = () => {};
const copy = target => {
if (typeof target === 'function') {
target = target();
}
if (typeof target === 'object') {
target = JSON.stringify(target);
}
return window.navigator.clipboard.writeText(target).then(onCopy);
};
function Clipboard(Alpine) {
Alpine.magic('clipboard', () => {
return copy;
});
Alpine.directive('clipboard', (el, {
modifiers,
expression
}, {
evaluateLater,
cleanup
}) => {
const getCopyContent = modifiers.includes('raw') ? c => c(expression) : evaluateLater(expression);
const clickHandler = () => getCopyContent(copy);
el.addEventListener('click', clickHandler);
cleanup(() => {
el.removeEventListener('click', clickHandler);
});
});
}
Clipboard.configure = config => {
if (config.hasOwnProperty('onCopy') && typeof config.onCopy === 'function') {
onCopy = config.onCopy;
}
return Clipboard;
};
document.addEventListener('alpine:initializing', () => {
Clipboard(window.Alpine);
});
})));
//# sourceMappingURL=alpine-clipboard.js.map