From d49af06fbdd4799206bcb3f76e2535adbd4de3c9 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Sun, 26 Aug 2018 16:40:48 -0700 Subject: [PATCH] use async clipboard API (#499) --- routes/_actions/copyText.js | 17 +++++++++++++++++ .../components/AccountProfileOptionsDialog.html | 7 ++++--- .../dialog/components/StatusOptionsDialog.html | 15 +++++++-------- 3 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 routes/_actions/copyText.js diff --git a/routes/_actions/copyText.js b/routes/_actions/copyText.js new file mode 100644 index 0000000..f6e9bfd --- /dev/null +++ b/routes/_actions/copyText.js @@ -0,0 +1,17 @@ +import { importShowCopyDialog } from '../_components/dialog/asyncDialogs' +import { toast } from '../_utils/toast' + +export async function copyText (text) { + if (navigator.clipboard) { // not supported in all browsers + try { + await navigator.clipboard.writeText(text) + toast.say('Copied to clipboard') + return + } catch (e) { + console.error(e) + } + } + + let showCopyDialog = await importShowCopyDialog() + showCopyDialog(text) +} diff --git a/routes/_components/dialog/components/AccountProfileOptionsDialog.html b/routes/_components/dialog/components/AccountProfileOptionsDialog.html index 5f41597..ec24976 100644 --- a/routes/_components/dialog/components/AccountProfileOptionsDialog.html +++ b/routes/_components/dialog/components/AccountProfileOptionsDialog.html @@ -10,7 +10,7 @@ import ModalDialog from './ModalDialog.html' import { store } from '../../../_store/store' import GenericDialogList from './GenericDialogList.html' -import { importShowComposeDialog, importShowCopyDialog } from '../asyncDialogs' +import { importShowComposeDialog } from '../asyncDialogs' import { createDialogId } from '../helpers/createDialogId' import { show } from '../helpers/showDialog' import { close } from '../helpers/closeDialog' @@ -20,6 +20,7 @@ import { setAccountMuted } from '../../../_actions/mute' import { setAccountFollowed } from '../../../_actions/follow' import { setShowReblogs } from '../../../_actions/setShowReblogs' import { setDomainBlocked } from '../../../_actions/setDomainBlocked' +import { copyText } from '../../../_actions/copyText' export default { oncreate, @@ -175,8 +176,8 @@ export default { async onCopyClicked () { let { account } = this.get() let { url } = account - let showCopyDialog = await importShowCopyDialog() - showCopyDialog(url) + this.close() + await copyText(url) } }, components: { diff --git a/routes/_components/dialog/components/StatusOptionsDialog.html b/routes/_components/dialog/components/StatusOptionsDialog.html index 91e6257..e927dbf 100644 --- a/routes/_components/dialog/components/StatusOptionsDialog.html +++ b/routes/_components/dialog/components/StatusOptionsDialog.html @@ -18,8 +18,8 @@ import { oncreate } from '../helpers/onCreateDialog' import { setAccountBlocked } from '../../../_actions/block' import { setAccountMuted } from '../../../_actions/mute' import { setStatusPinnedOrUnpinned } from '../../../_actions/pin' -import { importShowCopyDialog } from '../asyncDialogs' import { setConversationMuted } from '../../../_actions/muteConversation' +import { copyText } from '../../../_actions/copyText' export default { oncreate, @@ -162,17 +162,16 @@ export default { this.close() await setAccountMuted(accountId, !muting, true) }, - async onCopyClicked () { - let { status } = this.get() - let { url } = status - let showCopyDialog = await importShowCopyDialog() - this.close() - showCopyDialog(url) - }, async onMuteConversationClicked () { let { statusId, mutingConversation } = this.get() this.close() await setConversationMuted(statusId, !mutingConversation, true) + }, + async onCopyClicked () { + let { status } = this.get() + let { url } = status + await copyText(url) + this.close() } } }