import { InputBoolean } from 'ngx-tethys/core' ; import { ThyNotifyService } from 'ngx-tethys/notify' ; import { TooltipService } from 'ngx-tethys/tooltip' ; import { coerceElement } from '@angular/cdk/coercion' ; import { DOCUMENT } from '@angular/common' ; import { Directive , ElementRef , EventEmitter , HostListener , Inject , Input , OnDestroy , OnInit , Output , ViewContainerRef } from '@angular/core' ; export interface ThyCopyEvent { isSuccess : boolean ; event : Event ; } @ Directive ( { selector : '[thyCopy]' , providers : [ TooltipService ] } ) export class ThyCopyDirective implements OnInit , OnDestroy { @ Output ( ) thyCopy = new EventEmitter < ThyCopyEvent > ( ) ; @ Input ( ) thyCopySuccessText = '复制成功' ; @ Input ( ) thyCopyTips = '点击复制' ; @ Input ( ) thyCopyContent : string | ElementRef | HTMLElement ; @ Input ( ) @ InputBoolean ( ) thyShowNotify = true ; constructor ( @ Inject ( DOCUMENT ) private document , public tooltipService , private elementRef , private viewContainerRef , private notifyService ) { } ngOnInit ( ) { this . tooltipService . attach ( this . elementRef , this . viewContainerRef , 'hover' ) ; this . tooltipService . thyTooltipDirective . content = this . thyCopyTips ? this . thyCopyTips : '点击复制' ; } private getContent ( event ) { if ( typeof this . thyCopyContent === 'string' ) { return this . thyCopyContent ; } else { const target = this . thyCopyContent ? coerceElement ( this . thyCopyContent ) : event . target ; return target . value || target . textContent ; } } @ HostListener ( 'click' , [ '$event' ] ) public onClick ( event ) { const textarea = this . document . createElement ( 'textarea' ) ; this . document . body . appendChild ( textarea ) ; textarea . value = this . getContent ( event ) ; textarea . select ( ) ; try { document . execCommand ( 'copy' , false , null ) ; this . thyCopy . emit ( { isSuccess : true , event } ) ; if ( this . thyShowNotify ) { this . notifyService . success ( this . thyCopySuccessText ) ; } } catch ( err ) { this . thyCopy . emit ( { isSuccess : false , event } ) ; if ( this . thyShowNotify ) { this . notifyService . error ( '复制失败' ) ; } } finally { textarea . remove ( ) ; } } ngOnDestroy ( ) { this . tooltipService . detach ( ) ; } }