@@ -5,37 +5,30 @@ import {clippie} from 'clippie';
5
5
const { copy_success, copy_error} = window . config . i18n ;
6
6
7
7
// Enable clipboard copy from HTML attributes. These properties are supported:
8
- // - data-clipboard-text: Direct text to copy, has highest precedence
8
+ // - data-clipboard-text: Direct text to copy
9
9
// - data-clipboard-target: Holds a selector for a <input> or <textarea> whose content is copied
10
10
// - data-clipboard-text-type: When set to 'url' will convert relative to absolute urls
11
11
export function initGlobalCopyToClipboardListener ( ) {
12
- document . addEventListener ( 'click' , ( e ) => {
13
- let target = e . target ;
14
- // In case <button data-clipboard-text><svg></button>, so we just search
15
- // up to 3 levels for performance
16
- for ( let i = 0 ; i < 3 && target ; i ++ ) {
17
- let text = target . getAttribute ( 'data-clipboard-text' ) ;
12
+ document . addEventListener ( 'click' , async ( e ) => {
13
+ const target = e . target . closest ( '[data-clipboard-text], [data-clipboard-target]' ) ;
14
+ if ( ! target ) return ;
18
15
19
- if ( ! text && target . getAttribute ( 'data-clipboard-target' ) ) {
20
- text = document . querySelector ( target . getAttribute ( 'data-clipboard-target' ) ) ?. value ;
21
- }
16
+ e . preventDefault ( ) ;
22
17
23
- if ( text && target . getAttribute ( 'data-clipboard-text-type' ) === 'url' ) {
24
- text = toAbsoluteUrl ( text ) ;
25
- }
26
-
27
- if ( text ) {
28
- e . preventDefault ( ) ;
29
-
30
- ( async ( ) => {
31
- const success = await clippie ( text ) ;
32
- showTemporaryTooltip ( target , success ? copy_success : copy_error ) ;
33
- } ) ( ) ;
18
+ let text ;
19
+ if ( target . hasAttribute ( 'data-clipboard-text' ) ) {
20
+ text = target . getAttribute ( 'data-clipboard-text' ) ;
21
+ } else {
22
+ text = document . querySelector ( target . getAttribute ( 'data-clipboard-target' ) ) ?. value ;
23
+ }
34
24
35
- break ;
36
- }
25
+ if ( text && target . getAttribute ( 'data-clipboard-text-type' ) === 'url' ) {
26
+ text = toAbsoluteUrl ( text ) ;
27
+ }
37
28
38
- target = target . parentElement ;
29
+ if ( text ) {
30
+ const success = await clippie ( text ) ;
31
+ showTemporaryTooltip ( target , success ? copy_success : copy_error ) ;
39
32
}
40
33
} ) ;
41
34
}
0 commit comments