@ -1,48 +1,9 @@
import { showTemporaryTooltip } from '../modules/tippy.js' ;
import { showTemporaryTooltip } from '../modules/tippy.js' ;
import { toAbsoluteUrl } from '../utils.js' ;
import { toAbsoluteUrl } from '../utils.js' ;
import { clippie } from 'clippie' ;
const { copy _success , copy _error } = window . config . i18n ;
const { copy _success , copy _error } = window . config . i18n ;
export async function copyToClipboard ( content ) {
if ( content instanceof Blob ) {
const item = new ClipboardItem ( { [ content . type ] : content } ) ;
await navigator . clipboard . write ( [ item ] ) ;
} else { // text
try {
await navigator . clipboard . writeText ( content ) ;
} catch {
return fallbackCopyToClipboard ( content ) ;
}
}
return true ;
}
// Fallback to use if navigator.clipboard doesn't exist. Achieved via creating
// a temporary textarea element, selecting the text, and using document.execCommand
function fallbackCopyToClipboard ( text ) {
if ( ! document . execCommand ) return false ;
const tempTextArea = document . createElement ( 'textarea' ) ;
tempTextArea . value = text ;
// avoid scrolling
tempTextArea . style . top = 0 ;
tempTextArea . style . left = 0 ;
tempTextArea . style . position = 'fixed' ;
document . body . appendChild ( tempTextArea ) ;
tempTextArea . select ( ) ;
// if unsecure (not https), there is no navigator.clipboard, but we can still
// use document.execCommand to copy to clipboard
const success = document . execCommand ( 'copy' ) ;
document . body . removeChild ( tempTextArea ) ;
return success ;
}
// For all DOM elements with [data-clipboard-target] or [data-clipboard-text],
// For all DOM elements with [data-clipboard-target] or [data-clipboard-text],
// this copy-to-clipboard will work for them
// this copy-to-clipboard will work for them
export function initGlobalCopyToClipboardListener ( ) {
export function initGlobalCopyToClipboardListener ( ) {
@ -61,7 +22,7 @@ export function initGlobalCopyToClipboardListener() {
e . preventDefault ( ) ;
e . preventDefault ( ) ;
( async ( ) => {
( async ( ) => {
const success = await c opyToClipboard ( text ) ;
const success = await c lippie ( text ) ;
showTemporaryTooltip ( target , success ? copy _success : copy _error ) ;
showTemporaryTooltip ( target , success ? copy _success : copy _error ) ;
} ) ( ) ;
} ) ( ) ;