UNPKG

@angular/cdk

Version:

Angular Material Component Development Kit

1 lines 13.1 kB
{"version":3,"file":"clipboard.mjs","sources":["../../../../../k8-fastbuild-ST-46c76129e412/bin/src/cdk/clipboard/pending-copy.ts","../../../../../k8-fastbuild-ST-46c76129e412/bin/src/cdk/clipboard/clipboard.ts","../../../../../k8-fastbuild-ST-46c76129e412/bin/src/cdk/clipboard/copy-to-clipboard.ts","../../../../../k8-fastbuild-ST-46c76129e412/bin/src/cdk/clipboard/clipboard-module.ts"],"sourcesContent":["/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\n/**\n * A pending copy-to-clipboard operation.\n *\n * The implementation of copying text to the clipboard modifies the DOM and\n * forces a re-layout. This re-layout can take too long if the string is large,\n * causing the execCommand('copy') to happen too long after the user clicked.\n * This results in the browser refusing to copy. This object lets the\n * re-layout happen in a separate tick from copying by providing a copy function\n * that can be called later.\n *\n * Destroy must be called when no longer in use, regardless of whether `copy` is\n * called.\n */\nexport class PendingCopy {\n private _textarea: HTMLTextAreaElement | undefined;\n\n constructor(\n text: string,\n private readonly _document: Document,\n ) {\n const textarea = (this._textarea = this._document.createElement('textarea'));\n const styles = textarea.style;\n\n // Hide the element for display and accessibility. Set a fixed position so the page layout\n // isn't affected. We use `fixed` with `top: 0`, because focus is moved into the textarea\n // for a split second and if it's off-screen, some browsers will attempt to scroll it into view.\n styles.position = 'fixed';\n styles.top = styles.opacity = '0';\n styles.left = '-999em';\n textarea.setAttribute('aria-hidden', 'true');\n textarea.value = text;\n // Making the textarea `readonly` prevents the screen from jumping on iOS Safari (see #25169).\n textarea.readOnly = true;\n // The element needs to be inserted into the fullscreen container, if the page\n // is in fullscreen mode, otherwise the browser won't execute the copy command.\n (this._document.fullscreenElement || this._document.body).appendChild(textarea);\n }\n\n /** Finishes copying the text. */\n copy(): boolean {\n const textarea = this._textarea;\n let successful = false;\n\n try {\n // Older browsers could throw if copy is not supported.\n if (textarea) {\n const currentFocus = this._document.activeElement as HTMLOrSVGElement | null;\n\n textarea.select();\n textarea.setSelectionRange(0, textarea.value.length);\n successful = this._document.execCommand('copy');\n\n if (currentFocus) {\n currentFocus.focus();\n }\n }\n } catch {\n // Discard error.\n // Initial setting of {@code successful} will represent failure here.\n }\n\n return successful;\n }\n\n /** Cleans up DOM changes used to perform the copy operation. */\n destroy() {\n const textarea = this._textarea;\n\n if (textarea) {\n textarea.remove();\n this._textarea = undefined;\n }\n }\n}\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {DOCUMENT} from '@angular/common';\nimport {Injectable, inject} from '@angular/core';\nimport {PendingCopy} from './pending-copy';\n\n/**\n * A service for copying text to the clipboard.\n */\n@Injectable({providedIn: 'root'})\nexport class Clipboard {\n private readonly _document = inject(DOCUMENT);\n\n constructor(...args: unknown[]);\n constructor() {}\n\n /**\n * Copies the provided text into the user's clipboard.\n *\n * @param text The string to copy.\n * @returns Whether the operation was successful.\n */\n copy(text: string): boolean {\n const pendingCopy = this.beginCopy(text);\n const successful = pendingCopy.copy();\n pendingCopy.destroy();\n\n return successful;\n }\n\n /**\n * Prepares a string to be copied later. This is useful for large strings\n * which take too long to successfully render and be copied in the same tick.\n *\n * The caller must call `destroy` on the returned `PendingCopy`.\n *\n * @param text The string to copy.\n * @returns the pending copy operation.\n */\n beginCopy(text: string): PendingCopy {\n return new PendingCopy(text, this._document);\n }\n}\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {\n Directive,\n EventEmitter,\n Input,\n Output,\n NgZone,\n InjectionToken,\n OnDestroy,\n inject,\n} from '@angular/core';\nimport {Clipboard} from './clipboard';\nimport {PendingCopy} from './pending-copy';\n\n/** Object that can be used to configure the default options for `CdkCopyToClipboard`. */\nexport interface CdkCopyToClipboardConfig {\n /** Default number of attempts to make when copying text to the clipboard. */\n attempts?: number;\n}\n\n/** Injection token that can be used to provide the default options to `CdkCopyToClipboard`. */\nexport const CDK_COPY_TO_CLIPBOARD_CONFIG = new InjectionToken<CdkCopyToClipboardConfig>(\n 'CDK_COPY_TO_CLIPBOARD_CONFIG',\n);\n\n/**\n * Provides behavior for a button that when clicked copies content into user's\n * clipboard.\n */\n@Directive({\n selector: '[cdkCopyToClipboard]',\n host: {\n '(click)': 'copy()',\n },\n})\nexport class CdkCopyToClipboard implements OnDestroy {\n private _clipboard = inject(Clipboard);\n private _ngZone = inject(NgZone);\n\n /** Content to be copied. */\n @Input('cdkCopyToClipboard') text: string = '';\n\n /**\n * How many times to attempt to copy the text. This may be necessary for longer text, because\n * the browser needs time to fill an intermediate textarea element and copy the content.\n */\n @Input('cdkCopyToClipboardAttempts') attempts: number = 1;\n\n /**\n * Emits when some text is copied to the clipboard. The\n * emitted value indicates whether copying was successful.\n */\n @Output('cdkCopyToClipboardCopied') readonly copied = new EventEmitter<boolean>();\n\n /** Copies that are currently being attempted. */\n private _pending = new Set<PendingCopy>();\n\n /** Whether the directive has been destroyed. */\n private _destroyed: boolean;\n\n /** Timeout for the current copy attempt. */\n private _currentTimeout: any;\n\n constructor(...args: unknown[]);\n\n constructor() {\n const config = inject(CDK_COPY_TO_CLIPBOARD_CONFIG, {optional: true});\n\n if (config && config.attempts != null) {\n this.attempts = config.attempts;\n }\n }\n\n /** Copies the current text to the clipboard. */\n copy(attempts: number = this.attempts): void {\n if (attempts > 1) {\n let remainingAttempts = attempts;\n const pending = this._clipboard.beginCopy(this.text);\n this._pending.add(pending);\n\n const attempt = () => {\n const successful = pending.copy();\n if (!successful && --remainingAttempts && !this._destroyed) {\n // We use 1 for the timeout since it's more predictable when flushing in unit tests.\n this._currentTimeout = this._ngZone.runOutsideAngular(() => setTimeout(attempt, 1));\n } else {\n this._currentTimeout = null;\n this._pending.delete(pending);\n pending.destroy();\n this.copied.emit(successful);\n }\n };\n attempt();\n } else {\n this.copied.emit(this._clipboard.copy(this.text));\n }\n }\n\n ngOnDestroy() {\n if (this._currentTimeout) {\n clearTimeout(this._currentTimeout);\n }\n\n this._pending.forEach(copy => copy.destroy());\n this._pending.clear();\n this._destroyed = true;\n }\n}\n","/**\n * @license\n * Copyright Google LLC All Rights Reserved.\n *\n * Use of this source code is governed by an MIT-style license that can be\n * found in the LICENSE file at https://angular.dev/license\n */\n\nimport {NgModule} from '@angular/core';\n\nimport {CdkCopyToClipboard} from './copy-to-clipboard';\n\n@NgModule({\n imports: [CdkCopyToClipboard],\n exports: [CdkCopyToClipboard],\n})\nexport class ClipboardModule {}\n"],"names":[],"mappings":";;;;AAQA;;;;;;;;;;;;AAYG;MACU,WAAW,CAAA;AAKH,IAAA,SAAA;AAJX,IAAA,SAAS;IAEjB,WACE,CAAA,IAAY,EACK,SAAmB,EAAA;QAAnB,IAAS,CAAA,SAAA,GAAT,SAAS;AAE1B,QAAA,MAAM,QAAQ,IAAI,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC,SAAS,CAAC,aAAa,CAAC,UAAU,CAAC,CAAC;AAC5E,QAAA,MAAM,MAAM,GAAG,QAAQ,CAAC,KAAK;;;;AAK7B,QAAA,MAAM,CAAC,QAAQ,GAAG,OAAO;QACzB,MAAM,CAAC,GAAG,GAAG,MAAM,CAAC,OAAO,GAAG,GAAG;AACjC,QAAA,MAAM,CAAC,IAAI,GAAG,QAAQ;AACtB,QAAA,QAAQ,CAAC,YAAY,CAAC,aAAa,EAAE,MAAM,CAAC;AAC5C,QAAA,QAAQ,CAAC,KAAK,GAAG,IAAI;;AAErB,QAAA,QAAQ,CAAC,QAAQ,GAAG,IAAI;;;AAGxB,QAAA,CAAC,IAAI,CAAC,SAAS,CAAC,iBAAiB,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,WAAW,CAAC,QAAQ,CAAC;;;IAIjF,IAAI,GAAA;AACF,QAAA,MAAM,Q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