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
+1
View File
@@ -365,6 +365,7 @@
"shareOriginalFormat": "Compartilhar no formato original", "shareOriginalFormat": "Compartilhar no formato original",
"shareDialogTitle": "Compartilhar %{resource} '%{name}'", "shareDialogTitle": "Compartilhar %{resource} '%{name}'",
"shareBatchDialogTitle": "Compartilhar 1 %{resource} |||| Compartilhar %{smart_count} %{resource}", "shareBatchDialogTitle": "Compartilhar 1 %{resource} |||| Compartilhar %{smart_count} %{resource}",
"shareCopyToClipboard": "Copie para o clipboard: Ctrl+C, Enter",
"shareSuccess": "Link copiado para o clipboard : %{url}", "shareSuccess": "Link copiado para o clipboard : %{url}",
"shareFailure": "Erro ao copiar o link %{url} para o clipboard", "shareFailure": "Erro ao copiar o link %{url} para o clipboard",
"downloadDialogTitle": "Baixar %{resource} '%{name}' (%{size})" "downloadDialogTitle": "Baixar %{resource} '%{name}' (%{size})"
+2
View File
@@ -47,6 +47,7 @@ export const ShareDialog = () => {
{ {
onSuccess: (res) => { onSuccess: (res) => {
const url = shareUrl(res?.data?.id) const url = shareUrl(res?.data?.id)
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard navigator.clipboard
.writeText(url) .writeText(url)
.then(() => { .then(() => {
@@ -62,6 +63,7 @@ export const ShareDialog = () => {
} }
) )
}) })
} else prompt(translate('message.shareCopyToClipboard'), url)
}, },
onFailure: (error) => onFailure: (error) =>
notify(translate('ra.page.error') + ': ' + error.message, { notify(translate('ra.page.error') + ': ' + error.message, {
+1
View File
@@ -369,6 +369,7 @@
"shareOriginalFormat": "Share in original format", "shareOriginalFormat": "Share in original format",
"shareDialogTitle": "Share %{resource} '%{name}'", "shareDialogTitle": "Share %{resource} '%{name}'",
"shareBatchDialogTitle": "Share 1 %{resource} |||| Share %{smart_count} %{resource}", "shareBatchDialogTitle": "Share 1 %{resource} |||| Share %{smart_count} %{resource}",
"shareCopyToClipboard": "Copy to clipboard: Ctrl+C, Enter",
"shareSuccess": "URL copied to clipboard: %{url}", "shareSuccess": "URL copied to clipboard: %{url}",
"shareFailure": "Error copying URL %{url} to clipboard", "shareFailure": "Error copying URL %{url} to clipboard",
"downloadDialogTitle": "Download %{resource} '%{name}' (%{size})", "downloadDialogTitle": "Download %{resource} '%{name}' (%{size})",
+3
View File
@@ -29,6 +29,7 @@ const ShareList = (props) => {
const handleShare = (r) => (e) => { const handleShare = (r) => (e) => {
const url = shareUrl(r?.id) const url = shareUrl(r?.id)
if (navigator.clipboard && window.isSecureContext) {
navigator.clipboard navigator.clipboard
.writeText(url) .writeText(url)
.then(() => { .then(() => {
@@ -48,6 +49,8 @@ const ShareList = (props) => {
} }
) )
}) })
} else prompt(translate('message.shareCopyToClipboard'), url)
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
} }