a39cb4e764985faba1e629aae5380b21a6397d0995ef5c92db2cc64313408f9bff0c848c61dfbbf927a287375287e51840854a4daafd897c848b3cf720e80a 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { ref, getCurrentInstance, inject, computed, unref, provide } from 'vue';
  2. import { configProviderContextKey } from '../constants.mjs';
  3. import { useNamespace, defaultNamespace, namespaceContextKey } from '../../../../hooks/use-namespace/index.mjs';
  4. import { useZIndex, defaultInitialZIndex, zIndexContextKey } from '../../../../hooks/use-z-index/index.mjs';
  5. import { useLocale, localeContextKey } from '../../../../hooks/use-locale/index.mjs';
  6. import { SIZE_INJECTION_KEY } from '../../../../hooks/use-size/index.mjs';
  7. import { emptyValuesContextKey } from '../../../../hooks/use-empty-values/index.mjs';
  8. import { keysOf } from '../../../../utils/objects.mjs';
  9. const globalConfig = ref();
  10. function useGlobalConfig(key, defaultValue = void 0) {
  11. const config = getCurrentInstance() ? inject(configProviderContextKey, globalConfig) : globalConfig;
  12. if (key) {
  13. return computed(() => {
  14. var _a, _b;
  15. return (_b = (_a = config.value) == null ? void 0 : _a[key]) != null ? _b : defaultValue;
  16. });
  17. } else {
  18. return config;
  19. }
  20. }
  21. function useGlobalComponentSettings(block, sizeFallback) {
  22. const config = useGlobalConfig();
  23. const ns = useNamespace(block, computed(() => {
  24. var _a;
  25. return ((_a = config.value) == null ? void 0 : _a.namespace) || defaultNamespace;
  26. }));
  27. const locale = useLocale(computed(() => {
  28. var _a;
  29. return (_a = config.value) == null ? void 0 : _a.locale;
  30. }));
  31. const zIndex = useZIndex(computed(() => {
  32. var _a;
  33. return ((_a = config.value) == null ? void 0 : _a.zIndex) || defaultInitialZIndex;
  34. }));
  35. const size = computed(() => {
  36. var _a;
  37. return unref(sizeFallback) || ((_a = config.value) == null ? void 0 : _a.size) || "";
  38. });
  39. provideGlobalConfig(computed(() => unref(config) || {}));
  40. return {
  41. ns,
  42. locale,
  43. zIndex,
  44. size
  45. };
  46. }
  47. const provideGlobalConfig = (config, app, global = false) => {
  48. var _a;
  49. const inSetup = !!getCurrentInstance();
  50. const oldConfig = inSetup ? useGlobalConfig() : void 0;
  51. const provideFn = (_a = app == null ? void 0 : app.provide) != null ? _a : inSetup ? provide : void 0;
  52. if (!provideFn) {
  53. return;
  54. }
  55. const context = computed(() => {
  56. const cfg = unref(config);
  57. if (!(oldConfig == null ? void 0 : oldConfig.value))
  58. return cfg;
  59. return mergeConfig(oldConfig.value, cfg);
  60. });
  61. provideFn(configProviderContextKey, context);
  62. provideFn(localeContextKey, computed(() => context.value.locale));
  63. provideFn(namespaceContextKey, computed(() => context.value.namespace));
  64. provideFn(zIndexContextKey, computed(() => context.value.zIndex));
  65. provideFn(SIZE_INJECTION_KEY, {
  66. size: computed(() => context.value.size || "")
  67. });
  68. provideFn(emptyValuesContextKey, computed(() => ({
  69. emptyValues: context.value.emptyValues,
  70. valueOnClear: context.value.valueOnClear
  71. })));
  72. if (global || !globalConfig.value) {
  73. globalConfig.value = context.value;
  74. }
  75. return context;
  76. };
  77. const mergeConfig = (a, b) => {
  78. const keys = [.../* @__PURE__ */ new Set([...keysOf(a), ...keysOf(b)])];
  79. const obj = {};
  80. for (const key of keys) {
  81. obj[key] = b[key] !== void 0 ? b[key] : a[key];
  82. }
  83. return obj;
  84. };
  85. export { provideGlobalConfig, useGlobalComponentSettings, useGlobalConfig };
  86. //# sourceMappingURL=use-global-config.mjs.map