CopyTextToClipboard fixed for mobile browsers (#7672)

This commit is contained in:
Alexander Platov 2025-01-16 18:21:13 +03:00 committed by GitHub
parent 8e498027f6
commit 475e7ee62d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 36 additions and 3 deletions

View File

@ -549,6 +549,23 @@ export async function getBlobURL (blob: Blob): Promise<string> {
}) })
} }
/**
* @public
*/
export function copyTextToClipboardOldBrowser (text: string): void {
const textarea = document.createElement('textarea')
textarea.value = text
textarea.classList.add('hulyClipboardArea')
document.body.appendChild(textarea)
textarea.select()
try {
document.execCommand('copy')
} catch (err) {
console.error(err)
}
document.body.removeChild(textarea)
}
/** /**
* @public * @public
*/ */
@ -562,7 +579,9 @@ export async function copyTextToClipboard (text: string | Promise<string>): Prom
await navigator.clipboard.write([clipboardItem]) await navigator.clipboard.write([clipboardItem])
} catch { } catch {
// Fallback to default clipboard API implementation // Fallback to default clipboard API implementation
await navigator.clipboard.writeText(text instanceof Promise ? await text : text) if (navigator.clipboard != null && typeof navigator.clipboard.writeText === 'function') {
await navigator.clipboard.writeText(text instanceof Promise ? await text : text)
} else copyTextToClipboardOldBrowser(text instanceof Promise ? await text : text)
} }
} }

View File

@ -917,6 +917,11 @@ a.no-line {
.text-line-through { text-decoration: line-through; } .text-line-through { text-decoration: line-through; }
.hulyClipboardArea {
width: 0;
height: 0;
opacity: 0;
}
.hidden-text { .hidden-text {
position: absolute; position: absolute;
visibility: hidden; visibility: hidden;

View File

@ -9,7 +9,14 @@ import {
getCurrentAccount getCurrentAccount
} from '@hcengineering/core' } from '@hcengineering/core'
import { type Asset, type IntlString, type Resource, getResource } from '@hcengineering/platform' import { type Asset, type IntlString, type Resource, getResource } from '@hcengineering/platform'
import { MessageBox, getClient, updateAttribute, type ContextStore, contextStore } from '@hcengineering/presentation' import {
MessageBox,
getClient,
updateAttribute,
type ContextStore,
contextStore,
copyTextToClipboardOldBrowser
} from '@hcengineering/presentation'
import { import {
type AnyComponent, type AnyComponent,
type AnySvelteComponent, type AnySvelteComponent,
@ -73,7 +80,9 @@ async function CopyTextToClipboard (
const text = Array.isArray(doc) const text = Array.isArray(doc)
? (await Promise.all(doc.map(async (d) => await getText(d, props.props)))).join(',') ? (await Promise.all(doc.map(async (d) => await getText(d, props.props)))).join(',')
: await getText(doc, props.props) : await getText(doc, props.props)
await navigator.clipboard.writeText(text) if (navigator.clipboard != null && typeof navigator.clipboard.writeText === 'function') {
await navigator.clipboard.writeText(text)
} else copyTextToClipboardOldBrowser(text)
} }
} }