@@ -63,6 +63,21 @@ class WorkerPool {
63
63
}
64
64
}
65
65
66
+ const createClipButton = ( ) => {
67
+ const clip = document . createElement ( 'button' ) ;
68
+
69
+ clip . className = 'fa-copy clip-button' ;
70
+ clip . setAttribute ( 'aria-label' , 'Copy to clipboard' ) ;
71
+ clip . innerHTML = '<div class="tooltiptext"></div>' ;
72
+
73
+ const buttons = document . createElement ( 'div' ) ;
74
+
75
+ buttons . className = 'buttons' ;
76
+ buttons . insertBefore ( clip , buttons . firstChild ) ;
77
+
78
+ return buttons ;
79
+ } ;
80
+
66
81
const codeCopy = trigger => {
67
82
const elem = trigger . target ;
68
83
@@ -99,17 +114,7 @@ export const codeBlock = async () => {
99
114
return ;
100
115
}
101
116
102
- // capture hover event in iOS
103
- if ( globalThis . ontouchstart !== undefined ) {
104
- document . addEventListener ( 'touchstart' , ( ) => { } , { once : false , passive : true } ) ;
105
- }
106
-
107
- const clip = document . createElement ( 'button' ) ;
108
-
109
- clip . className = 'fa-copy clip-button' ;
110
- clip . setAttribute ( 'aria-label' , 'Copy to clipboard' ) ;
111
- clip . innerHTML = '<i class="tooltiptext"></i>' ;
112
-
117
+ const clipButton = createClipButton ( ) ;
113
118
const workerPool = new WorkerPool ( codeQuery . length ) ;
114
119
115
120
for ( const code of codeQuery ) {
@@ -132,12 +137,16 @@ export const codeBlock = async () => {
132
137
console . error ( e ) ;
133
138
} ) ;
134
139
135
- const buttons = document . createElement ( 'div' ) ;
136
- buttons . className = 'buttons' ;
137
- buttons . insertBefore ( document . importNode ( clip , true ) , buttons . firstChild ) ;
138
- buttons . addEventListener ( 'mousedown' , codeCopy ) ;
139
-
140
140
const parent = code . parentNode ;
141
- parent . insertBefore ( buttons , parent . firstChild ) ;
141
+
142
+ const cb = document . importNode ( clipButton , true ) ;
143
+ cb . addEventListener ( 'mousedown' , codeCopy ) ;
144
+
145
+ parent . insertBefore ( cb , parent . firstChild ) ;
146
+ }
147
+
148
+ // capture hover event in iOS
149
+ if ( globalThis . ontouchstart !== undefined ) {
150
+ document . addEventListener ( 'touchstart' , ( ) => { } , { once : false , passive : true } ) ;
142
151
}
143
152
} ;
0 commit comments