In my vue project, I have a copy-button which should copy an attribute value from data on click:
<span class="btn-action" :class="!data.value.copy ? 'btn-disabled' : 'btn-active'" @click="copyCode(data.item)"><font-awesome-icon :icon="['far', 'copy']" /></span>
and my copyCode function:
copyCode: function(item) {
// tests if item exists...
let copyText = item.code;
if (navigator && navigator.clipboard) {
console.log('navigator:', navigator);
// this works on chrome/firefox
navigator.clipboard.writeText(copyText);
} else {
console.log('no navigator');
this.writeTextIOS(copyText);
}
}
writeTextIOS: function(str) {
return new Promise(function(resolve, reject) {
let success = false;
function listener(e) {
e.clipboardData.setData("text/plain", str);
e.preventDefault();
success = true;
}
document.addEventListener("copy", listener);
document.execCommand("copy");
document.removeEventListener("copy", listener);
success ? resolve() : reject();
});
},
I found out on safari, there is navigator, but this does not have clipboard property (I could probably check navigator.userAgent
as well, but that is not the issure here, since it definately go into else-statement when I am using safari).
I got the writeTextIOS function from here: https://gist.github.com/lgarron/d1dee380f4ed9d825ca7 But I only get an Unhandled Promise Rejectio: undefined
exception.
So what should I do here. I don't want to use any third-party package like clipboard.js (yet). Is there any "native" solution for my problem?