Skip to content

Commit

Permalink
chore(plugin-copy-code): tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Feb 21, 2024
1 parent 0b8fff7 commit 47a1aec
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions plugins/plugin-copy-code/src/client/composables/useCopyCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import { usePageData } from 'vuepress/client'
import type { CopyCodePluginLocaleConfig } from '../../shared/index.js'
import { isMobile } from '../utils/index.js'

const timeoutIdMap = new Map<HTMLElement, number>()

export interface UseCopyCodeOptions {
locales: CopyCodePluginLocaleConfig
selector: string[]
Expand All @@ -26,7 +24,10 @@ export const useCopyCode = ({
selector,
showInMobile,
}: UseCopyCodeOptions): void => {
const { copy } = useClipboard({ legacy: true })
const { copy, copied } = useClipboard({
legacy: true,
copiedDuring: duration,
})
const locale = useLocaleConfig(locales)
const page = usePageData()

Expand Down Expand Up @@ -77,15 +78,15 @@ export const useCopyCode = ({

copy(text).then(() => {
button.classList.add('copied')
clearTimeout(timeoutIdMap.get(button))

const timeoutId = setTimeout(() => {
button.classList.remove('copied')
button.blur()
timeoutIdMap.delete(button)
}, duration) as unknown as number

timeoutIdMap.set(button, timeoutId)
watch(
copied,
() => {
button.classList.remove('copied')
button.blur()
},
{ once: true },
)
})
}

Expand Down

0 comments on commit 47a1aec

Please sign in to comment.