@xbeeant/form-engine-designer 0.0.3 → 0.0.4
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/dist/es/Canvas.js +42 -47
- package/dist/es/Canvas.js.map +1 -1
- package/dist/es/Designer.js +121 -98
- package/dist/es/Designer.js.map +1 -1
- package/dist/es/DesignerContext.js +86 -73
- package/dist/es/DesignerContext.js.map +1 -1
- package/dist/es/Palette.js +3 -3
- package/dist/es/Palette.js.map +1 -1
- package/dist/es/PropertyPanel.js +99 -100
- package/dist/es/PropertyPanel.js.map +1 -1
- package/dist/es/catalog.js +17 -10
- package/dist/es/catalog.js.map +1 -1
- package/dist/es/designer-context.js +110 -0
- package/dist/es/designer-context.js.map +1 -0
- package/dist/es/history.js +73 -0
- package/dist/es/history.js.map +1 -0
- package/dist/es/index.css +1 -1
- package/dist/es/index.js +3 -3
- package/dist/es/property/basic-property.js +6 -0
- package/dist/es/property/basic-property.js.map +1 -1
- package/dist/es/property-panel.js +294 -0
- package/dist/es/property-panel.js.map +1 -0
- package/dist/es/resizable-panel.js +103 -0
- package/dist/es/resizable-panel.js.map +1 -0
- package/dist/es/schema-utils.js +286 -0
- package/dist/es/schema-utils.js.map +1 -0
- package/dist/es/widgets/BindEditor.js +60 -49
- package/dist/es/widgets/BindEditor.js.map +1 -1
- package/dist/es/widgets/CodeEditor.js +41 -49
- package/dist/es/widgets/CodeEditor.js.map +1 -1
- package/dist/es/widgets/DefaultEditor.js +25 -30
- package/dist/es/widgets/DefaultEditor.js.map +1 -1
- package/dist/es/widgets/DependenciesEditor.js +21 -21
- package/dist/es/widgets/DependenciesEditor.js.map +1 -1
- package/dist/es/widgets/EnumEditor.js +25 -31
- package/dist/es/widgets/EnumEditor.js.map +1 -1
- package/dist/es/widgets/ExpressionSwitch.js +32 -30
- package/dist/es/widgets/ExpressionSwitch.js.map +1 -1
- package/dist/es/widgets/ReactionsEditor.js +103 -103
- package/dist/es/widgets/ReactionsEditor.js.map +1 -1
- package/dist/es/widgets/RemoteDataEditor.js +63 -63
- package/dist/es/widgets/RemoteDataEditor.js.map +1 -1
- package/dist/es/widgets/ValidateEditor.js +49 -49
- package/dist/es/widgets/ValidateEditor.js.map +1 -1
- package/dist/es/widgets/bind-editor.js +78 -0
- package/dist/es/widgets/bind-editor.js.map +1 -0
- package/dist/es/widgets/code-editor.js +76 -0
- package/dist/es/widgets/code-editor.js.map +1 -0
- package/dist/es/widgets/default-editor.js +50 -0
- package/dist/es/widgets/default-editor.js.map +1 -0
- package/dist/es/widgets/dependencies-editor.js +36 -0
- package/dist/es/widgets/dependencies-editor.js.map +1 -0
- package/dist/es/widgets/enum-editor.js +43 -0
- package/dist/es/widgets/enum-editor.js.map +1 -0
- package/dist/es/widgets/expression-builder.js +367 -0
- package/dist/es/widgets/expression-builder.js.map +1 -0
- package/dist/es/widgets/expression-model.js +340 -0
- package/dist/es/widgets/expression-model.js.map +1 -0
- package/dist/es/widgets/expression-switch.js +49 -0
- package/dist/es/widgets/expression-switch.js.map +1 -0
- package/dist/es/widgets/index.js +13 -13
- package/dist/es/widgets/index.js.map +1 -1
- package/dist/es/widgets/options-editor.js +102 -0
- package/dist/es/widgets/options-editor.js.map +1 -0
- package/dist/es/widgets/reactions-editor.js +401 -0
- package/dist/es/widgets/reactions-editor.js.map +1 -0
- package/dist/es/widgets/reactions-model.js +107 -0
- package/dist/es/widgets/reactions-model.js.map +1 -0
- package/dist/es/widgets/remote-data-editor.js +314 -0
- package/dist/es/widgets/remote-data-editor.js.map +1 -0
- package/dist/es/widgets/use-form-data-fields.js +16 -0
- package/dist/es/widgets/use-form-data-fields.js.map +1 -0
- package/dist/es/widgets/validate-editor.js +118 -0
- package/dist/es/widgets/validate-editor.js.map +1 -0
- package/package.json +4 -5
- package/dist/es/react/src/components/FormController.js +0 -160
- package/dist/es/react/src/components/FormController.js.map +0 -1
- package/dist/es/react/src/components/NexusField.js +0 -98
- package/dist/es/react/src/components/NexusField.js.map +0 -1
- package/dist/es/react/src/components/NexusForm.js +0 -128
- package/dist/es/react/src/components/NexusForm.js.map +0 -1
- package/dist/es/react/src/components/NexusFormProvider.js +0 -19
- package/dist/es/react/src/components/NexusFormProvider.js.map +0 -1
- package/dist/es/react/src/components/NexusLayout.js +0 -49
- package/dist/es/react/src/components/NexusLayout.js.map +0 -1
- package/dist/es/react/src/components/NexusObject.js +0 -65
- package/dist/es/react/src/components/NexusObject.js.map +0 -1
- package/dist/es/react/src/contexts/FieldInheritContext.js +0 -6
- package/dist/es/react/src/contexts/FieldInheritContext.js.map +0 -1
- package/dist/es/react/src/contexts/GridContext.js +0 -6
- package/dist/es/react/src/contexts/GridContext.js.map +0 -1
- package/dist/es/react/src/contexts/LayoutConfigContext.js +0 -6
- package/dist/es/react/src/contexts/LayoutConfigContext.js.map +0 -1
- package/dist/es/react/src/contexts/NexusContext.js +0 -13
- package/dist/es/react/src/contexts/NexusContext.js.map +0 -1
- package/dist/es/react/src/hooks/useForm.js +0 -13
- package/dist/es/react/src/hooks/useForm.js.map +0 -1
- package/dist/es/react/src/utils/renderTreeNode.js +0 -18
- package/dist/es/react/src/utils/renderTreeNode.js.map +0 -1
- package/dist/es/react/src/utils/resolveColSpan.js +0 -10
- package/dist/es/react/src/utils/resolveColSpan.js.map +0 -1
- package/src/Canvas.tsx +0 -1111
- package/src/Designer.css +0 -206
- package/src/Designer.tsx +0 -254
- package/src/DesignerContext.tsx +0 -192
- package/src/Palette.tsx +0 -261
- package/src/PropertyPanel.tsx +0 -486
- package/src/ResizablePanel.tsx +0 -210
- package/src/catalog.ts +0 -527
- package/src/index.ts +0 -25
- package/src/property/basic-property.ts +0 -126
- package/src/schemaUtils.test.ts +0 -261
- package/src/schemaUtils.ts +0 -627
- package/src/types.ts +0 -38
- package/src/vite-env.d.ts +0 -1
- package/src/widgets/BindEditor.tsx +0 -105
- package/src/widgets/CodeEditor.tsx +0 -114
- package/src/widgets/DefaultEditor.tsx +0 -73
- package/src/widgets/DependenciesEditor.tsx +0 -56
- package/src/widgets/EnumEditor.tsx +0 -66
- package/src/widgets/ExpressionBuilder.tsx +0 -509
- package/src/widgets/ExpressionSwitch.tsx +0 -62
- package/src/widgets/OptionsEditor.tsx +0 -141
- package/src/widgets/ReactionsEditor.tsx +0 -532
- package/src/widgets/RemoteDataEditor.tsx +0 -382
- package/src/widgets/ValidateEditor.tsx +0 -162
- package/src/widgets/expressionModel.test.ts +0 -262
- package/src/widgets/expressionModel.ts +0 -573
- package/src/widgets/index.ts +0 -45
- package/src/widgets/reactionsModel.ts +0 -190
- package/src/widgets/useFormDataFields.ts +0 -21
package/src/Canvas.tsx
DELETED
|
@@ -1,1111 +0,0 @@
|
|
|
1
|
-
// ============================================================================
|
|
2
|
-
// @xbeeant/form-engine-designer — 中间画布面板
|
|
3
|
-
// ============================================================================
|
|
4
|
-
|
|
5
|
-
import type { NexusSchema, SchemaNode } from '@xbeeant/form-engine';
|
|
6
|
-
import { isDataField, isLayoutNode } from '@xbeeant/form-engine';
|
|
7
|
-
import {
|
|
8
|
-
GridContext,
|
|
9
|
-
NexusField,
|
|
10
|
-
NexusForm,
|
|
11
|
-
type NexusFormConfig,
|
|
12
|
-
NexusFormProvider,
|
|
13
|
-
useForm,
|
|
14
|
-
} from '@xbeeant/form-engine-react';
|
|
15
|
-
import { Button, Empty, Modal, Space, Tag, Typography } from 'antd';
|
|
16
|
-
import type React from 'react';
|
|
17
|
-
import { memo, useCallback, useEffect, useMemo, useState } from 'react';
|
|
18
|
-
import { useDesigner } from './DesignerContext';
|
|
19
|
-
import {
|
|
20
|
-
generateKey,
|
|
21
|
-
getNodeAtProperties,
|
|
22
|
-
getNodeLabel,
|
|
23
|
-
getPropertiesOf,
|
|
24
|
-
moveNodeInSchema,
|
|
25
|
-
removeNodeFromSchema,
|
|
26
|
-
} from './schemaUtils';
|
|
27
|
-
import type { CatalogItem, FieldDef } from './types';
|
|
28
|
-
|
|
29
|
-
// ── 拖拽数据载荷 ────────────────────────────────────────────────────────────
|
|
30
|
-
interface PaletteDragPayload {
|
|
31
|
-
source: 'palette';
|
|
32
|
-
catalogItem: CatalogItem;
|
|
33
|
-
}
|
|
34
|
-
interface FieldDefDragPayload {
|
|
35
|
-
source: 'fieldDef';
|
|
36
|
-
fieldDef: FieldDef;
|
|
37
|
-
}
|
|
38
|
-
interface CanvasDragPayload {
|
|
39
|
-
source: 'canvas';
|
|
40
|
-
fromPath: string[];
|
|
41
|
-
}
|
|
42
|
-
type DragPayload = PaletteDragPayload | FieldDefDragPayload | CanvasDragPayload;
|
|
43
|
-
|
|
44
|
-
const DRAG_MIME = 'application/json';
|
|
45
|
-
|
|
46
|
-
// ── Drop 目标类型 ───────────────────────────────────────────────────────────
|
|
47
|
-
type DropTarget =
|
|
48
|
-
| { type: 'inside'; path: string[] } // 拖入容器内部(追加到末尾)
|
|
49
|
-
| { type: 'before'; path: string[] } // 插入到某节点之前
|
|
50
|
-
| { type: 'after'; path: string[] } // 插入到某节点之后
|
|
51
|
-
| null;
|
|
52
|
-
|
|
53
|
-
function pathEquals(a: string[] | null, b: string[]): boolean {
|
|
54
|
-
if (!a) {
|
|
55
|
-
return false;
|
|
56
|
-
}
|
|
57
|
-
if (a.length !== b.length) {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
60
|
-
return a.every((seg, i) => seg === b[i]);
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
function dropTargetEquals(a: DropTarget, b: DropTarget): boolean {
|
|
64
|
-
if (a === null && b === null) {
|
|
65
|
-
return true;
|
|
66
|
-
}
|
|
67
|
-
if (a === null || b === null) {
|
|
68
|
-
return false;
|
|
69
|
-
}
|
|
70
|
-
return a.type === b.type && pathEquals(a.path, b.path);
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
// ────────────────────────────────────────────────────────────────────────────
|
|
74
|
-
// 面板节点(tabPane / collapsePanel / step)与容器类型的映射
|
|
75
|
-
// 面板只能放入对应的 tabs / collapse / steps 容器(否则渲染结构非法);
|
|
76
|
-
// 普通节点直接拖入这些容器时自动包裹成新面板,保证运行时渲染结构合法。
|
|
77
|
-
// ────────────────────────────────────────────────────────────────────────────
|
|
78
|
-
|
|
79
|
-
/** 面板类型 → 唯一允许的父容器类型 */
|
|
80
|
-
const PANE_PARENT_TYPE: Record<string, string> = {
|
|
81
|
-
tabPane: 'tabs',
|
|
82
|
-
collapsePanel: 'collapse',
|
|
83
|
-
step: 'steps',
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
/** 容器类型 → 默认面板类型 */
|
|
87
|
-
const CONTAINER_PANE_TYPE: Record<string, string> = {
|
|
88
|
-
tabs: 'tabPane',
|
|
89
|
-
collapse: 'collapsePanel',
|
|
90
|
-
steps: 'step',
|
|
91
|
-
};
|
|
92
|
-
|
|
93
|
-
/** 面板默认标题 */
|
|
94
|
-
const PANE_DEFAULT_TITLE: Record<string, string> = {
|
|
95
|
-
tabPane: '标签页',
|
|
96
|
-
collapsePanel: '折叠面板',
|
|
97
|
-
step: '步骤',
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
/** 获取路径终点节点的容器类型(根路径返回 undefined) */
|
|
101
|
-
function getContainerTypeAt(
|
|
102
|
-
schema: NexusSchema,
|
|
103
|
-
path: string[],
|
|
104
|
-
): string | undefined {
|
|
105
|
-
if (path.length === 0) {
|
|
106
|
-
return undefined;
|
|
107
|
-
}
|
|
108
|
-
const node = getNodeAtProperties(schema.properties, path);
|
|
109
|
-
return node ? (node.type as string) : undefined;
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
/**
|
|
113
|
-
* 解析一次「新增节点」插入所需的节点构造器:
|
|
114
|
-
* - 面板节点(tabPane/collapsePanel/step):仅当目标父级是对应容器时放行,否则拒绝
|
|
115
|
-
* - 普通节点拖入 tabs/collapse/steps 容器:自动包裹成新面板
|
|
116
|
-
* - 其余情况:原样返回
|
|
117
|
-
*
|
|
118
|
-
* @param innerKey 外部字段拖入时使用的固定 key(作为包裹面板内部字段的 key)
|
|
119
|
-
* @returns 插入用构造器;返回 null 表示本次插入被拒绝
|
|
120
|
-
*/
|
|
121
|
-
function resolveInsertFactory(
|
|
122
|
-
schema: NexusSchema,
|
|
123
|
-
toParentPath: string[],
|
|
124
|
-
createNode: () => SchemaNode,
|
|
125
|
-
nodeType: string,
|
|
126
|
-
innerKey?: string,
|
|
127
|
-
): (() => SchemaNode) | null {
|
|
128
|
-
const parentType = getContainerTypeAt(schema, toParentPath);
|
|
129
|
-
|
|
130
|
-
const requiredParent = PANE_PARENT_TYPE[nodeType];
|
|
131
|
-
if (requiredParent) {
|
|
132
|
-
return parentType === requiredParent ? createNode : null;
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
const paneType = parentType ? CONTAINER_PANE_TYPE[parentType] : undefined;
|
|
136
|
-
if (paneType) {
|
|
137
|
-
return () =>
|
|
138
|
-
({
|
|
139
|
-
type: paneType,
|
|
140
|
-
title: PANE_DEFAULT_TITLE[paneType],
|
|
141
|
-
properties: {
|
|
142
|
-
[innerKey ?? generateKey({}, nodeType)]: createNode(),
|
|
143
|
-
},
|
|
144
|
-
}) as unknown as SchemaNode;
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
return createNode;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
/**
|
|
151
|
-
* 解析一次「移动已有节点」的落点:
|
|
152
|
-
* - 移动面板节点:仅允许放入对应容器(否则返回 null)
|
|
153
|
-
* - 普通节点移入 tabs/collapse/steps 容器:包裹成新面板
|
|
154
|
-
*
|
|
155
|
-
* @returns wrapped 为 true 时 node 是新建的包裹面板,需删除原节点
|
|
156
|
-
*/
|
|
157
|
-
function resolveMoveNode(
|
|
158
|
-
schema: NexusSchema,
|
|
159
|
-
fromPath: string[],
|
|
160
|
-
toParentPath: string[],
|
|
161
|
-
): { node: SchemaNode; wrapped: boolean } | null {
|
|
162
|
-
const node = getNodeAtProperties(schema.properties, fromPath);
|
|
163
|
-
if (!node) {
|
|
164
|
-
return null;
|
|
165
|
-
}
|
|
166
|
-
const parentType = getContainerTypeAt(schema, toParentPath);
|
|
167
|
-
const nodeType = node.type as string;
|
|
168
|
-
|
|
169
|
-
const requiredParent = PANE_PARENT_TYPE[nodeType];
|
|
170
|
-
if (requiredParent) {
|
|
171
|
-
return parentType === requiredParent ? { node, wrapped: false } : null;
|
|
172
|
-
}
|
|
173
|
-
|
|
174
|
-
const paneType = parentType ? CONTAINER_PANE_TYPE[parentType] : undefined;
|
|
175
|
-
if (paneType) {
|
|
176
|
-
return {
|
|
177
|
-
wrapped: true,
|
|
178
|
-
node: {
|
|
179
|
-
type: paneType,
|
|
180
|
-
title: PANE_DEFAULT_TITLE[paneType],
|
|
181
|
-
properties: {
|
|
182
|
-
[fromPath[fromPath.length - 1]]: node,
|
|
183
|
-
},
|
|
184
|
-
} as unknown as SchemaNode,
|
|
185
|
-
};
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
return { node, wrapped: false };
|
|
189
|
-
}
|
|
190
|
-
|
|
191
|
-
// 判断 targetPath 是否是 fromPath 自身或其后代
|
|
192
|
-
function isDescendantOrSelf(fromPath: string[], targetPath: string[]): boolean {
|
|
193
|
-
if (fromPath.length <= targetPath.length) {
|
|
194
|
-
return targetPath
|
|
195
|
-
.slice(0, fromPath.length)
|
|
196
|
-
.every((seg, i) => seg === fromPath[i]);
|
|
197
|
-
}
|
|
198
|
-
return false;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
// 判断是否允许拖入(纯函数:不依赖组件作用域)
|
|
202
|
-
function canDrop(fromPath: string[], targetPath: string[]): boolean {
|
|
203
|
-
if (isDescendantOrSelf(fromPath, targetPath)) {
|
|
204
|
-
return false;
|
|
205
|
-
}
|
|
206
|
-
const fromParent = fromPath.slice(0, -1);
|
|
207
|
-
return !(
|
|
208
|
-
fromParent.length === targetPath.length &&
|
|
209
|
-
fromParent.every((seg, i) => seg === targetPath[i])
|
|
210
|
-
);
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// 计算 insertIndex:将节点移动/新增到目标位置(纯函数:不依赖组件作用域)
|
|
214
|
-
function computeInsertIndex(
|
|
215
|
-
target: DropTarget,
|
|
216
|
-
schemaProps: Record<string, SchemaNode>,
|
|
217
|
-
): { toParentPath: string[]; insertIndex: number } | null {
|
|
218
|
-
if (!target || target.type === 'inside') {
|
|
219
|
-
return null;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const nodeIndex = getIndexInParent(schemaProps, target.path);
|
|
223
|
-
if (nodeIndex === -1) {
|
|
224
|
-
return null;
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
const toParentPath = target.path.slice(0, -1);
|
|
228
|
-
|
|
229
|
-
if (target.type === 'before') {
|
|
230
|
-
return { toParentPath, insertIndex: nodeIndex };
|
|
231
|
-
} else {
|
|
232
|
-
// after
|
|
233
|
-
return { toParentPath, insertIndex: nodeIndex + 1 };
|
|
234
|
-
}
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function resolveCatalogItem(
|
|
238
|
-
payload: CatalogItem,
|
|
239
|
-
widgetCatalog: CatalogItem[],
|
|
240
|
-
layoutCatalog: CatalogItem[],
|
|
241
|
-
): CatalogItem | undefined {
|
|
242
|
-
if (payload.category === 'widget') {
|
|
243
|
-
return widgetCatalog.find((c) => c.widget === payload.widget);
|
|
244
|
-
}
|
|
245
|
-
return layoutCatalog.find((c) => c.layoutType === payload.layoutType);
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
// 根据 widget 名推断 schema type(用于外部字段拖入时构造节点)
|
|
249
|
-
function inferSchemaType(widget: string): string {
|
|
250
|
-
if (['checkbox', 'switch'].includes(widget)) {
|
|
251
|
-
return 'boolean';
|
|
252
|
-
}
|
|
253
|
-
if (['number', 'slider'].includes(widget)) {
|
|
254
|
-
return 'number';
|
|
255
|
-
}
|
|
256
|
-
if (['checkboxes', 'multiSelect'].includes(widget)) {
|
|
257
|
-
return 'array';
|
|
258
|
-
}
|
|
259
|
-
if (widget === 'object') {
|
|
260
|
-
return 'object';
|
|
261
|
-
}
|
|
262
|
-
return 'string';
|
|
263
|
-
}
|
|
264
|
-
|
|
265
|
-
// 获取节点在其父级中的 index
|
|
266
|
-
function getIndexInParent(
|
|
267
|
-
schemaProps: Record<string, SchemaNode>,
|
|
268
|
-
path: string[],
|
|
269
|
-
): number {
|
|
270
|
-
const parentPath = path.slice(0, -1);
|
|
271
|
-
const key = path[path.length - 1];
|
|
272
|
-
const parentProps =
|
|
273
|
-
parentPath.length === 0
|
|
274
|
-
? schemaProps
|
|
275
|
-
: getNodeAtProperties(schemaProps, parentPath) &&
|
|
276
|
-
getPropertiesOf(getNodeAtProperties(schemaProps, parentPath)!);
|
|
277
|
-
if (!parentProps) {
|
|
278
|
-
return -1;
|
|
279
|
-
}
|
|
280
|
-
return Object.keys(parentProps).indexOf(key);
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
// 与 getParentProps 相同,但作用于指定 schema(用于在副本上定位 properties)
|
|
284
|
-
function getPropsAtPath(
|
|
285
|
-
targetSchema: NexusSchema,
|
|
286
|
-
parentPath: string[],
|
|
287
|
-
): Record<string, SchemaNode> | undefined {
|
|
288
|
-
if (parentPath.length === 0) {
|
|
289
|
-
return targetSchema.properties;
|
|
290
|
-
}
|
|
291
|
-
const parent = getNodeAtProperties(targetSchema.properties, parentPath);
|
|
292
|
-
return parent ? getPropertiesOf(parent) : undefined;
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
// 从事件中计算 drop 目标(避免状态延迟)
|
|
296
|
-
function computeDropTargetFromEvent(
|
|
297
|
-
e: React.DragEvent,
|
|
298
|
-
path: string[],
|
|
299
|
-
): DropTarget {
|
|
300
|
-
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
|
|
301
|
-
const y = e.clientY - rect.top;
|
|
302
|
-
const type: 'before' | 'after' = y < rect.height / 2 ? 'before' : 'after';
|
|
303
|
-
return { type, path };
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/** 路径数组 → 稳定字符串 key(JSON 序列化,任意 key 内容均安全) */
|
|
307
|
-
function toPathKey(path: string[]): string {
|
|
308
|
-
return JSON.stringify(path);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// ────────────────────────────────────────────────────────────────────────────
|
|
312
|
-
// CanvasActions — 下发给 CanvasNode 的回调集合
|
|
313
|
-
// 全部 useCallback 稳定化:schema/catalog 未变时引用不变,
|
|
314
|
-
// 配合 React.memo 让拖拽热路径(dragOver → setDropTarget)只重渲染受影响节点
|
|
315
|
-
// ────────────────────────────────────────────────────────────────────────────
|
|
316
|
-
|
|
317
|
-
interface CanvasActions {
|
|
318
|
-
onSelect: (path: string[]) => void;
|
|
319
|
-
onDelete: (path: string[]) => void;
|
|
320
|
-
onReorder: (path: string[], direction: 'up' | 'down') => void;
|
|
321
|
-
onDragStart: (e: React.DragEvent, path: string[]) => void;
|
|
322
|
-
onDragOver: (e: React.DragEvent, path: string[]) => void;
|
|
323
|
-
onNodeDrop: (e: React.DragEvent, path: string[]) => void;
|
|
324
|
-
onContainerDragOver: (e: React.DragEvent) => void;
|
|
325
|
-
onContainerDrop: (e: React.DragEvent, path: string[]) => void;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
interface CanvasNodeProps {
|
|
329
|
-
node: SchemaNode;
|
|
330
|
-
/** 节点在 schema 中的路径 key(JSON 字符串,跨渲染引用稳定) */
|
|
331
|
-
pathKey: string;
|
|
332
|
-
depth: number;
|
|
333
|
-
/** 祖先数据路径(布局节点 key 不进入,与 SchemaParser 对齐) */
|
|
334
|
-
parentDataPath: string;
|
|
335
|
-
/** 父级计算好的派生值:仅本节点相关(boolean/string,参与 memo 比较) */
|
|
336
|
-
isSelected: boolean;
|
|
337
|
-
dropTargetType: 'before' | 'after' | null;
|
|
338
|
-
/** 原始状态透传(供本节点计算其子节点的派生值;不参与 memo 比较) */
|
|
339
|
-
selectedPathKey: string | null;
|
|
340
|
-
dropTarget: DropTarget;
|
|
341
|
-
actions: CanvasActions;
|
|
342
|
-
}
|
|
343
|
-
|
|
344
|
-
// ────────────────────────────────────────────────────────────────────────────
|
|
345
|
-
// CanvasNode — 单个节点的递归渲染(memo 化,自定义比较器)
|
|
346
|
-
// 比较器只对比派生值(isSelected / dropTargetType)与稳定引用(node / pathKey /
|
|
347
|
-
// depth / parentDataPath / actions):拖拽过程中只有 drop 目标节点与选中节点
|
|
348
|
-
// 的派生值变化 → 其余子树整体跳过重渲染,热路径从 O(N) 降为 O(1)
|
|
349
|
-
// ────────────────────────────────────────────────────────────────────────────
|
|
350
|
-
|
|
351
|
-
const CanvasNode = memo(
|
|
352
|
-
function CanvasNode({
|
|
353
|
-
node,
|
|
354
|
-
pathKey,
|
|
355
|
-
depth,
|
|
356
|
-
parentDataPath,
|
|
357
|
-
isSelected,
|
|
358
|
-
dropTargetType,
|
|
359
|
-
selectedPathKey,
|
|
360
|
-
dropTarget,
|
|
361
|
-
actions,
|
|
362
|
-
}: CanvasNodeProps) {
|
|
363
|
-
const path = useMemo(() => JSON.parse(pathKey) as string[], [pathKey]);
|
|
364
|
-
const childProps = getPropertiesOf(node);
|
|
365
|
-
const hasChildren = childProps !== undefined;
|
|
366
|
-
const label = getNodeLabel(node, path[path.length - 1]);
|
|
367
|
-
const badge = node.widget || node.type;
|
|
368
|
-
// 计算数据路径:布局节点 key 不进入数据路径(与 SchemaParser 对齐)
|
|
369
|
-
const isLayout = isLayoutNode(node);
|
|
370
|
-
const dataPath = isLayout
|
|
371
|
-
? parentDataPath
|
|
372
|
-
: parentDataPath
|
|
373
|
-
? `${parentDataPath}.${path[path.length - 1]}`
|
|
374
|
-
: path[path.length - 1];
|
|
375
|
-
const childEntries = childProps ? Object.entries(childProps) : [];
|
|
376
|
-
const isField = isDataField(node);
|
|
377
|
-
|
|
378
|
-
return (
|
|
379
|
-
<div
|
|
380
|
-
key={pathKey}
|
|
381
|
-
className='mb-1.5 relative'
|
|
382
|
-
style={{ marginLeft: depth }}
|
|
383
|
-
>
|
|
384
|
-
{/* before 插入指示线 — 显示在容器上方 */}
|
|
385
|
-
{dropTargetType === 'before' && (
|
|
386
|
-
<div className='drop-indicator-line relative h-1 pointer-events-none z-10 -mt-0.5' />
|
|
387
|
-
)}
|
|
388
|
-
|
|
389
|
-
{/* 统一包裹容器:header + 预览/子节点 */}
|
|
390
|
-
<div
|
|
391
|
-
className={`relative rounded border bg-white transition-all ${
|
|
392
|
-
isSelected
|
|
393
|
-
? 'border-[#1677ff] bg-[#e6f4ff] shadow-[0_0_0_2px_rgba(22,119,255,0.1)]'
|
|
394
|
-
: 'border-[#e8e8e8] hover:border-[#1677ff]'
|
|
395
|
-
}`}
|
|
396
|
-
>
|
|
397
|
-
{/* header 行:拖拽 + 信息 + 操作 */}
|
|
398
|
-
<div
|
|
399
|
-
className='flex items-center gap-1.5 px-2 py-1.5 cursor-grab'
|
|
400
|
-
draggable
|
|
401
|
-
onDragStart={(e) => actions.onDragStart(e, path)}
|
|
402
|
-
onDragOver={(e) => actions.onDragOver(e, path)}
|
|
403
|
-
onDragLeave={(e) => {
|
|
404
|
-
e.stopPropagation();
|
|
405
|
-
}}
|
|
406
|
-
onDrop={(e) => actions.onNodeDrop(e, path)}
|
|
407
|
-
onClick={(e) => {
|
|
408
|
-
e.stopPropagation();
|
|
409
|
-
actions.onSelect(path);
|
|
410
|
-
}}
|
|
411
|
-
>
|
|
412
|
-
<Tag
|
|
413
|
-
color={hasChildren || isLayout ? 'blue' : undefined}
|
|
414
|
-
className='m-0! shrink-0'
|
|
415
|
-
>
|
|
416
|
-
{badge}
|
|
417
|
-
</Tag>
|
|
418
|
-
<Typography.Text className='flex-1 overflow-hidden text-ellipsis whitespace-nowrap'>
|
|
419
|
-
{label}
|
|
420
|
-
</Typography.Text>
|
|
421
|
-
<Typography.Text className='text-[#bfbfbf] text-[11px] font-mono shrink-0'>
|
|
422
|
-
{path[path.length - 1]}
|
|
423
|
-
</Typography.Text>
|
|
424
|
-
{isSelected && (
|
|
425
|
-
<span className='inline-flex gap-1'>
|
|
426
|
-
<Button
|
|
427
|
-
size='small'
|
|
428
|
-
onClick={(e) => {
|
|
429
|
-
e.stopPropagation();
|
|
430
|
-
actions.onReorder(path, 'up');
|
|
431
|
-
}}
|
|
432
|
-
>
|
|
433
|
-
↑
|
|
434
|
-
</Button>
|
|
435
|
-
<Button
|
|
436
|
-
size='small'
|
|
437
|
-
onClick={(e) => {
|
|
438
|
-
e.stopPropagation();
|
|
439
|
-
actions.onReorder(path, 'down');
|
|
440
|
-
}}
|
|
441
|
-
>
|
|
442
|
-
↓
|
|
443
|
-
</Button>
|
|
444
|
-
<Button
|
|
445
|
-
size='small'
|
|
446
|
-
danger
|
|
447
|
-
onClick={(e) => {
|
|
448
|
-
e.stopPropagation();
|
|
449
|
-
actions.onDelete(path);
|
|
450
|
-
}}
|
|
451
|
-
>
|
|
452
|
-
×
|
|
453
|
-
</Button>
|
|
454
|
-
</span>
|
|
455
|
-
)}
|
|
456
|
-
</div>
|
|
457
|
-
|
|
458
|
-
{/* 字段节点:通过 NexusField 渲染,复用引擎 reaction/联动 */}
|
|
459
|
-
{isField && (
|
|
460
|
-
<div className='px-2 pb-2'>
|
|
461
|
-
<NexusField
|
|
462
|
-
dataPath={dataPath}
|
|
463
|
-
layoutKey={path[path.length - 1]}
|
|
464
|
-
/>
|
|
465
|
-
</div>
|
|
466
|
-
)}
|
|
467
|
-
|
|
468
|
-
{/* 有子节点的容器:显示子节点 */}
|
|
469
|
-
{!isField &&
|
|
470
|
-
hasChildren &&
|
|
471
|
-
(() => {
|
|
472
|
-
const isGridNode = node.type === 'grid';
|
|
473
|
-
const isFlexNode = node.type === 'flex';
|
|
474
|
-
|
|
475
|
-
// 根据布局类型构建容器 CSS 样式
|
|
476
|
-
let containerStyle: React.CSSProperties | undefined;
|
|
477
|
-
let gridContextValue: { column: number } | null = null;
|
|
478
|
-
|
|
479
|
-
if (isGridNode) {
|
|
480
|
-
const column = Math.max(1, (node as any).column ?? 2);
|
|
481
|
-
const gap = (node as any).gap ?? 12;
|
|
482
|
-
containerStyle = {
|
|
483
|
-
display: 'grid',
|
|
484
|
-
gridTemplateColumns: `repeat(${column}, 1fr)`,
|
|
485
|
-
gap: `${gap}px`,
|
|
486
|
-
};
|
|
487
|
-
gridContextValue = { column };
|
|
488
|
-
} else if (isFlexNode) {
|
|
489
|
-
const gap = (node as any).gap ?? 8;
|
|
490
|
-
const direction = (node as any).direction ?? 'row';
|
|
491
|
-
const flexAlign = (node as any).align ?? 'flex-start';
|
|
492
|
-
const flexJustify = (node as any).justify ?? 'flex-start';
|
|
493
|
-
const flexWrap = (node as any).wrap ?? false;
|
|
494
|
-
containerStyle = {
|
|
495
|
-
display: 'flex',
|
|
496
|
-
flexDirection: direction === 'column' ? 'column' : 'row',
|
|
497
|
-
alignItems: flexAlign,
|
|
498
|
-
justifyContent: flexJustify,
|
|
499
|
-
flexWrap: flexWrap ? 'wrap' : 'nowrap',
|
|
500
|
-
gap: `${gap}px`,
|
|
501
|
-
};
|
|
502
|
-
}
|
|
503
|
-
|
|
504
|
-
const innerContent = (
|
|
505
|
-
<>
|
|
506
|
-
{childEntries.map(([childKey, childNode]) => {
|
|
507
|
-
const childPathKey = toPathKey([...path, childKey]);
|
|
508
|
-
const childIsSelected = selectedPathKey === childPathKey;
|
|
509
|
-
const childDropTargetType =
|
|
510
|
-
dropTarget &&
|
|
511
|
-
(dropTarget.type === 'before' ||
|
|
512
|
-
dropTarget.type === 'after') &&
|
|
513
|
-
dropTargetPathKey(dropTarget) === childPathKey
|
|
514
|
-
? dropTarget.type
|
|
515
|
-
: null;
|
|
516
|
-
return (
|
|
517
|
-
<CanvasNode
|
|
518
|
-
key={childPathKey}
|
|
519
|
-
node={childNode}
|
|
520
|
-
pathKey={childPathKey}
|
|
521
|
-
depth={depth + 1}
|
|
522
|
-
parentDataPath={dataPath}
|
|
523
|
-
isSelected={childIsSelected}
|
|
524
|
-
dropTargetType={childDropTargetType}
|
|
525
|
-
selectedPathKey={selectedPathKey}
|
|
526
|
-
dropTarget={dropTarget}
|
|
527
|
-
actions={actions}
|
|
528
|
-
/>
|
|
529
|
-
);
|
|
530
|
-
})}
|
|
531
|
-
{childEntries.length === 0 && (
|
|
532
|
-
<div className='text-[#bfbfbf] text-xs text-center py-2'>
|
|
533
|
-
拖拽组件到此处
|
|
534
|
-
</div>
|
|
535
|
-
)}
|
|
536
|
-
</>
|
|
537
|
-
);
|
|
538
|
-
|
|
539
|
-
const childDiv = (
|
|
540
|
-
<div
|
|
541
|
-
className='mx-2 mb-2 p-2 border border-dashed rounded min-h-8 bg-[rgba(250,250,250,0.6)] border-[#d9d9d9]'
|
|
542
|
-
style={containerStyle}
|
|
543
|
-
onClick={(e) => {
|
|
544
|
-
e.stopPropagation();
|
|
545
|
-
}}
|
|
546
|
-
onDragOver={actions.onContainerDragOver}
|
|
547
|
-
onDrop={(e) => actions.onContainerDrop(e, path)}
|
|
548
|
-
>
|
|
549
|
-
{innerContent}
|
|
550
|
-
</div>
|
|
551
|
-
);
|
|
552
|
-
|
|
553
|
-
// grid 需要提供 GridContext 给子项中的 NexusField 解析 colSpan
|
|
554
|
-
if (gridContextValue) {
|
|
555
|
-
return (
|
|
556
|
-
<GridContext.Provider value={gridContextValue}>
|
|
557
|
-
{childDiv}
|
|
558
|
-
</GridContext.Provider>
|
|
559
|
-
);
|
|
560
|
-
}
|
|
561
|
-
|
|
562
|
-
return childDiv;
|
|
563
|
-
})()}
|
|
564
|
-
</div>
|
|
565
|
-
|
|
566
|
-
{/* after 插入指示线 */}
|
|
567
|
-
{dropTargetType === 'after' && (
|
|
568
|
-
<div className='drop-indicator-line relative h-1 pointer-events-none z-10 -mb-0.5' />
|
|
569
|
-
)}
|
|
570
|
-
</div>
|
|
571
|
-
);
|
|
572
|
-
},
|
|
573
|
-
(prev, next) =>
|
|
574
|
-
prev.node === next.node &&
|
|
575
|
-
prev.pathKey === next.pathKey &&
|
|
576
|
-
prev.depth === next.depth &&
|
|
577
|
-
prev.parentDataPath === next.parentDataPath &&
|
|
578
|
-
prev.isSelected === next.isSelected &&
|
|
579
|
-
prev.dropTargetType === next.dropTargetType &&
|
|
580
|
-
prev.actions === next.actions,
|
|
581
|
-
);
|
|
582
|
-
|
|
583
|
-
/** 取 before/after 类型 drop 目标的路径 key(inside 目标无节点级指示) */
|
|
584
|
-
function dropTargetPathKey(target: DropTarget): string | null {
|
|
585
|
-
if (target === null || target.type === 'inside') {
|
|
586
|
-
return null;
|
|
587
|
-
}
|
|
588
|
-
return toPathKey(target.path);
|
|
589
|
-
}
|
|
590
|
-
|
|
591
|
-
export function Canvas() {
|
|
592
|
-
const {
|
|
593
|
-
schema,
|
|
594
|
-
selectedPath,
|
|
595
|
-
mode,
|
|
596
|
-
selectNode,
|
|
597
|
-
addNode,
|
|
598
|
-
removeNode,
|
|
599
|
-
setSchema,
|
|
600
|
-
registerUI,
|
|
601
|
-
widgetCatalog,
|
|
602
|
-
layoutCatalog,
|
|
603
|
-
} = useDesigner();
|
|
604
|
-
// 设计/预览各持有一个独立的 form 实例(engine):
|
|
605
|
-
// 设计态由下方 effect 手动 init,预览态由 <NexusForm> 内部 init,
|
|
606
|
-
// 二者互不干扰,避免切换模式时 fieldState/data 相互污染。
|
|
607
|
-
const [designForm] = useForm();
|
|
608
|
-
const [previewForm] = useForm();
|
|
609
|
-
const designEngine = designForm._getEngine();
|
|
610
|
-
const previewEngine = previewForm._getEngine();
|
|
611
|
-
const [dropTarget, setDropTarget] = useState<DropTarget>(null);
|
|
612
|
-
const [previewResult, setPreviewResult] = useState<Record<
|
|
613
|
-
string,
|
|
614
|
-
unknown
|
|
615
|
-
> | null>(null);
|
|
616
|
-
|
|
617
|
-
// 注册 widgets/layouts:仅供引擎消费一次,避免每次渲染重复注册
|
|
618
|
-
// registerUI 由使用方传入(如 registerAntdUI),解耦对具体 UI 库的依赖
|
|
619
|
-
useEffect(() => {
|
|
620
|
-
registerUI?.(designEngine);
|
|
621
|
-
registerUI?.(previewEngine);
|
|
622
|
-
}, [registerUI, designEngine, previewEngine]);
|
|
623
|
-
|
|
624
|
-
// 编辑模式下初始化引擎,使 NexusField 能读取 fieldState 并触发 reactions(联动)
|
|
625
|
-
// 预览模式由 <NexusForm> 内部负责 engine.init
|
|
626
|
-
useEffect(() => {
|
|
627
|
-
if (mode === 'preview') {
|
|
628
|
-
return;
|
|
629
|
-
}
|
|
630
|
-
const currentData = designEngine.getFormData();
|
|
631
|
-
designEngine.init(
|
|
632
|
-
schema,
|
|
633
|
-
Object.keys(currentData).length > 0 ? currentData : undefined,
|
|
634
|
-
);
|
|
635
|
-
}, [designEngine, schema, mode]);
|
|
636
|
-
|
|
637
|
-
const formConfig = useMemo<NexusFormConfig>(
|
|
638
|
-
() => ({
|
|
639
|
-
displayType: schema.displayType ?? 'row',
|
|
640
|
-
labelWidth: schema.labelWidth,
|
|
641
|
-
colon: schema.colon,
|
|
642
|
-
label: schema.label ?? true,
|
|
643
|
-
readOnly: schema.readOnly ?? false,
|
|
644
|
-
column: schema.column,
|
|
645
|
-
}),
|
|
646
|
-
[schema],
|
|
647
|
-
);
|
|
648
|
-
|
|
649
|
-
useEffect(() => {
|
|
650
|
-
const handleDragEnd = () => setDropTarget(null);
|
|
651
|
-
window.addEventListener('dragend', handleDragEnd);
|
|
652
|
-
return () => window.removeEventListener('dragend', handleDragEnd);
|
|
653
|
-
}, []);
|
|
654
|
-
|
|
655
|
-
const getParentProps = useCallback(
|
|
656
|
-
(parentPath: string[]) => {
|
|
657
|
-
if (parentPath.length === 0) {
|
|
658
|
-
return schema.properties;
|
|
659
|
-
}
|
|
660
|
-
const parent = getNodeAtProperties(schema.properties, parentPath);
|
|
661
|
-
return parent ? getPropertiesOf(parent) : undefined;
|
|
662
|
-
},
|
|
663
|
-
[schema],
|
|
664
|
-
);
|
|
665
|
-
|
|
666
|
-
// 统一处理 drop(依赖 schema,schema 变化时重建;拖拽过程中保持稳定)
|
|
667
|
-
const handleDrop = useCallback(
|
|
668
|
-
(e: React.DragEvent, target: DropTarget) => {
|
|
669
|
-
e.preventDefault();
|
|
670
|
-
e.stopPropagation();
|
|
671
|
-
setDropTarget(null);
|
|
672
|
-
if (!target) {
|
|
673
|
-
return;
|
|
674
|
-
}
|
|
675
|
-
|
|
676
|
-
const data = e.dataTransfer.getData(DRAG_MIME);
|
|
677
|
-
if (!data) {
|
|
678
|
-
return;
|
|
679
|
-
}
|
|
680
|
-
let payload: DragPayload;
|
|
681
|
-
try {
|
|
682
|
-
payload = JSON.parse(data) as DragPayload;
|
|
683
|
-
} catch {
|
|
684
|
-
return;
|
|
685
|
-
}
|
|
686
|
-
|
|
687
|
-
// ── 情况一:从画布拖动已有节点 ──
|
|
688
|
-
if (payload.source === 'canvas') {
|
|
689
|
-
const fromPath = payload.fromPath;
|
|
690
|
-
|
|
691
|
-
if (target.type === 'inside') {
|
|
692
|
-
// 拖入容器内部
|
|
693
|
-
if (!canDrop(fromPath, target.path)) {
|
|
694
|
-
return;
|
|
695
|
-
}
|
|
696
|
-
// 面板节点仅入对应容器;普通节点进 tabs/collapse/steps 时自动包裹
|
|
697
|
-
const resolved = resolveMoveNode(schema, fromPath, target.path);
|
|
698
|
-
if (!resolved) {
|
|
699
|
-
return;
|
|
700
|
-
}
|
|
701
|
-
if (!resolved.wrapped) {
|
|
702
|
-
setSchema(moveNodeInSchema(schema, fromPath, target.path));
|
|
703
|
-
} else {
|
|
704
|
-
let next = structuredClone(schema) as NexusSchema;
|
|
705
|
-
const toProps = getPropsAtPath(next, target.path);
|
|
706
|
-
if (!toProps) {
|
|
707
|
-
return;
|
|
708
|
-
}
|
|
709
|
-
const paneKey = generateKey(toProps, 'panel');
|
|
710
|
-
toProps[paneKey] = resolved.node;
|
|
711
|
-
next = removeNodeFromSchema(next, fromPath);
|
|
712
|
-
setSchema(next);
|
|
713
|
-
}
|
|
714
|
-
selectNode(null);
|
|
715
|
-
return;
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
// before / after
|
|
719
|
-
if (!canDrop(fromPath, target.path)) {
|
|
720
|
-
return;
|
|
721
|
-
}
|
|
722
|
-
const result = computeInsertIndex(target, schema.properties);
|
|
723
|
-
if (!result) {
|
|
724
|
-
return;
|
|
725
|
-
}
|
|
726
|
-
|
|
727
|
-
// 同父级移动时调整 insertIndex
|
|
728
|
-
const fromParent = fromPath.slice(0, -1);
|
|
729
|
-
const sameParent =
|
|
730
|
-
fromParent.length === result.toParentPath.length &&
|
|
731
|
-
fromParent.every((seg, i) => seg === result.toParentPath[i]);
|
|
732
|
-
|
|
733
|
-
let insertIndex = result.insertIndex;
|
|
734
|
-
if (sameParent) {
|
|
735
|
-
const fromIndex = getIndexInParent(schema.properties, fromPath);
|
|
736
|
-
if (fromIndex !== -1 && fromIndex < insertIndex) {
|
|
737
|
-
insertIndex -= 1;
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
|
|
741
|
-
const resolved = resolveMoveNode(schema, fromPath, result.toParentPath);
|
|
742
|
-
if (!resolved) {
|
|
743
|
-
return;
|
|
744
|
-
}
|
|
745
|
-
if (!resolved.wrapped) {
|
|
746
|
-
setSchema(
|
|
747
|
-
moveNodeInSchema(
|
|
748
|
-
schema,
|
|
749
|
-
fromPath,
|
|
750
|
-
result.toParentPath,
|
|
751
|
-
insertIndex,
|
|
752
|
-
),
|
|
753
|
-
);
|
|
754
|
-
} else {
|
|
755
|
-
let next = structuredClone(schema) as NexusSchema;
|
|
756
|
-
const toProps = getPropsAtPath(next, result.toParentPath);
|
|
757
|
-
if (!toProps) {
|
|
758
|
-
return;
|
|
759
|
-
}
|
|
760
|
-
const paneKey = generateKey(toProps, 'panel');
|
|
761
|
-
toProps[paneKey] = resolved.node;
|
|
762
|
-
next = removeNodeFromSchema(next, fromPath);
|
|
763
|
-
// 将包裹面板移动到目标插入位置
|
|
764
|
-
setSchema(
|
|
765
|
-
moveNodeInSchema(
|
|
766
|
-
next,
|
|
767
|
-
[...result.toParentPath, paneKey],
|
|
768
|
-
result.toParentPath,
|
|
769
|
-
insertIndex,
|
|
770
|
-
),
|
|
771
|
-
);
|
|
772
|
-
}
|
|
773
|
-
selectNode(null);
|
|
774
|
-
return;
|
|
775
|
-
}
|
|
776
|
-
|
|
777
|
-
// ── 情况二:从 palette 或 fieldDef 拖入新组件 ──
|
|
778
|
-
let base: string;
|
|
779
|
-
let createNode: () => SchemaNode;
|
|
780
|
-
let fixedKey: string | undefined;
|
|
781
|
-
|
|
782
|
-
if (payload.source === 'fieldDef') {
|
|
783
|
-
// 外部字段:id 作为 key,name 作为 title,且不允许修改
|
|
784
|
-
const fd = payload.fieldDef;
|
|
785
|
-
base = fd.widget;
|
|
786
|
-
fixedKey = fd.id;
|
|
787
|
-
createNode = () =>
|
|
788
|
-
({
|
|
789
|
-
type: inferSchemaType(fd.widget),
|
|
790
|
-
widget: fd.widget,
|
|
791
|
-
title: fd.name,
|
|
792
|
-
// 锁定标记:禁止在设计器中修改 key 和 title
|
|
793
|
-
_lockedKey: true,
|
|
794
|
-
_lockedTitle: true,
|
|
795
|
-
}) as unknown as SchemaNode;
|
|
796
|
-
} else {
|
|
797
|
-
// palette 组件目录
|
|
798
|
-
const found = resolveCatalogItem(
|
|
799
|
-
payload.catalogItem,
|
|
800
|
-
widgetCatalog,
|
|
801
|
-
layoutCatalog,
|
|
802
|
-
);
|
|
803
|
-
if (!found) {
|
|
804
|
-
return;
|
|
805
|
-
}
|
|
806
|
-
base = found.widget || found.layoutType || 'field';
|
|
807
|
-
createNode = () => found.createNode() as unknown as SchemaNode;
|
|
808
|
-
}
|
|
809
|
-
|
|
810
|
-
if (target.type === 'inside') {
|
|
811
|
-
const parentProps = getParentProps(target.path);
|
|
812
|
-
if (!parentProps) {
|
|
813
|
-
return;
|
|
814
|
-
}
|
|
815
|
-
// 面板类型校验 / 普通节点进面板容器时自动包裹
|
|
816
|
-
const factory = resolveInsertFactory(
|
|
817
|
-
schema,
|
|
818
|
-
target.path,
|
|
819
|
-
createNode,
|
|
820
|
-
base,
|
|
821
|
-
fixedKey,
|
|
822
|
-
);
|
|
823
|
-
if (!factory) {
|
|
824
|
-
return;
|
|
825
|
-
}
|
|
826
|
-
const key = fixedKey ?? generateKey(parentProps, base);
|
|
827
|
-
// 若 fixedKey 已存在,则跳过
|
|
828
|
-
if (fixedKey && key in parentProps) {
|
|
829
|
-
return;
|
|
830
|
-
}
|
|
831
|
-
addNode(target.path, key, factory());
|
|
832
|
-
return;
|
|
833
|
-
}
|
|
834
|
-
|
|
835
|
-
// before / after
|
|
836
|
-
const result = computeInsertIndex(target, schema.properties);
|
|
837
|
-
if (!result) {
|
|
838
|
-
return;
|
|
839
|
-
}
|
|
840
|
-
// 面板类型校验 / 普通节点进面板容器时自动包裹
|
|
841
|
-
const factory = resolveInsertFactory(
|
|
842
|
-
schema,
|
|
843
|
-
result.toParentPath,
|
|
844
|
-
createNode,
|
|
845
|
-
base,
|
|
846
|
-
fixedKey,
|
|
847
|
-
);
|
|
848
|
-
if (!factory) {
|
|
849
|
-
return;
|
|
850
|
-
}
|
|
851
|
-
|
|
852
|
-
// 在副本上操作:先 add 再 move
|
|
853
|
-
const next = structuredClone(schema) as NexusSchema;
|
|
854
|
-
const nextParentProps = getPropsAtPath(next, result.toParentPath);
|
|
855
|
-
if (!nextParentProps) {
|
|
856
|
-
return;
|
|
857
|
-
}
|
|
858
|
-
|
|
859
|
-
const newKey = fixedKey ?? generateKey(nextParentProps, base);
|
|
860
|
-
if (fixedKey && newKey in nextParentProps) {
|
|
861
|
-
return;
|
|
862
|
-
}
|
|
863
|
-
nextParentProps[newKey] = factory();
|
|
864
|
-
|
|
865
|
-
const fullNewPath = [...result.toParentPath, newKey];
|
|
866
|
-
setSchema(
|
|
867
|
-
moveNodeInSchema(
|
|
868
|
-
next,
|
|
869
|
-
fullNewPath,
|
|
870
|
-
result.toParentPath,
|
|
871
|
-
result.insertIndex,
|
|
872
|
-
),
|
|
873
|
-
);
|
|
874
|
-
},
|
|
875
|
-
[
|
|
876
|
-
schema,
|
|
877
|
-
widgetCatalog,
|
|
878
|
-
layoutCatalog,
|
|
879
|
-
addNode,
|
|
880
|
-
getParentProps,
|
|
881
|
-
setSchema,
|
|
882
|
-
selectNode,
|
|
883
|
-
],
|
|
884
|
-
);
|
|
885
|
-
|
|
886
|
-
// 节点 header 拖拽源 + drop 目标(仅依赖 setDropTarget,拖拽过程中引用稳定)
|
|
887
|
-
const handleNodeDragStart = useCallback(
|
|
888
|
-
(e: React.DragEvent, path: string[]) => {
|
|
889
|
-
e.stopPropagation();
|
|
890
|
-
const payload: CanvasDragPayload = { source: 'canvas', fromPath: path };
|
|
891
|
-
e.dataTransfer.setData(DRAG_MIME, JSON.stringify(payload));
|
|
892
|
-
e.dataTransfer.effectAllowed = 'move';
|
|
893
|
-
},
|
|
894
|
-
[],
|
|
895
|
-
);
|
|
896
|
-
|
|
897
|
-
// 基于鼠标 Y 坐标判断 before/after(仅依赖 setDropTarget,引用稳定)
|
|
898
|
-
const handleNodeDragOver = useCallback(
|
|
899
|
-
(e: React.DragEvent, path: string[]) => {
|
|
900
|
-
e.preventDefault();
|
|
901
|
-
e.stopPropagation();
|
|
902
|
-
const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
|
|
903
|
-
const y = e.clientY - rect.top;
|
|
904
|
-
const type: 'before' | 'after' = y < rect.height / 2 ? 'before' : 'after';
|
|
905
|
-
const newTarget: DropTarget = { type, path };
|
|
906
|
-
// 根据 effectAllowed 设置 dropEffect,避免 palette(copy) 与 canvas(move) 互斥
|
|
907
|
-
e.dataTransfer.dropEffect =
|
|
908
|
-
e.dataTransfer.effectAllowed === 'copy' ? 'copy' : 'move';
|
|
909
|
-
setDropTarget((prev) => {
|
|
910
|
-
if (dropTargetEquals(prev, newTarget)) {
|
|
911
|
-
return prev;
|
|
912
|
-
}
|
|
913
|
-
return newTarget;
|
|
914
|
-
});
|
|
915
|
-
},
|
|
916
|
-
[],
|
|
917
|
-
);
|
|
918
|
-
|
|
919
|
-
const handleNodeDrop = useCallback(
|
|
920
|
-
(e: React.DragEvent, path: string[]) => {
|
|
921
|
-
e.stopPropagation();
|
|
922
|
-
const target = computeDropTargetFromEvent(e, path);
|
|
923
|
-
handleDrop(e, target);
|
|
924
|
-
},
|
|
925
|
-
[handleDrop],
|
|
926
|
-
);
|
|
927
|
-
|
|
928
|
-
const handleDelete = useCallback(
|
|
929
|
-
(path: string[]) => {
|
|
930
|
-
removeNode(path);
|
|
931
|
-
},
|
|
932
|
-
[removeNode],
|
|
933
|
-
);
|
|
934
|
-
|
|
935
|
-
const handleSelect = useCallback(
|
|
936
|
-
(path: string[]) => {
|
|
937
|
-
selectNode(path);
|
|
938
|
-
},
|
|
939
|
-
[selectNode],
|
|
940
|
-
);
|
|
941
|
-
|
|
942
|
-
const handleReorder = useCallback(
|
|
943
|
-
(path: string[], direction: 'up' | 'down') => {
|
|
944
|
-
const parentPath = path.slice(0, -1);
|
|
945
|
-
const key = path[path.length - 1];
|
|
946
|
-
const parentProps = getParentProps(parentPath);
|
|
947
|
-
if (!parentProps) {
|
|
948
|
-
return;
|
|
949
|
-
}
|
|
950
|
-
const keys = Object.keys(parentProps);
|
|
951
|
-
const idx = keys.indexOf(key);
|
|
952
|
-
if (idx < 0) {
|
|
953
|
-
return;
|
|
954
|
-
}
|
|
955
|
-
if (direction === 'up' && idx === 0) {
|
|
956
|
-
return;
|
|
957
|
-
}
|
|
958
|
-
if (direction === 'down' && idx === keys.length - 1) {
|
|
959
|
-
return;
|
|
960
|
-
}
|
|
961
|
-
const insertIndex = direction === 'up' ? idx - 1 : idx + 1;
|
|
962
|
-
setSchema(moveNodeInSchema(schema, path, parentPath, insertIndex));
|
|
963
|
-
selectNode(path);
|
|
964
|
-
},
|
|
965
|
-
[getParentProps, schema, setSchema, selectNode],
|
|
966
|
-
);
|
|
967
|
-
|
|
968
|
-
// 容器空白区 dragOver:清除节点上的 drop target 指示
|
|
969
|
-
const handleContainerDragOver = useCallback((e: React.DragEvent) => {
|
|
970
|
-
e.preventDefault();
|
|
971
|
-
e.dataTransfer.dropEffect =
|
|
972
|
-
e.dataTransfer.effectAllowed === 'copy' ? 'copy' : 'move';
|
|
973
|
-
setDropTarget((prev) => {
|
|
974
|
-
if (prev === null) {
|
|
975
|
-
return prev;
|
|
976
|
-
}
|
|
977
|
-
return null;
|
|
978
|
-
});
|
|
979
|
-
}, []);
|
|
980
|
-
|
|
981
|
-
const handleContainerDrop = useCallback(
|
|
982
|
-
(e: React.DragEvent, path: string[]) => {
|
|
983
|
-
e.stopPropagation();
|
|
984
|
-
handleDrop(e, { type: 'inside', path });
|
|
985
|
-
},
|
|
986
|
-
[handleDrop],
|
|
987
|
-
);
|
|
988
|
-
|
|
989
|
-
// 下发给 CanvasNode 的稳定回调集合:
|
|
990
|
-
// 拖拽过程(dropTarget 变化)中所有回调引用不变 → 子节点 memo 生效
|
|
991
|
-
const actions = useMemo<CanvasActions>(
|
|
992
|
-
() => ({
|
|
993
|
-
onSelect: handleSelect,
|
|
994
|
-
onDelete: handleDelete,
|
|
995
|
-
onReorder: handleReorder,
|
|
996
|
-
onDragStart: handleNodeDragStart,
|
|
997
|
-
onDragOver: handleNodeDragOver,
|
|
998
|
-
onNodeDrop: handleNodeDrop,
|
|
999
|
-
onContainerDragOver: handleContainerDragOver,
|
|
1000
|
-
onContainerDrop: handleContainerDrop,
|
|
1001
|
-
}),
|
|
1002
|
-
[
|
|
1003
|
-
handleSelect,
|
|
1004
|
-
handleDelete,
|
|
1005
|
-
handleReorder,
|
|
1006
|
-
handleNodeDragStart,
|
|
1007
|
-
handleNodeDragOver,
|
|
1008
|
-
handleNodeDrop,
|
|
1009
|
-
handleContainerDragOver,
|
|
1010
|
-
handleContainerDrop,
|
|
1011
|
-
],
|
|
1012
|
-
);
|
|
1013
|
-
|
|
1014
|
-
if (mode === 'preview') {
|
|
1015
|
-
return (
|
|
1016
|
-
<div className='flex-1 overflow-y-auto p-4 bg-[#f5f5f5] min-h-full border border-transparent box-border'>
|
|
1017
|
-
<NexusForm form={previewForm} schema={schema} />
|
|
1018
|
-
<div style={{ marginTop: 16, textAlign: 'center' }}>
|
|
1019
|
-
<Space>
|
|
1020
|
-
<Button
|
|
1021
|
-
type='primary'
|
|
1022
|
-
onClick={async () => {
|
|
1023
|
-
try {
|
|
1024
|
-
const errors = await previewForm.validateFields();
|
|
1025
|
-
if (errors.size > 0) {
|
|
1026
|
-
return;
|
|
1027
|
-
}
|
|
1028
|
-
const values = previewForm.getValues();
|
|
1029
|
-
setPreviewResult(values);
|
|
1030
|
-
} catch {
|
|
1031
|
-
// 校验失败,不弹窗
|
|
1032
|
-
}
|
|
1033
|
-
}}
|
|
1034
|
-
>
|
|
1035
|
-
提交
|
|
1036
|
-
</Button>
|
|
1037
|
-
<Button onClick={() => previewForm.resetFields()}>重置</Button>
|
|
1038
|
-
</Space>
|
|
1039
|
-
</div>
|
|
1040
|
-
<Modal
|
|
1041
|
-
title='表单数据'
|
|
1042
|
-
open={previewResult !== null}
|
|
1043
|
-
onCancel={() => setPreviewResult(null)}
|
|
1044
|
-
footer={[
|
|
1045
|
-
<Button key='close' onClick={() => setPreviewResult(null)}>
|
|
1046
|
-
关闭
|
|
1047
|
-
</Button>,
|
|
1048
|
-
]}
|
|
1049
|
-
width={600}
|
|
1050
|
-
>
|
|
1051
|
-
<pre
|
|
1052
|
-
style={{
|
|
1053
|
-
maxHeight: 400,
|
|
1054
|
-
overflow: 'auto',
|
|
1055
|
-
background: '#f5f5f5',
|
|
1056
|
-
padding: 12,
|
|
1057
|
-
borderRadius: 6,
|
|
1058
|
-
fontSize: 13,
|
|
1059
|
-
margin: 0,
|
|
1060
|
-
}}
|
|
1061
|
-
>
|
|
1062
|
-
{JSON.stringify(previewResult, null, 2)}
|
|
1063
|
-
</pre>
|
|
1064
|
-
</Modal>
|
|
1065
|
-
</div>
|
|
1066
|
-
);
|
|
1067
|
-
}
|
|
1068
|
-
|
|
1069
|
-
const rootEntries = Object.entries(schema.properties);
|
|
1070
|
-
|
|
1071
|
-
return (
|
|
1072
|
-
<NexusFormProvider
|
|
1073
|
-
form={designForm}
|
|
1074
|
-
engine={designEngine}
|
|
1075
|
-
config={formConfig}
|
|
1076
|
-
>
|
|
1077
|
-
<div
|
|
1078
|
-
className='flex-1 overflow-y-auto p-4 bg-[#f5f5f5] min-h-full border border-transparent box-border'
|
|
1079
|
-
onClick={() => selectNode(null)}
|
|
1080
|
-
onDragOver={handleContainerDragOver}
|
|
1081
|
-
onDrop={(e) => handleContainerDrop(e, [])}
|
|
1082
|
-
>
|
|
1083
|
-
<div>
|
|
1084
|
-
{rootEntries.map(([key, node]) => (
|
|
1085
|
-
<CanvasNode
|
|
1086
|
-
key={toPathKey([key])}
|
|
1087
|
-
node={node}
|
|
1088
|
-
pathKey={toPathKey([key])}
|
|
1089
|
-
depth={0}
|
|
1090
|
-
parentDataPath=''
|
|
1091
|
-
isSelected={pathEquals(selectedPath, [key])}
|
|
1092
|
-
dropTargetType={
|
|
1093
|
-
dropTarget &&
|
|
1094
|
-
(dropTarget.type === 'before' || dropTarget.type === 'after') &&
|
|
1095
|
-
pathEquals(dropTarget.path, [key])
|
|
1096
|
-
? dropTarget.type
|
|
1097
|
-
: null
|
|
1098
|
-
}
|
|
1099
|
-
selectedPathKey={selectedPath ? toPathKey(selectedPath) : null}
|
|
1100
|
-
dropTarget={dropTarget}
|
|
1101
|
-
actions={actions}
|
|
1102
|
-
/>
|
|
1103
|
-
))}
|
|
1104
|
-
{rootEntries.length === 0 && (
|
|
1105
|
-
<Empty description='拖拽组件到此处开始设计' />
|
|
1106
|
-
)}
|
|
1107
|
-
</div>
|
|
1108
|
-
</div>
|
|
1109
|
-
</NexusFormProvider>
|
|
1110
|
-
);
|
|
1111
|
-
}
|