This commit is contained in:
Louis Lam 2023-03-24 19:16:12 +08:00
parent 70572af1af
commit a2014278b8

View File

@ -13,6 +13,9 @@
:disabled="disabled" :disabled="disabled"
> >
<!-- A hidden textarea for copying text on non-https -->
<textarea ref="hiddenTextarea" style="position: fixed; left: -999999px; top: -999999px;"></textarea>
<a class="btn btn-outline-primary" @click="copyToClipboard(model)"> <a class="btn btn-outline-primary" @click="copyToClipboard(model)">
<font-awesome-icon :icon="icon" /> <font-awesome-icon :icon="icon" />
</a> </a>
@ -111,24 +114,19 @@ export default {
}, 3000); }, 3000);
// navigator clipboard api needs a secure context (https) // navigator clipboard api needs a secure context (https)
// For http, use the text area method (else part)
if (navigator.clipboard && window.isSecureContext) { if (navigator.clipboard && window.isSecureContext) {
// navigator clipboard api method' // navigator clipboard api method'
return navigator.clipboard.writeText(textToCopy); return navigator.clipboard.writeText(textToCopy);
} else { } else {
// text area method // text area method
let textArea = document.createElement("textarea"); let textArea = this.$refs.hiddenTextarea;
textArea.value = textToCopy; textArea.value = textToCopy;
// make the textarea out of viewport
textArea.style.position = "fixed";
textArea.style.left = "-999999px";
textArea.style.top = "-999999px";
document.body.appendChild(textArea);
textArea.focus(); textArea.focus();
textArea.select(); textArea.select();
return new Promise((res, rej) => { return new Promise((res, rej) => {
// here the magic happens // here the magic happens
document.execCommand("copy") ? res() : rej(); document.execCommand("copy") ? res() : rej();
textArea.remove();
}); });
} }
} }