1 | import * as i1 from '@angular/common';
|
---|
2 | import { DOCUMENT } from '@angular/common';
|
---|
3 | import * as i0 from '@angular/core';
|
---|
4 | import { Injectable, Inject, InjectionToken, EventEmitter, Directive, NgZone, Optional, Input, Output, NgModule } from '@angular/core';
|
---|
5 |
|
---|
6 | /**
|
---|
7 | * @license
|
---|
8 | * Copyright Google LLC All Rights Reserved.
|
---|
9 | *
|
---|
10 | * Use of this source code is governed by an MIT-style license that can be
|
---|
11 | * found in the LICENSE file at https://angular.io/license
|
---|
12 | */
|
---|
13 | /**
|
---|
14 | * A pending copy-to-clipboard operation.
|
---|
15 | *
|
---|
16 | * The implementation of copying text to the clipboard modifies the DOM and
|
---|
17 | * forces a relayout. This relayout can take too long if the string is large,
|
---|
18 | * causing the execCommand('copy') to happen too long after the user clicked.
|
---|
19 | * This results in the browser refusing to copy. This object lets the
|
---|
20 | * relayout happen in a separate tick from copying by providing a copy function
|
---|
21 | * that can be called later.
|
---|
22 | *
|
---|
23 | * Destroy must be called when no longer in use, regardless of whether `copy` is
|
---|
24 | * called.
|
---|
25 | */
|
---|
26 | class PendingCopy {
|
---|
27 | constructor(text, _document) {
|
---|
28 | this._document = _document;
|
---|
29 | const textarea = this._textarea = this._document.createElement('textarea');
|
---|
30 | const styles = textarea.style;
|
---|
31 | // Hide the element for display and accessibility. Set a fixed position so the page layout
|
---|
32 | // isn't affected. We use `fixed` with `top: 0`, because focus is moved into the textarea
|
---|
33 | // for a split second and if it's off-screen, some browsers will attempt to scroll it into view.
|
---|
34 | styles.position = 'fixed';
|
---|
35 | styles.top = styles.opacity = '0';
|
---|
36 | styles.left = '-999em';
|
---|
37 | textarea.setAttribute('aria-hidden', 'true');
|
---|
38 | textarea.value = text;
|
---|
39 | this._document.body.appendChild(textarea);
|
---|
40 | }
|
---|
41 | /** Finishes copying the text. */
|
---|
42 | copy() {
|
---|
43 | const textarea = this._textarea;
|
---|
44 | let successful = false;
|
---|
45 | try { // Older browsers could throw if copy is not supported.
|
---|
46 | if (textarea) {
|
---|
47 | const currentFocus = this._document.activeElement;
|
---|
48 | textarea.select();
|
---|
49 | textarea.setSelectionRange(0, textarea.value.length);
|
---|
50 | successful = this._document.execCommand('copy');
|
---|
51 | if (currentFocus) {
|
---|
52 | currentFocus.focus();
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|
56 | catch (_a) {
|
---|
57 | // Discard error.
|
---|
58 | // Initial setting of {@code successful} will represent failure here.
|
---|
59 | }
|
---|
60 | return successful;
|
---|
61 | }
|
---|
62 | /** Cleans up DOM changes used to perform the copy operation. */
|
---|
63 | destroy() {
|
---|
64 | const textarea = this._textarea;
|
---|
65 | if (textarea) {
|
---|
66 | if (textarea.parentNode) {
|
---|
67 | textarea.parentNode.removeChild(textarea);
|
---|
68 | }
|
---|
69 | this._textarea = undefined;
|
---|
70 | }
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * @license
|
---|
76 | * Copyright Google LLC All Rights Reserved.
|
---|
77 | *
|
---|
78 | * Use of this source code is governed by an MIT-style license that can be
|
---|
79 | * found in the LICENSE file at https://angular.io/license
|
---|
80 | */
|
---|
81 | /**
|
---|
82 | * A service for copying text to the clipboard.
|
---|
83 | */
|
---|
84 | class Clipboard {
|
---|
85 | constructor(document) {
|
---|
86 | this._document = document;
|
---|
87 | }
|
---|
88 | /**
|
---|
89 | * Copies the provided text into the user's clipboard.
|
---|
90 | *
|
---|
91 | * @param text The string to copy.
|
---|
92 | * @returns Whether the operation was successful.
|
---|
93 | */
|
---|
94 | copy(text) {
|
---|
95 | const pendingCopy = this.beginCopy(text);
|
---|
96 | const successful = pendingCopy.copy();
|
---|
97 | pendingCopy.destroy();
|
---|
98 | return successful;
|
---|
99 | }
|
---|
100 | /**
|
---|
101 | * Prepares a string to be copied later. This is useful for large strings
|
---|
102 | * which take too long to successfully render and be copied in the same tick.
|
---|
103 | *
|
---|
104 | * The caller must call `destroy` on the returned `PendingCopy`.
|
---|
105 | *
|
---|
106 | * @param text The string to copy.
|
---|
107 | * @returns the pending copy operation.
|
---|
108 | */
|
---|
109 | beginCopy(text) {
|
---|
110 | return new PendingCopy(text, this._document);
|
---|
111 | }
|
---|
112 | }
|
---|
113 | Clipboard.ɵprov = i0.ɵɵdefineInjectable({ factory: function Clipboard_Factory() { return new Clipboard(i0.ɵɵinject(i1.DOCUMENT)); }, token: Clipboard, providedIn: "root" });
|
---|
114 | Clipboard.decorators = [
|
---|
115 | { type: Injectable, args: [{ providedIn: 'root' },] }
|
---|
116 | ];
|
---|
117 | Clipboard.ctorParameters = () => [
|
---|
118 | { type: undefined, decorators: [{ type: Inject, args: [DOCUMENT,] }] }
|
---|
119 | ];
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * @license
|
---|
123 | * Copyright Google LLC All Rights Reserved.
|
---|
124 | *
|
---|
125 | * Use of this source code is governed by an MIT-style license that can be
|
---|
126 | * found in the LICENSE file at https://angular.io/license
|
---|
127 | */
|
---|
128 | /** Injection token that can be used to provide the default options to `CdkCopyToClipboard`. */
|
---|
129 | const CDK_COPY_TO_CLIPBOARD_CONFIG = new InjectionToken('CDK_COPY_TO_CLIPBOARD_CONFIG');
|
---|
130 | /**
|
---|
131 | * @deprecated Use `CDK_COPY_TO_CLIPBOARD_CONFIG` instead.
|
---|
132 | * @breaking-change 13.0.0
|
---|
133 | */
|
---|
134 | const CKD_COPY_TO_CLIPBOARD_CONFIG = CDK_COPY_TO_CLIPBOARD_CONFIG;
|
---|
135 | /**
|
---|
136 | * Provides behavior for a button that when clicked copies content into user's
|
---|
137 | * clipboard.
|
---|
138 | */
|
---|
139 | class CdkCopyToClipboard {
|
---|
140 | constructor(_clipboard, _ngZone, config) {
|
---|
141 | this._clipboard = _clipboard;
|
---|
142 | this._ngZone = _ngZone;
|
---|
143 | /** Content to be copied. */
|
---|
144 | this.text = '';
|
---|
145 | /**
|
---|
146 | * How many times to attempt to copy the text. This may be necessary for longer text, because
|
---|
147 | * the browser needs time to fill an intermediate textarea element and copy the content.
|
---|
148 | */
|
---|
149 | this.attempts = 1;
|
---|
150 | /**
|
---|
151 | * Emits when some text is copied to the clipboard. The
|
---|
152 | * emitted value indicates whether copying was successful.
|
---|
153 | */
|
---|
154 | this.copied = new EventEmitter();
|
---|
155 | /** Copies that are currently being attempted. */
|
---|
156 | this._pending = new Set();
|
---|
157 | if (config && config.attempts != null) {
|
---|
158 | this.attempts = config.attempts;
|
---|
159 | }
|
---|
160 | }
|
---|
161 | /** Copies the current text to the clipboard. */
|
---|
162 | copy(attempts = this.attempts) {
|
---|
163 | if (attempts > 1) {
|
---|
164 | let remainingAttempts = attempts;
|
---|
165 | const pending = this._clipboard.beginCopy(this.text);
|
---|
166 | this._pending.add(pending);
|
---|
167 | const attempt = () => {
|
---|
168 | const successful = pending.copy();
|
---|
169 | if (!successful && --remainingAttempts && !this._destroyed) {
|
---|
170 | // We use 1 for the timeout since it's more predictable when flushing in unit tests.
|
---|
171 | this._currentTimeout = this._ngZone.runOutsideAngular(() => setTimeout(attempt, 1));
|
---|
172 | }
|
---|
173 | else {
|
---|
174 | this._currentTimeout = null;
|
---|
175 | this._pending.delete(pending);
|
---|
176 | pending.destroy();
|
---|
177 | this.copied.emit(successful);
|
---|
178 | }
|
---|
179 | };
|
---|
180 | attempt();
|
---|
181 | }
|
---|
182 | else {
|
---|
183 | this.copied.emit(this._clipboard.copy(this.text));
|
---|
184 | }
|
---|
185 | }
|
---|
186 | ngOnDestroy() {
|
---|
187 | if (this._currentTimeout) {
|
---|
188 | clearTimeout(this._currentTimeout);
|
---|
189 | }
|
---|
190 | this._pending.forEach(copy => copy.destroy());
|
---|
191 | this._pending.clear();
|
---|
192 | this._destroyed = true;
|
---|
193 | }
|
---|
194 | }
|
---|
195 | CdkCopyToClipboard.decorators = [
|
---|
196 | { type: Directive, args: [{
|
---|
197 | selector: '[cdkCopyToClipboard]',
|
---|
198 | host: {
|
---|
199 | '(click)': 'copy()',
|
---|
200 | }
|
---|
201 | },] }
|
---|
202 | ];
|
---|
203 | CdkCopyToClipboard.ctorParameters = () => [
|
---|
204 | { type: Clipboard },
|
---|
205 | { type: NgZone },
|
---|
206 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [CKD_COPY_TO_CLIPBOARD_CONFIG,] }] }
|
---|
207 | ];
|
---|
208 | CdkCopyToClipboard.propDecorators = {
|
---|
209 | text: [{ type: Input, args: ['cdkCopyToClipboard',] }],
|
---|
210 | attempts: [{ type: Input, args: ['cdkCopyToClipboardAttempts',] }],
|
---|
211 | copied: [{ type: Output, args: ['cdkCopyToClipboardCopied',] }]
|
---|
212 | };
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * @license
|
---|
216 | * Copyright Google LLC All Rights Reserved.
|
---|
217 | *
|
---|
218 | * Use of this source code is governed by an MIT-style license that can be
|
---|
219 | * found in the LICENSE file at https://angular.io/license
|
---|
220 | */
|
---|
221 | class ClipboardModule {
|
---|
222 | }
|
---|
223 | ClipboardModule.decorators = [
|
---|
224 | { type: NgModule, args: [{
|
---|
225 | declarations: [CdkCopyToClipboard],
|
---|
226 | exports: [CdkCopyToClipboard],
|
---|
227 | },] }
|
---|
228 | ];
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * @license
|
---|
232 | * Copyright Google LLC All Rights Reserved.
|
---|
233 | *
|
---|
234 | * Use of this source code is governed by an MIT-style license that can be
|
---|
235 | * found in the LICENSE file at https://angular.io/license
|
---|
236 | */
|
---|
237 |
|
---|
238 | /**
|
---|
239 | * Generated bundle index. Do not edit.
|
---|
240 | */
|
---|
241 |
|
---|
242 | export { CDK_COPY_TO_CLIPBOARD_CONFIG, CKD_COPY_TO_CLIPBOARD_CONFIG, CdkCopyToClipboard, Clipboard, ClipboardModule, PendingCopy };
|
---|
243 | //# sourceMappingURL=clipboard.js.map
|
---|