/** * @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 */ import { Directive, EventEmitter, Input, Output, NgZone, InjectionToken, Inject, Optional, } from '@angular/core'; import { Clipboard } from './clipboard'; import * as i0 from "@angular/core"; import * as i1 from "./clipboard"; /** Injection token that can be used to provide the default options to `CdkCopyToClipboard`. */ export const CDK_COPY_TO_CLIPBOARD_CONFIG = new InjectionToken('CDK_COPY_TO_CLIPBOARD_CONFIG'); /** * Provides behavior for a button that when clicked copies content into user's * clipboard. */ export class CdkCopyToClipboard { constructor(_clipboard, _ngZone, config) { this._clipboard = _clipboard; this._ngZone = _ngZone; /** Content to be copied. */ this.text = ''; /** * How many times to attempt to copy the text. This may be necessary for longer text, because * the browser needs time to fill an intermediate textarea element and copy the content. */ this.attempts = 1; /** * Emits when some text is copied to the clipboard. The * emitted value indicates whether copying was successful. */ this.copied = new EventEmitter(); /** Copies that are currently being attempted. */ this._pending = new Set(); if (config && config.attempts != null) { this.attempts = config.attempts; } } /** Copies the current text to the clipboard. */ copy(attempts = this.attempts) { if (attempts > 1) { let remainingAttempts = attempts; const pending = this._clipboard.beginCopy(this.text); this._pending.add(pending); const attempt = () => { const successful = pending.copy(); if (!successful && --remainingAttempts && !this._destroyed) { // We use 1 for the timeout since it's more predictable when flushing in unit tests. this._currentTimeout = this._ngZone.runOutsideAngular(() => setTimeout(attempt, 1)); } else { this._currentTimeout = null; this._pending.delete(pending); pending.destroy(); this.copied.emit(successful); } }; attempt(); } else { this.copied.emit(this._clipboard.copy(this.text)); } } ngOnDestroy() { if (this._currentTimeout) { clearTimeout(this._currentTimeout); } this._pending.forEach(copy => copy.destroy()); this._pending.clear(); this._destroyed = true; } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "16.1.1", ngImport: i0, type: CdkCopyToClipboard, deps: [{ token: i1.Clipboard }, { token: i0.NgZone }, { token: CDK_COPY_TO_CLIPBOARD_CONFIG, optional: true }], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "16.1.1", type: CdkCopyToClipboard, selector: "[cdkCopyToClipboard]", inputs: { text: ["cdkCopyToClipboard", "text"], attempts: ["cdkCopyToClipboardAttempts", "attempts"] }, outputs: { copied: "cdkCopyToClipboardCopied" }, host: { listeners: { "click": "copy()" } }, ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "16.1.1", ngImport: i0, type: CdkCopyToClipboard, decorators: [{ type: Directive, args: [{ selector: '[cdkCopyToClipboard]', host: { '(click)': 'copy()', }, }] }], ctorParameters: function () { return [{ type: i1.Clipboard }, { type: i0.NgZone }, { type: undefined, decorators: [{ type: Optional }, { type: Inject, args: [CDK_COPY_TO_CLIPBOARD_CONFIG] }] }]; }, propDecorators: { text: [{ type: Input, args: ['cdkCopyToClipboard'] }], attempts: [{ type: Input, args: ['cdkCopyToClipboardAttempts'] }], copied: [{ type: Output, args: ['cdkCopyToClipboardCopied'] }] } }); //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY29weS10by1jbGlwYm9hcmQuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi8uLi8uLi8uLi8uLi8uLi9zcmMvY2RrL2NsaXBib2FyZC9jb3B5LXRvLWNsaXBib2FyZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQTs7Ozs7O0dBTUc7QUFFSCxPQUFPLEVBQ0wsU0FBUyxFQUNULFlBQVksRUFDWixLQUFLLEVBQ0wsTUFBTSxFQUNOLE1BQU0sRUFDTixjQUFjLEVBQ2QsTUFBTSxFQUNOLFFBQVEsR0FFVCxNQUFNLGVBQWUsQ0FBQztBQUN2QixPQUFPLEVBQUMsU0FBUyxFQUFDLE1BQU0sYUFBYSxDQUFDOzs7QUFTdEMsK0ZBQStGO0FBQy9GLE1BQU0sQ0FBQyxNQ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