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