UNPKG

ngx-copypaste

Version:
74 lines (69 loc) 2.71 kB
import * as i0 from '@angular/core'; import { output, inject, ElementRef, Directive } from '@angular/core'; class NgxCopyPasteDirective { constructor() { this.successCb = output(); this._elementRef = inject(ElementRef); } copyWithSelection() { let select = window.getSelection(); if (select) { select.removeAllRanges(); } const element = this._elementRef.nativeElement; if (element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement) { element.select(); } else { const range = document.createRange(); range.selectNodeContents(element); select = window.getSelection(); if (select) { select.addRange(range); } } this.successCb.emit(); // eslint-disable-next-line @typescript-eslint/no-deprecated document.execCommand('copy'); } async copy() { this.copyWithSelection(); try { if (typeof navigator.clipboard === 'undefined') { this.copyWithSelection(); return; } const element = this._elementRef.nativeElement; const value = element instanceof HTMLInputElement || element instanceof HTMLTextAreaElement ? element.value : element.textContent; if (!value) { return; } await navigator.clipboard.writeText(value.trim()); this.successCb.emit(); } catch (err) { // eslint-disable-next-line no-console console.error('Error copying content: ', err); } } static { this.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: NgxCopyPasteDirective, deps: [], target: i0.ɵɵFactoryTarget.Directive }); } static { this.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "14.0.0", version: "20.1.4", type: NgxCopyPasteDirective, isStandalone: true, selector: "[ngxCopyPaste]", outputs: { successCb: "successCb" }, exportAs: ["copy", "ngxCopy"], ngImport: i0 }); } } i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "20.1.4", ngImport: i0, type: NgxCopyPasteDirective, decorators: [{ type: Directive, args: [{ selector: '[ngxCopyPaste]', exportAs: 'copy,ngxCopy', standalone: true, }] }] }); /* * Public API Surface of ngx-copypaste-lib */ /** * Generated bundle index. Do not edit. */ export { NgxCopyPasteDirective }; //# sourceMappingURL=ngx-copypaste.mjs.map