fix(theme): keep display copied hint when click multiple times (#1262)

pull/1268/head
CHOYSEN 2 years ago committed by GitHub
parent 12591a9487
commit bb11d0f178
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -2,6 +2,7 @@ import { inBrowser } from '../utils.js'
export function useCopyCode() { export function useCopyCode() {
if (inBrowser) { if (inBrowser) {
const timeoutIdMap: Map<HTMLElement, number> = new Map()
window.addEventListener('click', (e) => { window.addEventListener('click', (e) => {
const el = e.target as HTMLElement const el = e.target as HTMLElement
if (el.matches('div[class*="language-"] > button.copy')) { if (el.matches('div[class*="language-"] > button.copy')) {
@ -24,10 +25,13 @@ export function useCopyCode() {
copyToClipboard(text).then(() => { copyToClipboard(text).then(() => {
el.classList.add('copied') el.classList.add('copied')
setTimeout(() => { clearTimeout(timeoutIdMap.get(el))
const timeoutId = setTimeout(() => {
el.classList.remove('copied') el.classList.remove('copied')
el.blur() el.blur()
timeoutIdMap.delete(el)
}, 2000) }, 2000)
timeoutIdMap.set(el, timeoutId)
}) })
} }
}) })

Loading…
Cancel
Save