Merge branch fix/code-generator-children into release/1.0.0

Title: fix: children 在 schema 和 props 中并存的情况处理 

在 schema 里同时在节点上和 props 中存在 children 时,方案优化,保持与渲染模块逻辑一致

Link: https://code.aone.alibaba-inc.com/ali-lowcode/ali-lowcode-engine/codereview/3975915
This commit is contained in:
wuyue.xht 2020-10-26 14:03:16 +08:00
commit 81c80c6acb
3 changed files with 40 additions and 17 deletions

View File

@ -1,6 +1,9 @@
# 出码模块
详细介绍看这里:<https://yuque.antfin-inc.com/docs/share/2b342641-6e01-4c77-b8e0-30421f55f69b>
**重要!!! 本模块是 Node 端运行的!本模块是 Node 端运行的!本模块是 Node 端运行的!暂不支持 Web 端直接跑。**
如果有业务诉求需要在 Web 端运行,可以联系 @春希(armslave.yy),会在架构组讨论优先度。
详细介绍看这里:[出码模块 语雀文档](https://yuque.antfin.com/docs/share/2b342641-6e01-4c77-b8e0-30421f55f69b)
## 安装接入

View File

@ -3,7 +3,7 @@
* schema
*/
import changeCase from 'change-case';
import { UtilItem, NodeDataType, NodeSchema, ContainerSchema, ProjectSchema, PropsMap } from '@ali/lowcode-types';
import { UtilItem, NodeDataType, NodeSchema, ContainerSchema, ProjectSchema, PropsMap, NodeData } from '@ali/lowcode-types';
import { IPageMeta,
CodeGeneratorError,
CompatibilityError,
@ -46,6 +46,38 @@ function getRootComponentName(typeName: string, maps: Record<string, IExternalDe
return typeName;
}
function processChildren(schema: NodeSchema): void {
if (schema.props) {
if (Array.isArray(schema.props)) {
// FIXME: is array type props description
} else {
const nodeProps = schema.props as PropsMap;
if (nodeProps.children) {
if (!schema.children) {
schema.children = nodeProps.children as NodeDataType;
} else {
let _children: NodeData[] = [];
if (Array.isArray(schema.children)) {
_children = _children.concat(schema.children);
} else {
_children.push(schema.children);
}
if (Array.isArray(nodeProps.children)) {
_children = _children.concat(nodeProps.children as NodeData[]);
} else {
_children.push(nodeProps.children as NodeData);
}
schema.children = _children;
}
delete nodeProps.children;
}
}
}
}
class SchemaParser implements ISchemaParser {
validate(schema: ProjectSchema): boolean {
if (SUPPORT_SCHEMA_VERSION_LIST.indexOf(schema.version) < 0) {
@ -146,22 +178,10 @@ class SchemaParser implements ISchemaParser {
// 处理 children 写在了 props 里的情况
containers.forEach((container) => {
if (container.children) {
handleSubNodes<string>(
handleSubNodes<void>(
container.children,
{
node: (i: NodeSchema) => {
if (i.props) {
if (Array.isArray(i.props)) {
// FIXME: is array type props description
} else {
const nodeProps = i.props as PropsMap;
if (nodeProps.children && !i.children) {
i.children = nodeProps.children as NodeDataType;
}
}
}
return '';
},
node: (i: NodeSchema) => processChildren(i),
},
{
rerun: true,

View File

@ -53,5 +53,5 @@
"publishConfig": {
"registry": "http://registry.npm.alibaba-inc.com"
},
"homepage": "https://unpkg.alibaba-inc.com/@ali/lowcode-react-renderer@1.0.11/build/index.html"
"homepage": "https://unpkg.alibaba-inc.com/@ali/lowcode-react-renderer@1.0.12/build/index.html"
}