@@ -2,24 +2,22 @@ import {clippie} from 'clippie';
22import { showTemporaryTooltip } from '../modules/tippy.ts' ;
33import { convertImage } from '../utils.ts' ;
44import { GET } from '../modules/fetch.ts' ;
5+ import { registerGlobalEventFunc } from '../modules/observer.ts' ;
56
67const { i18n} = window . config ;
78
89export function initCopyContent ( ) {
9- const btn = document . querySelector ( '#copy-content' ) ;
10- if ( ! btn || btn . classList . contains ( 'disabled' ) ) return ;
11-
12- btn . addEventListener ( 'click' , async ( ) => {
13- if ( btn . classList . contains ( 'is-loading' ) ) return ;
10+ registerGlobalEventFunc ( 'click' , 'onCopyContentButtonClick' , async ( el : HTMLInputElement ) => {
11+ if ( el . classList . contains ( 'is-loading' ) ) return ;
1412 let content ;
1513 let isRasterImage = false ;
16- const link = btn . getAttribute ( 'data-link' ) ;
14+ const link = el . getAttribute ( 'data-link' ) ;
1715
1816 // when data-link is present, we perform a fetch. this is either because
1917 // the text to copy is not in the DOM or it is an image which should be
2018 // fetched to copy in full resolution
2119 if ( link ) {
22- btn . classList . add ( 'is-loading' , 'loading-icon-2px' ) ;
20+ el . classList . add ( 'is-loading' , 'loading-icon-2px' ) ;
2321 try {
2422 const res = await GET ( link , { credentials : 'include' , redirect : 'follow' } ) ;
2523 const contentType = res . headers . get ( 'content-type' ) ;
@@ -31,9 +29,9 @@ export function initCopyContent() {
3129 content = await res . text ( ) ;
3230 }
3331 } catch {
34- return showTemporaryTooltip ( btn , i18n . copy_error ) ;
32+ return showTemporaryTooltip ( el , i18n . copy_error ) ;
3533 } finally {
36- btn . classList . remove ( 'is-loading' , 'loading-icon-2px' ) ;
34+ el . classList . remove ( 'is-loading' , 'loading-icon-2px' ) ;
3735 }
3836 } else { // text, read from DOM
3937 const lineEls = document . querySelectorAll ( '.file-view .lines-code' ) ;
@@ -43,13 +41,13 @@ export function initCopyContent() {
4341 // try copy original first, if that fails and it's an image, convert it to png
4442 const success = await clippie ( content ) ;
4543 if ( success ) {
46- showTemporaryTooltip ( btn , i18n . copy_success ) ;
44+ showTemporaryTooltip ( el , i18n . copy_success ) ;
4745 } else {
4846 if ( isRasterImage ) {
4947 const success = await clippie ( await convertImage ( content as Blob , 'image/png' ) ) ;
50- showTemporaryTooltip ( btn , success ? i18n . copy_success : i18n . copy_error ) ;
48+ showTemporaryTooltip ( el , success ? i18n . copy_success : i18n . copy_error ) ;
5149 } else {
52- showTemporaryTooltip ( btn , i18n . copy_error ) ;
50+ showTemporaryTooltip ( el , i18n . copy_error ) ;
5351 }
5452 }
5553 } ) ;
0 commit comments