2022-03-25 15:35:56 +08:00
|
|
|
import { computed, getCurrentInstance, inject, provide, ref, unref } from 'vue'
|
2021-11-05 18:10:07 +08:00
|
|
|
import { configProviderContextKey } from '@element-plus/tokens'
|
2022-02-11 11:03:15 +08:00
|
|
|
import { debugWarn, keysOf } from '@element-plus/utils'
|
2022-01-01 13:43:08 +08:00
|
|
|
import type { MaybeRef } from '@vueuse/core'
|
2022-03-25 15:35:56 +08:00
|
|
|
import type { App, Ref } from 'vue'
|
2021-12-01 22:26:57 +08:00
|
|
|
import type { ConfigProviderContext } from '@element-plus/tokens'
|
2021-11-05 18:10:07 +08:00
|
|
|
|
2022-01-08 19:36:13 +08:00
|
|
|
// this is meant to fix global methods like `ElMessage(opts)`, this way we can inject current locale
|
|
|
|
// into the component as default injection value.
|
|
|
|
// refer to: https://github.com/element-plus/element-plus/issues/2610#issuecomment-887965266
|
2022-02-09 16:31:31 +08:00
|
|
|
const globalConfig = ref<ConfigProviderContext>()
|
2022-01-01 13:43:08 +08:00
|
|
|
|
2022-02-09 16:31:31 +08:00
|
|
|
export function useGlobalConfig<
|
|
|
|
K extends keyof ConfigProviderContext,
|
|
|
|
D extends ConfigProviderContext[K]
|
|
|
|
>(
|
|
|
|
key: K,
|
|
|
|
defaultValue?: D
|
|
|
|
): Ref<Exclude<ConfigProviderContext[K], undefined> | D>
|
2022-01-01 13:43:08 +08:00
|
|
|
export function useGlobalConfig(): Ref<ConfigProviderContext>
|
2022-02-09 16:31:31 +08:00
|
|
|
export function useGlobalConfig(
|
|
|
|
key?: keyof ConfigProviderContext,
|
|
|
|
defaultValue = undefined
|
|
|
|
) {
|
|
|
|
const config = getCurrentInstance()
|
|
|
|
? inject(configProviderContextKey, globalConfig)
|
|
|
|
: globalConfig
|
2021-12-10 17:21:01 +08:00
|
|
|
if (key) {
|
2022-02-09 16:31:31 +08:00
|
|
|
return computed(() => config.value?.[key] ?? defaultValue)
|
2021-12-10 17:21:01 +08:00
|
|
|
} else {
|
|
|
|
return config
|
|
|
|
}
|
2021-11-05 18:10:07 +08:00
|
|
|
}
|
2022-01-01 13:43:08 +08:00
|
|
|
|
|
|
|
export const provideGlobalConfig = (
|
|
|
|
config: MaybeRef<ConfigProviderContext>,
|
2022-02-09 16:31:31 +08:00
|
|
|
app?: App,
|
|
|
|
global = false
|
2022-01-01 13:43:08 +08:00
|
|
|
) => {
|
|
|
|
const inSetup = !!getCurrentInstance()
|
|
|
|
const oldConfig = inSetup ? useGlobalConfig() : undefined
|
|
|
|
|
|
|
|
const provideFn = app?.provide ?? (inSetup ? provide : undefined)
|
|
|
|
if (!provideFn) {
|
|
|
|
debugWarn(
|
|
|
|
'provideGlobalConfig',
|
|
|
|
'provideGlobalConfig() can only be used inside setup().'
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
const context = computed(() => {
|
|
|
|
const cfg = unref(config)
|
2022-02-09 16:31:31 +08:00
|
|
|
if (!oldConfig?.value) return cfg
|
2022-02-11 11:03:15 +08:00
|
|
|
return mergeConfig(oldConfig.value, cfg)
|
2022-01-01 13:43:08 +08:00
|
|
|
})
|
|
|
|
provideFn(configProviderContextKey, context)
|
2022-02-09 16:31:31 +08:00
|
|
|
if (global || !globalConfig.value) {
|
|
|
|
globalConfig.value = context.value
|
|
|
|
}
|
2022-01-01 13:43:08 +08:00
|
|
|
return context
|
|
|
|
}
|
2022-02-11 11:03:15 +08:00
|
|
|
|
|
|
|
const mergeConfig = (
|
|
|
|
a: ConfigProviderContext,
|
|
|
|
b: ConfigProviderContext
|
|
|
|
): ConfigProviderContext => {
|
|
|
|
const keys = [...new Set([...keysOf(a), ...keysOf(b)])]
|
|
|
|
const obj = {}
|
|
|
|
for (const key of keys) {
|
|
|
|
obj[key] = b[key] ?? a[key]
|
|
|
|
}
|
|
|
|
return obj
|
|
|
|
}
|