From d7bc7d76ddf92f80acaf9aa68029eaee7107a36b Mon Sep 17 00:00:00 2001 From: Emma Date: Wed, 19 Oct 2022 12:29:48 -0400 Subject: [PATCH] Re-adding cordova integration for `copyToClipboard` However, it doesn't seem to work anymore, so I have to investigate that I guess. --- src/renderer/helpers/utils.js | 14 +++++++++----- src/renderer/store/modules/utils.js | 28 ---------------------------- 2 files changed, 9 insertions(+), 33 deletions(-) diff --git a/src/renderer/helpers/utils.js b/src/renderer/helpers/utils.js index 6994ab54f257c..f61bcfac6ce43 100644 --- a/src/renderer/helpers/utils.js +++ b/src/renderer/helpers/utils.js @@ -168,16 +168,20 @@ export function showToast(message, time = null, action = null) { * @param {string} messageOnSuccess the message to be displayed as a toast when the copy succeeds (optional) * @param {string} messageOnError the message to be displayed as a toast when the copy fails (optional) */ -export async function copyToClipboard(content, { messageOnSuccess = null, messageOnError = null }) { - if (navigator.clipboard !== undefined && window.isSecureContext) { +export async function copyToClipboard ({ dispatch }, { content, messageOnSuccess, messageOnError }) { + let clipboardAPI = navigator.clipboard?.writeText.bind(navigator.clipboard) + if (window.cordova !== undefined) { + clipboardAPI = window.cordova.plugins.clipboard.copy + } + if (clipboardAPI !== undefined && window.isSecureContext) { try { - await navigator.clipboard.writeText(content) - if (messageOnSuccess !== null) { + await clipboardAPI(content) + if (messageOnSuccess !== undefined) { showToast(messageOnSuccess) } } catch (error) { console.error(`Failed to copy ${content} to clipboard`, error) - if (messageOnError !== null) { + if (messageOnError !== undefined) { showToast(`${messageOnError}: ${error}`, 5000) } else { showToast(`${i18n.t('Clipboard.Copy failed')}: ${error}`, 5000) diff --git a/src/renderer/store/modules/utils.js b/src/renderer/store/modules/utils.js index ad7e39b76d892..611caa05566de 100644 --- a/src/renderer/store/modules/utils.js +++ b/src/renderer/store/modules/utils.js @@ -150,34 +150,6 @@ const actions = { return filenameNew }, - /** - * This writes to the clipboard. If an error occurs during the copy, - * a toast with the error is shown. If the copy is successful and - * there is a success message, a toast with that message is shown. - * @param {string} content the content to be copied to the clipboard - * @param {string} messageOnSuccess the message to be displayed as a toast when the copy succeeds (optional) - * @param {string} messageOnError the message to be displayed as a toast when the copy fails (optional) - */ - async copyToClipboard ({ dispatch }, { content, messageOnSuccess, messageOnError }) { - if (navigator.clipboard !== undefined && window.isSecureContext) { - try { - await navigator.clipboard.writeText(content) - if (messageOnSuccess !== undefined) { - showToast(messageOnSuccess) - } - } catch (error) { - console.error(`Failed to copy ${content} to clipboard`, error) - if (messageOnError !== undefined) { - showToast(`${messageOnError}: ${error}`, 5000) - } else { - showToast(`${i18n.t('Clipboard.Copy failed')}: ${error}`, 5000) - } - } - } else { - showToast(i18n.t('Clipboard.Cannot access clipboard without a secure connection'), 5000) - } - }, - async downloadMedia({ rootState, dispatch }, { url, title, extension, fallingBackPath }) { const fileName = `${await dispatch('replaceFilenameForbiddenChars', title)}.${extension}` const errorMessage = i18n.t('Downloading failed', { videoTitle: title })