import ClipboardJS from "clipboard" ; import { Observable , Subject , mapTo , tap } from "rxjs" ; import { translation } from "~/_" ; import { getElement } from "~/browser" ; /**
 * Setup options
 */ interface SetupOptions { alert$ : Subject < string > ; } param el returns function extract ( el ) { el . setAttribute ( "data-md-copying" , "" ) ; const text = el . innerText ; el . removeAttribute ( "data-md-copying" ) ; return text ; } param options export function setupClipboardJS ( { alert$ } ) { if ( ClipboardJS . isSupported ( ) ) { new Observable < ClipboardJS . Event > ( subscriber => { new ClipboardJS ( "[data-clipboard-target], [data-clipboard-text]" , { text : el => ( el . getAttribute ( "data-clipboard-text" ) ! || extract ( getElement ( el . getAttribute ( "data-clipboard-target" ) ! ) ) ) } ) . on ( "success" , ev => subscriber . next ( ev ) ) ; } ) . pipe ( tap ( ev => { const trigger = ev . trigger as HTMLElement ; trigger . focus ( ) ; } ) , mapTo ( translation ( "clipboard.copied" ) ) ) . subscribe ( alert$ ) ; } }