力皓 0aafafe6ea fix: i18n 绑定变量后消失
fix: block 区块组件无法显示
2020-09-23 21:38:58 +08:00

59 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import Env from './env';
import { isJSSlot, isI18nData, isJSExpression } from '@ali/lowcode-types';
import { isPlainObject } from '@ali/lowcode-utils';
import i18nUtil from './i18n-util';
import { editor } from './editor';
import { isVariable } from './utils';
// FIXME: 表达式使用 mock 值未来live 模式直接使用原始值
// TODO: designType
export function deepValueParser(obj?: any): any {
if (isJSExpression(obj)) {
if (editor.get('designMode') === 'live') {
return obj;
}
obj = obj.mock;
}
// 兼容 ListSetter 中的变量结构
if (isVariable(obj)) {
if (editor.get('designMode') === 'live') {
return {
type: 'JSExpression',
value: obj.variable,
mock: obj.value,
};
}
obj = obj.value;
}
if (!obj) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map((item) => deepValueParser(item));
}
if (isPlainObject(obj)) {
if (isI18nData(obj)) {
// FIXME! use editor.get
let locale = Env.getLocale();
if (obj.key) {
// FIXME: 此处需要升级I18nUtil改成响应式
return i18nUtil.get(obj.key, locale);
}
if (locale !== 'zh_CN' && locale !== 'zh_TW' && !obj[locale]) {
locale = 'en_US';
}
return obj[obj.use || locale] || obj.zh_CN;
}
if (isJSSlot(obj)) {
return obj;
}
const out: any = {};
Object.keys(obj).forEach((key) => {
out[key] = deepValueParser(obj[key]);
});
return out;
}
return obj;
}