🥅 Catch error, if clipboard not enabled (#681)

This commit is contained in:
Alicia Sykes 2022-05-31 18:48:24 +01:00 committed by Alicia Sykes
parent 972e3f7571
commit ac97be20be
3 changed files with 28 additions and 14 deletions

View File

@ -38,7 +38,7 @@ import { modalNames } from '@/utils/defaults';
import AccessError from '@/components/Configuration/AccessError'; import AccessError from '@/components/Configuration/AccessError';
import DownloadConfigIcon from '@/assets/interface-icons/config-download-file.svg'; import DownloadConfigIcon from '@/assets/interface-icons/config-download-file.svg';
import CopyConfigIcon from '@/assets/interface-icons/interactive-editor-copy-clipboard.svg'; import CopyConfigIcon from '@/assets/interface-icons/interactive-editor-copy-clipboard.svg';
import { InfoHandler, InfoKeys } from '@/utils/ErrorHandler'; import { ErrorHandler, InfoHandler, InfoKeys } from '@/utils/ErrorHandler';
export default { export default {
name: 'ExportConfigMenu', name: 'ExportConfigMenu',
@ -80,8 +80,13 @@ export default {
}, },
copyConfigToClipboard() { copyConfigToClipboard() {
const config = this.convertJsonToYaml(); const config = this.convertJsonToYaml();
navigator.clipboard.writeText(config); if (navigator.clipboard) {
this.$toasted.show(this.$t('config.data-copied-msg')); navigator.clipboard.writeText(config);
this.$toasted.show(this.$t('config.data-copied-msg'));
} else {
ErrorHandler('Clipboard access requires HTTPS. See: https://bit.ly/3N5WuAA');
this.$toasted.show('Unable to copy, see log', { className: 'toast-error' });
}
InfoHandler('Config copied to clipboard', InfoKeys.EDITOR); InfoHandler('Config copied to clipboard', InfoKeys.EDITOR);
}, },
modalClosed() { modalClosed() {

View File

@ -2,6 +2,7 @@
import axios from 'axios'; import axios from 'axios';
import router from '@/router'; import router from '@/router';
import longPress from '@/directives/LongPress'; import longPress from '@/directives/LongPress';
import ErrorHandler from '@/utils/ErrorHandler';
import { import {
openingMethod as defaultOpeningMethod, openingMethod as defaultOpeningMethod,
serviceEndpoints, serviceEndpoints,
@ -149,8 +150,7 @@ export default {
router.push({ name: 'workspace', query: { url } }); router.push({ name: 'workspace', query: { url } });
} else if (this.accumulatedTarget === 'clipboard') { } else if (this.accumulatedTarget === 'clipboard') {
e.preventDefault(); e.preventDefault();
navigator.clipboard.writeText(url); this.copyToClipboard(url);
this.$toasted.show(this.$t('context-menus.item.copied-toast'));
} }
// Emit event to clear search field, etc // Emit event to clear search field, etc
this.$emit('itemClicked'); this.$emit('itemClicked');
@ -178,8 +178,7 @@ export default {
router.push({ name: 'workspace', query: { url } }); router.push({ name: 'workspace', query: { url } });
break; break;
case 'clipboard': case 'clipboard':
navigator.clipboard.writeText(url); this.copyToClipboard(url);
this.$toasted.show(this.$t('context-menus.item.copied-toast'));
break; break;
default: window.open(url, '_blank'); default: window.open(url, '_blank');
} }
@ -199,6 +198,19 @@ export default {
closeContextMenu() { closeContextMenu() {
this.contextMenuOpen = false; this.contextMenuOpen = false;
}, },
/* Copies a string to the users clipboard / shows error if not possible */
copyToClipboard(content) {
if (navigator.clipboard) {
navigator.clipboard.writeText(content);
this.$toasted.show(
this.$t('context-menus.item.copied-toast'),
{ className: 'toast-success' },
);
} else {
ErrorHandler('Clipboard access requires HTTPS. See: https://bit.ly/3N5WuAA');
this.$toasted.show('Unable to copy, see log', { className: 'toast-error' });
}
},
/* Used for smart-sort when sorting items by most used apps */ /* Used for smart-sort when sorting items by most used apps */
incrementMostUsedCount(itemId) { incrementMostUsedCount(itemId) {
const mostUsed = JSON.parse(localStorage.getItem(localStorageKeys.MOST_USED) || '{}'); const mostUsed = JSON.parse(localStorage.getItem(localStorageKeys.MOST_USED) || '{}');

View File

@ -22,13 +22,10 @@ const appendToErrorLog = (msg) => {
* If error reporting is enabled, will also log the message to Sentry * If error reporting is enabled, will also log the message to Sentry
* If you wish to use your own error logging service, put code for it here * If you wish to use your own error logging service, put code for it here
*/ */
const ErrorHandler = function handler(msg, errorStack) { export const ErrorHandler = function handler(msg, errorStack) {
// Print to console warningMsg(msg, errorStack); // Print to console
warningMsg(msg, errorStack); appendToErrorLog(msg); // Save to local storage
// Save to local storage Sentry.captureMessage(`[USER-WARN] ${msg}`); // Report to bug tracker (if enabled)
appendToErrorLog(msg);
// Report to bug tracker (if enabled)
Sentry.captureMessage(`[USER-WARN] ${msg}`);
}; };
/* Similar to error handler, but for recording general info */ /* Similar to error handler, but for recording general info */