forked from openWB/openwb-ui-settings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenwbBaseCopyToClipboard.vue
81 lines (77 loc) · 1.97 KB
/
OpenwbBaseCopyToClipboard.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<template>
<span ref="slot-wrapper" :title="tooltip" class="copy-me" @click="click">
<slot />
<font-awesome-icon
fixed-width
:icon="isCopied ? ['fas', 'clipboard-check'] : ['fas', 'clipboard']"
/>
</span>
</template>
<script>
import { library } from "@fortawesome/fontawesome-svg-core";
import {
faClipboard as fasClipboard,
faClipboardCheck as fasClipboardCheck,
} from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
library.add(fasClipboard, fasClipboardCheck);
export default {
name: "OpenwbCopyToClipboard",
components: {
FontAwesomeIcon,
},
props: {
tooltip: { type: String, default: "Wert kopieren" },
},
data() {
return {
clipboardApiAvailable: navigator.clipboard != undefined,
isCopied: false,
};
},
methods: {
click() {
// event.target may be our icon, so we use a ref here
console.debug(this.$refs["slot-wrapper"].innerText);
if (this.clipboardApiAvailable) {
navigator.clipboard
.writeText(this.$refs["slot-wrapper"].innerText)
.then(
() => {
this.isCopied = true;
},
() => {
console.error("copy to clipboard failed");
},
);
} else {
console.debug(
"clipboard api not supported/enabled, fallback to select",
);
if (window.getSelection) {
console.debug("using 'window.getSelection'");
const selection = window.getSelection();
const range = document.createRange();
range.selectNodeContents(this.$refs["slot-wrapper"]);
selection.removeAllRanges();
selection.addRange(range);
return;
}
if (document.body.createTextRange) {
console.debug("using 'document.body.createTextRange'");
const range = document.body.createTextRange();
range.moveToElementText(this.$refs["slot-wrapper"]);
range.select();
} else {
console.warn("could not select text, unsupported browser");
}
}
},
},
};
</script>
<style scoped>
span.copy-me {
cursor: copy;
}
</style>