xdesign-vue-next
Version:
XDesign Component for vue-next
74 lines (70 loc) • 2.39 kB
JavaScript
/**
* xdesign v1.0.6
* (c) 2023 xdesign
* @license MIT
*/
import { ref, getCurrentInstance, inject, computed, provide, h } from 'vue';
import isFunction from 'lodash/isFunction';
import cloneDeep from 'lodash/cloneDeep';
import isString from 'lodash/isString';
import { configProviderInjectKey, defaultGlobalConfig, mergeWith } from './context.js';
import 'lodash/mergeWith';
import 'lodash/merge';
import '../_common/js/global-config/default-config.js';
import '../_common/js/global-config/locale/en_US.js';
import '../_chunks/dep-3a1cce9f.js';
import 'lodash/isArray';
var globalConfigCopy = ref();
function useConfig(componentName) {
var injectGlobalConfig = getCurrentInstance() ? inject(configProviderInjectKey, null) : globalConfigCopy;
var mergedGlobalConfig = computed(function () {
return (injectGlobalConfig === null || injectGlobalConfig === void 0 ? void 0 : injectGlobalConfig.value) || defaultGlobalConfig;
});
var globalConfig = computed(function () {
return mergedGlobalConfig.value[componentName];
});
var classPrefix = computed(function () {
return mergedGlobalConfig.value.classPrefix;
});
var t = function t(pattern) {
for (var _len = arguments.length, args = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
var data = args[0];
if (isString(pattern)) {
if (!data) return pattern;
var regular = /\{\s*([\w-]+)\s*\}/g;
var translated = pattern.replace(regular, function (match, key) {
if (data) {
return String(data[key]);
}
return "";
});
return translated;
}
if (isFunction(pattern)) {
if (!args.length) return pattern(h);
return pattern.apply(void 0, args);
}
return "";
};
return {
t: t,
global: globalConfig,
globalConfig: globalConfig,
classPrefix: classPrefix
};
}
var provideConfig = function provideConfig(props) {
var defaultData = cloneDeep(defaultGlobalConfig);
var mergedGlobalConfig = computed(function () {
return mergeWith(defaultData, props.globalConfig);
});
provide(configProviderInjectKey, mergedGlobalConfig);
if (!globalConfigCopy.value) {
globalConfigCopy.value = mergedGlobalConfig.value;
}
return mergedGlobalConfig;
};
export { provideConfig, useConfig };
//# sourceMappingURL=useConfig.js.map