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

9c6588973e856dbc27ebb283f0d963c792faa8df
wotaku/docs/.vitepress/theme/composables/medium-zoom.ts history · blame · raw

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}