File tree Expand file tree Collapse file tree 1 file changed +20
-14
lines changed Expand file tree Collapse file tree 1 file changed +20
-14
lines changed Original file line number Diff line number Diff line change @@ -18,20 +18,26 @@ function onError(btn) {
18
18
19
19
export default function initGlobalCopyToClipboardListener ( ) {
20
20
document . addEventListener ( 'click' , async ( e ) => {
21
- const target = e . target ;
22
- let text ;
23
- if ( target . dataset . clipboardText ) {
24
- text = target . dataset . clipboardText ;
25
- } else if ( target . dataset . clipboardTarget ) {
26
- text = document . querySelector ( target . dataset . clipboardTarget ) ?. value ;
27
- }
28
- if ( ! text ) return ;
29
-
30
- try {
31
- await navigator . clipboard . writeText ( text ) ;
32
- onSuccess ( target ) ;
33
- } catch {
34
- onError ( target ) ;
21
+ let target = e . target ;
22
+ // in case <button data-clipboard-text><svg></button>, so we just search up to 3 levels for performance.
23
+ for ( let i = 0 ; i < 3 && target ; i ++ ) {
24
+ let text ;
25
+ if ( target . dataset . clipboardText ) {
26
+ text = target . dataset . clipboardText ;
27
+ } else if ( target . dataset . clipboardTarget ) {
28
+ text = document . querySelector ( target . dataset . clipboardTarget ) ?. value ;
29
+ }
30
+ if ( text ) {
31
+ try {
32
+ await navigator . clipboard . writeText ( text ) ;
33
+ onSuccess ( target ) ;
34
+ } catch {
35
+ onError ( target ) ;
36
+ }
37
+ e . preventDefault ( ) ;
38
+ break ;
39
+ }
40
+ target = target . parentElement ;
35
41
}
36
42
} ) ;
37
43
}
You can’t perform that action at this time.
0 commit comments