CopyTextToClipboard fixed for mobile browsers ()

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
packages
presentation/src
theme/styles
plugins/view-resources/src

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
*/
@ -562,7 +579,9 @@ export async function copyTextToClipboard (text: string | Promise<string>): Prom
await navigator.clipboard.write([clipboardItem])
} catch {
// 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; }
.hulyClipboardArea {
width: 0;
height: 0;
opacity: 0;
}
.hidden-text {
position: absolute;
visibility: hidden;

View File

@ -9,7 +9,14 @@ import {
getCurrentAccount
} from '@hcengineering/core'
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 {
type AnyComponent,
type AnySvelteComponent,
@ -73,7 +80,9 @@ async function CopyTextToClipboard (
const text = Array.isArray(doc)
? (await Promise.all(doc.map(async (d) => await getText(d, props.props)))).join(',')
: 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)
}
}