Skip to content

Commit

Permalink
Do existence check on navigator.clipboard.
Browse files Browse the repository at this point in the history
  • Loading branch information
rtibbles committed Sep 29, 2022
1 parent 796b011 commit 2e2cb2d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,14 @@
{{ value }}
</div>
</VChip>
<VBtn icon small right data-test="copy" @click="copyToClipboard">
<VBtn
v-if="clipboardAvailable"
icon
small
right
data-test="copy"
@click="copyToClipboard"
>
<Icon small>
content_copy
</Icon>
Expand All @@ -30,11 +37,18 @@
type: String,
},
},
computed: {
clipboardAvailable() {
return Boolean(navigator.clipboard);
},
},
methods: {
copyToClipboard() {
navigator.clipboard.writeText(this.value).then(() => {
this.$store.dispatch('showSnackbarSimple', this.successMessage);
});
if (this.clipboardAvailable) {
navigator.clipboard.writeText(this.value).then(() => {
this.$store.dispatch('showSnackbarSimple', this.successMessage);
});
}
},
},
};
Expand Down
31 changes: 18 additions & 13 deletions contentcuration/contentcuration/frontend/shared/views/CopyToken.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
<VTextField
v-if="token"
v-model="displayToken"
:title="$tr('copyPrompt')"
appendIcon="content_copy"
:title="clipboardAvailable ? $tr('copyPrompt') : ''"
:appendIcon="clipboardAvailable ? 'content_copy' : null"
readonly
color="primary"
:hideDetails="true"
Expand Down Expand Up @@ -46,20 +46,25 @@
displayToken() {
return this.hyphenate ? this.token.slice(0, 5) + '-' + this.token.slice(5) : this.token;
},
clipboardAvailable() {
return Boolean(navigator.clipboard);
},
},
methods: {
copyToken() {
navigator.clipboard
.writeText(this.displayToken)
.then(() => {
let text = this.successText || this.$tr('copiedTokenId');
this.$store.dispatch('showSnackbar', { text });
this.$analytics.trackEvent('copy_token');
this.$emit('copied');
})
.catch(() => {
this.$store.dispatch('showSnackbar', { text: this.$tr('copyFailed') });
});
if (this.clipboardAvailable) {
navigator.clipboard
.writeText(this.displayToken)
.then(() => {
let text = this.successText || this.$tr('copiedTokenId');
this.$store.dispatch('showSnackbar', { text });
this.$analytics.trackEvent('copy_token');
this.$emit('copied');
})
.catch(() => {
this.$store.dispatch('showSnackbar', { text: this.$tr('copyFailed') });
});
}
},
},
$trs: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<pre ref="textBox" class="hidden-screen-only">{{ text }}</pre>
<div>
<KButton
v-if="clipboardAvailable"
ref="copyButton"
:style="{ marginTop: '8px', marginBottom: '8px' }"
:primary="false"
Expand Down Expand Up @@ -60,19 +61,24 @@
minHeight: `${this.minHeight}px`,
};
},
clipboardAvailable() {
return Boolean(navigator.clipboard);
},
},
methods: {
copyError() {
navigator.clipboard
.writeText(this.formattedText)
.then(() => {
this.$store.dispatch('showSnackbar', {
text: this.$tr('copiedToClipboardConfirmation'),
if (this.clipboardAvailable) {
navigator.clipboard
.writeText(this.formattedText)
.then(() => {
this.$store.dispatch('showSnackbar', {
text: this.$tr('copiedToClipboardConfirmation'),
});
})
.catch(() => {
this.$store.dispatch('showSnackbar', { text: this.$tr('copiedToClipboardFailure') });
});
})
.catch(() => {
this.$store.dispatch('showSnackbar', { text: this.$tr('copiedToClipboardFailure') });
});
}
},
},
$trs: {
Expand Down

0 comments on commit 2e2cb2d

Please sign in to comment.