Skip to content

Commit

Permalink
Re-adding cordova integration for copyToClipboard
Browse files Browse the repository at this point in the history
However, it doesn't seem to work anymore, so I have to investigate
that I guess.
  • Loading branch information
MarmadileManteater committed Oct 19, 2022
1 parent b21c5a7 commit d7bc7d7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 33 deletions.
14 changes: 9 additions & 5 deletions src/renderer/helpers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 0 additions & 28 deletions src/renderer/store/modules/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
Expand Down

0 comments on commit d7bc7d7

Please sign in to comment.