docs/.vitepress/theme/composables/medium-zoom.ts

58b0371c0fd4c50247aad75469b86607f89656a1
wotaku/docs/.vitepress/theme/composables/medium-zoom.ts

38 lines · 1125 bytes

 1/**
 2*  All Rights Reserved
 3*
 4*  Copyright (c) 2025 taskylizard
 5*
 6*  All rights reserved. This code and its associated files may not be copied, modified, distributed, sublicensed, or used in any form, in whole or in part, without prior written permission from the copyright holder.
 7*/
 8import mediumZoom from 'medium-zoom'
 9import { inject, nextTick, onMounted, watch } from 'vue'
10import type { Zoom } from 'medium-zoom'
11import type { App, InjectionKey } from 'vue'
12import type { Router } from 'vitepress'
13
14declare module 'medium-zoom' {
15  interface Zoom {
16    refresh: (selector?: string) => void
17  }
18}
19
20export const mediumZoomSymbol: InjectionKey<Zoom> = Symbol('mediumZoom')
21
22export function useMediumZoom() {
23  return onMounted(() => inject(mediumZoomSymbol)?.refresh())
24}
25
26export function createMediumZoomProvider(app: App, router: Router) {
27  if (import.meta.env.SSR) return
28  const zoom = mediumZoom()
29  zoom.refresh = () => {
30    zoom.detach()
31    zoom.attach(':not(a) > img:not(.image-src)')
32  }
33  app.provide(mediumZoomSymbol, zoom)
34  watch(
35    () => router.route.path,
36    () => nextTick(() => zoom.refresh())
37  )
38}