Skip to content

Commit

Permalink
feat(helper): add wait utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Feb 21, 2024
1 parent 28f29ba commit 75a409a
Show file tree
Hide file tree
Showing 8 changed files with 18 additions and 9 deletions.
10 changes: 5 additions & 5 deletions plugins/plugin-copy-code/src/client/composables/useCopyCode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useLocaleConfig } from '@vuepress/helper/client'
import { useLocaleConfig, wait } from '@vuepress/helper/client'
import { useClipboard, useEventListener } from '@vueuse/core'
import { nextTick, onMounted, watch } from 'vue'
import { usePageData } from 'vuepress/client'
Expand Down Expand Up @@ -51,13 +51,13 @@ export const useCopyCode = ({
}

const appendCopyButton = (): void => {
nextTick().then(() =>
setTimeout(() => {
nextTick()
.then(() => wait(delay))
.then(() => {
selector.forEach((item) => {
document.querySelectorAll<HTMLElement>(item).forEach(insertCopyButton)
})
}, delay),
)
})
}

const copyContent = (
Expand Down
1 change: 1 addition & 0 deletions plugins/plugin-medium-zoom/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"copy": "cpx \"src/**/*.css\" lib"
},
"dependencies": {
"@vuepress/helper": "workspace:~",
"medium-zoom": "^1.1.0",
"vue": "^3.4.19"
},
Expand Down
3 changes: 2 additions & 1 deletion plugins/plugin-medium-zoom/src/client/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { wait } from '@vuepress/helper/client'
import mediumZoom from 'medium-zoom'
import type { ZoomOptions } from 'medium-zoom'
import { defineClientConfig } from 'vuepress/client'
Expand Down Expand Up @@ -28,7 +29,7 @@ export default defineClientConfig({
app.provide(mediumZoomSymbol, zoom)

router.afterEach(() => {
setTimeout(() => zoom.refresh(), delay)
wait(delay).then(() => zoom.refresh())
})
},
}) as ClientConfig
3 changes: 2 additions & 1 deletion plugins/plugin-medium-zoom/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
"outDir": "./lib",
"types": ["vuepress/client-types"]
},
"include": ["./src"]
"include": ["./src"],
"references": [{ "path": "../../tools/helper/tsconfig.build.json" }]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isString, useLocaleConfig } from '@vuepress/helper/client'
import { isString, useLocaleConfig, wait } from '@vuepress/helper/client'
import { nextTick, onMounted, onUnmounted, watch } from 'vue'
import { usePageData, usePageFrontmatter } from 'vuepress/client'
import type { PhotoSwipePluginLocaleData } from '../../shared/index.js'
Expand Down Expand Up @@ -38,7 +38,7 @@ export const usePhotoSwipe = ({

if (photoSwipe !== false)
nextTick()
.then(() => new Promise<void>((resolve) => setTimeout(resolve, delay)))
.then(() => wait(delay))
.then(async () => {
const imageSelector = isString(photoSwipe) ? photoSwipe : selector

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions tools/helper/src/client/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './data.js'
export * from './hasGlobalComponent.js'
export * from './wait.js'
2 changes: 2 additions & 0 deletions tools/helper/src/client/utils/wait.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const wait = (ms: number): Promise<void> =>
new Promise<void>((resolve) => setTimeout(resolve, ms))

0 comments on commit 75a409a

Please sign in to comment.