Add a fallback when the browser does not support copying the share link to clipboard (not a secure origin)

See: https://stackoverflow.com/a/51823007
This commit is contained in:
Deluan 2023-01-30 12:09:01 -05:00
parent 762a1ba998
commit b6fcfa9fc8
4 changed files with 40 additions and 33 deletions

View File

@ -365,6 +365,7 @@
"shareOriginalFormat": "Compartilhar no formato original",
"shareDialogTitle": "Compartilhar %{resource} '%{name}'",
"shareBatchDialogTitle": "Compartilhar 1 %{resource} |||| Compartilhar %{smart_count} %{resource}",
"shareCopyToClipboard": "Copie para o clipboard: Ctrl+C, Enter",
"shareSuccess": "Link copiado para o clipboard : %{url}",
"shareFailure": "Erro ao copiar o link %{url} para o clipboard",
"downloadDialogTitle": "Baixar %{resource} '%{name}' (%{size})"
@ -451,4 +452,4 @@
"current_song": "Vai para música atual"
}
}
}
}

View File

@ -47,21 +47,23 @@ export const ShareDialog = () => {
{
onSuccess: (res) => {
const url = shareUrl(res?.data?.id)
navigator.clipboard
.writeText(url)
.then(() => {
notify('message.shareSuccess', 'info', { url }, false, 0)
})
.catch((err) => {
notify(
translate('message.shareFailure', { url }) + ': ' + err.message,
{
type: 'warning',
multiLine: true,
duration: 0,
}
)
})
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard
.writeText(url)
.then(() => {
notify('message.shareSuccess', 'info', { url }, false, 0)
})
.catch((err) => {
notify(
translate('message.shareFailure', { url }) + ': ' + err.message,
{
type: 'warning',
multiLine: true,
duration: 0,
}
)
})
} else prompt(translate('message.shareCopyToClipboard'), url)
},
onFailure: (error) =>
notify(translate('ra.page.error') + ': ' + error.message, {

View File

@ -369,6 +369,7 @@
"shareOriginalFormat": "Share in original format",
"shareDialogTitle": "Share %{resource} '%{name}'",
"shareBatchDialogTitle": "Share 1 %{resource} |||| Share %{smart_count} %{resource}",
"shareCopyToClipboard": "Copy to clipboard: Ctrl+C, Enter",
"shareSuccess": "URL copied to clipboard: %{url}",
"shareFailure": "Error copying URL %{url} to clipboard",
"downloadDialogTitle": "Download %{resource} '%{name}' (%{size})",

View File

@ -29,25 +29,28 @@ const ShareList = (props) => {
const handleShare = (r) => (e) => {
const url = shareUrl(r?.id)
navigator.clipboard
.writeText(url)
.then(() => {
notify(translate('message.shareSuccess', { url }), {
type: 'info',
multiLine: true,
duration: 0,
})
})
.catch((err) => {
notify(
translate('message.shareFailure', { url }) + ': ' + err.message,
{
type: 'warning',
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard
.writeText(url)
.then(() => {
notify(translate('message.shareSuccess', { url }), {
type: 'info',
multiLine: true,
duration: 0,
}
)
})
})
})
.catch((err) => {
notify(
translate('message.shareFailure', { url }) + ': ' + err.message,
{
type: 'warning',
multiLine: true,
duration: 0,
}
)
})
} else prompt(translate('message.shareCopyToClipboard'), url)
e.preventDefault()
e.stopPropagation()
}