1bfd8179d99c844401569587f91c3233893993707b61f43630e8fb9af238db4b035aee696a8881545d4ff20a2158d4aa7454683852de080c3a8072806a8a86 3.4 KB

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