import { Injectable } from '@angular/core' ; @ Injectable ( ) export class UtilitiesService { highlightText ( e ) { const range = document . createRange ( ) ; range . selectNodeContents ( e ) ; const sel = window . getSelection ( ) ; sel . removeAllRanges ( ) ; sel . addRange ( range ) ; } unHighlightText ( ) { const sel = window . getSelection ( ) ; sel . removeAllRanges ( ) ; } fallbackCopyTextToClipboard ( text ) { const textArea = document . createElement ( 'textarea' ) ; textArea . value = text ; document . body . appendChild ( textArea ) ; textArea . focus ( ) ; textArea . select ( ) ; let listener = ( e ? ) => { if ( e ) { let clipboard = e . clipboardData || window [ 'clipboardData' ] ; clipboard . setData ( 'text' , text ) ; e . preventDefault ( ) ; } } ; document . addEventListener ( 'copy' , listener , false ) ; document . execCommand ( 'copy' ) ; document . removeEventListener ( 'copy' , listener , false ) ; document . body . removeChild ( textArea ) ; } copyContentToClipboard ( text ) { const nav = navigator as any ; if ( ! nav . clipboard ) { this . fallbackCopyTextToClipboard ( text ) ; return ; } nav . clipboard . writeText ( text ) . catch ( ( ) => this . fallbackCopyTextToClipboard ( text ) ) ; } }