-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83b4629
commit e70c93d
Showing
6 changed files
with
142 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,96 @@ | ||
<template> | ||
<span @click="copyContent($event)" class="text-red-500"> | ||
<div | ||
@click="copyContent($event)" | ||
@mouseenter="showCopyIcon($event)" | ||
@mouseleave="removeCopyIcon($event)" | ||
> | ||
<slot /> | ||
</span> | ||
</div> | ||
</template> | ||
|
||
|
||
<script> | ||
const svgCopyIcon = ` | ||
<span data-copy-btn><svg xmlns="http://www.w3.org/2000/svg" height="18px" viewBox="0 0 24 24" width="18px" fill="currentColor"><path d="M0 0h24v24H0V0z" fill="none"/><path d="M19 2h-4.18C14.4.84 13.3 0 12 0S9.6.84 9.18 2H5c-1.1 0-2 .9-2 2v16c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V4c0-1.1-.9-2-2-2zm-7 0c.55 0 1 .45 1 1s-.45 1-1 1-1-.45-1-1 .45-1 1-1zm7 18H5V4h2v3h10V4h2v16z"/></svg><label>Copy</label></span>`; | ||
export default { | ||
methods: { | ||
copyContent(event) { | ||
this.selectContent(event); | ||
this.updateClipboard(event.target.innerText); | ||
// clone node and remove copy button | ||
const nodeContent = event.target.parentNode.cloneNode(true); | ||
nodeContent.querySelector("div[data-copy-element]").remove(); | ||
this.selectContent(event.target.parentNode); | ||
this.updateClipboard(nodeContent.textContent, () => { | ||
const copySvg = this.getCopyButton(event); | ||
if (!copySvg) return; | ||
copySvg.querySelector("label").innerText = "Copied !"; | ||
}); | ||
}, | ||
showCopyIcon(event) { | ||
event.target.setAttribute("data-copy-parent", 1); | ||
const copySvg = this.getCopyButton(event); | ||
if (copySvg) return; | ||
const copyElement = document.createElement("div"); | ||
copyElement.setAttribute("data-copy-element", 1); | ||
copyElement.innerHTML = svgCopyIcon; | ||
event.target.prepend(copyElement); | ||
}, | ||
removeCopyIcon(event) { | ||
const copySvg = this.getCopyButton(event); | ||
if (!copySvg) return; | ||
copySvg.remove(); | ||
}, | ||
getCopyButton(event) { | ||
return event.target.parentNode.querySelector("div[data-copy-element]"); | ||
}, | ||
selectContent(event) { | ||
selectContent(node) { | ||
var r = document.createRange(); | ||
r.selectNodeContents(event.target); | ||
r.selectNodeContents(node); | ||
var sel = window.getSelection(); | ||
sel.removeAllRanges(); | ||
sel.addRange(r); | ||
}, | ||
updateClipboard(content) { | ||
navigator.clipboard.writeText(content).then( | ||
function () { | ||
/* clipboard successfully set */ | ||
}, | ||
function () { | ||
/* clipboard write failed */ | ||
alert("Copy action is not granted"); | ||
} | ||
); | ||
updateClipboard(content, done = () => {}) { | ||
navigator.clipboard && | ||
navigator.clipboard.writeText(content).then( | ||
() => { | ||
done(); | ||
}, | ||
function () { | ||
/* clipboard write failed */ | ||
console.error("Copy action is not granted"); | ||
} | ||
); | ||
}, | ||
}, | ||
}; | ||
</script> | ||
</script> | ||
|
||
|
||
<style lang="scss" > | ||
*[data-copy-parent] { | ||
@apply relative; | ||
label { | ||
@apply text-xs; | ||
} | ||
} | ||
div[data-copy-element] { | ||
@apply bg-green-500 bg-opacity-10; | ||
@apply absolute flex items-center justify-center; | ||
@apply w-full h-full; | ||
@apply select-none; | ||
span[data-copy-btn] { | ||
@apply text-white; | ||
@apply flex items-center justify-center gap-x-1; | ||
@apply p-1 px-2 bg-primary bg-opacity-90 rounded; | ||
} | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters