-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathuseMediumZoom.ts
More file actions
32 lines (28 loc) · 890 Bytes
/
useMediumZoom.ts
File metadata and controls
32 lines (28 loc) · 890 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import mediumZoom from 'medium-zoom'
import { inject, nextTick, onMounted, watch } from 'vue'
import type { Zoom } from 'medium-zoom'
import type { App, InjectionKey } from 'vue'
import type { Router } from 'vitepress'
declare module 'medium-zoom' {
interface Zoom {
refresh: (selector?: string) => void
}
}
export const mediumZoomSymbol: InjectionKey<Zoom> = Symbol('mediumZoom')
export function useMediumZoom() {
return onMounted(() => inject(mediumZoomSymbol)?.refresh())
}
export function createMediumZoomProvider(app: App, router: Router) {
if (import.meta.env.SSR)
return
const zoom = mediumZoom()
zoom.refresh = () => {
zoom.detach()
zoom.attach(':not(a) > img:not(.image-src)')
}
app.provide(mediumZoomSymbol, zoom)
watch(
() => router.route.path,
() => nextTick(() => zoom.refresh()),
)
}