@macrowing/inbiz-codegen 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +592 -2
- package/bin/inbiz-codegen.mjs +7 -0
- package/package.json +32 -4
- package/scripts/prepare-templates.mjs +35 -0
- package/src/backend/init.mjs +170 -0
- package/src/config.mjs +150 -0
- package/src/favicon.mjs +100 -0
- package/src/generate.mjs +935 -0
- package/src/generated-output.mjs +107 -0
- package/src/index.mjs +346 -0
- package/src/menu/build.mjs +252 -0
- package/src/menu/check.mjs +163 -0
- package/src/menu/command.mjs +179 -0
- package/src/migrate-runtime.mjs +106 -0
- package/src/platform/client.mjs +167 -0
- package/src/platform/command.mjs +386 -0
- package/src/platform/pack.mjs +591 -0
- package/src/platform/recover.mjs +236 -0
- package/src/platform/startup.mjs +343 -0
- package/src/platform/state.mjs +116 -0
- package/src/platform/update.mjs +157 -0
- package/src/platform/zip.mjs +134 -0
- package/src/pull.mjs +284 -0
- package/src/scaffold/commands.mjs +349 -0
- package/src/scaffold/init.mjs +370 -0
- package/src/scaffold/template-files.mjs +40 -0
- package/src/scaffold/template-source.mjs +235 -0
- package/src/scaffold/template-sync.mjs +335 -0
- package/src/transform/heaviness.mjs +380 -0
- package/src/transform/preview-props.mjs +108 -0
- package/src/transform/published-page-links.mjs +61 -0
- package/src/transform/published-script.mjs +110 -0
- package/src/transform/registry.mjs +781 -0
- package/src/transform/schema-to-jsx.mjs +1708 -0
- package/src/util/des.mjs +10 -0
- package/templates/README.md +34 -0
- package/templates/backend/Directory.Build.props +19 -0
- package/templates/backend/Directory.Packages.props +49 -0
- package/templates/backend/Macrowing.Scaffold.slnx +13 -0
- package/templates/backend/host/Macrowing.Start/Init/app.json +8 -0
- package/templates/backend/host/Macrowing.Start/Init/menus.json +4 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationDefinition.cs +245 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationEndpoints.cs +57 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationHostedService.cs +63 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationOptions.cs +44 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationRunner.cs +294 -0
- package/templates/backend/host/Macrowing.Start/Initialization/InitializationState.cs +72 -0
- package/templates/backend/host/Macrowing.Start/Initialization/MenuFactory.cs +118 -0
- package/templates/backend/host/Macrowing.Start/Initialization/PackageBuilder.cs +317 -0
- package/templates/backend/host/Macrowing.Start/Initialization/PlatformAppImporter.cs +371 -0
- package/templates/backend/host/Macrowing.Start/Macrowing.Start.csproj +26 -0
- package/templates/backend/host/Macrowing.Start/Program.cs +33 -0
- package/templates/backend/host/Macrowing.Start/StartModule.cs +67 -0
- package/templates/backend/host/Macrowing.Start/appsettings.json +38 -0
- package/templates/backend/modules/Macrowing.Generated/Application/GeneratedApplicationAutoMapperProfile.cs +42 -0
- package/templates/backend/modules/Macrowing.Generated/Domain/Entities/GeneratedEntityBase.cs +72 -0
- package/templates/backend/modules/Macrowing.Generated/Domain/GeneratedConsts.cs +50 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContext.cs +55 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/GeneratedDbContextModelBuilderExtensions.cs +61 -0
- package/templates/backend/modules/Macrowing.Generated/EntityFrameworkCore/IGeneratedDbContext.cs +32 -0
- package/templates/backend/modules/Macrowing.Generated/GeneratedModule.cs +89 -0
- package/templates/backend/modules/Macrowing.Generated/Macrowing.Generated.csproj +22 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsClient.cs +95 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Cms/CmsModels.cs +81 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Dictionary/DictionaryClient.cs +44 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/InBizSdkModule.cs +16 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/InterfaceEngine/InterfaceEngineClient.cs +50 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Localization/LocalizationClient.cs +28 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Macrowing.InBiz.SDK.csproj +16 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageClient.cs +99 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Message/MessageModels.cs +63 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelClient.cs +131 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelQuery.cs +108 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Model/ModelWrite.cs +44 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Permission/PermissionClient.cs +86 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformException.cs +42 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformRegistration.cs +60 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizPlatformTransport.cs +96 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/Platform/InBizRequestContext.cs +27 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/README.md +117 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsClient.cs +35 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Details/WorkFlowDetailsQuery.cs +20 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationRequests.cs +105 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Operations/WorkFlowOperationsClient.cs +53 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/README.md +132 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Tracking/WorkFlowTrackingClient.cs +50 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeClient.cs +78 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Upgrade/WorkFlowUpgradeModels.cs +34 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowContracts.cs +88 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowRegistration.cs +59 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/WorkFlowTransport.cs +163 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchClient.cs +59 -0
- package/templates/backend/modules/Macrowing.InBiz.SDK/WorkFlow/Workbench/WorkFlowWorkbenchModels.cs +76 -0
- package/templates/backend/modules/Macrowing.OC.SDK/Macrowing.OC.SDK.csproj +20 -0
- package/templates/backend/modules/Macrowing.OC.SDK/OcSdkModule.cs +18 -0
- package/templates/backend/modules/Macrowing.OC.SDK/Protos/README.md +22 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/Macrowing.InBiz.SDK.Tests.csproj +14 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformContractTests.cs +288 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/PlatformLiveTests.cs +173 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowHttpServer.cs +113 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowLiveTests.cs +269 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowOperationsTests.cs +80 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowQueryTests.cs +163 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowTransportTests.cs +111 -0
- package/templates/backend/tests/Macrowing.InBiz.SDK.Tests/WorkFlowUpgradeTests.cs +87 -0
- package/templates/frontend/components/README.md +39 -0
- package/templates/frontend/components/app-components/README.md +33 -0
- package/templates/frontend/components/app-components/common/.gitkeep +0 -0
- package/templates/frontend/components/app-components/components/.gitkeep +0 -0
- package/templates/frontend/components/app-components/package.json +20 -0
- package/templates/frontend/mgt/.umirc.ts +574 -0
- package/templates/frontend/mgt/package.json +96 -0
- package/templates/frontend/mgt/src/app-extends.ts +9 -0
- package/templates/frontend/mgt/src/app.tsx +20 -0
- package/templates/frontend/mgt/src/config/app.ts +31 -0
- package/templates/frontend/mgt/src/layout/header.tsx +14 -0
- package/templates/frontend/mgt/src/layout/index.tsx +11 -0
- package/templates/frontend/mgt/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/mgt/src/locales.ts +8 -0
- package/templates/frontend/mgt/src/pages/.gitkeep +0 -0
- package/templates/frontend/mgt/src/root.tsx +12 -0
- package/templates/frontend/mgt/src/route-view.tsx +3 -0
- package/templates/frontend/mgt/src/runtime.ts +21 -0
- package/templates/frontend/mgt/src/styles/app.css +1 -0
- package/templates/frontend/mgt/src/styles/layout.less +79 -0
- package/templates/frontend/mgt/tsconfig.json +65 -0
- package/templates/frontend/mgt/typings.d.ts +57 -0
- package/templates/frontend/wap/.umirc.ts +574 -0
- package/templates/frontend/wap/package.json +73 -0
- package/templates/frontend/wap/src/app-extends.ts +9 -0
- package/templates/frontend/wap/src/app.tsx +28 -0
- package/templates/frontend/wap/src/config/app.ts +31 -0
- package/templates/frontend/wap/src/layout/header.tsx +14 -0
- package/templates/frontend/wap/src/layout/index.tsx +18 -0
- package/templates/frontend/wap/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/wap/src/locales.ts +8 -0
- package/templates/frontend/wap/src/pages/.gitkeep +0 -0
- package/templates/frontend/wap/src/root.tsx +12 -0
- package/templates/frontend/wap/src/route-view.tsx +3 -0
- package/templates/frontend/wap/src/runtime.ts +21 -0
- package/templates/frontend/wap/src/styles/adaptation.less +190 -0
- package/templates/frontend/wap/src/styles/app.css +7 -0
- package/templates/frontend/wap/src/styles/global.less +81 -0
- package/templates/frontend/wap/src/styles/layout.less +92 -0
- package/templates/frontend/wap/tsconfig.json +66 -0
- package/templates/frontend/wap/typings.d.ts +24 -0
- package/templates/frontend/web/.umirc.ts +574 -0
- package/templates/frontend/web/package.json +26 -0
- package/templates/frontend/web/src/app-extends.ts +9 -0
- package/templates/frontend/web/src/app.tsx +25 -0
- package/templates/frontend/web/src/config/app.ts +31 -0
- package/templates/frontend/web/src/layout/header.tsx +14 -0
- package/templates/frontend/web/src/layout/index.tsx +11 -0
- package/templates/frontend/web/src/layout/sidebar.tsx +14 -0
- package/templates/frontend/web/src/locales.ts +8 -0
- package/templates/frontend/web/src/root.tsx +12 -0
- package/templates/frontend/web/src/route-view.tsx +3 -0
- package/templates/frontend/web/src/runtime.ts +21 -0
- package/templates/frontend/web/src/styles/app.css +104 -0
- package/templates/frontend/web/src/styles/layout.less +124 -0
- package/templates/frontend/web/tsconfig.json +91 -0
- package/templates/frontend/web/typings.d.ts +46 -0
- package/templates/package.json +14 -0
|
@@ -0,0 +1,1708 @@
|
|
|
1
|
+
import fs from 'node:fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import { resolveComponent, explainMissingComponent } from './registry.mjs';
|
|
4
|
+
import { classifyComponent } from './heaviness.mjs';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Formily schema → 真实 JSX 代码。
|
|
8
|
+
*
|
|
9
|
+
* 出码的产物里不允许残留 schema:每个节点都翻译成组件标签 + 字面量 props,
|
|
10
|
+
* 这样代码可以被 AI 改写、被 git diff、被真正的开发者接走。
|
|
11
|
+
*
|
|
12
|
+
* 节点形态(来自 InBiz 的 LayoutInfo / 页面 schema):
|
|
13
|
+
* {
|
|
14
|
+
* type: 'string',
|
|
15
|
+
* 'x-decorator': 'Container',
|
|
16
|
+
* 'x-decorator-props': { style: {...} },
|
|
17
|
+
* 'x-component': 'InbizMenu',
|
|
18
|
+
* 'x-component-props': {...},
|
|
19
|
+
* 'x-component-biz-props': {...},
|
|
20
|
+
* title: { value: 'InbizMenu.title' },
|
|
21
|
+
* name, 'x-index',
|
|
22
|
+
* properties: { ...子节点... }
|
|
23
|
+
* }
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const IND = ' ';
|
|
27
|
+
const TRANSLATED_VALUE = Symbol('translated-value');
|
|
28
|
+
|
|
29
|
+
const configNamePart = text => String(text ?? '').split(/[^A-Za-z0-9]+/).filter(Boolean)
|
|
30
|
+
.map(word => word[0].toUpperCase() + word.slice(1)).join('');
|
|
31
|
+
|
|
32
|
+
/** 生成合法 JSX 属性名:连字符属性名需要引号 */
|
|
33
|
+
function attrName(name) {
|
|
34
|
+
return /^[A-Za-z_$][\w$]*$/.test(name) ? name : JSON.stringify(name);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** JSON 值 → JSX 字面量字面(对象/数组展开成可读形式) */
|
|
38
|
+
export function literal(value, indent = 0, depth = 0) {
|
|
39
|
+
if (value === null) return 'null';
|
|
40
|
+
if (value === undefined) return 'undefined';
|
|
41
|
+
if (typeof value === 'string') return JSON.stringify(value);
|
|
42
|
+
if (typeof value === 'number' || typeof value === 'boolean') return String(value);
|
|
43
|
+
|
|
44
|
+
const pad = IND.repeat(indent + 1);
|
|
45
|
+
const closePad = IND.repeat(indent);
|
|
46
|
+
|
|
47
|
+
if (Array.isArray(value)) {
|
|
48
|
+
if (value.length === 0) return '[]';
|
|
49
|
+
const items = value.map((v) => pad + literal(v, indent + 1, depth + 1));
|
|
50
|
+
return '[\n' + items.join(',\n') + ',\n' + closePad + ']';
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
if (typeof value === 'object') {
|
|
54
|
+
if (value[TRANSLATED_VALUE]) {
|
|
55
|
+
const [token, fallback] = value[TRANSLATED_VALUE];
|
|
56
|
+
return `translateAppText(${JSON.stringify(token)}, ${JSON.stringify(fallback)})`;
|
|
57
|
+
}
|
|
58
|
+
const entries = Object.entries(value);
|
|
59
|
+
if (entries.length === 0) return '{}';
|
|
60
|
+
const props = entries.map(
|
|
61
|
+
([k, v]) => pad + attrName(k) + ': ' + literal(v, indent + 1, depth + 1),
|
|
62
|
+
);
|
|
63
|
+
return '{\n' + props.join(',\n') + ',\n' + closePad + '}';
|
|
64
|
+
}
|
|
65
|
+
return 'undefined';
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function isScalar(v) {
|
|
69
|
+
return typeof v === 'string' || typeof v === 'number' || typeof v === 'boolean';
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* 判断「替代丢掉了哪些 props」时忽略的属性。
|
|
74
|
+
*
|
|
75
|
+
* 这些是平台渲染器的契约(数据绑定 model/modelend、控件标识 controlId、
|
|
76
|
+
* 容器 className/style),与具体组件实现无关:出码产物本来就没有 Formily
|
|
77
|
+
* 表单上下文,任何组件都拿不到它们。列进「替身不识别」只会淹没真正的差异。
|
|
78
|
+
*/
|
|
79
|
+
const SUBSTITUTE_IGNORED = new Set(['className', 'style', 'model', 'modelend', 'controlId', 'name']);
|
|
80
|
+
|
|
81
|
+
/** 原生标签(div 等)允许出现的属性:全局属性 + 常见元素属性 */
|
|
82
|
+
const DOM_ATTRS = new Set([
|
|
83
|
+
'id', 'className', 'style', 'title', 'role', 'tabIndex', 'tabindex', 'hidden', 'lang', 'dir',
|
|
84
|
+
'draggable', 'accessKey', 'contentEditable', 'spellCheck', 'translate', 'slot',
|
|
85
|
+
'src', 'srcSet', 'sizes', 'alt', 'width', 'height', 'href', 'target', 'rel', 'type', 'value',
|
|
86
|
+
'defaultValue', 'name', 'placeholder', 'htmlFor', 'disabled', 'readOnly', 'checked', 'required',
|
|
87
|
+
'maxLength', 'minLength', 'rows', 'cols', 'colSpan', 'rowSpan', 'scope', 'start', 'reversed',
|
|
88
|
+
'download', 'poster', 'controls', 'autoPlay', 'loop', 'muted', 'preload', 'accept', 'multiple',
|
|
89
|
+
'step', 'min', 'max', 'pattern', 'autoComplete', 'autoFocus', 'form', 'formAction', 'method',
|
|
90
|
+
'encType', 'noValidate', 'open', 'label', 'selected', 'crossOrigin', 'loading', 'referrerPolicy',
|
|
91
|
+
]);
|
|
92
|
+
|
|
93
|
+
/** data-* / aria-* 允许,但值必须是标量 */
|
|
94
|
+
const DATA_OR_ARIA = /^(data|aria)-[a-z0-9-]+$/i;
|
|
95
|
+
/** 事件属性:onClick 这类驼峰 */
|
|
96
|
+
const EVENT_ATTR = /^on[A-Z][A-Za-z]*$/;
|
|
97
|
+
|
|
98
|
+
/** CSS 文本 → React style 对象(`color: red; padding: 4px`) */
|
|
99
|
+
export function parseStyleText(text) {
|
|
100
|
+
const style = {};
|
|
101
|
+
for (const decl of String(text).split(';')) {
|
|
102
|
+
const at = decl.indexOf(':');
|
|
103
|
+
if (at < 0) continue;
|
|
104
|
+
const key = decl.slice(0, at).trim();
|
|
105
|
+
const val = decl.slice(at + 1).trim();
|
|
106
|
+
if (!key || !val) continue;
|
|
107
|
+
const prop = key.startsWith('--')
|
|
108
|
+
? key
|
|
109
|
+
: key.replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
110
|
+
style[prop] = val;
|
|
111
|
+
}
|
|
112
|
+
return style;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* 原生元素(div / span …)的属性白名单。
|
|
117
|
+
*
|
|
118
|
+
* React 会把不合法的值直接丢掉并打告警:
|
|
119
|
+
* Invalid value for prop `xxx` on <div> tag
|
|
120
|
+
* 所以原生标签上只允许标量属性;对象值一律不落 DOM ——
|
|
121
|
+
* `style` 转成 style 对象,`className` 的数组/对象拼成字符串,其余丢弃。
|
|
122
|
+
*
|
|
123
|
+
* 组件元素(大写开头)不走这里:它们的 props 是业务数据,原样传。
|
|
124
|
+
*/
|
|
125
|
+
export function domPropsToJsx(props, indent = 0) {
|
|
126
|
+
const parts = [];
|
|
127
|
+
for (const [k, v] of Object.entries(props || {})) {
|
|
128
|
+
if (v === undefined || v === null) continue;
|
|
129
|
+
if (typeof v === 'function') continue;
|
|
130
|
+
if (k === 'style') {
|
|
131
|
+
const style = typeof v === 'string' ? parseStyleText(v) : v;
|
|
132
|
+
if (style && typeof style === 'object' && !Array.isArray(style) && Object.keys(style).length) {
|
|
133
|
+
parts.push('style={' + literal(style, indent) + '}');
|
|
134
|
+
}
|
|
135
|
+
continue;
|
|
136
|
+
}
|
|
137
|
+
if (k === 'className') {
|
|
138
|
+
let cls = '';
|
|
139
|
+
if (typeof v === 'string') cls = v;
|
|
140
|
+
else if (Array.isArray(v)) cls = v.filter(isScalar).join(' ');
|
|
141
|
+
else if (v && typeof v === 'object') {
|
|
142
|
+
cls = Object.entries(v).filter(([, on]) => on).map(([name]) => name).join(' ');
|
|
143
|
+
}
|
|
144
|
+
if (cls) parts.push('className={' + JSON.stringify(cls) + '}');
|
|
145
|
+
continue;
|
|
146
|
+
}
|
|
147
|
+
if (!isScalar(v) && !v?.[TRANSLATED_VALUE]) continue; // 普通对象/数组不许落到原生标签上
|
|
148
|
+
if (!(DOM_ATTRS.has(k) || DATA_OR_ARIA.test(k) || EVENT_ATTR.test(k))) continue;
|
|
149
|
+
// JSX 属性名原样写(data-* 带连字符是合法的,不能加引号)
|
|
150
|
+
parts.push(k + '={' + literal(v, indent) + '}');
|
|
151
|
+
}
|
|
152
|
+
return parts;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/** 组件源码是否真的把 inbiz 当 prop 读(排除 `import '@inbiz/xxx'` 这类字符串) */
|
|
156
|
+
const inbizProbe = new Map();
|
|
157
|
+
function moduleReadsInbizProp(entry) {
|
|
158
|
+
const file = entry?.absPath;
|
|
159
|
+
if (!file) return false;
|
|
160
|
+
if (inbizProbe.has(file)) return inbizProbe.get(file);
|
|
161
|
+
let ok = false;
|
|
162
|
+
try {
|
|
163
|
+
const code = fs
|
|
164
|
+
.readFileSync(file, 'utf8')
|
|
165
|
+
.replace(/^\s*import[\s\S]*?from\s*['"][^'"]*['"];?/gm, '')
|
|
166
|
+
.replace(/\/\*[\s\S]*?\*\//g, '')
|
|
167
|
+
.replace(/'[^'\n]*'|"[^"\n]*"|`[^`]*`/g, '');
|
|
168
|
+
ok = /props\s*\??\.\s*inbiz|[{,]\s*inbiz\s*[,}]/.test(code);
|
|
169
|
+
} catch {
|
|
170
|
+
ok = false;
|
|
171
|
+
}
|
|
172
|
+
inbizProbe.set(file, ok);
|
|
173
|
+
return ok;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* 要不要给这个组件传 `inbiz`。
|
|
178
|
+
*
|
|
179
|
+
* 属性组件(`X.GridColumn`、
|
|
180
|
+
* `X.column`、`X.TabPane` …)更是宿主里的布局壳子:组件库源码里
|
|
181
|
+
* `FormGrid.GridColumn` 的写法就是 `({gridSpan, children, ...props}) =>
|
|
182
|
+
* <div {...props}>`。inbiz 是对象,一旦被展开到原生标签上,React 会报
|
|
183
|
+
*
|
|
184
|
+
* Invalid value for prop `inbiz` on <div> tag
|
|
185
|
+
*
|
|
186
|
+
* 首页 ShouyeIndexPage 的那条告警就是这么来的。所以属性组件只在
|
|
187
|
+
* 模块里真的读 props.inbiz 时才传;普通组件维持原行为。
|
|
188
|
+
*/
|
|
189
|
+
function injectsInbiz(entry) {
|
|
190
|
+
if (!entry?.usesInbiz) return false;
|
|
191
|
+
if (entry.memberAccess || entry.compoundMember) return moduleReadsInbizProp(entry);
|
|
192
|
+
return true;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* 组件源码是否「接进了表单」——决定它要不要 FieldScope。
|
|
197
|
+
*
|
|
198
|
+
* 判据不用组件名白名单(一升级就漏),看源码:组件自己或它的本地模块里
|
|
199
|
+
* 有没有 Formily 的字段/表单耦合。四类命中都要紧:
|
|
200
|
+
* useField / useFieldSchema / useForm → 直接读字段状态
|
|
201
|
+
* ReactiveField / recursionField → 自己渲染字段
|
|
202
|
+
* connect( → 组件被 connect 包过,value/onChange
|
|
203
|
+
* 由 ReactiveField 注入,脱离字段就收不到值
|
|
204
|
+
*
|
|
205
|
+
* 为什么要跟本地 import(深度 3):`TreeSelect` 的 index.tsx 只做了一层转包,
|
|
206
|
+
* 真正读 field 的是 ./components/view;只看入口文件会把它误判成「不需要字段」,
|
|
207
|
+
* 表单里就会出现一个永远空着的选择器。
|
|
208
|
+
*/
|
|
209
|
+
const FIELD_COUPLING = new RegExp(
|
|
210
|
+
[
|
|
211
|
+
// 前面加 (?<![.\w$]):`Form.useForm()`(antd)不是 Formily 的 useForm,
|
|
212
|
+
// `grid.connect(...)` 也不是 Formily 的 connect —— 不排除的话布局容器、
|
|
213
|
+
// 弹窗类业务组件会被统统误判成表单控件,绕一圈又回到「什么都套一层」。
|
|
214
|
+
'(?<![.\\w$])useField\\b', '(?<![.\\w$])useFieldSchema\\b',
|
|
215
|
+
'(?<![.\\w$])useForm\\b', '(?<![.\\w$])useFormEffects\\b',
|
|
216
|
+
'(?<![.\\w$])recursionField\\b', '(?<![.\\w$])ReactiveField\\b',
|
|
217
|
+
// connect(...) 是 Formily 的值注入入口
|
|
218
|
+
'(?<![.\\w$])connect\\s*\\(',
|
|
219
|
+
// 直接吃 value/onChange 的组件,值只能由 ReactiveField 注入
|
|
220
|
+
'\\bprops\\s*\\??\\.\\s*(value|onChange|onFocus|onBlur)\\b',
|
|
221
|
+
].join('|'),
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
/** 读 `field.decoratorProps` 的组件才需要把装饰器配置传进 FieldScope */
|
|
225
|
+
const DECORATOR_PROP_READ = /(?<![.\w$])decoratorProps\b/;
|
|
226
|
+
|
|
227
|
+
const moduleProbeCache = new Map();
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* 组件源码(含本地 import,深 3 层)是否命中某个特征。
|
|
231
|
+
*
|
|
232
|
+
* 为什么跟本地 import:`TreeSelect` 的入口只是转包,真正读 field 的是
|
|
233
|
+
* ./components/view —— 只看入口会把它误判成「不需要字段」,
|
|
234
|
+
* 表单里就会出现一个永远空着的选择器。
|
|
235
|
+
*/
|
|
236
|
+
function moduleMatches(file, pattern, depth = 0, seen = new Set()) {
|
|
237
|
+
if (!file || depth > 3) return false;
|
|
238
|
+
const key = `${pattern.source}\u0000${file}`;
|
|
239
|
+
if (moduleProbeCache.has(key)) return moduleProbeCache.get(key);
|
|
240
|
+
let code = '';
|
|
241
|
+
try {
|
|
242
|
+
code = fs.readFileSync(file, 'utf8');
|
|
243
|
+
} catch {
|
|
244
|
+
return false;
|
|
245
|
+
}
|
|
246
|
+
if (pattern.test(code)) {
|
|
247
|
+
moduleProbeCache.set(key, true);
|
|
248
|
+
return true;
|
|
249
|
+
}
|
|
250
|
+
const dir = path.dirname(file);
|
|
251
|
+
let hit = false;
|
|
252
|
+
for (const match of code.matchAll(/from\s+['"](\.[^'"]+)['"]/g)) {
|
|
253
|
+
const base = path.resolve(dir, match[1]);
|
|
254
|
+
const target = [base, `${base}.tsx`, `${base}.ts`, `${base}.js`,
|
|
255
|
+
path.join(base, 'index.tsx'), path.join(base, 'index.ts')]
|
|
256
|
+
.find((candidate) => {
|
|
257
|
+
try {
|
|
258
|
+
return fs.statSync(candidate).isFile();
|
|
259
|
+
} catch {
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
});
|
|
263
|
+
if (!target || seen.has(target)) continue;
|
|
264
|
+
seen.add(target);
|
|
265
|
+
if (moduleMatches(target, pattern, depth + 1, seen)) {
|
|
266
|
+
hit = true;
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
moduleProbeCache.set(key, hit);
|
|
271
|
+
return hit;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function moduleReadsField(file) {
|
|
275
|
+
return moduleMatches(file, FIELD_COUPLING);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function moduleReadsDecoratorProps(file) {
|
|
279
|
+
return moduleMatches(file, DECORATOR_PROP_READ);
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** 元素 props:值为 undefined 的跳过,保持输出干净 */
|
|
283
|
+
function propsToJsx(props, renderValue) {
|
|
284
|
+
const parts = [];
|
|
285
|
+
for (const [k, v] of Object.entries(props || {})) {
|
|
286
|
+
if (v === undefined) continue;
|
|
287
|
+
if (typeof v === 'function') continue; // schema 里不会有真函数,防御
|
|
288
|
+
parts.push(attrName(k) + '={' + renderValue(v, k) + '}');
|
|
289
|
+
}
|
|
290
|
+
return parts;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/* ---------------------------------------------------------------- 词条 ------- */
|
|
294
|
+
|
|
295
|
+
/**
|
|
296
|
+
* 应用语言包里「这条是词条」的标记前缀。
|
|
297
|
+
*
|
|
298
|
+
* 平台侧同一个字面量出现在三处,写法一致:
|
|
299
|
+
* inbiz-runtime/src/i18n.ts:22、inbiz-render/utils/language/index.ts:getLangText
|
|
300
|
+
*/
|
|
301
|
+
export const MULTILINGUAL_PREFIX = '{multilingual}';
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* 生成期的词条查表。
|
|
305
|
+
*
|
|
306
|
+
* 为什么生成期就要查:
|
|
307
|
+
* 平台上 `{multilingual}` 有两条解析路径 ——
|
|
308
|
+
* 1. `LocaleRoot`(inbiz-render/utils/locales/index.tsx:52)把 `placeholder`
|
|
309
|
+
* 这类 props 统一换成人话;
|
|
310
|
+
* 2. 组件里的 `TextWidget` → `takeMessage`(inbiz-runtime/src/i18n.ts)。
|
|
311
|
+
* 出码产物里没有 LocaleRoot(全仓 grep 不到任何引用),第 1 条路径不存在,
|
|
312
|
+
* 于是 `{multilingual}global.i18n-xxxx` 会原样落到组件 props 上显示出来。
|
|
313
|
+
*
|
|
314
|
+
* key 的归一规则照抄平台,一步都不能少:
|
|
315
|
+
* - 先剥 `{multilingual}`(takeMessage 的第一行);
|
|
316
|
+
* - 语言包按 `{ [lang]: { global: { 'i18n-xxx': '文案' } } }` 注册
|
|
317
|
+
* (Web 由 runtime/web/language.ts 加载,与平台 effect/useLangEffect.ts 一致),
|
|
318
|
+
* 所以查表要剥掉 `global.` 前缀,否则 622 个标题一个都命中不了;
|
|
319
|
+
* - 注册时 key 里的冒号会被去掉。
|
|
320
|
+
*/
|
|
321
|
+
export function createMessageLookup(messages = {}) {
|
|
322
|
+
const table = new Map();
|
|
323
|
+
const put = (key, text) => {
|
|
324
|
+
if (typeof key !== 'string' || typeof text !== 'string') return;
|
|
325
|
+
const clean = key.replace(/:/g, '');
|
|
326
|
+
if (!clean) return;
|
|
327
|
+
if (!table.has(clean)) table.set(clean, text);
|
|
328
|
+
const lower = clean.toLowerCase();
|
|
329
|
+
if (!table.has(lower)) table.set(lower, text);
|
|
330
|
+
};
|
|
331
|
+
for (const [key, text] of Object.entries(messages || {})) put(key, text);
|
|
332
|
+
|
|
333
|
+
return function lookup(raw) {
|
|
334
|
+
if (typeof raw !== 'string') return undefined;
|
|
335
|
+
const token = raw.split(MULTILINGUAL_PREFIX).join('').trim();
|
|
336
|
+
if (!token) return undefined;
|
|
337
|
+
const candidates = [token];
|
|
338
|
+
if (/^global\./i.test(token)) candidates.push(token.slice('global.'.length));
|
|
339
|
+
for (const key of candidates) {
|
|
340
|
+
if (table.has(key)) return table.get(key);
|
|
341
|
+
const lower = key.toLowerCase();
|
|
342
|
+
if (table.has(lower)) return table.get(lower);
|
|
343
|
+
}
|
|
344
|
+
return undefined;
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* 单条 `{multilingual}` 字符串 → 文案;查不到时**原样返回**。
|
|
350
|
+
*
|
|
351
|
+
* 保留原文而不是替成空串:运行时还有 `takeMessage` 这一层,
|
|
352
|
+
* 语言包在浏览器里加载成功后仍然能翻出来;生成期替成空串就再也回不来了。
|
|
353
|
+
*/
|
|
354
|
+
export function resolveMultilingual(value, lookup) {
|
|
355
|
+
if (typeof value !== 'string' || value.indexOf(MULTILINGUAL_PREFIX) < 0) return value;
|
|
356
|
+
// 分段规则来自平台 utils.ts:1450 `multilingualFormat`:按逗号切开逐段查。
|
|
357
|
+
// schema 里 `page: 'page/xxx,outId,{multilingual}global.i18n-xxx'` 这类复合值
|
|
358
|
+
// 只有最后一段是词条,整串直接查必然落空。
|
|
359
|
+
return value
|
|
360
|
+
.split(',')
|
|
361
|
+
.map((part) => {
|
|
362
|
+
if (part.indexOf(MULTILINGUAL_PREFIX) < 0) return part;
|
|
363
|
+
const hit = lookup(part);
|
|
364
|
+
return hit === undefined ? part : hit;
|
|
365
|
+
})
|
|
366
|
+
.join(',');
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/** 递归翻译 props 里的 `{multilingual}`(平台靠 LocaleRoot,产物只能落成字面量) */
|
|
370
|
+
export function translateMultilingual(value, lookup) {
|
|
371
|
+
if (typeof value === 'string') return resolveMultilingual(value, lookup);
|
|
372
|
+
if (Array.isArray(value)) return value.map((item) => translateMultilingual(item, lookup));
|
|
373
|
+
if (value && typeof value === 'object') {
|
|
374
|
+
const out = {};
|
|
375
|
+
for (const [key, item] of Object.entries(value)) {
|
|
376
|
+
out[key] = translateMultilingual(item, lookup);
|
|
377
|
+
}
|
|
378
|
+
return out;
|
|
379
|
+
}
|
|
380
|
+
return value;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* schema 节点 title → 可显示文本。
|
|
385
|
+
*
|
|
386
|
+
* `status: false` 是作者在设计器里主动关掉了标题(18 个节点),不是「没有词条」。
|
|
387
|
+
* 平台 FormItem 的取值条件是 `title?.status && title?.value`
|
|
388
|
+
* (render/core/schemaField.tsx:100),这里保持一致。
|
|
389
|
+
*/
|
|
390
|
+
export function resolveTitleText(title, lookup) {
|
|
391
|
+
if (title === undefined || title === null) return '';
|
|
392
|
+
if (typeof title === 'object') {
|
|
393
|
+
if (title.status === false) return '';
|
|
394
|
+
return resolveMultilingual(title.value, lookup) || '';
|
|
395
|
+
}
|
|
396
|
+
return resolveMultilingual(title, lookup) || '';
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* 传给组件的 `title` prop。
|
|
401
|
+
*
|
|
402
|
+
* 平台在 `onFieldInit('*')` 里做 `field.componentProps.title = field.title`
|
|
403
|
+
* (inbiz-render/render/sdk/index.ts:135)—— 组件拿到的是**对象** `{value, status}`,
|
|
404
|
+
* 不是字符串。EformButton 里直接写 `title.status == true`(preview/web/index.tsx:169),
|
|
405
|
+
* 没有可选链,缺这个 prop 会抛 TypeError(整块按钮树崩掉)。
|
|
406
|
+
*
|
|
407
|
+
* value 在生成期就翻成文案:产物没有 LocaleRoot,留着 token 只会在界面上
|
|
408
|
+
* 显示 `{multilingual}global.i18n-xxxx`。
|
|
409
|
+
*/
|
|
410
|
+
export function titlePropOf(title, lookup) {
|
|
411
|
+
if (title === undefined || title === null) return undefined;
|
|
412
|
+
if (typeof title === 'object') {
|
|
413
|
+
const out = {};
|
|
414
|
+
for (const [key, item] of Object.entries(title)) {
|
|
415
|
+
out[key] = key === 'value' ? resolveMultilingual(item, lookup) : item;
|
|
416
|
+
}
|
|
417
|
+
return out;
|
|
418
|
+
}
|
|
419
|
+
return resolveMultilingual(title, lookup);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/* ------------------------------------------------------ 节点根级字段 → props -- */
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* JSON-Schema 的保留关键字:描述的是「字段」而不是「组件配置」,不能当 props 传。
|
|
426
|
+
*
|
|
427
|
+
* 清单取自 @formily/json-schema 的 SchemaStateMap / SchemaValidatorMap
|
|
428
|
+
* (node_modules/@formily/json-schema/esm/shared.js:17),只保留「传下去会让组件
|
|
429
|
+
* 误以为在配置」的那些。
|
|
430
|
+
*/
|
|
431
|
+
const SCHEMA_KEYWORDS = new Set([
|
|
432
|
+
'type', 'name', 'title', 'description', 'properties', 'patternProperties',
|
|
433
|
+
'additionalProperties', 'items', 'additionalItems', 'definitions',
|
|
434
|
+
'$ref', '$id', '$schema', 'default', 'enum', 'const', 'required',
|
|
435
|
+
'readOnly', 'writeOnly', 'dependencies', 'maxLength', 'minLength',
|
|
436
|
+
'maxItems', 'minItems', 'uniqueItems', 'pattern', 'multipleOf', 'maximum',
|
|
437
|
+
'minimum', 'exclusiveMaximum', 'exclusiveMinimum', 'maxProperties',
|
|
438
|
+
'minProperties',
|
|
439
|
+
]);
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* `sourceModelUpdate` → `primarykey`。
|
|
443
|
+
*
|
|
444
|
+
* 逐行对齐平台 onFieldInit(inbiz-render/render/sdk/index.ts:155-172):
|
|
445
|
+
* 先取 `primaryKey` / `searchData.primaryKey`,其次取 queryField 里 `IsSingle`
|
|
446
|
+
* 的字段名,最后兜底 'id';JSON 解析失败也是 'id'。
|
|
447
|
+
* 表格/动态列表靠这个 prop 决定行主键,缺了就退化成「第一列当 key」。
|
|
448
|
+
*/
|
|
449
|
+
export function primaryKeyOf(raw) {
|
|
450
|
+
if (!raw || typeof raw !== 'string') return undefined;
|
|
451
|
+
try {
|
|
452
|
+
const data = JSON.parse(raw);
|
|
453
|
+
const primary =
|
|
454
|
+
data?.primaryKey ||
|
|
455
|
+
data?.searchData?.primaryKey ||
|
|
456
|
+
(Array.isArray(data?.queryField)
|
|
457
|
+
? data.queryField.find((item) => item && item.IsSingle)?.Name
|
|
458
|
+
: undefined);
|
|
459
|
+
return primary || 'id';
|
|
460
|
+
} catch {
|
|
461
|
+
return 'id';
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* 节点根级字段 → 组件 props。
|
|
467
|
+
*
|
|
468
|
+
* 平台上这些键落在 Formily 的 **field state** 上(SchemaStateMap),随后在
|
|
469
|
+
* onFieldInit 里被搬进 componentProps(inbiz-render/render/sdk/index.ts:130-172)。
|
|
470
|
+
* 产物没有 field,只能把这层搬运写进生成器:
|
|
471
|
+
*
|
|
472
|
+
* required → props.required(同时决定 FormItem 的必填星号)
|
|
473
|
+
* x-pattern → disabled / readOnly,映射关系抄 ReactiveField.js:116-121
|
|
474
|
+
* ('disabled' / 'readPretty' → disabled,'readOnly' → readOnly)
|
|
475
|
+
* maxLength → props.maxLength
|
|
476
|
+
* sourceModelUpdate → props.primarykey
|
|
477
|
+
* 其余非 schema 关键字 → 原样补位(日期区间的 start / end 默认值等 48 组全在
|
|
478
|
+
* 这一类),优先级低于 x-component-props,不会覆盖作者配置
|
|
479
|
+
*
|
|
480
|
+
* `x-validator` 是 Formily 的校验规则,组件根本不读;现存 152 个全是空数组,
|
|
481
|
+
* 这里不往 props 里塞(塞进去反而会被展开到 DOM 上)。
|
|
482
|
+
*/
|
|
483
|
+
export function componentPropsFromNode(node) {
|
|
484
|
+
const props = {};
|
|
485
|
+
for (const [key, value] of Object.entries(node)) {
|
|
486
|
+
if (key.startsWith('x-')) continue;
|
|
487
|
+
if (SCHEMA_KEYWORDS.has(key)) continue;
|
|
488
|
+
// sourceModelUpdate 是一整段数据源 JSON(几十 KB),平台只从中解出
|
|
489
|
+
// primarykey 塞给组件,原文从不进 componentProps —— 照搬会把产物撑爆
|
|
490
|
+
if (key === 'sourceModelUpdate') continue;
|
|
491
|
+
props[key] = value;
|
|
492
|
+
}
|
|
493
|
+
if (node.required === true) props.required = true;
|
|
494
|
+
if (node.maxLength !== undefined) props.maxLength = node.maxLength;
|
|
495
|
+
|
|
496
|
+
const pattern = node['x-pattern'];
|
|
497
|
+
if (pattern === 'disabled' || pattern === 'readPretty') props.disabled = true;
|
|
498
|
+
else if (pattern === 'readOnly') props.readOnly = true;
|
|
499
|
+
|
|
500
|
+
const primarykey = primaryKeyOf(node.sourceModelUpdate);
|
|
501
|
+
if (primarykey) props.primarykey = primarykey;
|
|
502
|
+
return props;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/* ------------------------------------------------------------- FormItem ----- */
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* 页面级表单配置的默认值。
|
|
509
|
+
*
|
|
510
|
+
* 平台把 `content.form` 整块铺在 `<Form>` 上(render/core/index.tsx:213),
|
|
511
|
+
* Formily 再把它当 FormLayout context 用。实测 108 个页面里 105 个是
|
|
512
|
+
* `{ layout:'horizontal', labelCol:6, wrapperCol:12, labelAlign:'right' }`
|
|
513
|
+
* (.inbiz/app.json → detail.Component.content.form)。
|
|
514
|
+
*
|
|
515
|
+
* generate.mjs 目前只取 `form.style/className`(pageRootProps),还没把 labelCol
|
|
516
|
+
* 这些传进来;先用实测值兜底,等它把 `form` 传进来就自动以真实数据为准。
|
|
517
|
+
*/
|
|
518
|
+
export const DEFAULT_FORM_LAYOUT = {
|
|
519
|
+
layout: 'horizontal',
|
|
520
|
+
labelCol: 6,
|
|
521
|
+
wrapperCol: 12,
|
|
522
|
+
labelAlign: 'right',
|
|
523
|
+
};
|
|
524
|
+
|
|
525
|
+
/**
|
|
526
|
+
* FormItem 的布局参数。
|
|
527
|
+
*
|
|
528
|
+
* 逐行对齐 @formily/antd 的 `useFormItemLayout`
|
|
529
|
+
* (node_modules/@formily/antd/esm/form-item/index.js:57)与 `BaseItem`
|
|
530
|
+
* 的固定宽度/栅格分支(同文件 141-153):节点上的 `x-decorator-props` 覆盖
|
|
531
|
+
* 页面表单配置,`labelWidth/wrapperWidth` 一旦有值就退出栅格模式。
|
|
532
|
+
*/
|
|
533
|
+
export function formItemLayout(decoratorProps = {}, pageForm = DEFAULT_FORM_LAYOUT) {
|
|
534
|
+
const form = pageForm || {};
|
|
535
|
+
const pick = (key) =>
|
|
536
|
+
decoratorProps[key] !== undefined ? decoratorProps[key] : form[key];
|
|
537
|
+
|
|
538
|
+
const layout = pick('layout') || 'horizontal';
|
|
539
|
+
const labelCol = pick('labelCol');
|
|
540
|
+
const wrapperCol = pick('wrapperCol');
|
|
541
|
+
const labelAlign = pick('labelAlign') || (layout === 'vertical' ? 'left' : 'right');
|
|
542
|
+
const colon = decoratorProps.colon !== undefined
|
|
543
|
+
? decoratorProps.colon
|
|
544
|
+
: form.colon !== undefined ? form.colon : true;
|
|
545
|
+
const labelWidth = pick('labelWidth');
|
|
546
|
+
const wrapperWidth = pick('wrapperWidth');
|
|
547
|
+
|
|
548
|
+
const labelStyle = { ...(decoratorProps.labelStyle || {}) };
|
|
549
|
+
const wrapperStyle = { ...(decoratorProps.wrapperStyle || {}) };
|
|
550
|
+
|
|
551
|
+
if (labelWidth || wrapperWidth) {
|
|
552
|
+
if (labelWidth) {
|
|
553
|
+
labelStyle.width = labelWidth === 'auto' ? undefined : labelWidth;
|
|
554
|
+
labelStyle.maxWidth = labelWidth === 'auto' ? undefined : labelWidth;
|
|
555
|
+
}
|
|
556
|
+
if (wrapperWidth) {
|
|
557
|
+
wrapperStyle.width = wrapperWidth === 'auto' ? undefined : wrapperWidth;
|
|
558
|
+
wrapperStyle.maxWidth = wrapperWidth === 'auto' ? undefined : wrapperWidth;
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
// 栅格模式:只有 labelWidth/wrapperWidth 都没配时才启用(同 BaseItem)
|
|
563
|
+
const enableCol =
|
|
564
|
+
Boolean(labelCol || wrapperCol) &&
|
|
565
|
+
!labelStyle.width &&
|
|
566
|
+
!wrapperStyle.width &&
|
|
567
|
+
layout !== 'vertical';
|
|
568
|
+
|
|
569
|
+
return { layout, labelCol, wrapperCol, labelAlign, colon, labelStyle, wrapperStyle, enableCol };
|
|
570
|
+
}
|
|
571
|
+
|
|
572
|
+
/** 小对象内联成一行:`{ gridColumn: "span 1 / auto" }`,别为 1-2 个键撑出 4 行 */
|
|
573
|
+
export function valueJsx(value, indent = 0) {
|
|
574
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
575
|
+
const entries = Object.entries(value);
|
|
576
|
+
if (entries.length > 0 && entries.length <= 5 && entries.every(([, v]) => isScalar(v) || v === null)) {
|
|
577
|
+
return '{ ' + entries.map(([k, v]) => `${attrName(k)}: ${literal(v)}`).join(', ') + ' }';
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
return literal(value, indent);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
/**
|
|
584
|
+
* @param {object} options
|
|
585
|
+
* @param {Map} options.registry 组件注册表
|
|
586
|
+
* @param {object} [options.messages] 应用语言包
|
|
587
|
+
* @param {object} [options.form] 页面级表单布局
|
|
588
|
+
* @param {Set<string>} [options.scriptNames] 本页脚本里按名字访问过的控件
|
|
589
|
+
* (`inbiz('xxx')`)。脚本能开关显隐 / 改必填 / 读写值的前提是
|
|
590
|
+
* **这个控件在 form 里注册过**,所以这些名字必须保留字段上下文。
|
|
591
|
+
* @param {boolean} [options.readsValues] 本页脚本是否会读整表的值
|
|
592
|
+
* (`getValue()` / `validate()` / `setValues()`)。有这类调用时,
|
|
593
|
+
* 带值的控件即使没被点名也必须注册,否则提交时会少数据。
|
|
594
|
+
*/
|
|
595
|
+
export function createTransformer({
|
|
596
|
+
registry,
|
|
597
|
+
messages = {},
|
|
598
|
+
runtimeTranslations = false,
|
|
599
|
+
dataSourceContracts,
|
|
600
|
+
form = DEFAULT_FORM_LAYOUT,
|
|
601
|
+
scriptNames,
|
|
602
|
+
bindLayoutFieldEvents,
|
|
603
|
+
readsValues = false,
|
|
604
|
+
extractPageConfig = false,
|
|
605
|
+
}) {
|
|
606
|
+
/** 收集 import:别名 → { name, path, lazy? } */
|
|
607
|
+
const imports = new Map();
|
|
608
|
+
/** 无法自动翻译、需要人看的节点 */
|
|
609
|
+
const warnings = [];
|
|
610
|
+
/** 被懒加载的组件:本地标识符 → 判定说明(用于生成注释与统计) */
|
|
611
|
+
const lazyComponents = new Map();
|
|
612
|
+
let uid = 0;
|
|
613
|
+
/** 是否有组件需要 InBiz 运行时对象(决定生成文件要不要引 useInbiz) */
|
|
614
|
+
let needsInbiz = false;
|
|
615
|
+
let needsFields = false;
|
|
616
|
+
/** 有节点翻译成了 FormItem 结构(决定生成文件要不要引 @formily/antd 的基础样式) */
|
|
617
|
+
let needsFormItem = false;
|
|
618
|
+
/** 应用语言包查表;queryForm 是页面级表单布局(labelCol 等) */
|
|
619
|
+
const lookup = runtimeTranslations ? () => undefined : createMessageLookup(messages);
|
|
620
|
+
const fallbackLookup = createMessageLookup(messages);
|
|
621
|
+
let needsTranslation = false;
|
|
622
|
+
let configNeedsTranslation = false;
|
|
623
|
+
const pageConfig = new Map();
|
|
624
|
+
function localize(value, markTranslation = () => { needsTranslation = true; }) {
|
|
625
|
+
if (!runtimeTranslations) return value;
|
|
626
|
+
if (typeof value === 'string' && value.includes(MULTILINGUAL_PREFIX)) {
|
|
627
|
+
markTranslation();
|
|
628
|
+
return { [TRANSLATED_VALUE]: [value, value.split(',').map((part) => resolveMultilingual(part, fallbackLookup))] };
|
|
629
|
+
}
|
|
630
|
+
if (Array.isArray(value)) return value.map(item => localize(item, markTranslation));
|
|
631
|
+
if (value && typeof value === 'object') {
|
|
632
|
+
return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, localize(item, markTranslation)]));
|
|
633
|
+
}
|
|
634
|
+
return value;
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
function renderComponentProp(value, prop, indent, component, binding, field) {
|
|
638
|
+
let translated = false;
|
|
639
|
+
const localized = localize(value, () => { translated = true; });
|
|
640
|
+
if (extractPageConfig && value && typeof value === 'object') {
|
|
641
|
+
// Measure without JSX nesting so moving a field does not change the decision.
|
|
642
|
+
const source = literal(localized);
|
|
643
|
+
if (source.split('\n').length >= 12 || source.length >= 500) {
|
|
644
|
+
const stem = `create${configNamePart(field) || configNamePart(component)}${configNamePart(prop) || 'Config'}`;
|
|
645
|
+
let name = stem;
|
|
646
|
+
for (let suffix = 2; pageConfig.has(name); suffix++) name = `${stem}${suffix}`;
|
|
647
|
+
pageConfig.set(name, { binding, component, prop, source });
|
|
648
|
+
configNeedsTranslation ||= translated;
|
|
649
|
+
return `pageConfig.${name}()`;
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
needsTranslation ||= translated;
|
|
653
|
+
return literal(localized, indent);
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
function renderPageConfig() {
|
|
657
|
+
if (!pageConfig.size) return '';
|
|
658
|
+
const lines = [];
|
|
659
|
+
if (configNeedsTranslation) lines.push("import { translateAppText } from '@macrowing/inbiz-runtime';");
|
|
660
|
+
lines.push(
|
|
661
|
+
"import type { ComponentProps } from 'react';",
|
|
662
|
+
'',
|
|
663
|
+
// Distribute across prop unions. Absent keys are still diagnosed at the JSX call site.
|
|
664
|
+
'type PropValue<Props, Key extends PropertyKey> = Props extends unknown',
|
|
665
|
+
' ? Key extends keyof Props ? Props[Key] : never',
|
|
666
|
+
' : never;',
|
|
667
|
+
'type ConfigValue<Props, Key extends PropertyKey> = [PropValue<Props, Key>] extends [never]',
|
|
668
|
+
' ? unknown : PropValue<Props, Key>;',
|
|
669
|
+
);
|
|
670
|
+
const componentTypes = new Map();
|
|
671
|
+
const typeNames = new Set();
|
|
672
|
+
for (const { binding, component } of pageConfig.values()) {
|
|
673
|
+
if (componentTypes.has(component)) continue;
|
|
674
|
+
const stem = `${component.replaceAll('.', '_')}Props`;
|
|
675
|
+
let alias = stem;
|
|
676
|
+
for (let suffix = 2; typeNames.has(alias); suffix++) alias = `${stem}${suffix}`;
|
|
677
|
+
typeNames.add(alias);
|
|
678
|
+
componentTypes.set(component, alias);
|
|
679
|
+
const { isDefault, exportName, path } = imports.get(binding);
|
|
680
|
+
const member = component.slice(binding.length);
|
|
681
|
+
lines.push(`type ${alias} = ComponentProps<typeof import('${path}').${isDefault ? 'default' : exportName}${member}>;`);
|
|
682
|
+
}
|
|
683
|
+
for (const [name, { component, prop, source }] of pageConfig) {
|
|
684
|
+
// Called in JSX to keep the original locale and a fresh object per render.
|
|
685
|
+
const type = `ConfigValue<${componentTypes.get(component)}, ${JSON.stringify(prop)}>`;
|
|
686
|
+
lines.push('', `export function ${name}(): ${type} {`,
|
|
687
|
+
` return ${source.split('\n').join('\n ')};`, '}');
|
|
688
|
+
}
|
|
689
|
+
return lines.join('\n') + '\n';
|
|
690
|
+
}
|
|
691
|
+
function localizeTitle(value) {
|
|
692
|
+
// Platform field decorators also resolve unmarked, dotted catalog keys.
|
|
693
|
+
if (typeof value === 'string' && /^[\w-]+(?:\.[\w-]+)+$/.test(value)) {
|
|
694
|
+
const fallback = fallbackLookup(value) ?? value;
|
|
695
|
+
if (!runtimeTranslations) return fallback;
|
|
696
|
+
needsTranslation = true;
|
|
697
|
+
return { [TRANSLATED_VALUE]: [MULTILINGUAL_PREFIX + value, [fallback]] };
|
|
698
|
+
}
|
|
699
|
+
return localize(value);
|
|
700
|
+
}
|
|
701
|
+
const queryForm = form || DEFAULT_FORM_LAYOUT;
|
|
702
|
+
/** 页面脚本按名字访问过的控件(布局区域没有脚本,为空集) */
|
|
703
|
+
const scriptRefs = scriptNames instanceof Set ? scriptNames : new Set(scriptNames || []);
|
|
704
|
+
|
|
705
|
+
/**
|
|
706
|
+
* 纯布局组件:只负责排版,不承载表单值。
|
|
707
|
+
*
|
|
708
|
+
* 判据看源码路径(`.../form/layout/...`),不看名字 —— 出码产物的组件注册表
|
|
709
|
+
* 本来就从目录结构来,这条只是沿用同一个口径。`FormGrid` / `FormTab` 这类
|
|
710
|
+
* 即使读了 useField,也是读 tab 的激活态,不是值。
|
|
711
|
+
*/
|
|
712
|
+
function isLayoutContainer(entry) {
|
|
713
|
+
if (!entry?.absPath) return false;
|
|
714
|
+
return entry.absPath.replace(/\\/g, '/').includes('/components/form/layout/');
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
/**
|
|
718
|
+
* 这个节点要不要一个字段(生成 `<FieldScope>`)。
|
|
719
|
+
*
|
|
720
|
+
* 判据是「**产物里这段代码是否真的需要 form 状态**」,不是「有没有 name」:
|
|
721
|
+
*
|
|
722
|
+
* 1. 脚本按名字访问过 —— 平台脚本的 `inbiz('x').display = 'hidden'`、
|
|
723
|
+
* `.required = true`、`.value`、`.loadData()` 全都落在字段状态上,
|
|
724
|
+
* 控件不入 form 就一个都不生效;
|
|
725
|
+
* 2. 组件(含它的本地 import)接进了 Formily —— value/onChange 由
|
|
726
|
+
* ReactiveField 注入,脱离字段就是个空壳;
|
|
727
|
+
* 3. 本页会整表取值(getValue / validate / setValues)且节点带值 ——
|
|
728
|
+
* 不带值就没有「表单数据」这回事;
|
|
729
|
+
* 4. 带 x-reactions 联动 —— 生成期算不出来,只能留在运行期。
|
|
730
|
+
*
|
|
731
|
+
* 其余(Message、News、Navigation、纯布局容器……)一律输出普通 JSX。
|
|
732
|
+
* 这一条正是用户反馈的「Message 也要套一层 RuntimeField」的根因。
|
|
733
|
+
*/
|
|
734
|
+
function needsFieldScope(entry, node, name) {
|
|
735
|
+
if (!entry) return false;
|
|
736
|
+
if (node['x-reactions']) return true;
|
|
737
|
+
// x-display 隐藏的节点:**不能**在生成期删掉它。
|
|
738
|
+
// 1. 平台此时只是「不渲染」,字段本身还在 form 里,`getValue()` 仍取得到
|
|
739
|
+
// 它的默认值(编号、语言代码这类隐藏字段就是靠这个提交的);
|
|
740
|
+
// 2. 脚本经常在没有点名的情况下把它改回 visible —— 例如
|
|
741
|
+
// `versionTypeChange(value, "majorLetterType", …)` 通过变量传名字,
|
|
742
|
+
// 静态扫描根本看不到 `inbiz('majorLetterType')`。
|
|
743
|
+
// 所以保留节点,把 display 交给运行时(FieldScope 建字段时带 display)。
|
|
744
|
+
if (node['x-display'] !== undefined && node['x-display'] !== 'visible') return true;
|
|
745
|
+
// FormItem 装饰器 = 设计器里明确标成「表单项」的节点。判据比源码探针更权威:
|
|
746
|
+
// 组件自己可能只是把 value 透传给第三方控件(FormNumber / MemberSelect 这类),
|
|
747
|
+
// 源码里看不到 `props.value`,但它确实是表单数据的一部分。
|
|
748
|
+
// 少建一个字段的代价是「提交时少一项」,比多建一个字段严重得多。
|
|
749
|
+
if (node['x-decorator'] === 'FormItem') return true;
|
|
750
|
+
if (scriptRefs.has(String(name ?? ''))) return true;
|
|
751
|
+
if (moduleReadsField(entry.absPath)) return true;
|
|
752
|
+
// 本页会整表取值(getValue / validate / setValues)时,带值的控件不能丢 ——
|
|
753
|
+
// 没有字段就没有「表单数据」,提交会少项。布局容器除外:form/layout 下
|
|
754
|
+
// 的 Block / FormGrid / Space 只排版不承载值,schema 里的 type 是设计器
|
|
755
|
+
// 留下的(常见是误写成 "string"),按它建字段纯属浪费。
|
|
756
|
+
const type = node.type;
|
|
757
|
+
if (readsValues && type !== undefined && type !== 'void' && !isLayoutContainer(entry)) {
|
|
758
|
+
return true;
|
|
759
|
+
}
|
|
760
|
+
return false;
|
|
761
|
+
}
|
|
762
|
+
|
|
763
|
+
/** 节点 type → FieldScope 的 kind(决定 createField / createVoidField …) */
|
|
764
|
+
function fieldKindOf(node) {
|
|
765
|
+
const type = node.type;
|
|
766
|
+
if (type === 'void') return 'void';
|
|
767
|
+
if (type === 'array' || type === 'Array') return 'array';
|
|
768
|
+
if (type === 'object') return 'object';
|
|
769
|
+
return 'field';
|
|
770
|
+
}
|
|
771
|
+
|
|
772
|
+
/**
|
|
773
|
+
* 容器装饰器 → 真实 div 属性。
|
|
774
|
+
*
|
|
775
|
+
* 平台 `Container` 装饰器的产物是一个带 style/className 的 div,并且**永远**
|
|
776
|
+
* 在最前面挂 `inbiz-decorator-container`:
|
|
777
|
+
*
|
|
778
|
+
* packages/inbiz-components/src/core/common/decorator/Decorators/Container.tsx:38
|
|
779
|
+
* classNames('inbiz-decorator-container', { 'inbiz-decorator-container-edit-view': … },
|
|
780
|
+
* props.className, props.cls)
|
|
781
|
+
* style={{ ...style, position: 'relative' }} ← 同文件 42 行
|
|
782
|
+
*
|
|
783
|
+
* 这个类不是纯装饰:edoc2 主题给它压了 `min-height: 20px`
|
|
784
|
+
* (edoc2-frontend/src/ecm/standard/plugins/ECM/src/less/extend/global.less:42,
|
|
785
|
+
* 注释「容器最小高度 30>20」覆盖了平台基底的 30px,
|
|
786
|
+
* 见 Frontend/src/frontend/designer/src/global.less:919)。
|
|
787
|
+
* 少了它,装饰 div 在 flex 列里退回 `min-height: auto`(= 内容高度),
|
|
788
|
+
* 压不下去 —— 首页欢迎条被居中溢出顶到容器外面(实测 y=16.6,平台 y=73)。
|
|
789
|
+
*
|
|
790
|
+
* 类名的排列顺序照抄平台:平台类在前,schema 的 className 在后。
|
|
791
|
+
* `inbiz-decorator-container-edit-view` 只属于设计器(`getEditor()` 为真时),
|
|
792
|
+
* 运行态不生成。
|
|
793
|
+
*
|
|
794
|
+
* 值走 domPropsToJsx 的白名单:`wrapperWidth` / `clickable` 这类设计器字段
|
|
795
|
+
* 一旦展开到原生标签上,React 每条都会告警。
|
|
796
|
+
*/
|
|
797
|
+
function containerAttrs(decoratorProps, className, title, indent, slotComponentName) {
|
|
798
|
+
const style = slotComponentName ? { ...decoratorProps.style, position: 'relative' } : decoratorProps.style;
|
|
799
|
+
if (slotComponentName && style.height === 'flex') {
|
|
800
|
+
delete style.height;
|
|
801
|
+
style.flex = 1;
|
|
802
|
+
}
|
|
803
|
+
return domPropsToJsx(
|
|
804
|
+
{
|
|
805
|
+
className: ['inbiz-decorator-container', slotComponentName ? `${slotComponentName}Container` : '',
|
|
806
|
+
className, slotComponentName ? decoratorProps.cls : ''].filter(Boolean).join(' '),
|
|
807
|
+
style,
|
|
808
|
+
'data-title': title ? localizeTitle(title) : undefined,
|
|
809
|
+
},
|
|
810
|
+
indent,
|
|
811
|
+
);
|
|
812
|
+
}
|
|
813
|
+
|
|
814
|
+
/**
|
|
815
|
+
* FieldScope 的属性(**只有运行时值**,没有任何 schema 语义键)。
|
|
816
|
+
*
|
|
817
|
+
* name form 里的字段路径,也是脚本 `inbiz('name')` 的键
|
|
818
|
+
* componentName 原始组件名(日期区间、附件这类组件按它走特判)
|
|
819
|
+
* kind void / array / object / field —— 决定有没有 value
|
|
820
|
+
* required 必填(脚本还会改)
|
|
821
|
+
* pattern x-pattern 翻译过来的交互态
|
|
822
|
+
* display 静态判不掉时(脚本会改)才带
|
|
823
|
+
* decoratorProps 组件通过 useField().decoratorProps 读的配置
|
|
824
|
+
*/
|
|
825
|
+
function fieldScopeProps(node, name, compRaw, decoratorProps, indent, entry) {
|
|
826
|
+
const props = [
|
|
827
|
+
`name=${JSON.stringify(name)}`,
|
|
828
|
+
`componentName=${JSON.stringify(String(compRaw))}`,
|
|
829
|
+
];
|
|
830
|
+
const kind = fieldKindOf(node);
|
|
831
|
+
if (kind !== 'field') props.push(`kind=${JSON.stringify(kind)}`);
|
|
832
|
+
if (node.required === true) props.push('required');
|
|
833
|
+
if (node['x-pattern']) props.push(`pattern=${JSON.stringify(String(node['x-pattern']))}`);
|
|
834
|
+
const display = node['x-display'];
|
|
835
|
+
if (display !== undefined && display !== 'visible') {
|
|
836
|
+
props.push(`display=${JSON.stringify(String(display))}`);
|
|
837
|
+
}
|
|
838
|
+
// 只有容器装饰器的 decoratorProps 在旧实现里进过 field.decoratorProps
|
|
839
|
+
// (其余节点的 metadata 本来就不带它)。再收一道:组件本体不读
|
|
840
|
+
// `decoratorProps` 的,连这一点也不传 —— 传了也没人用,只是噪音。
|
|
841
|
+
if (node['x-decorator'] === 'Container' && moduleReadsDecoratorProps(entry?.absPath)) {
|
|
842
|
+
const decorated = {};
|
|
843
|
+
for (const [key, item] of Object.entries(decoratorProps)) {
|
|
844
|
+
// clickable 是设计器开关;空 style 只是噪音
|
|
845
|
+
if (key === 'clickable' || item === undefined) continue;
|
|
846
|
+
if (key === 'style' && (!item || (typeof item === 'object' && !Object.keys(item).length))) continue;
|
|
847
|
+
if (key === 'className' && !item) continue;
|
|
848
|
+
decorated[key] = item;
|
|
849
|
+
}
|
|
850
|
+
const own = node['x-component-props']?.className || decoratorProps.className;
|
|
851
|
+
if (own) decorated.className = own;
|
|
852
|
+
if (Object.keys(decorated).length) {
|
|
853
|
+
props.push(`decoratorProps={${literal(decorated, indent)}}`);
|
|
854
|
+
}
|
|
855
|
+
}
|
|
856
|
+
return props;
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
/**
|
|
860
|
+
* 一个组件该不该懒加载。
|
|
861
|
+
*
|
|
862
|
+
* 判定看组件源码的 import 图(见 transform/heaviness.mjs),不看名字。
|
|
863
|
+
* 只有「运行态一定用不到、但一旦静态引入就会把几 MB 拖进首屏」的组件
|
|
864
|
+
* 才懒加载;普通组件保持静态导入 —— 全量懒加载会多出一层 Suspense
|
|
865
|
+
* 边界和网络往返,首屏反而更慢。
|
|
866
|
+
*/
|
|
867
|
+
function heavinessOf(entry) {
|
|
868
|
+
if (!entry?.absPath) return { lazy: false, reasons: [] };
|
|
869
|
+
// 属性组件(`X.column`)是挂在默认导出上的成员,必须是同步值:
|
|
870
|
+
// React.lazy 得到的是组件本身,`Lazy.column` 取不到。
|
|
871
|
+
if (entry.memberAccess || entry.compoundMember || entry.componentSlot) return { lazy: false, reasons: [] };
|
|
872
|
+
return classifyComponent(entry.absPath);
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
function registerImport(entry) {
|
|
876
|
+
if (!entry) return null;
|
|
877
|
+
const isDefault = entry.hasNamedExport === false && entry.hasDefaultExport !== false;
|
|
878
|
+
const exportName = entry.exportName || entry.name;
|
|
879
|
+
let localName = entry.name;
|
|
880
|
+
// 同名字不同路径(极少见):加后缀区分
|
|
881
|
+
const matches = (item) => item.path === entry.importPath &&
|
|
882
|
+
item.exportName === exportName && item.isDefault === isDefault;
|
|
883
|
+
if ((extractPageConfig && localName === 'pageConfig') || (imports.has(localName) && !matches(imports.get(localName)))) {
|
|
884
|
+
localName = [...imports].find(([name, item]) => name.startsWith(`${entry.name}_`) && matches(item))?.[0]
|
|
885
|
+
?? `${entry.name}_${++uid}`;
|
|
886
|
+
}
|
|
887
|
+
if (!imports.has(localName)) {
|
|
888
|
+
const heavy = heavinessOf(entry);
|
|
889
|
+
imports.set(localName, {
|
|
890
|
+
name: localName,
|
|
891
|
+
exportName,
|
|
892
|
+
isDefault,
|
|
893
|
+
path: entry.importPath,
|
|
894
|
+
lazy: heavy.lazy,
|
|
895
|
+
});
|
|
896
|
+
if (heavy.lazy) {
|
|
897
|
+
lazyComponents.set(localName, { path: entry.importPath, reasons: heavy.reasons });
|
|
898
|
+
}
|
|
899
|
+
} else if (entry.memberAccess && imports.get(localName).lazy) {
|
|
900
|
+
// 同一组件既被当组件用、又被当命名空间用(`X` 和 `X.column` 同时出现):
|
|
901
|
+
// 退回静态导入,保证成员访问可用。体积上多付一次,但正确性优先。
|
|
902
|
+
imports.get(localName).lazy = false;
|
|
903
|
+
lazyComponents.delete(localName);
|
|
904
|
+
warnings.push(
|
|
905
|
+
`${entry.name} 同时以组件与命名空间成员的形式出现,已改为静态导入(失去按需加载)`,
|
|
906
|
+
);
|
|
907
|
+
}
|
|
908
|
+
return localName;
|
|
909
|
+
}
|
|
910
|
+
|
|
911
|
+
/** 按 x-index 排序子节点 */
|
|
912
|
+
function sortedChildren(node) {
|
|
913
|
+
const props = node?.properties || {};
|
|
914
|
+
return Object.entries(props)
|
|
915
|
+
.map(([key, child], i) => ({ key, child, order: child?.['x-index'] ?? i }))
|
|
916
|
+
.sort((a, b) => a.order - b.order);
|
|
917
|
+
}
|
|
918
|
+
|
|
919
|
+
function renderChildren(node, indent, kids) {
|
|
920
|
+
const list = kids || sortedChildren(node);
|
|
921
|
+
if (!list.length) return [];
|
|
922
|
+
const lines = [];
|
|
923
|
+
for (const { key, child } of list) {
|
|
924
|
+
lines.push(...renderNode(child, indent, key));
|
|
925
|
+
}
|
|
926
|
+
return lines;
|
|
927
|
+
}
|
|
928
|
+
|
|
929
|
+
/**
|
|
930
|
+
* 宿主组件不存在时的子节点渲染。
|
|
931
|
+
*
|
|
932
|
+
* 插槽节点(`X.Plugin`)本来要挂到宿主内部的位置,宿主不在就没有位置可挂。
|
|
933
|
+
* 若仍按普通节点递归,只会得到一层又一层嵌套的空占位 div;这里直接把插槽
|
|
934
|
+
* 自己的子节点铺到当前位置 —— 内容不丢,结构也不会被占位容器淹没。
|
|
935
|
+
*/
|
|
936
|
+
function renderChildrenFlattened(indent, list) {
|
|
937
|
+
const lines = [];
|
|
938
|
+
for (const { key, child } of list) {
|
|
939
|
+
if (slotKeyOf(child)) {
|
|
940
|
+
// 插槽宿主缺失时铺平插槽:插槽节点本身(`Card$outId.Plugin$outId`)渲染不出组件,
|
|
941
|
+
// 但它的 Container 装饰器还带着尺寸 —— 首页 `Card_2tgyxynuid7` 是
|
|
942
|
+
// `height: calc(100% - 40)`,正好等于「卡片头 40px 以下的高度」。
|
|
943
|
+
// 直接摊平子节点会把这一层高度一起丢掉,文件列表就顶到卡片头上去了。
|
|
944
|
+
const inner = renderChildren(child, indent + 1, sortedChildren(child));
|
|
945
|
+
const dp = child['x-decorator-props'] || {};
|
|
946
|
+
const style = dp.style;
|
|
947
|
+
const hasBox =
|
|
948
|
+
child['x-decorator'] === 'Container' &&
|
|
949
|
+
((style && typeof style === 'object' && Object.keys(style).length) || dp.className);
|
|
950
|
+
if (!hasBox) {
|
|
951
|
+
lines.push(...inner);
|
|
952
|
+
continue;
|
|
953
|
+
}
|
|
954
|
+
const pad = IND.repeat(indent);
|
|
955
|
+
// 这里的 div 同样是平台的 Container 装饰器产物(宿主没了,装饰器还在),
|
|
956
|
+
// 类名合成规则与 containerAttrs 一致,见那里的注释。
|
|
957
|
+
const attrs = domPropsToJsx(
|
|
958
|
+
{ className: ['inbiz-decorator-container', dp.className].filter(Boolean).join(' '), style },
|
|
959
|
+
indent,
|
|
960
|
+
);
|
|
961
|
+
lines.push(`${pad}<div${attrs.length ? ' ' + attrs.join(' ') : ''}>`);
|
|
962
|
+
lines.push(...inner);
|
|
963
|
+
lines.push(`${pad}</div>`);
|
|
964
|
+
} else {
|
|
965
|
+
lines.push(...renderNode(child, indent, key));
|
|
966
|
+
}
|
|
967
|
+
}
|
|
968
|
+
return lines;
|
|
969
|
+
}
|
|
970
|
+
|
|
971
|
+
/** 缺口汇总:组件名 → { base, count, plugin, sourcePath } */
|
|
972
|
+
const missingComponents = new Map();
|
|
973
|
+
|
|
974
|
+
function noteMissing(info) {
|
|
975
|
+
const rec = missingComponents.get(info.base) || {
|
|
976
|
+
base: info.base,
|
|
977
|
+
count: 0,
|
|
978
|
+
plugin: info.plugin?.name || '',
|
|
979
|
+
sourcePath: info.sourcePath || '',
|
|
980
|
+
};
|
|
981
|
+
rec.count += 1;
|
|
982
|
+
// 先遇到的那次可能没定位到源码,后来的定位到了就补上
|
|
983
|
+
if (!rec.sourcePath && info.sourcePath) rec.sourcePath = info.sourcePath;
|
|
984
|
+
missingComponents.set(info.base, rec);
|
|
985
|
+
}
|
|
986
|
+
|
|
987
|
+
/**
|
|
988
|
+
* 缺口汇总行(接在 warnings 末尾)。
|
|
989
|
+
*
|
|
990
|
+
* 逐节点的告警会散落在上百个页面之间,翻不动;汇总放在最后一条,
|
|
991
|
+
* 一眼就能看出「这次出码缺了哪几个组件、各几处、源码在不在本机」。
|
|
992
|
+
*/
|
|
993
|
+
function missingSummaryLine() {
|
|
994
|
+
if (!missingComponents.size) return [];
|
|
995
|
+
const items = [...missingComponents.values()].sort(
|
|
996
|
+
(a, b) => b.count - a.count || a.base.localeCompare(b.base),
|
|
997
|
+
);
|
|
998
|
+
const total = items.reduce((n, i) => n + i.count, 0);
|
|
999
|
+
const detail = items.map((i) => {
|
|
1000
|
+
const owner = i.plugin ? `${i.plugin} 插件` : '无插件归属';
|
|
1001
|
+
const src = i.sourcePath ? `源码 ${i.sourcePath}` : '本机无源码';
|
|
1002
|
+
return `${i.base} ×${i.count}(${owner},${src})`;
|
|
1003
|
+
});
|
|
1004
|
+
return [
|
|
1005
|
+
`[组件缺口] ${items.length} 个组件未解析,共 ${total} 处,已渲染占位容器:` +
|
|
1006
|
+
detail.join(';'),
|
|
1007
|
+
];
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* 子节点是不是插槽:返回它在插槽注册表里的名字,不是则 null。
|
|
1012
|
+
*
|
|
1013
|
+
* 判据来自宿主组件的源码(registry 扫出来的 jsxSlots / widgetSlots):
|
|
1014
|
+
* jsxSlots `useSlot('Table.Query')` —— 宿主主动读某个具名插槽
|
|
1015
|
+
* widgetSlots `<RenderDropableWidget pluginName={'X.Y'}>` —— 宿主源码里的挂载点
|
|
1016
|
+
* 第二条是 Block.Content / PageTopBottomLayout.Content / ModalDesign.Body 这类
|
|
1017
|
+
* 「内容区」插槽的来源:它们不写 useSlot,以前只按 `X.Plugin` 后缀识别,
|
|
1018
|
+
* 于是整块子节点被当普通 children 塞给宿主,而宿主自己用 RenderDropableWidget
|
|
1019
|
+
* 去取 —— 内容区永远是空的(page/xinjianbanbencelve 的 Block 就是这样)。
|
|
1020
|
+
*
|
|
1021
|
+
* 后缀兜底(`plugin` / `solt` / `slot`)兼容源码无法确认的旧插件节点。
|
|
1022
|
+
*/
|
|
1023
|
+
function slotKeyOf(child, hostEntry) {
|
|
1024
|
+
const raw = child?.['x-component'];
|
|
1025
|
+
if (!raw || String(raw).indexOf('.') < 0) return null;
|
|
1026
|
+
const name = String(raw).split('.').map((part) => part.replace(/\$[0-9a-zA-Z-]+$/, '')).join('.');
|
|
1027
|
+
// 三种声明形态都算「宿主声明的插槽」:useSlot / RenderDropableWidget / slotName 静态属性。
|
|
1028
|
+
// 少了 componentSlots,`Table.BtnGroup` 不会被当成插槽子节点,
|
|
1029
|
+
// 会被当普通子组件渲染到插槽外面 —— 页面能显示,但按钮区不在 Table 的插槽里。
|
|
1030
|
+
if (!hostEntry?.memberAccess && (hostEntry?.jsxSlots?.has(name) || hostEntry?.widgetSlots?.has(name)
|
|
1031
|
+
|| hostEntry?.componentSlots?.has(name))) return name;
|
|
1032
|
+
const dot = name.lastIndexOf('.');
|
|
1033
|
+
const suffix = name.slice(dot + 1).toLowerCase();
|
|
1034
|
+
if (!/^(plugin|solt|slot)$/.test(suffix)) return null;
|
|
1035
|
+
return name;
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
/**
|
|
1039
|
+
* 一个插槽节点 → 插槽值里的 JSX 行。
|
|
1040
|
+
*
|
|
1041
|
+
* 平台递归渲染选中的插槽节点。声明了挂载点且有真实预览导出的,
|
|
1042
|
+
* 要保留节点及其装饰器,不按 Plugin / Slot 等名字猜测它的用途。
|
|
1043
|
+
* 无法确认导出或兼容宿主不提供该成员时,才回退为内容。
|
|
1044
|
+
*/
|
|
1045
|
+
function renderSlotContent(child, key, slotKey, hostEntry, indent) {
|
|
1046
|
+
/**
|
|
1047
|
+
* 插槽有三种声明形态,判定「宿主确实声明了这个插槽」时三种都要认:
|
|
1048
|
+
* 1. `useSlot('X.Y')` → jsxSlots
|
|
1049
|
+
* 2. `<RenderDropableWidget/>` → widgetSlots
|
|
1050
|
+
* 3. `X.Query.slotName = 'X.Query'` → componentSlots
|
|
1051
|
+
*
|
|
1052
|
+
* 第 3 种是 AGENTS.md 要求的「有真实实现的插槽用该组件导出的 slotName 注册」。
|
|
1053
|
+
* 漏掉它时,`Table.BtnGroup` 这类只有 slotName 静态、没有 useSlot/RenderDropableWidget
|
|
1054
|
+
* 的插槽会被判成「未声明」,生成代码退化为不透明字符串键
|
|
1055
|
+
* `"Table.BtnGroup": (...)` —— 页面仍能显示,但失去了对具名导出的直接引用。
|
|
1056
|
+
*/
|
|
1057
|
+
const declared = !hostEntry?.memberAccess &&
|
|
1058
|
+
(hostEntry?.jsxSlots?.has(slotKey) || hostEntry?.widgetSlots?.has(slotKey)
|
|
1059
|
+
|| hostEntry?.componentSlots?.has(slotKey));
|
|
1060
|
+
const member = resolveComponent(registry, child['x-component']);
|
|
1061
|
+
|
|
1062
|
+
// 兼容层(@inbiz/containers/*)是 runtime 里按 children 重写的实现:
|
|
1063
|
+
// 组件库里的 `FormTab.LeftExtraContent` 这类成员在替身上并不存在,
|
|
1064
|
+
// 写成 `<FormTab.LeftExtraContent>` 会得到一个 undefined 元素(渲染即崩)。
|
|
1065
|
+
// 替身也不读插槽,所以这里只把内容铺进插槽值,绝不写成员壳子。
|
|
1066
|
+
const compatHost = String(hostEntry?.importPath || '').startsWith('@inbiz/containers/');
|
|
1067
|
+
|
|
1068
|
+
const renderable = member && (member.memberAccess || member.namedExports?.has(member.exportName));
|
|
1069
|
+
if (!declared || compatHost || !renderable) {
|
|
1070
|
+
return { inner: renderChildren(child, indent, sortedChildren(child)) };
|
|
1071
|
+
}
|
|
1072
|
+
// Some hosts use slot presence to decide whether to mount a whole region.
|
|
1073
|
+
// Keep the widget index but not the empty shell for those declared slots.
|
|
1074
|
+
const empty = hostEntry.slotsRequireChildren?.has(slotKey) && !Object.keys(child.properties || {}).length;
|
|
1075
|
+
if (empty && !hostEntry.widgetSlots?.has(slotKey)) return { inner: [] };
|
|
1076
|
+
|
|
1077
|
+
const restoredShell = /\.(plugin|solt|slot)$/i.test(slotKey) || hostEntry?.pluginIdSlots?.has(slotKey);
|
|
1078
|
+
const inner = empty ? [] : renderNode(child, indent, key, restoredShell);
|
|
1079
|
+
const local = member.componentSlot === slotKey && registerImport(member);
|
|
1080
|
+
const reference = local && (member.memberAccess ? `${local}.${member.memberAccess}` : local);
|
|
1081
|
+
return { inner, reference };
|
|
1082
|
+
}
|
|
1083
|
+
|
|
1084
|
+
/**
|
|
1085
|
+
* FormItem 装饰器 → 平台同款的 label/control 结构。
|
|
1086
|
+
*
|
|
1087
|
+
* 平台这条链路是:`x-decorator: 'FormItem'` → `createSchemaField` 里注册的
|
|
1088
|
+
* 自定义 FormItem(render/core/schemaField.tsx:71-128)→ `@formily/antd` 的
|
|
1089
|
+
* FormItem → antd 的 `.ant-formily-item` DOM。出码时展开为 JSX,
|
|
1090
|
+
* 但对已注册的字段,整层装饰器随字段一起显隐。
|
|
1091
|
+
*
|
|
1092
|
+
* 逐项对齐 `@formily/antd/esm/form-item/index.js` 的 `BaseItem`(109-186 行):
|
|
1093
|
+
*
|
|
1094
|
+
* <div class="ant-formily-item ant-formily-item-layout-X ..." style={decoratorProps.style}>
|
|
1095
|
+
* <div class="ant-formily-item-label ant-formily-item-item-col-N"> ← labelCol
|
|
1096
|
+
* <div class="ant-formily-item-label-content">
|
|
1097
|
+
* <span><span class="ant-formily-item-asterisk">*</span><label>标签</label></span>
|
|
1098
|
+
* </div>
|
|
1099
|
+
* <span class="ant-formily-item-colon">:</span>
|
|
1100
|
+
* </div>
|
|
1101
|
+
* <div class="ant-formily-item-control ant-formily-item-item-col-M"> ← wrapperCol
|
|
1102
|
+
* <div class="ant-formily-item-control-content">
|
|
1103
|
+
* <div class="ant-formily-item-control-content-component">…组件…</div>
|
|
1104
|
+
* </div>
|
|
1105
|
+
* </div>
|
|
1106
|
+
* </div>
|
|
1107
|
+
*
|
|
1108
|
+
* 必填星号的条件抄 `mapProps` 的 `takeAsterisk`(同文件 233 行):
|
|
1109
|
+
* `field.required && field.pattern !== 'readPretty'`。
|
|
1110
|
+
*
|
|
1111
|
+
* @param {string[]} inner 组件内容的 JSX 行
|
|
1112
|
+
*/
|
|
1113
|
+
function wrapFormItem(inner, { pad, indent, decoratorProps, className, title, required, pattern, compRaw, nameAttr }) {
|
|
1114
|
+
if (!inner.length) return inner;
|
|
1115
|
+
|
|
1116
|
+
const layout = formItemLayout(decoratorProps, queryForm);
|
|
1117
|
+
const hasLabel = Boolean(title);
|
|
1118
|
+
const asterisk = Boolean(required) && pattern !== 'readPretty';
|
|
1119
|
+
|
|
1120
|
+
// 平台 onFieldInit 会给每个字段的 decoratorProps.className 补上
|
|
1121
|
+
// `<componentType>Container`,再拼上平台名(sdk/index.ts:193/216)。
|
|
1122
|
+
// 页面级 CSS 就是按它选中的(例如 xinjianchangqu 的 `.BlockContainer`)。
|
|
1123
|
+
const wrapperClass = [
|
|
1124
|
+
'ant-formily-item',
|
|
1125
|
+
`ant-formily-item-layout-${layout.layout}`,
|
|
1126
|
+
'ant-formily-item-feedback-layout-loose',
|
|
1127
|
+
`ant-formily-item-label-align-${layout.labelAlign}`,
|
|
1128
|
+
'ant-formily-item-control-align-left',
|
|
1129
|
+
compRaw ? `${compRaw}Container` : '',
|
|
1130
|
+
'web',
|
|
1131
|
+
// 平台会把 x-component-props.className 并进 decoratorProps.className
|
|
1132
|
+
// (sdk/index.ts:215-219),页面级 CSS 有时就挂在这个类名上
|
|
1133
|
+
decoratorProps.className || '',
|
|
1134
|
+
className || '',
|
|
1135
|
+
]
|
|
1136
|
+
.filter(Boolean)
|
|
1137
|
+
.join(' ');
|
|
1138
|
+
|
|
1139
|
+
const one = IND.repeat(indent + 1);
|
|
1140
|
+
const two = IND.repeat(indent + 2);
|
|
1141
|
+
const three = IND.repeat(indent + 3);
|
|
1142
|
+
|
|
1143
|
+
const outerAttrs = [`className={${JSON.stringify(wrapperClass)}}`];
|
|
1144
|
+
const outerStyle = decoratorProps.style;
|
|
1145
|
+
if (outerStyle && typeof outerStyle === 'object' && Object.keys(outerStyle).length) {
|
|
1146
|
+
outerAttrs.push('style={' + valueJsx(outerStyle) + '}');
|
|
1147
|
+
}
|
|
1148
|
+
if (isScalar(nameAttr) && String(nameAttr) !== '') {
|
|
1149
|
+
// 平台把它写在 FormItem 上:props.children.props.controlId(schemaField.tsx:126)
|
|
1150
|
+
outerAttrs.push('data-component-key={' + JSON.stringify(String(nameAttr)) + '}');
|
|
1151
|
+
}
|
|
1152
|
+
|
|
1153
|
+
const lines = [`${pad}<div ${outerAttrs.join(' ')}>`];
|
|
1154
|
+
|
|
1155
|
+
if (hasLabel) {
|
|
1156
|
+
const labelClass = ['ant-formily-item-label'];
|
|
1157
|
+
if (layout.enableCol && layout.labelCol) {
|
|
1158
|
+
labelClass.push(`ant-formily-item-item-col-${layout.labelCol}`);
|
|
1159
|
+
}
|
|
1160
|
+
const labelAttrs = [`className={${JSON.stringify(labelClass.join(' '))}}`];
|
|
1161
|
+
if (layout.labelStyle && Object.keys(layout.labelStyle).length) {
|
|
1162
|
+
labelAttrs.push('style={' + valueJsx(layout.labelStyle) + '}');
|
|
1163
|
+
}
|
|
1164
|
+
lines.push(
|
|
1165
|
+
`${one}<div ${labelAttrs.join(' ')}>`,
|
|
1166
|
+
`${two}<div className="ant-formily-item-label-content">`,
|
|
1167
|
+
`${three}<span>`,
|
|
1168
|
+
);
|
|
1169
|
+
if (asterisk) lines.push(`${three} <span className="ant-formily-item-asterisk">{"*"}</span>`);
|
|
1170
|
+
// 标签文本走字面量而不是裸文本:词条文案里可能带 `{`/`}`,裸写会被当成 JSX 表达式
|
|
1171
|
+
lines.push(
|
|
1172
|
+
`${three} <label>{${valueJsx(localizeTitle(title))}}</label>`,
|
|
1173
|
+
`${three}</span>`,
|
|
1174
|
+
`${two}</div>`,
|
|
1175
|
+
);
|
|
1176
|
+
// Formily 的写法是「标签在就渲染冒号 span,colon=false 时留空」
|
|
1177
|
+
// (form-item/index.js:189)—— 留空而不是不渲染,因为 `-colon` 上的
|
|
1178
|
+
// margin-right:8px 就是标签与控件的间距,去掉会贴在一起。
|
|
1179
|
+
lines.push(`${two}<span className="ant-formily-item-colon">{${JSON.stringify(layout.colon ? ':' : '')}}</span>`);
|
|
1180
|
+
lines.push(`${one}</div>`);
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
const controlClass = ['ant-formily-item-control'];
|
|
1184
|
+
if (layout.enableCol && layout.wrapperCol && hasLabel) {
|
|
1185
|
+
controlClass.push(`ant-formily-item-item-col-${layout.wrapperCol}`);
|
|
1186
|
+
}
|
|
1187
|
+
const controlAttrs = [`className={${JSON.stringify(controlClass.join(' '))}}`];
|
|
1188
|
+
if (layout.wrapperStyle && Object.keys(layout.wrapperStyle).length) {
|
|
1189
|
+
controlAttrs.push('style={' + valueJsx(layout.wrapperStyle) + '}');
|
|
1190
|
+
}
|
|
1191
|
+
|
|
1192
|
+
lines.push(
|
|
1193
|
+
`${one}<div ${controlAttrs.join(' ')}>`,
|
|
1194
|
+
`${two}<div className="ant-formily-item-control-content">`,
|
|
1195
|
+
`${three}<div className="ant-formily-item-control-content-component">`,
|
|
1196
|
+
// inner 原本从 pad 开始,整体再下沉 4 级:label/control/content/component 各一层
|
|
1197
|
+
...inner.map((line) => IND.repeat(4) + line),
|
|
1198
|
+
`${three}</div>`,
|
|
1199
|
+
`${two}</div>`,
|
|
1200
|
+
`${one}</div>`,
|
|
1201
|
+
`${pad}</div>`,
|
|
1202
|
+
);
|
|
1203
|
+
|
|
1204
|
+
needsFormItem = true;
|
|
1205
|
+
return lines;
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
/**
|
|
1209
|
+
* 渲染单个 schema 节点 → JSX 文本行
|
|
1210
|
+
* @returns {string[]} 按行返回,便于缩进
|
|
1211
|
+
*/
|
|
1212
|
+
function renderNode(node, indent, fallbackName, slotShell = false) {
|
|
1213
|
+
if (!node || typeof node !== 'object') return [];
|
|
1214
|
+
|
|
1215
|
+
// x-platform-visible:只给哪一端看,是**静态条件**,生成期就判掉
|
|
1216
|
+
// (平台侧同样是渲染期按端过滤,与用户身份无关)。只出 web 端,
|
|
1217
|
+
// 不含 'web' 的节点直接不出现在产物里,也就不需要运行期再查一次。
|
|
1218
|
+
const platformVisible = node['x-platform-visible'];
|
|
1219
|
+
if (platformVisible !== undefined && platformVisible !== null) {
|
|
1220
|
+
const ends = Array.isArray(platformVisible) ? platformVisible : [platformVisible];
|
|
1221
|
+
if (!ends.map((item) => String(item)).includes('web')) return [];
|
|
1222
|
+
}
|
|
1223
|
+
|
|
1224
|
+
const pad = IND.repeat(indent);
|
|
1225
|
+
// 标题先按语言包查文案:产物没有 LocaleRoot,留着 `{multilingual}global.i18n-xxx`
|
|
1226
|
+
// 只会把词条 ID 显示到界面上(见 createMessageLookup 的说明)。
|
|
1227
|
+
const title = resolveTitleText(node.title, lookup);
|
|
1228
|
+
|
|
1229
|
+
// x-display:平台由 Formily 统一处理 —— ReactiveField 对 display !== 'visible'
|
|
1230
|
+
// 的节点直接 return null(@formily/react/esm/components/ReactiveField.js:64),
|
|
1231
|
+
// 连子节点一起不渲染。出码原先完全不看这个字段,被隐藏的区块/按钮照样画出来,
|
|
1232
|
+
// 页面上就多出本来不存在的行,把下面的间距整体顶开。
|
|
1233
|
+
//
|
|
1234
|
+
// 带 x-reactions 的节点例外:它的显隐由联动表达式在运行期改写,
|
|
1235
|
+
// 生成期丢掉就再也回不来了,宁可先画出来并留告警。
|
|
1236
|
+
const display = node['x-display'];
|
|
1237
|
+
|
|
1238
|
+
// 子节点分成「真实子组件」和「插槽」两类。
|
|
1239
|
+
// 插槽内容要在宿主组件内部的固定位置渲染,不能当普通子组件处理。
|
|
1240
|
+
const allKids = sortedChildren(node);
|
|
1241
|
+
const compRaw = node['x-component'];
|
|
1242
|
+
const entry = resolveComponent(registry, compRaw);
|
|
1243
|
+
const slotKids = [];
|
|
1244
|
+
const normalKids = [];
|
|
1245
|
+
for (const k of allKids) {
|
|
1246
|
+
const slotKey = slotKeyOf(k.child, entry);
|
|
1247
|
+
if (slotKey) slotKids.push({ ...k, slotKey });
|
|
1248
|
+
else normalKids.push(k);
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
// 1) 容器装饰器:还原成带 style 的 div
|
|
1252
|
+
const decorator = node['x-decorator'];
|
|
1253
|
+
const decoratorProps = node['x-decorator-props'] || {};
|
|
1254
|
+
const isContainer = decorator === 'Container';
|
|
1255
|
+
const isFormItem = decorator === 'FormItem';
|
|
1256
|
+
let formItemInField = false;
|
|
1257
|
+
const style = decoratorProps.style;
|
|
1258
|
+
const className = node['x-component-props']?.className || decoratorProps.className;
|
|
1259
|
+
|
|
1260
|
+
// 2) 组件本身
|
|
1261
|
+
//
|
|
1262
|
+
const baseName = entry ? registerImport(entry) : null;
|
|
1263
|
+
const compName = baseName && entry.memberAccess ? `${baseName}.${entry.memberAccess}` : baseName;
|
|
1264
|
+
|
|
1265
|
+
// 宿主存在时插槽归宿主,不存在时按原顺序铺平(见 renderChildrenFlattened)
|
|
1266
|
+
const childLines = compName
|
|
1267
|
+
? renderChildren(node, indent + 1, normalKids)
|
|
1268
|
+
: renderChildrenFlattened(indent + 1, allKids);
|
|
1269
|
+
|
|
1270
|
+
// 3) props:业务 props 在前,展示 props 在后(后者可覆盖前者)
|
|
1271
|
+
//
|
|
1272
|
+
// 三层来源,优先级从低到高:
|
|
1273
|
+
// rootProps 节点根级字段(平台的 onFieldInit 从 field state 搬进 componentProps 的那批:
|
|
1274
|
+
// sourceModelUpdate→primarykey,以及日期区间那 48 组 start*/end* 默认值)
|
|
1275
|
+
// bizProps x-component-biz-props
|
|
1276
|
+
// viewProps x-component-props(作者在设计器里配的,最高)
|
|
1277
|
+
const rootProps = translateMultilingual(componentPropsFromNode(node), lookup);
|
|
1278
|
+
const bizProps = translateMultilingual(node['x-component-biz-props'] || {}, lookup);
|
|
1279
|
+
const viewProps = translateMultilingual(node['x-component-props'] || {}, lookup);
|
|
1280
|
+
const merged = { ...rootProps, ...bizProps, ...viewProps };
|
|
1281
|
+
// Only the selected preview may declare which published props do not apply.
|
|
1282
|
+
// Unknown props stay visible to TypeScript instead of being silently dropped.
|
|
1283
|
+
for (const prop of entry?.omitProps ?? []) delete merged[prop];
|
|
1284
|
+
const source = merged.sourceModel;
|
|
1285
|
+
if (dataSourceContracts && typeof source?.url === 'string' && Array.isArray(source.params)) {
|
|
1286
|
+
const endpoint = `${(source.method || 'POST').toUpperCase()} ${source.url}`;
|
|
1287
|
+
const required = dataSourceContracts[endpoint]?.requiredQuery;
|
|
1288
|
+
if (required) {
|
|
1289
|
+
merged.sourceModel = {
|
|
1290
|
+
...source,
|
|
1291
|
+
params: source.params.map(param => Object.hasOwn(required, param.name)
|
|
1292
|
+
? { ...param, required: true, requiredMessageKey: param.requiredMessageKey ?? required[param.name] }
|
|
1293
|
+
: param),
|
|
1294
|
+
};
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
|
|
1298
|
+
// FormGrid 会去读子节点的 schema(`child.props.schema['x-component-props'].gridSpan`,
|
|
1299
|
+
// components/form/layout/SyncComponents/FormGrid/preview/web/index.tsx:83)来算总列数。
|
|
1300
|
+
// 产物里的子节点是普通 JSX,没有 schema,那个 reduce 会抛
|
|
1301
|
+
// TypeError: Cannot read properties of undefined (reading 'x-component-props')
|
|
1302
|
+
// 且只在「恰好一列 + 列里 2 个以上表单项」时才走到 —— 正是 so 难排查的那类。
|
|
1303
|
+
//
|
|
1304
|
+
// 组件本身留了 `totalColumns` 入参(同文件 79 行:`totalColumns ? totalColumns : ...`),
|
|
1305
|
+
// 生成期把 gridSpan 求和填进去,等价于平台按 schema 现算的结果,也顺手把这段
|
|
1306
|
+
// 结构依赖从运行期挪到了生成期。
|
|
1307
|
+
if (String(compRaw).replace(/\$.*$/, '') === 'FormGrid' && merged.totalColumns === undefined) {
|
|
1308
|
+
merged.totalColumns = sortedChildren(node).reduce(
|
|
1309
|
+
(sum, { child }) => sum + (child?.['x-component-props']?.gridSpan ?? 1),
|
|
1310
|
+
0,
|
|
1311
|
+
);
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
const nameAttr = merged.name ?? (fallbackName || node.name);
|
|
1315
|
+
const formItemOptions = {
|
|
1316
|
+
pad,
|
|
1317
|
+
indent,
|
|
1318
|
+
decoratorProps,
|
|
1319
|
+
className: node['x-component-props']?.className,
|
|
1320
|
+
title,
|
|
1321
|
+
required: node.required === true,
|
|
1322
|
+
pattern: node['x-pattern'],
|
|
1323
|
+
compRaw,
|
|
1324
|
+
nameAttr,
|
|
1325
|
+
};
|
|
1326
|
+
|
|
1327
|
+
// 4) 组装
|
|
1328
|
+
const selfLines = [];
|
|
1329
|
+
// 占位容器已经自带外层 div,不能再套一层容器装饰器(否则样式重复、DOM 变深)
|
|
1330
|
+
let wrappedByPlaceholder = false;
|
|
1331
|
+
if (compName) {
|
|
1332
|
+
const configsBefore = pageConfig.size;
|
|
1333
|
+
const { title: configuredTitle, ...previewProps } = merged;
|
|
1334
|
+
const renderProp = (value, prop) => renderComponentProp(value, prop, indent + 1, compName, baseName, nameAttr);
|
|
1335
|
+
const attrs = propsToJsx(previewProps, renderProp);
|
|
1336
|
+
// 需要 InBiz 运行时对象的组件,显式传入(平台上由 schema scope 注入)
|
|
1337
|
+
//
|
|
1338
|
+
// 组件库里的 `X.Content` / `X.Header` 这类壳子
|
|
1339
|
+
// 多半写成 `<div {...props}>{children}</div>`,inbiz 是对象,展开到 DOM 上
|
|
1340
|
+
// 会让 React 每页告警 "Invalid value for prop `inbiz` on <div> tag"。
|
|
1341
|
+
// 宿主自己已经拿到了 inbiz,壳子不需要。
|
|
1342
|
+
if (injectsInbiz(entry)) {
|
|
1343
|
+
attrs.unshift('inbiz={inbiz}');
|
|
1344
|
+
needsInbiz = true;
|
|
1345
|
+
}
|
|
1346
|
+
// Layout fields have a platform event bus even when the subscribers live in
|
|
1347
|
+
// another region, an installed component, or a dynamically selected script.
|
|
1348
|
+
if (entry?.usesEmit || (bindLayoutFieldEvents && isScalar(nameAttr) && String(nameAttr) !== '')) {
|
|
1349
|
+
const eventKey = String(nameAttr ?? node['x-designable-id'] ?? compRaw);
|
|
1350
|
+
attrs.unshift('emit={inbiz(' + JSON.stringify(eventKey) + ').emit}');
|
|
1351
|
+
needsInbiz = true;
|
|
1352
|
+
}
|
|
1353
|
+
// title 走**普通 prop**传下去,和平台上组件拿到的形状一致:
|
|
1354
|
+
// { value, status } —— 组件按 `title.status` 判断要不要显示标题
|
|
1355
|
+
// (EformButton/preview/web/index.tsx:169 直接解引用,缺了会抛)
|
|
1356
|
+
// 以前的传递路径是 schema → field.title → componentProps.title,
|
|
1357
|
+
// 现在一步到位,生成期就把 value 翻成人话。
|
|
1358
|
+
if (node.title !== undefined && !entry.omitProps?.has('title')) {
|
|
1359
|
+
const titleProp = titlePropOf(node.title, lookup);
|
|
1360
|
+
const localized = titleProp && typeof titleProp === 'object'
|
|
1361
|
+
? { ...titleProp, value: localizeTitle(titleProp.value) }
|
|
1362
|
+
: localizeTitle(titleProp);
|
|
1363
|
+
attrs.push('title={' + valueJsx(localized, pageConfig.size > configsBefore ? indent + 1 : 0) + '}');
|
|
1364
|
+
} else if (configuredTitle !== undefined) {
|
|
1365
|
+
attrs.push(...propsToJsx({ title: configuredTitle }, renderProp));
|
|
1366
|
+
}
|
|
1367
|
+
// data-name 只接受字符串(数字也转一下):对象值挂到 DOM 上会触发
|
|
1368
|
+
// Invalid value for prop `data-name` on <div> tag
|
|
1369
|
+
if (isScalar(nameAttr) && String(nameAttr) !== '') {
|
|
1370
|
+
attrs.push('data-name={' + JSON.stringify(String(nameAttr)) + '}');
|
|
1371
|
+
}
|
|
1372
|
+
if (title) attrs.push('data-title={' + valueJsx(localizeTitle(title)) + '}');
|
|
1373
|
+
if (node['x-reactions']) {
|
|
1374
|
+
// x-reactions 是联动表达式,不能机械翻译,留可搜索标记
|
|
1375
|
+
attrs.push('data-reactions={"__REACTIONS__"}');
|
|
1376
|
+
warnings.push(`${fallbackName || node.name}: 存在 x-reactions 联动,需人工确认`);
|
|
1377
|
+
}
|
|
1378
|
+
|
|
1379
|
+
// 替代关系:替身不是等价物。被丢掉的 props 必须逐条写出来 ——
|
|
1380
|
+
// 界面上「控件在」但配置全按替身默认值跑,是排查成本最高的一类问题。
|
|
1381
|
+
if (entry.substitution) {
|
|
1382
|
+
const sub = entry.substitution;
|
|
1383
|
+
const dropped = Object.keys(merged).filter(
|
|
1384
|
+
(k) => !sub.accepts.includes(k) && !SUBSTITUTE_IGNORED.has(k),
|
|
1385
|
+
);
|
|
1386
|
+
selfLines.push(
|
|
1387
|
+
`${pad}{/* 组件替代:${sub.from} → ${sub.to}` +
|
|
1388
|
+
(dropped.length ? `;以下配置替身不识别:${dropped.join(', ')}` : '') +
|
|
1389
|
+
` */}`,
|
|
1390
|
+
);
|
|
1391
|
+
warnings.push(
|
|
1392
|
+
`[组件替代] ${sub.from} → ${sub.to}:` +
|
|
1393
|
+
(dropped.length
|
|
1394
|
+
? `以下 schema 配置替身不识别,将按替身默认值渲染:${dropped.join('、')}`
|
|
1395
|
+
: 'schema 传入的 props 全部覆盖') +
|
|
1396
|
+
`(依据:${sub.evidence})`,
|
|
1397
|
+
);
|
|
1398
|
+
}
|
|
1399
|
+
|
|
1400
|
+
const multiline = pageConfig.size > configsBefore;
|
|
1401
|
+
const close = childLines.length ? '>' : '/>';
|
|
1402
|
+
if (multiline) {
|
|
1403
|
+
selfLines.push(`${pad}<${compName}`, ...attrs.flatMap(attr => `${pad}${IND}${attr}`.split('\n')), `${pad}${close}`);
|
|
1404
|
+
} else {
|
|
1405
|
+
const props = attrs.length ? ' ' + attrs.join(' ') : '';
|
|
1406
|
+
selfLines.push(`${pad}<${compName}${props}${childLines.length ? close : ' ' + close}`);
|
|
1407
|
+
}
|
|
1408
|
+
if (childLines.length) {
|
|
1409
|
+
selfLines.push(...childLines);
|
|
1410
|
+
selfLines.push(`${pad}</${compName}>`);
|
|
1411
|
+
}
|
|
1412
|
+
|
|
1413
|
+
// 装饰器:容器装饰器翻译成真实的包裹 div,而不是运行时的 decoratorProps。
|
|
1414
|
+
//
|
|
1415
|
+
// 平台上的 `Container` 装饰器(common/decorator/Decorators/Container.tsx)
|
|
1416
|
+
// 就是 `<div className={classNames('inbiz-decorator-container', className)} style={{...style, position:'relative'}}>`,
|
|
1417
|
+
// 所以生成期可以一字不差地落成普通 JSX。`clickable` 是设计器的点击穿透开关,
|
|
1418
|
+
// 运行态没有意义(getEditor() 为假时它不产生任何效果),直接不进 DOM ——
|
|
1419
|
+
// 以前它被塞进 decoratorProps 再展开到 div 上,React 每页都会告警
|
|
1420
|
+
// "Received `true` for a non-boolean attribute `clickable`"。
|
|
1421
|
+
const boxAttrs = isContainer ? containerAttrs(
|
|
1422
|
+
decoratorProps, slotShell ? merged.className : className, title, indent, slotShell ? compRaw : undefined,
|
|
1423
|
+
) : [];
|
|
1424
|
+
const box = `<div${boxAttrs.length ? ' ' + boxAttrs.join(' ') : ''}>`;
|
|
1425
|
+
|
|
1426
|
+
// 需要表单状态的控件:套 FieldScope(只带运行时值,见 inbiz-runtime/src/field-scope.tsx)。
|
|
1427
|
+
// 其余组件(Message / News / Navigation / 纯布局)保持普通 JSX —— 它们不读
|
|
1428
|
+
// useField(),套一层只会平白多出一份表单字段和一次 createField。
|
|
1429
|
+
const scopeName = String(nameAttr || compRaw);
|
|
1430
|
+
if (needsFieldScope(entry, node, scopeName)) {
|
|
1431
|
+
const scopeProps = fieldScopeProps(node, scopeName, compRaw, decoratorProps, indent, entry);
|
|
1432
|
+
if (isContainer) scopeProps.push(`decorate={(control) => ${box}{control}</div>}`);
|
|
1433
|
+
if (isFormItem) {
|
|
1434
|
+
const decoratorPad = pad + IND.repeat(2);
|
|
1435
|
+
const lines = wrapFormItem([`${decoratorPad}{control}`], {
|
|
1436
|
+
...formItemOptions,
|
|
1437
|
+
pad: decoratorPad,
|
|
1438
|
+
indent: indent + 2,
|
|
1439
|
+
});
|
|
1440
|
+
scopeProps.push(`decorate={(control) => (\n${lines.join('\n')}\n${pad}${IND})}`);
|
|
1441
|
+
formItemInField = true;
|
|
1442
|
+
}
|
|
1443
|
+
const inner = selfLines.map((line) => IND + line);
|
|
1444
|
+
selfLines.length = 0;
|
|
1445
|
+
selfLines.push(
|
|
1446
|
+
`${pad}<FieldScope ${scopeProps.join(' ')}>`,
|
|
1447
|
+
...inner,
|
|
1448
|
+
`${pad}</FieldScope>`,
|
|
1449
|
+
);
|
|
1450
|
+
needsFields = true;
|
|
1451
|
+
} else if (isContainer) {
|
|
1452
|
+
const inner = selfLines.map((line) => IND + line);
|
|
1453
|
+
selfLines.length = 0;
|
|
1454
|
+
selfLines.push(`${pad}${box}`, ...inner, `${pad}</div>`);
|
|
1455
|
+
}
|
|
1456
|
+
|
|
1457
|
+
// 重型组件套 Suspense 边界。
|
|
1458
|
+
//
|
|
1459
|
+
// 每个实例单独一个边界,而不是在页面根部套一个:流程中心这类组件
|
|
1460
|
+
// 只是页面里的一块,包在局部时它加载期间页面其余部分照常可见,
|
|
1461
|
+
// 整体套一个边界会让整页空白几百毫秒。
|
|
1462
|
+
// 插槽壳子不能套 Suspense:`Host.Y` 是宿主对象上的属性,React.lazy 只
|
|
1463
|
+
// 交出组件本身,`Lazy.Y` 取不到(这类宿主也不会被判成重型组件,防御性跳过)
|
|
1464
|
+
if (!entry?.memberAccess && imports.get(baseName)?.lazy) {
|
|
1465
|
+
// 用 React.Suspense 而不是具名导入:每个生成文件本来就 `import React`,
|
|
1466
|
+
// 少一行 import,也不会和组件库里的同名导出撞车。
|
|
1467
|
+
const inner = selfLines.map((l) => IND + l);
|
|
1468
|
+
selfLines.length = 0;
|
|
1469
|
+
selfLines.push(
|
|
1470
|
+
`${pad}<React.Suspense fallback={<div className="app-async-placeholder" style={{ minHeight: 80 }} />}>`,
|
|
1471
|
+
...inner,
|
|
1472
|
+
`${pad}</React.Suspense>`,
|
|
1473
|
+
);
|
|
1474
|
+
}
|
|
1475
|
+
} else if (!compRaw) {
|
|
1476
|
+
// 没有 x-component:这只是 schema 里的一个分组节点(容器没配组件),
|
|
1477
|
+
// 不是「组件缺失」。套占位容器会把纯布局多包一层 div,样式全乱。
|
|
1478
|
+
selfLines.push(...childLines);
|
|
1479
|
+
} else {
|
|
1480
|
+
// 有 x-component 但解析不到:这是真的缺口。
|
|
1481
|
+
//
|
|
1482
|
+
// 以前这里只把子节点透出去:宿主自己的标题、样式、尺寸全蒸发,页面上
|
|
1483
|
+
// 只剩一片空白 —— 分不清「这里本该有东西」还是「设计上就没有」。
|
|
1484
|
+
// 现在渲染一个可辨识的占位容器,并在报告里汇总缺口:
|
|
1485
|
+
// data-component 原始 x-component(含插件 OutId),可回查 schema
|
|
1486
|
+
// data-missing-plugin 插件身份(ECM / Document …)
|
|
1487
|
+
// data-missing-source 本机上的插件源码目录,顺着一路就能找到实现
|
|
1488
|
+
const info = explainMissingComponent(compRaw);
|
|
1489
|
+
noteMissing(info);
|
|
1490
|
+
warnings.push(
|
|
1491
|
+
`[组件缺口] ${fallbackName || node.name}: ${compRaw} 未解析` +
|
|
1492
|
+
(info.plugin ? `(${info.plugin.name} 插件` : '(') +
|
|
1493
|
+
(info.sourcePath ? `,源码 ${info.sourcePath}` : ',本机无源码') +
|
|
1494
|
+
'),已渲染占位容器',
|
|
1495
|
+
);
|
|
1496
|
+
|
|
1497
|
+
// 占位容器同时承担容器装饰器的职责,样式照旧落在这个 div 上。
|
|
1498
|
+
// 节点本来有 Container 装饰器时,平台会在外面套一层带
|
|
1499
|
+
// `inbiz-decorator-container` 的 div;占位容器既然接了这层职责,
|
|
1500
|
+
// 类名也要一并接过来(min-height 20px 兜底,见 containerAttrs 注释)。
|
|
1501
|
+
const boxAttrs = domPropsToJsx(
|
|
1502
|
+
{
|
|
1503
|
+
className: [
|
|
1504
|
+
isContainer ? 'inbiz-decorator-container' : '',
|
|
1505
|
+
'app-component-placeholder',
|
|
1506
|
+
className,
|
|
1507
|
+
]
|
|
1508
|
+
.filter(Boolean)
|
|
1509
|
+
.join(' '),
|
|
1510
|
+
style,
|
|
1511
|
+
},
|
|
1512
|
+
indent,
|
|
1513
|
+
);
|
|
1514
|
+
const phAttrs = [...boxAttrs];
|
|
1515
|
+
phAttrs.push('data-component={' + JSON.stringify(String(compRaw)) + '}');
|
|
1516
|
+
phAttrs.push('data-missing-component={' + JSON.stringify(info.base) + '}');
|
|
1517
|
+
if (info.plugin) {
|
|
1518
|
+
phAttrs.push('data-missing-plugin={' + JSON.stringify(info.plugin.name) + '}');
|
|
1519
|
+
}
|
|
1520
|
+
if (info.sourceRel) {
|
|
1521
|
+
// 写插件相对路径(ECM/src/components/FormTab/Card)而不是本机绝对路径:
|
|
1522
|
+
// 生成文件要提交进版本库,绝对路径换台机器就会整片 diff
|
|
1523
|
+
phAttrs.push('data-missing-source={' + JSON.stringify(info.sourceRel) + '}');
|
|
1524
|
+
}
|
|
1525
|
+
if (isScalar(nameAttr) && String(nameAttr) !== '') {
|
|
1526
|
+
phAttrs.push('data-name={' + JSON.stringify(String(nameAttr)) + '}');
|
|
1527
|
+
}
|
|
1528
|
+
if (title) phAttrs.push('data-title={' + valueJsx(localizeTitle(title)) + '}');
|
|
1529
|
+
if (node['x-reactions']) {
|
|
1530
|
+
// 组件都不在了,联动表达式更不可能被翻译;留标记让人知道这块还欠逻辑
|
|
1531
|
+
phAttrs.push('data-reactions={"__REACTIONS__"}');
|
|
1532
|
+
warnings.push(`${fallbackName || node.name}: 存在 x-reactions 联动,需人工确认`);
|
|
1533
|
+
}
|
|
1534
|
+
|
|
1535
|
+
selfLines.push(
|
|
1536
|
+
`${pad}<div${phAttrs.length ? ' ' + phAttrs.join(' ') : ''}>`,
|
|
1537
|
+
...childLines,
|
|
1538
|
+
`${pad}</div>`,
|
|
1539
|
+
);
|
|
1540
|
+
wrappedByPlaceholder = true;
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
// 5) 插槽:用 <Slots> 把内容包在宿主组件外层,
|
|
1544
|
+
// 宿主内部的 RenderDropableWidget 按 pluginName 取到这段 JSX 渲染。
|
|
1545
|
+
// 与 renderSlotContent 的判定保持一致:只用 slotName 静态声明插槽的宿主
|
|
1546
|
+
// (如 Table)同样需要一个 <Slots> 包裹,否则其插槽内容无处可放。
|
|
1547
|
+
const ownsJSXSlots = !entry?.memberAccess &&
|
|
1548
|
+
(entry?.jsxSlots?.size || entry?.widgetSlots?.size || entry?.componentSlots?.size);
|
|
1549
|
+
if (compName && (slotKids.length || ownsJSXSlots)) {
|
|
1550
|
+
const slotPad = IND.repeat(indent + 1);
|
|
1551
|
+
const groups = new Map();
|
|
1552
|
+
for (const { slotKey, child, key } of slotKids) {
|
|
1553
|
+
const { inner, reference } = renderSlotContent(child, key, slotKey, entry, indent + 2);
|
|
1554
|
+
// An empty widget still occupies its original index.
|
|
1555
|
+
if (!inner.length && !entry.widgetSlots?.has(slotKey)) continue;
|
|
1556
|
+
if (!groups.has(slotKey)) groups.set(slotKey, []);
|
|
1557
|
+
groups.get(slotKey).push({ inner, key, reference });
|
|
1558
|
+
}
|
|
1559
|
+
const entries = Array.from(groups, ([slotKey, nodes]) => {
|
|
1560
|
+
const reference = nodes[0].reference;
|
|
1561
|
+
const slotProp = reference && nodes.every((node) => node.reference === reference)
|
|
1562
|
+
? `[${reference}.slotName]`
|
|
1563
|
+
: JSON.stringify(slotKey);
|
|
1564
|
+
const prop = `${slotPad}${slotProp}: `;
|
|
1565
|
+
if (nodes.length === 1 && !nodes[0].inner.length) return `${prop}null,`;
|
|
1566
|
+
if (nodes.length === 1 || !entry.widgetSlots?.has(slotKey)) {
|
|
1567
|
+
return `${prop}(\n${slotPad} <>\n${nodes.flatMap(({ inner }) => inner).join('\n')}\n${slotPad} </>\n${slotPad}),`;
|
|
1568
|
+
}
|
|
1569
|
+
return `${prop}[\n${nodes.map(({ inner, key }) => (
|
|
1570
|
+
inner.length ? `${slotPad} <React.Fragment key={${JSON.stringify(key)}}>\n${inner.join('\n')}\n${slotPad} </React.Fragment>,` : `${slotPad} null,`
|
|
1571
|
+
)).join('\n')}\n${slotPad}],`;
|
|
1572
|
+
});
|
|
1573
|
+
// An empty named-slot host must not read an ancestor's slots.
|
|
1574
|
+
if (entries.length || ownsJSXSlots) {
|
|
1575
|
+
const Slots = registerImport({ name: 'Slots', importPath: '@macrowing/inbiz-runtime' });
|
|
1576
|
+
const wrapped = [
|
|
1577
|
+
...(entries.length
|
|
1578
|
+
? [`${pad}<${Slots} value={{`, ...entries, `${pad}}}>`]
|
|
1579
|
+
: [`${pad}<${Slots} value={{}}>`]),
|
|
1580
|
+
...selfLines.map((l) => IND + l),
|
|
1581
|
+
`${pad}</${Slots}>`,
|
|
1582
|
+
];
|
|
1583
|
+
selfLines.length = 0;
|
|
1584
|
+
selfLines.push(...wrapped);
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
|
|
1588
|
+
// 6) FormItem 装饰器
|
|
1589
|
+
//
|
|
1590
|
+
// 未解析的组件没有 FieldScope,仍保留占位项及标签用于诊断。
|
|
1591
|
+
if (isFormItem && !formItemInField) {
|
|
1592
|
+
const wrapped = wrapFormItem(selfLines, formItemOptions);
|
|
1593
|
+
selfLines.length = 0;
|
|
1594
|
+
selfLines.push(...wrapped);
|
|
1595
|
+
}
|
|
1596
|
+
|
|
1597
|
+
// 7) 容器包裹
|
|
1598
|
+
// 占位容器已经把自己写成了 div,再包一层只会让 DOM 变深、样式重复
|
|
1599
|
+
if (isContainer && !wrappedByPlaceholder && !compName) {
|
|
1600
|
+
// 这是原生 div:只走白名单属性,对象值(含 data-* 里的嵌套对象)
|
|
1601
|
+
// 一律不落 DOM,否则 React 会报 Invalid value for prop ... on <div> tag
|
|
1602
|
+
const boxAttrs = domPropsToJsx(
|
|
1603
|
+
{
|
|
1604
|
+
// 没配组件的纯布局节点也走平台 Container 装饰器:
|
|
1605
|
+
// `classNames('inbiz-decorator-container', className)`,见 containerAttrs 注释。
|
|
1606
|
+
className: ['inbiz-decorator-container', className].filter(Boolean).join(' '),
|
|
1607
|
+
style,
|
|
1608
|
+
'data-title': title ? localizeTitle(title) : undefined,
|
|
1609
|
+
},
|
|
1610
|
+
indent,
|
|
1611
|
+
);
|
|
1612
|
+
const head = `${pad}<div${boxAttrs.length ? ' ' + boxAttrs.join(' ') : ''}>`;
|
|
1613
|
+
return [head, ...selfLines, `${pad}</div>`];
|
|
1614
|
+
}
|
|
1615
|
+
|
|
1616
|
+
return selfLines;
|
|
1617
|
+
}
|
|
1618
|
+
|
|
1619
|
+
/** 渲染整个 schema(顶层 object) */
|
|
1620
|
+
function renderSchema(schema) {
|
|
1621
|
+
if (!schema) return '';
|
|
1622
|
+
return renderChildren(schema, 0).join('\n');
|
|
1623
|
+
}
|
|
1624
|
+
|
|
1625
|
+
/** 静态依赖的 import 语句 */
|
|
1626
|
+
function importBlock(runtimeExports = []) {
|
|
1627
|
+
const named = new Map();
|
|
1628
|
+
const defaults = new Map();
|
|
1629
|
+
for (const v of imports.values()) {
|
|
1630
|
+
if (v.lazy) continue;
|
|
1631
|
+
if (v.isDefault) {
|
|
1632
|
+
defaults.set(v.path, v.name);
|
|
1633
|
+
continue;
|
|
1634
|
+
}
|
|
1635
|
+
if (!named.has(v.path)) named.set(v.path, []);
|
|
1636
|
+
const spec = v.exportName === v.name ? v.name : `${v.exportName} as ${v.name}`;
|
|
1637
|
+
named.get(v.path).push(spec);
|
|
1638
|
+
}
|
|
1639
|
+
const runtimeNames = new Set([...runtimeExports, ...(named.get('@macrowing/inbiz-runtime') || [])]);
|
|
1640
|
+
if (needsFields) runtimeNames.add('FieldScope');
|
|
1641
|
+
if (needsTranslation) runtimeNames.add('translateAppText');
|
|
1642
|
+
if (needsInbiz) runtimeNames.add('useInbiz');
|
|
1643
|
+
if (runtimeNames.size) named.set('@macrowing/inbiz-runtime', [...runtimeNames]);
|
|
1644
|
+
const lines = [];
|
|
1645
|
+
// FormItem 的 DOM 是 `@formily/antd` 的 BaseItem(.ant-formily-item*),
|
|
1646
|
+
// 这套样式全仓没有 import 过 —— 平台在设计器侧引的,产物从来没人引。
|
|
1647
|
+
// 不引的话 label 和 control 会各自变成块级元素上下堆叠,标签宽度、
|
|
1648
|
+
// 必填星号、栅格列宽全部失效。
|
|
1649
|
+
if (needsFormItem) lines.push("import '@formily/antd/dist/antd.css';");
|
|
1650
|
+
for (const [p, localName] of defaults) {
|
|
1651
|
+
lines.push(`import ${localName} from '${p}';`);
|
|
1652
|
+
}
|
|
1653
|
+
for (const [p, specs] of named) {
|
|
1654
|
+
lines.push(`import { ${specs.sort().join(', ')} } from '${p}';`);
|
|
1655
|
+
}
|
|
1656
|
+
return lines.join('\n');
|
|
1657
|
+
}
|
|
1658
|
+
|
|
1659
|
+
/**
|
|
1660
|
+
* 重型组件的 `React.lazy` 声明,放在所有 import 之后。
|
|
1661
|
+
*
|
|
1662
|
+
* 静态 import 会让 webpack 把组件及其依赖(bpmn-js / echarts / x6 …)
|
|
1663
|
+
* 打进调用页面所在的同步 chunk —— 哪怕这个组件只是页面里的一块,
|
|
1664
|
+
* 甚至要点开弹窗才会渲染。改成动态 import 后它变成独立 chunk,
|
|
1665
|
+
* 只有真正渲染时才下载。
|
|
1666
|
+
*
|
|
1667
|
+
* 与静态导入使用同一份导出信息,保留模块自身的组件类型。
|
|
1668
|
+
*/
|
|
1669
|
+
function lazyBlock() {
|
|
1670
|
+
const lines = [];
|
|
1671
|
+
for (const v of imports.values()) {
|
|
1672
|
+
if (!v.lazy) continue;
|
|
1673
|
+
lines.push(
|
|
1674
|
+
`const ${v.name} = React.lazy(() =>\n` +
|
|
1675
|
+
` import('${v.path}')${v.isDefault ? '' : `.then((m) => ({ default: m.${v.exportName} }))`},\n` +
|
|
1676
|
+
`);`,
|
|
1677
|
+
);
|
|
1678
|
+
}
|
|
1679
|
+
return lines.join('\n');
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
return {
|
|
1683
|
+
renderSchema,
|
|
1684
|
+
renderNode,
|
|
1685
|
+
importBlock,
|
|
1686
|
+
lazyBlock,
|
|
1687
|
+
renderPageConfig,
|
|
1688
|
+
/**
|
|
1689
|
+
* 告警读的是「过程告警 + 缺口汇总」。
|
|
1690
|
+
*
|
|
1691
|
+
* 用 getter 而不是渲染时手工拼接:调用方(generate.mjs)都是在渲染完
|
|
1692
|
+
* 之后才读 warnings,汇总自然排在最后一行 —— 正好是报告里最显眼的位置。
|
|
1693
|
+
*/
|
|
1694
|
+
get warnings() {
|
|
1695
|
+
return warnings.concat(missingSummaryLine());
|
|
1696
|
+
},
|
|
1697
|
+
/** 缺口明细:组件名 → { base, count, plugin, sourcePath }(供上层统计) */
|
|
1698
|
+
get missingComponents() {
|
|
1699
|
+
return missingComponents;
|
|
1700
|
+
},
|
|
1701
|
+
imports,
|
|
1702
|
+
/** 被按需加载的组件:标识符 → { path, reasons } */
|
|
1703
|
+
lazyComponents,
|
|
1704
|
+
get needsInbiz() {
|
|
1705
|
+
return needsInbiz;
|
|
1706
|
+
},
|
|
1707
|
+
};
|
|
1708
|
+
}
|