yanzhen-design-vue
Version:
53 lines (52 loc) • 1.85 kB
JavaScript
;
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
const vue = require("vue");
const utils = require("@yanzhen-libs/utils");
const core = require("@yanzhen-lowcode/core");
const constants = require("../constants.cjs");
const globalConfig = vue.ref();
function useInjectGlobalConfig(key, defaultValue = void 0) {
const config = vue.getCurrentInstance() ? vue.inject(constants.configProviderContextKey, globalConfig) : globalConfig;
if (key) {
return vue.computed(() => {
var _a;
return ((_a = config.value) == null ? void 0 : _a[key]) ?? defaultValue;
});
} else {
return config;
}
}
const message = "provideGlobalConfig() can only be used inside setup().";
const provideGlobalConfig = (config, app, global = false) => {
const inSetup = !!vue.getCurrentInstance();
if (!inSetup) {
core.warn(`provideGlobalConfig${message}`);
return;
}
const oldConfig = inSetup ? useInjectGlobalConfig() : void 0;
const provideFn = (app == null ? void 0 : app.provide) ?? vue.provide;
if (!provideFn) {
core.warn(`provideGlobalConfig${message}`);
return;
}
const context = vue.computed(() => {
const cfg = vue.unref(config);
if (!(oldConfig == null ? void 0 : oldConfig.value)) return cfg;
return mergeConfig(oldConfig == null ? void 0 : oldConfig.value, cfg);
});
provideFn(constants.configProviderContextKey, context);
if (global || !globalConfig.value) {
globalConfig.value = context.value;
}
return context;
};
const mergeConfig = (a, b) => {
const keys = [.../* @__PURE__ */ new Set([...utils.keysOf(a), ...utils.keysOf(b)])];
const obj = {};
for (const key of keys) {
obj[key] = b[key] ?? a[key];
}
return obj;
};
exports.provideGlobalConfig = provideGlobalConfig;
exports.useInjectGlobalConfig = useInjectGlobalConfig;