Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do existence check on navigator.clipboard #3698

Merged
merged 1 commit into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ function makeWrapper() {
describe('clipboardChip', () => {
let wrapper;
beforeEach(() => {
navigator.clipboard = {
writeText: jest.fn(),
};
wrapper = makeWrapper();
});
afterEach(() => {
delete navigator.clipboard;
});
it('should fire a copy operation on button click', () => {
const copyToClipboard = jest.fn();
wrapper.setMethods({ copyToClipboard });
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