/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ /** * A pending copy-to-clipboard operation. * * The implementation of copying text to the clipboard modifies the DOM and * forces a re-layout. This re-layout can take too long if the string is large, * causing the execCommand('copy') to happen too long after the user clicked. * This results in the browser refusing to copy. This object lets the * re-layout happen in a separate tick from copying by providing a copy function * that can be called later. * * Destroy must be called when no longer in use, regardless of whether `copy` is * called. */ export class PendingCopy { constructor(text, _document) { this._document = _document; const textarea = (this._textarea = this._document.createElement('textarea')); const styles = textarea.style; // Hide the element for display and accessibility. Set a fixed position so the page layout // isn't affected. We use `fixed` with `top: 0`, because focus is moved into the textarea // for a split second and if it's off-screen, some browsers will attempt to scroll it into view. styles.position = 'fixed'; styles.top = styles.opacity = '0'; styles.left = '-999em'; textarea.setAttribute('aria-hidden', 'true'); textarea.value = text; // Making the textarea `readonly` prevents the screen from jumping on iOS Safari (see #25169). textarea.readOnly = true; // The element needs to be inserted into the fullscreen container, if the page // is in fullscreen mode, otherwise the browser won't execute the copy command. (this._document.fullscreenElement || this._document.body).appendChild(textarea); } /** Finishes copying the text. */ copy() { const textarea = this._textarea; let successful = false; try { // Older browsers could throw if copy is not supported. if (textarea) { const currentFocus = this._document.activeElement; textarea.select(); textarea.setSelectionRange(0, textarea.value.length); successful = this._document.execCommand('copy'); if (currentFocus) { currentFocus.focus(); } } } catch { // Discard error. // Initial setting of {@code successful} will represent failure here. } return successful; } /** Cleans up DOM changes used to perform the copy operation. */ destroy() { const textarea = this._textarea; if (textarea) { textarea.remove(); this._textarea = undefined; } } } //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGVuZGluZy1jb3B5LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vc3JjL2Nkay9jbGlwYm9hcmQvcGVuZGluZy1jb3B5LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ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