import { Directive , ElementRef , HostListener } from "@angular/core" ; import { PlatformUtilsService } from "jslib-common/abstractions/platformUtils.service" ; @ Directive ( { selector : "[appSelectCopy]" , } ) export class SelectCopyDirective { constructor ( private el , private platformUtilsService ) { } @ HostListener ( "copy" ) onCopy ( ) { if ( window == null ) { return ; } let copyText = "" ; const selection = window . getSelection ( ) ; for ( let i = 0 ; i < selection . rangeCount ; i ++ ) { const range = selection . getRangeAt ( i ) ; const text = range . toString ( ) ; let stringEndPos = text . length ; const newLinePos = text . search ( /(?:\r\n|\r|\n)/ ) ; if ( newLinePos > - 1 ) { const otherPart = text . substr ( newLinePos ) . trim ( ) ; if ( otherPart === "" ) { stringEndPos = newLinePos ; } } copyText += text . substring ( 0 , stringEndPos ) ; } this . platformUtilsService . copyToClipboard ( copyText , { window : window } ) ; } }