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 | * A pending copy-to-clipboard operation.
|
---|
10 | *
|
---|
11 | * The implementation of copying text to the clipboard modifies the DOM and
|
---|
12 | * forces a relayout. This relayout can take too long if the string is large,
|
---|
13 | * causing the execCommand('copy') to happen too long after the user clicked.
|
---|
14 | * This results in the browser refusing to copy. This object lets the
|
---|
15 | * relayout happen in a separate tick from copying by providing a copy function
|
---|
16 | * that can be called later.
|
---|
17 | *
|
---|
18 | * Destroy must be called when no longer in use, regardless of whether `copy` is
|
---|
19 | * called.
|
---|
20 | */
|
---|
21 | export class PendingCopy {
|
---|
22 | constructor(text, _document) {
|
---|
23 | this._document = _document;
|
---|
24 | const textarea = this._textarea = this._document.createElement('textarea');
|
---|
25 | const styles = textarea.style;
|
---|
26 | // Hide the element for display and accessibility. Set a fixed position so the page layout
|
---|
27 | // isn't affected. We use `fixed` with `top: 0`, because focus is moved into the textarea
|
---|
28 | // for a split second and if it's off-screen, some browsers will attempt to scroll it into view.
|
---|
29 | styles.position = 'fixed';
|
---|
30 | styles.top = styles.opacity = '0';
|
---|
31 | styles.left = '-999em';
|
---|
32 | textarea.setAttribute('aria-hidden', 'true');
|
---|
33 | textarea.value = text;
|
---|
34 | this._document.body.appendChild(textarea);
|
---|
35 | }
|
---|
36 | /** Finishes copying the text. */
|
---|
37 | copy() {
|
---|
38 | const textarea = this._textarea;
|
---|
39 | let successful = false;
|
---|
40 | try { // Older browsers could throw if copy is not supported.
|
---|
41 | if (textarea) {
|
---|
42 | const currentFocus = this._document.activeElement;
|
---|
43 | textarea.select();
|
---|
44 | textarea.setSelectionRange(0, textarea.value.length);
|
---|
45 | successful = this._document.execCommand('copy');
|
---|
46 | if (currentFocus) {
|
---|
47 | currentFocus.focus();
|
---|
48 | }
|
---|
49 | }
|
---|
50 | }
|
---|
51 | catch (_a) {
|
---|
52 | // Discard error.
|
---|
53 | // Initial setting of {@code successful} will represent failure here.
|
---|
54 | }
|
---|
55 | return successful;
|
---|
56 | }
|
---|
57 | /** Cleans up DOM changes used to perform the copy operation. */
|
---|
58 | destroy() {
|
---|
59 | const textarea = this._textarea;
|
---|
60 | if (textarea) {
|
---|
61 | if (textarea.parentNode) {
|
---|
62 | textarea.parentNode.removeChild(textarea);
|
---|
63 | }
|
---|
64 | this._textarea = undefined;
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoicGVuZGluZy1jb3B5LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vLi4vLi4vLi4vLi4vc3JjL2Nkay9jbGlwYm9hcmQvcGVuZGluZy1jb3B5LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBOzs7Ozs7R0FNRztBQUVIOzs7Ozs7Ozs7Ozs7R0FZRztBQUNILE1BQU0sT0FBTyxXQUFXO0lBR3RCLFlBQVksSUFBWSxFQUFtQixTQUFtQjtRQUFuQixjQUFTLEdBQVQsU0FBUyxDQUFVO1FBQzVELE1BQU0sUUFBUSxHQUFHLElBQUksQ0FBQyxTQUFTLEdBQUcsSUFBSSxDQUFDLFNBQVMsQ0FBQyxhQUFhLENBQUMsVUFBVSxDQUFDLENBQUM7UUFDM0UsTUFBTSxNQUFNLEdBQUcsUUFBUSxDQUFDLEtBQUssQ0FBQztRQUU5QiwwRkFBMEY7UUFDMUYseUZBQXlGO1FBQ3pGLGdHQUFnRztRQUNoRyxNQUFNLENBQUMsUUFBUSxHQUFHLE9BQU8sQ0FBQztRQUMxQixNQUFNLENBQUMsR0FBRyxHQUFHLE1BQU0sQ0FBQyxPQUFPLEdBQUcsR0FBRyxDQUFDO1FBQ2xDLE1BQU0sQ0FBQyxJQU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