@@ -336,37 +336,35 @@ for (const summary of document.querySelectorAll("#observablehq-sidebar summary")
336
336
}
337
337
338
338
// copy code cells
339
- document . addEventListener ( "pointerover" , ( { target} ) => {
340
- if ( typeof navigator ?. clipboard ?. writeText !== "function" ) return ;
341
- if ( target . nodeName === "PRE" && ! target . getAttribute ( "data-copy" ) ) {
342
- target . addEventListener ( "pointermove" , move ) ;
343
- target . addEventListener ( "pointerleave" , out ) ;
344
- }
345
- function out ( ) {
346
- target . removeEventListener ( "pointermove" , move ) ;
347
- target . removeEventListener ( "pointerleave" , out ) ;
348
- target . removeEventListener ( "click" , copy ) ;
349
- target . removeAttribute ( "data-copy" ) ;
350
- }
351
- function move ( { offsetX : x } ) {
352
- if ( 30 + x > parseInt ( getComputedStyle ( target ) . width ) ) {
353
- if ( ! target . getAttribute ( "data-copy" ) ) {
354
- target . setAttribute ( "data-copy" , "copy" ) ;
355
- target . addEventListener ( "click" , copy ) ;
356
- }
357
- } else {
358
- if ( target . getAttribute ( "data-copy" ) ) {
359
- target . removeAttribute ( "data-copy" ) ;
360
- target . removeEventListener ( "click" , copy ) ;
361
- }
339
+ for ( const pre of document . querySelectorAll ( "pre" ) ) {
340
+ pre . addEventListener ( "pointermove" , copymove ) ;
341
+ pre . addEventListener ( "pointerleave" , copyleave ) ;
342
+ }
343
+
344
+ function copyleave ( { currentTarget} ) {
345
+ currentTarget . removeEventListener ( "click" , copy ) ;
346
+ currentTarget . removeAttribute ( "data-copy" ) ;
347
+ }
348
+
349
+ function copymove ( { currentTarget, offsetX} ) {
350
+ if ( 60 + offsetX > currentTarget . clientWidth ) {
351
+ if ( ! currentTarget . hasAttribute ( "data-copy" ) ) {
352
+ currentTarget . setAttribute ( "data-copy" , "copy" ) ;
353
+ currentTarget . addEventListener ( "click" , copy ) ;
362
354
}
363
- }
364
- async function copy ( ) {
365
- try {
366
- await navigator . clipboard . writeText ( target . textContent ) ;
367
- target . setAttribute ( "data-copy" , "copied" ) ;
368
- } catch {
369
- target . setAttribute ( "data-copy" , "error" ) ;
355
+ } else {
356
+ if ( currentTarget . hasAttribute ( "data-copy" ) ) {
357
+ currentTarget . removeAttribute ( "data-copy" ) ;
358
+ currentTarget . removeEventListener ( "click" , copy ) ;
370
359
}
371
360
}
372
- } ) ;
361
+ }
362
+
363
+ async function copy ( { currentTarget} ) {
364
+ try {
365
+ await navigator . clipboard . writeText ( currentTarget . textContent ) ;
366
+ currentTarget . setAttribute ( "data-copy" , "copied" ) ;
367
+ } catch {
368
+ currentTarget . setAttribute ( "data-copy" , "error" ) ;
369
+ }
370
+ }
0 commit comments