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

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

46 lines · 1447 bytes

 1/**
 2 *  Copyright (c) 2024 taskylizard
 3 *
 4 *  Licensed under the Apache License, Version 2.0 (the "License");
 5 *  you may not use this file except in compliance with the License.
 6 *  You may obtain a copy of the License at
 7 *
 8 *  http://www.apache.org/licenses/LICENSE-2.0
 9 *
10 *  Unless required by applicable law or agreed to in writing, software
11 *  distributed under the License is distributed on an "AS IS" BASIS,
12 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 *  See the License for the specific language governing permissions and
14 *  limitations under the License.
15 */
16import mediumZoom from 'medium-zoom'
17import { inject, nextTick, onMounted, watch } from 'vue'
18import type { Zoom } from 'medium-zoom'
19import type { App, InjectionKey } from 'vue'
20import type { Router } from 'vitepress'
21
22declare module 'medium-zoom' {
23  interface Zoom {
24    refresh: (selector?: string) => void
25  }
26}
27
28export const mediumZoomSymbol: InjectionKey<Zoom> = Symbol('mediumZoom')
29
30export function useMediumZoom() {
31  return onMounted(() => inject(mediumZoomSymbol)?.refresh())
32}
33
34export function createMediumZoomProvider(app: App, router: Router) {
35  if (import.meta.env.SSR) return
36  const zoom = mediumZoom()
37  zoom.refresh = () => {
38    zoom.detach()
39    zoom.attach(':not(a) > img:not(.image-src)')
40  }
41  app.provide(mediumZoomSymbol, zoom)
42  watch(
43    () => router.route.path,
44    () => nextTick(() => zoom.refresh())
45  )
46}