From 4d227cbade4d1cf61a3c82ab500c26fb1b5a6c92 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 22 May 2022 08:05:39 -0500 Subject: [PATCH] Add copy to clipboard fallback --- client/plugins/init.client.js | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/client/plugins/init.client.js b/client/plugins/init.client.js index 90dd7814..0fab85aa 100644 --- a/client/plugins/init.client.js +++ b/client/plugins/init.client.js @@ -163,17 +163,26 @@ Vue.prototype.$sanitizeSlug = (str) => { Vue.prototype.$copyToClipboard = (str, ctx) => { return new Promise((resolve) => { if (!navigator.clipboard) { - console.warn('Clipboard not supported') - return resolve(false) + navigator.clipboard.writeText(str).then(() => { + if (ctx) ctx.$toast.success('Copied to clipboard') + resolve(true) + }, (err) => { + console.error('Clipboard copy failed', str, err) + resolve(false) + }) + } else { + const el = document.createElement('textarea') + el.value = str + el.setAttribute('readonly', '') + el.style.position = 'absolute' + el.style.left = '-9999px' + document.body.appendChild(el) + el.select() + document.execCommand('copy') + document.body.removeChild(el) + + if (ctx) ctx.$toast.success('Copied to clipboard') } - navigator.clipboard.writeText(str).then(() => { - console.log('Clipboard copy success', str) - ctx.$toast.success('Copied to clipboard') - resolve(true) - }, (err) => { - console.error('Clipboard copy failed', str, err) - resolve(false) - }) }) }