docs/.vitepress/theme/components/tabs/state.ts

114 lines · 2900 bytes

11 symbols in this file
  1import type { App, ComputedRef, InjectionKey, Ref } from 'vue'
  2import { computed, inject, onMounted, provide, reactive, ref, watch } from 'vue'
  3
  4const sharedStateKey: InjectionKey<{ content?: Record<string, string> }> =
  5  Symbol('vitepress:tabSharedState')
  6const singleStateKey: InjectionKey<{
  7  selected: ComputedRef<string>
  8  uid: string
  9}> = Symbol('vitepress:tabSingleState')
 10const renderAllKey: InjectionKey<boolean> = Symbol('wotaku:tabsRenderAll')
 11
 12const localStorageKey = 'vitepress:tabsSharedState'
 13const ls = typeof localStorage !== 'undefined' ? localStorage : null
 14
 15function getLocalStorageValue() {
 16  const rawValue = ls?.getItem(localStorageKey)
 17  if (rawValue) {
 18    try {
 19      return JSON.parse(rawValue)
 20    } catch {
 21      /* ignore invalid stored state */
 22    }
 23  }
 24  return {}
 25}
 26
 27function setLocalStorageValue(value: Record<string, string>) {
 28  if (!ls) return
 29  ls.setItem(localStorageKey, JSON.stringify(value))
 30}
 31
 32export function provideTabsSharedState(app: App, renderAll = false) {
 33  const state = reactive<{ content?: Record<string, string> }>({})
 34
 35  watch(
 36    () => state.content,
 37    (newStateContent, oldStateContent) => {
 38      if (newStateContent && oldStateContent) {
 39        setLocalStorageValue(newStateContent)
 40      }
 41    },
 42    { deep: true }
 43  )
 44
 45  app.provide(sharedStateKey, state)
 46  app.provide(renderAllKey, renderAll)
 47}
 48
 49export function useTabsSelectedState(
 50  acceptValues: ComputedRef<string[]>,
 51  sharedStateName: Ref<string | undefined>
 52) {
 53  const sharedState = inject(sharedStateKey)
 54  if (!sharedState) {
 55    throw new Error('[wotaku-tabs] TabsSharedState should be injected')
 56  }
 57
 58  onMounted(() => {
 59    if (!sharedState.content) sharedState.content = getLocalStorageValue()
 60  })
 61
 62  const nonSharedState = ref<string>()
 63  const selected = computed({
 64    get() {
 65      const key = sharedStateName.value
 66      const values = acceptValues.value
 67
 68      if (key) {
 69        const value = sharedState.content?.[key]
 70        if (value && values.includes(value)) return value
 71      } else if (
 72        nonSharedState.value && values.includes(nonSharedState.value)
 73      ) {
 74        return nonSharedState.value
 75      }
 76
 77      return values[0]
 78    },
 79    set(value) {
 80      const key = sharedStateName.value
 81      if (key) {
 82        if (sharedState.content) sharedState.content[key] = value
 83      } else {
 84        nonSharedState.value = value
 85      }
 86    }
 87  })
 88
 89  return {
 90    selected,
 91    select(value: string) {
 92      selected.value = value
 93    }
 94  }
 95}
 96
 97export function provideTabsSingleState(state: {
 98  selected: ComputedRef<string>
 99  uid: string
100}) {
101  provide(singleStateKey, state)
102}
103
104export function useTabsSingleState() {
105  const state = inject(singleStateKey)
106  if (!state) {
107    throw new Error('[wotaku-tabs] TabsSingleState should be injected')
108  }
109  return state
110}
111
112export function useTabsRenderAll() {
113  return inject(renderAllKey, false)
114}