From bb11d0f17852a3f2a35339f765acffca526a9ee8 Mon Sep 17 00:00:00 2001 From: CHOYSEN Date: Wed, 31 Aug 2022 10:49:31 +0800 Subject: [PATCH] fix(theme): keep display copied hint when click multiple times (#1262) --- src/client/app/composables/copyCode.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/client/app/composables/copyCode.ts b/src/client/app/composables/copyCode.ts index 58b60807cb9c..9d768a23b48e 100644 --- a/src/client/app/composables/copyCode.ts +++ b/src/client/app/composables/copyCode.ts @@ -2,6 +2,7 @@ import { inBrowser } from '../utils.js' export function useCopyCode() { if (inBrowser) { + const timeoutIdMap: Map = new Map() window.addEventListener('click', (e) => { const el = e.target as HTMLElement if (el.matches('div[class*="language-"] > button.copy')) { @@ -24,10 +25,13 @@ export function useCopyCode() { copyToClipboard(text).then(() => { el.classList.add('copied') - setTimeout(() => { + clearTimeout(timeoutIdMap.get(el)) + const timeoutId = setTimeout(() => { el.classList.remove('copied') el.blur() + timeoutIdMap.delete(el) }, 2000) + timeoutIdMap.set(el, timeoutId) }) } })