clipboard.mjs 9.2 KB

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