1
1
import { showTemporaryTooltip } from '../modules/tippy.js' ;
2
2
import { toAbsoluteUrl } from '../utils.js' ;
3
+ import { clippie } from 'clippie' ;
3
4
4
5
const { copy_success, copy_error} = window . config . i18n ;
5
6
6
- export async function copyToClipboard ( content ) {
7
- if ( content instanceof Blob ) {
8
- const item = new ClipboardItem ( { [ content . type ] : content } ) ;
9
- await navigator . clipboard . write ( [ item ] ) ;
10
- } else { // text
11
- try {
12
- await navigator . clipboard . writeText ( content ) ;
13
- } catch {
14
- return fallbackCopyToClipboard ( content ) ;
15
- }
16
- }
17
- return true ;
18
- }
19
-
20
- // Fallback to use if navigator.clipboard doesn't exist. Achieved via creating
21
- // a temporary textarea element, selecting the text, and using document.execCommand
22
- function fallbackCopyToClipboard ( text ) {
23
- if ( ! document . execCommand ) return false ;
24
-
25
- const tempTextArea = document . createElement ( 'textarea' ) ;
26
- tempTextArea . value = text ;
27
-
28
- // avoid scrolling
29
- tempTextArea . style . top = 0 ;
30
- tempTextArea . style . left = 0 ;
31
- tempTextArea . style . position = 'fixed' ;
32
-
33
- document . body . appendChild ( tempTextArea ) ;
34
-
35
- tempTextArea . select ( ) ;
36
-
37
- // if unsecure (not https), there is no navigator.clipboard, but we can still
38
- // use document.execCommand to copy to clipboard
39
- const success = document . execCommand ( 'copy' ) ;
40
-
41
- document . body . removeChild ( tempTextArea ) ;
42
-
43
- return success ;
44
- }
45
-
46
7
// For all DOM elements with [data-clipboard-target] or [data-clipboard-text],
47
8
// this copy-to-clipboard will work for them
48
9
export function initGlobalCopyToClipboardListener ( ) {
@@ -61,7 +22,7 @@ export function initGlobalCopyToClipboardListener() {
61
22
e . preventDefault ( ) ;
62
23
63
24
( async ( ) => {
64
- const success = await copyToClipboard ( text ) ;
25
+ const success = await clippie ( text ) ;
65
26
showTemporaryTooltip ( target , success ? copy_success : copy_error ) ;
66
27
} ) ( ) ;
67
28
0 commit comments