|
@@ -5,19 +5,37 @@
|
|
|
|
|
|
let onCopy = () => {};
|
|
|
|
|
|
- function Clipboard(Alpine) {
|
|
|
- Alpine.magic('clipboard', () => {
|
|
|
- return function (target) {
|
|
|
- if (typeof target === 'function') {
|
|
|
- target = target();
|
|
|
- }
|
|
|
+ const copy = target => {
|
|
|
+ if (typeof target === 'function') {
|
|
|
+ target = target();
|
|
|
+ }
|
|
|
|
|
|
- if (typeof target === 'object') {
|
|
|
- target = JSON.stringify(target);
|
|
|
- }
|
|
|
+ if (typeof target === 'object') {
|
|
|
+ target = JSON.stringify(target);
|
|
|
+ }
|
|
|
|
|
|
- return window.navigator.clipboard.writeText(target).then(onCopy);
|
|
|
- };
|
|
|
+ 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);
|
|
|
+ });
|
|
|
});
|
|
|
}
|
|
|
|