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 | import { Directive, EventEmitter, Input, Output, NgZone, InjectionToken, Inject, Optional, } from '@angular/core';
|
---|
9 | import { Clipboard } from './clipboard';
|
---|
10 | /** Injection token that can be used to provide the default options to `CdkCopyToClipboard`. */
|
---|
11 | export const CDK_COPY_TO_CLIPBOARD_CONFIG = new InjectionToken('CDK_COPY_TO_CLIPBOARD_CONFIG');
|
---|
12 | /**
|
---|
13 | * @deprecated Use `CDK_COPY_TO_CLIPBOARD_CONFIG` instead.
|
---|
14 | * @breaking-change 13.0.0
|
---|
15 | */
|
---|
16 | export const CKD_COPY_TO_CLIPBOARD_CONFIG = CDK_COPY_TO_CLIPBOARD_CONFIG;
|
---|
17 | /**
|
---|
18 | * Provides behavior for a button that when clicked copies content into user's
|
---|
19 | * clipboard.
|
---|
20 | */
|
---|
21 | export class CdkCopyToClipboard {
|
---|
22 | constructor(_clipboard, _ngZone, config) {
|
---|
23 | this._clipboard = _clipboard;
|
---|
24 | this._ngZone = _ngZone;
|
---|
25 | /** Content to be copied. */
|
---|
26 | this.text = '';
|
---|
27 | /**
|
---|
28 | * How many times to attempt to copy the text. This may be necessary for longer text, because
|
---|
29 | * the browser needs time to fill an intermediate textarea element and copy the content.
|
---|
30 | */
|
---|
31 | this.attempts = 1;
|
---|
32 | /**
|
---|
33 | * Emits when some text is copied to the clipboard. The
|
---|
34 | * emitted value indicates whether copying was successful.
|
---|
35 | */
|
---|
36 | this.copied = new EventEmitter();
|
---|
37 | /** Copies that are currently being attempted. */
|
---|
38 | this._pending = new Set();
|
---|
39 | if (config && config.attempts != null) {
|
---|
40 | this.attempts = config.attempts;
|
---|
41 | }
|
---|
42 | }
|
---|
43 | /** Copies the current text to the clipboard. */
|
---|
44 | copy(attempts = this.attempts) {
|
---|
45 | if (attempts > 1) {
|
---|
46 | let remainingAttempts = attempts;
|
---|
47 | const pending = this._clipboard.beginCopy(this.text);
|
---|
48 | this._pending.add(pending);
|
---|
49 | const attempt = () => {
|
---|
50 | const successful = pending.copy();
|
---|
51 | if (!successful && --remainingAttempts && !this._destroyed) {
|
---|
52 | // We use 1 for the timeout since it's more predictable when flushing in unit tests.
|
---|
53 | this._currentTimeout = this._ngZone.runOutsideAngular(() => setTimeout(attempt, 1));
|
---|
54 | }
|
---|
55 | else {
|
---|
56 | this._currentTimeout = null;
|
---|
57 | this._pending.delete(pending);
|
---|
58 | pending.destroy();
|
---|
59 | this.copied.emit(successful);
|
---|
60 | }
|
---|
61 | };
|
---|
62 | attempt();
|
---|
63 | }
|
---|
64 | else {
|
---|
65 | this.copied.emit(this._clipboard.copy(this.text));
|
---|
66 | }
|
---|
67 | }
|
---|
68 | ngOnDestroy() {
|
---|
69 | if (this._currentTimeout) {
|
---|
70 | clearTimeout(this._currentTimeout);
|
---|
71 | }
|
---|
72 | this._pending.forEach(copy => copy.destroy());
|
---|
73 | this._pending.clear();
|
---|
74 | this._destroyed = true;
|
---|
75 | }
|
---|
76 | }
|
---|
77 | CdkCopyToClipboard.decorators = [
|
---|
78 | { type: Directive, args: [{
|
---|
79 | selector: '[cdkCopyToClipboard]',
|
---|
80 | host: {
|
---|
81 | '(click)': 'copy()',
|
---|
82 | }
|
---|
83 | },] }
|
---|
84 | ];
|
---|
85 | CdkCopyToClipboard.ctorParameters = () => [
|
---|
86 | { type: Clipboard },
|
---|
87 | { type: NgZone },
|
---|
88 | { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [CKD_COPY_TO_CLIPBOARD_CONFIG,] }] }
|
---|
89 | ];
|
---|
90 | CdkCopyToClipboard.propDecorators = {
|
---|
91 | text: [{ type: Input, args: ['cdkCopyToClipboard',] }],
|
---|
92 | attempts: [{ type: Input, args: ['cdkCopyToClipboardAttempts',] }],
|
---|
93 | copied: [{ type: Output, args: ['cdkC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