fix(copyText): in iframe for chrome<85

This commit is contained in:
Hristo Terezov 2021-03-03 15:04:00 -06:00 committed by Дамян Минков
parent 62c06441b1
commit a2e2d31dfd
4 changed files with 45 additions and 24 deletions

View File

@ -49,9 +49,12 @@ function CopyButton({ className, displayedText, textToCopy, textOnHover, textOnC
*
* @returns {void}
*/
function onClick() {
async function onClick() {
setIsHovered(false);
if (copyText(textToCopy)) {
const isCopied = await copyText(textToCopy);
if (isCopied) {
setIsClicked(true);
setTimeout(() => {

View File

@ -7,7 +7,6 @@ import { translate } from '../../../i18n';
import { Icon, IconCopy, IconCheck } from '../../../icons';
import { connect } from '../../../redux';
import { copyText, getDecodedURI } from '../../../util';
import logger from '../../logger';
type Props = {
@ -75,8 +74,8 @@ class CopyMeetingUrl extends Component<Props, State> {
*
* @returns {void}
*/
_copyUrl() {
const success = copyText(this.props.url);
async _copyUrl() {
const success = await copyText(this.props.url);
if (success) {
this._showLinkCopied();
@ -152,15 +151,13 @@ class CopyMeetingUrl extends Component<Props, State> {
* @private
* @returns {void}
*/
_copyUrlAutomatically() {
navigator.clipboard.writeText(this.props.url)
.then(() => {
async _copyUrlAutomatically() {
const isCopied = await copyText(this.props.url);
if (isCopied) {
this._showLinkCopied();
window.setTimeout(this._hideLinkCopied, COPY_TIMEOUT);
})
.catch(e => {
logger.error(e);
});
}
}
/**

View File

@ -29,18 +29,36 @@ export function assignIfDefined(target: Object, source: Object) {
* Returns true if the action succeeds.
*
* @param {string} textToCopy - Text to be copied.
* @returns {boolean}
* @returns {Promise<boolean>}
*/
export async function copyText(textToCopy: string) {
try {
await navigator.clipboard.writeText(textToCopy);
return true;
} catch (clipboardAPIError) { // The Clipboard API is not supported.
let fakeTextArea = document.createElement('textarea');
// $FlowFixMe
fakeTextArea = document.body.appendChild(fakeTextArea);
fakeTextArea.value = textToCopy;
fakeTextArea.focus();
fakeTextArea.select();
let result;
try {
result = await navigator.clipboard.writeText(textToCopy);
} catch (err) {
result = document.execCommand('copy');
} catch (error) {
result = false;
}
// $FlowFixMe
document.body.removeChild(fakeTextArea);
return result;
}
}
/**

View File

@ -34,9 +34,12 @@ function LiveStreamSection({ liveStreamViewURL, t }: Props) {
*
* @returns {void}
*/
function onClick() {
async function onClick() {
setIsHovered(false);
if (copyText(liveStreamViewURL)) {
const isCopied = copyText(liveStreamViewURL);
if (isCopied) {
setIsClicked(true);
setTimeout(() => {