|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.io/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {DOCUMENT} from '@angular/common'; |
| 10 | +import {Inject, Injectable} from '@angular/core'; |
| 11 | + |
| 12 | +/** |
| 13 | + * A service for copying text to the clipboard. |
| 14 | + * |
| 15 | + * Example usage: |
| 16 | + * |
| 17 | + * clipboard.copy("copy this text"); |
| 18 | + */ |
| 19 | +@Injectable({providedIn: 'root'}) |
| 20 | +export class Clipboard { |
| 21 | + private _document: Document; |
| 22 | + |
| 23 | + constructor(@Inject(DOCUMENT) document: any) { |
| 24 | + this._document = document; |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * Copies the provided text into the user's clipboard. |
| 29 | + * |
| 30 | + * @param text The string to copy. |
| 31 | + * @returns Whether the operation was successful. |
| 32 | + */ |
| 33 | + copy(text: string): boolean { |
| 34 | + const pendingCopy = this.beginCopy(text); |
| 35 | + const successful = pendingCopy.copy(); |
| 36 | + pendingCopy.destroy(); |
| 37 | + |
| 38 | + return successful; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Prepares a string to be copied later. This is useful for large strings |
| 43 | + * which take too long to successfully render and be copied in the same tick. |
| 44 | + * |
| 45 | + * The caller must call `destroy` on the returned `PendingCopy`. |
| 46 | + * |
| 47 | + * @param text The string to copy. |
| 48 | + * @returns the pending copy operation. |
| 49 | + */ |
| 50 | + beginCopy(text: string): PendingCopy { |
| 51 | + return new PendingCopy(text, this._document); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * A pending copy-to-clipboard operation. |
| 57 | + * |
| 58 | + * The implementation of copying text to the clipboard modifies the DOM and |
| 59 | + * forces a relayout. This relayout can take too long if the string is large, |
| 60 | + * causing the execCommand('copy') to happen too long after the user clicked. |
| 61 | + * This results in the browser refusing to copy. This object lets the |
| 62 | + * relayout happen in a separate tick from copying by providing a copy function |
| 63 | + * that can be called later. |
| 64 | + * |
| 65 | + * Destroy must be called when no longer in use, regardless of whether `copy` is |
| 66 | + * called. |
| 67 | + */ |
| 68 | +export class PendingCopy { |
| 69 | + private _textarea: HTMLTextAreaElement|undefined; |
| 70 | + |
| 71 | + constructor(text: string, private readonly _document: Document) { |
| 72 | + const textarea = this._textarea = this._document.createElement('textarea'); |
| 73 | + |
| 74 | + // Hide the element for display and accessibility. |
| 75 | + textarea.setAttribute('style', 'opacity: 0;'); |
| 76 | + textarea.setAttribute('aria-hidden', 'true'); |
| 77 | + |
| 78 | + textarea.value = text; |
| 79 | + this._document.body.appendChild(textarea); |
| 80 | + } |
| 81 | + |
| 82 | + /** Finishes copying the text. */ |
| 83 | + copy(): boolean { |
| 84 | + const textarea = this._textarea; |
| 85 | + let successful = false; |
| 86 | + |
| 87 | + try { // Older browsers could throw if copy is not supported. |
| 88 | + if (textarea) { |
| 89 | + const currentFocus = document.activeElement; |
| 90 | + |
| 91 | + textarea.select(); |
| 92 | + successful = this._document.execCommand('copy'); |
| 93 | + |
| 94 | + if (currentFocus instanceof HTMLElement) { |
| 95 | + currentFocus.focus(); |
| 96 | + } |
| 97 | + } |
| 98 | + } catch { |
| 99 | + // Discard error. |
| 100 | + // Initial setting of {@code successful} will represent failure here. |
| 101 | + } |
| 102 | + |
| 103 | + return successful; |
| 104 | + } |
| 105 | + |
| 106 | + /** Cleans up DOM changes used to perform the copy operation. */ |
| 107 | + destroy() { |
| 108 | + if (this._textarea) { |
| 109 | + this._document.body.removeChild(this._textarea); |
| 110 | + this._textarea = undefined; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments