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

707e3cae13fffe04c951fdaec4935671b635a9fb
wotaku/docs/.vitepress/theme/composables/medium-zoom.ts

38 lines · 1131 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 type { Zoom } from 'medium-zoom'
10import type { Router } from 'vitepress'
11import { inject, nextTick, onMounted, watch } from 'vue'
12import type { App, InjectionKey } from 'vue'
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}