| 1 |
'use strict'; |
| 2 |
|
| 3 |
export function registerMagics(Alpine) { |
| 4 |
Alpine.magic('copy', (currentEl) => { |
| 5 |
return function (el) { |
| 6 |
if (!el) { |
| 7 |
el = currentEl; |
| 8 |
} |
| 9 |
|
| 10 |
// Select the element to copy. |
| 11 |
let range = document.createRange(); |
| 12 |
range.selectNode(el); |
| 13 |
window.getSelection().removeAllRanges(); |
| 14 |
window.getSelection().addRange(range); |
| 15 |
|
| 16 |
// Remove the selection after some time. |
| 17 |
setTimeout(() => { |
| 18 |
window.getSelection().removeAllRanges(); |
| 19 |
}, 500); |
| 20 |
|
| 21 |
// Trim whitespace. |
| 22 |
let text = el.textContent.trim(); |
| 23 |
|
| 24 |
navigator.clipboard.writeText(text); |
| 25 |
}; |
| 26 |
}); |
| 27 |
|
| 28 |
Alpine.magic('isScrollX', (currentEl) => { |
| 29 |
return function (el) { |
| 30 |
if (!el) { |
| 31 |
el = currentEl; |
| 32 |
} |
| 33 |
return el.clientWidth < el.scrollWidth; |
| 34 |
}; |
| 35 |
}); |
| 36 |
} |