@flowgram.ai/form-materials 0.2.0 → 0.2.2
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/esm/index.js +544 -107
- package/dist/esm/index.js.map +1 -1
- package/dist/index.d.mts +130 -39
- package/dist/index.d.ts +130 -39
- package/dist/index.js +544 -107
- package/dist/index.js.map +1 -1
- package/package.json +5 -4
- package/src/components/batch-variable-selector/index.tsx +3 -2
- package/src/components/condition-row/config.json +5 -0
- package/src/components/condition-row/constants.ts +123 -0
- package/src/components/condition-row/hooks/useOp.tsx +45 -0
- package/src/components/condition-row/hooks/useRule.ts +26 -0
- package/src/components/condition-row/index.tsx +71 -0
- package/src/components/condition-row/styles.tsx +25 -0
- package/src/components/condition-row/types.ts +37 -0
- package/src/components/constant-input/config.json +1 -1
- package/src/components/constant-input/types.ts +3 -3
- package/src/components/dynamic-value-input/index.tsx +13 -5
- package/src/components/index.ts +1 -0
- package/src/components/json-schema-editor/components/blur-input.tsx +22 -0
- package/src/components/json-schema-editor/config.json +1 -1
- package/src/components/json-schema-editor/hooks.tsx +2 -2
- package/src/components/json-schema-editor/index.tsx +7 -6
- package/src/components/json-schema-editor/types.ts +3 -3
- package/src/components/type-selector/config.json +1 -1
- package/src/components/type-selector/constants.tsx +2 -2
- package/src/components/type-selector/index.tsx +6 -6
- package/src/components/variable-selector/config.json +1 -1
- package/src/components/variable-selector/index.tsx +4 -4
- package/src/components/variable-selector/use-variable-tree.tsx +11 -5
- package/src/effects/auto-rename-ref/config.json +5 -0
- package/src/effects/auto-rename-ref/index.ts +104 -0
- package/src/effects/index.ts +1 -0
- package/src/typings/index.ts +1 -0
- package/src/typings/json-schema/config.json +5 -0
- package/src/typings/json-schema/index.ts +31 -0
- package/src/utils/index.ts +1 -0
- package/src/utils/json-schema/config.json +5 -0
- package/src/utils/json-schema/index.ts +161 -0
- package/src/components/type-selector/types.ts +0 -5
|
@@ -1,16 +1,18 @@
|
|
|
1
1
|
import React, { useCallback } from 'react';
|
|
2
2
|
|
|
3
|
-
import { useScopeAvailable, ASTMatch, BaseVariableField
|
|
3
|
+
import { useScopeAvailable, ASTMatch, BaseVariableField } from '@flowgram.ai/editor';
|
|
4
4
|
import { TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree';
|
|
5
5
|
import { Icon } from '@douyinfe/semi-ui';
|
|
6
6
|
|
|
7
7
|
import { ArrayIcons, VariableTypeIcons } from '../type-selector/constants';
|
|
8
|
+
import { JsonSchemaUtils } from '../../utils/json-schema';
|
|
9
|
+
import { IJsonSchema } from '../../typings/json-schema';
|
|
8
10
|
|
|
9
11
|
type VariableField = BaseVariableField<{ icon?: string | JSX.Element; title?: string }>;
|
|
10
12
|
|
|
11
13
|
export function useVariableTree(params: {
|
|
12
|
-
includeSchema?:
|
|
13
|
-
excludeSchema?:
|
|
14
|
+
includeSchema?: IJsonSchema | IJsonSchema[];
|
|
15
|
+
excludeSchema?: IJsonSchema | IJsonSchema[];
|
|
14
16
|
}): TreeNodeData[] {
|
|
15
17
|
const { includeSchema, excludeSchema } = params;
|
|
16
18
|
|
|
@@ -68,8 +70,12 @@ export function useVariableTree(params: {
|
|
|
68
70
|
const keyPath = [...parentFields.map((_field) => _field.key), variable.key];
|
|
69
71
|
const key = keyPath.join('.');
|
|
70
72
|
|
|
71
|
-
const isSchemaInclude = includeSchema
|
|
72
|
-
|
|
73
|
+
const isSchemaInclude = includeSchema
|
|
74
|
+
? JsonSchemaUtils.isASTMatchSchema(type, includeSchema)
|
|
75
|
+
: true;
|
|
76
|
+
const isSchemaExclude = excludeSchema
|
|
77
|
+
? JsonSchemaUtils.isASTMatchSchema(type, excludeSchema)
|
|
78
|
+
: false;
|
|
73
79
|
const isSchemaMatch = isSchemaInclude && !isSchemaExclude;
|
|
74
80
|
|
|
75
81
|
// If not match, and no children, return null
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { isArray, isObject } from 'lodash';
|
|
2
|
+
import {
|
|
3
|
+
DataEvent,
|
|
4
|
+
Effect,
|
|
5
|
+
EffectOptions,
|
|
6
|
+
VariableFieldKeyRenameService,
|
|
7
|
+
} from '@flowgram.ai/editor';
|
|
8
|
+
|
|
9
|
+
import { IFlowRefValue } from '../../typings';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Auto rename ref when form item's key is renamed
|
|
13
|
+
*
|
|
14
|
+
* Example:
|
|
15
|
+
*
|
|
16
|
+
* formMeta: {
|
|
17
|
+
* effects: {
|
|
18
|
+
* "inputsValues": autoRenameRefEffect,
|
|
19
|
+
* }
|
|
20
|
+
* }
|
|
21
|
+
*/
|
|
22
|
+
export const autoRenameRefEffect: EffectOptions[] = [
|
|
23
|
+
{
|
|
24
|
+
event: DataEvent.onValueInit,
|
|
25
|
+
effect: ((params) => {
|
|
26
|
+
const { context, form, name } = params;
|
|
27
|
+
|
|
28
|
+
const renameService = context.node.getService(VariableFieldKeyRenameService);
|
|
29
|
+
|
|
30
|
+
const disposable = renameService.onRename(({ before, after }) => {
|
|
31
|
+
const beforeKeyPath = [
|
|
32
|
+
...before.parentFields.map((_field) => _field.key).reverse(),
|
|
33
|
+
before.key,
|
|
34
|
+
];
|
|
35
|
+
const afterKeyPath = [
|
|
36
|
+
...after.parentFields.map((_field) => _field.key).reverse(),
|
|
37
|
+
after.key,
|
|
38
|
+
];
|
|
39
|
+
|
|
40
|
+
// traverse rename refs inside form item 'name'
|
|
41
|
+
traverseRef(name, form.getValueIn(name), (_drilldownName, _v) => {
|
|
42
|
+
if (isRefMatch(_v, beforeKeyPath)) {
|
|
43
|
+
_v.content = [...afterKeyPath, ...(_v.content || [])?.slice(beforeKeyPath.length)];
|
|
44
|
+
form.setValueIn(_drilldownName, _v);
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
return () => {
|
|
50
|
+
disposable.dispose();
|
|
51
|
+
};
|
|
52
|
+
}) as Effect,
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* If ref value's keyPath is the under as targetKeyPath
|
|
58
|
+
* @param value
|
|
59
|
+
* @param targetKeyPath
|
|
60
|
+
* @returns
|
|
61
|
+
*/
|
|
62
|
+
function isRefMatch(value: IFlowRefValue, targetKeyPath: string[]) {
|
|
63
|
+
return targetKeyPath.every((_key, index) => _key === value.content?.[index]);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* If value is ref
|
|
68
|
+
* @param value
|
|
69
|
+
* @returns
|
|
70
|
+
*/
|
|
71
|
+
function isRef(value: any): value is IFlowRefValue {
|
|
72
|
+
return (
|
|
73
|
+
value?.type === 'ref' && Array.isArray(value?.content) && typeof value?.content[0] === 'string'
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Traverse value to find ref
|
|
79
|
+
* @param value
|
|
80
|
+
* @param options
|
|
81
|
+
* @returns
|
|
82
|
+
*/
|
|
83
|
+
function traverseRef(name: string, value: any, cb: (name: string, _v: IFlowRefValue) => void) {
|
|
84
|
+
if (isObject(value)) {
|
|
85
|
+
if (isRef(value)) {
|
|
86
|
+
cb(name, value);
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
Object.entries(value).forEach(([_key, _value]) => {
|
|
91
|
+
traverseRef(`${name}.${_key}`, _value, cb);
|
|
92
|
+
});
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (isArray(value)) {
|
|
97
|
+
value.forEach((_value, idx) => {
|
|
98
|
+
traverseRef(`${name}[${idx}]`, _value, cb);
|
|
99
|
+
});
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return;
|
|
104
|
+
}
|
package/src/effects/index.ts
CHANGED
package/src/typings/index.ts
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
export type JsonSchemaBasicType =
|
|
2
|
+
| 'boolean'
|
|
3
|
+
| 'string'
|
|
4
|
+
| 'integer'
|
|
5
|
+
| 'number'
|
|
6
|
+
| 'object'
|
|
7
|
+
| 'array'
|
|
8
|
+
| 'map';
|
|
9
|
+
|
|
10
|
+
export interface IJsonSchema<T = string> {
|
|
11
|
+
type?: T;
|
|
12
|
+
default?: any;
|
|
13
|
+
title?: string;
|
|
14
|
+
description?: string;
|
|
15
|
+
enum?: (string | number)[];
|
|
16
|
+
properties?: Record<string, IJsonSchema<T>>;
|
|
17
|
+
additionalProperties?: IJsonSchema<T>;
|
|
18
|
+
items?: IJsonSchema<T>;
|
|
19
|
+
required?: string[];
|
|
20
|
+
$ref?: string;
|
|
21
|
+
extra?: {
|
|
22
|
+
index?: number;
|
|
23
|
+
// Used in BaseType.isEqualWithJSONSchema, the type comparison will be weak
|
|
24
|
+
weak?: boolean;
|
|
25
|
+
// Set the render component
|
|
26
|
+
formComponent?: string;
|
|
27
|
+
[key: string]: any;
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export type IBasicJsonSchema = IJsonSchema<JsonSchemaBasicType>;
|
package/src/utils/index.ts
CHANGED
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { get } from 'lodash';
|
|
2
|
+
import { ASTFactory, ASTKind, ASTMatch, ASTNode, ASTNodeJSON, BaseType } from '@flowgram.ai/editor';
|
|
3
|
+
|
|
4
|
+
import { IJsonSchema } from '../../typings/json-schema';
|
|
5
|
+
|
|
6
|
+
export namespace JsonSchemaUtils {
|
|
7
|
+
/**
|
|
8
|
+
* Converts a JSON schema to an Abstract Syntax Tree (AST) representation.
|
|
9
|
+
* This function recursively processes the JSON schema and creates corresponding AST nodes.
|
|
10
|
+
*
|
|
11
|
+
* For more information on JSON Schema, refer to the official documentation:
|
|
12
|
+
* https://json-schema.org/
|
|
13
|
+
*
|
|
14
|
+
* @param jsonSchema - The JSON schema to convert.
|
|
15
|
+
* @returns An AST node representing the JSON schema, or undefined if the schema type is not recognized.
|
|
16
|
+
*/
|
|
17
|
+
export function schemaToAST(jsonSchema: IJsonSchema): ASTNodeJSON | undefined {
|
|
18
|
+
const { type, extra } = jsonSchema || {};
|
|
19
|
+
const { weak = false } = extra || {};
|
|
20
|
+
|
|
21
|
+
if (!type) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
switch (type) {
|
|
26
|
+
case 'object':
|
|
27
|
+
if (weak) {
|
|
28
|
+
return { kind: ASTKind.Object, weak: true };
|
|
29
|
+
}
|
|
30
|
+
return ASTFactory.createObject({
|
|
31
|
+
properties: Object.entries(jsonSchema.properties || {})
|
|
32
|
+
/**
|
|
33
|
+
* Sorts the properties of a JSON schema based on the 'extra.index' field.
|
|
34
|
+
* If the 'extra.index' field is not present, the property will be treated as having an index of 0.
|
|
35
|
+
*/
|
|
36
|
+
.sort((a, b) => (get(a?.[1], 'extra.index') || 0) - (get(b?.[1], 'extra.index') || 0))
|
|
37
|
+
.map(([key, _property]) => ({
|
|
38
|
+
key,
|
|
39
|
+
type: schemaToAST(_property),
|
|
40
|
+
meta: { description: _property.description },
|
|
41
|
+
})),
|
|
42
|
+
});
|
|
43
|
+
case 'array':
|
|
44
|
+
if (weak) {
|
|
45
|
+
return { kind: ASTKind.Array, weak: true };
|
|
46
|
+
}
|
|
47
|
+
return ASTFactory.createArray({
|
|
48
|
+
items: schemaToAST(jsonSchema.items!),
|
|
49
|
+
});
|
|
50
|
+
case 'map':
|
|
51
|
+
if (weak) {
|
|
52
|
+
return { kind: ASTKind.Map, weak: true };
|
|
53
|
+
}
|
|
54
|
+
return ASTFactory.createMap({
|
|
55
|
+
valueType: schemaToAST(jsonSchema.additionalProperties!),
|
|
56
|
+
});
|
|
57
|
+
case 'string':
|
|
58
|
+
return ASTFactory.createString();
|
|
59
|
+
case 'number':
|
|
60
|
+
return ASTFactory.createNumber();
|
|
61
|
+
case 'boolean':
|
|
62
|
+
return ASTFactory.createBoolean();
|
|
63
|
+
case 'integer':
|
|
64
|
+
return ASTFactory.createInteger();
|
|
65
|
+
|
|
66
|
+
default:
|
|
67
|
+
// If the type is not recognized, return CustomType
|
|
68
|
+
return ASTFactory.createCustomType({ typeName: type });
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Convert AST To JSON Schema
|
|
74
|
+
* @param typeAST
|
|
75
|
+
* @returns
|
|
76
|
+
*/
|
|
77
|
+
export function astToSchema(
|
|
78
|
+
typeAST: ASTNode,
|
|
79
|
+
options?: { drilldown?: boolean }
|
|
80
|
+
): IJsonSchema | undefined {
|
|
81
|
+
const { drilldown = true } = options || {};
|
|
82
|
+
|
|
83
|
+
if (ASTMatch.isString(typeAST)) {
|
|
84
|
+
return {
|
|
85
|
+
type: 'string',
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
if (ASTMatch.isBoolean(typeAST)) {
|
|
90
|
+
return {
|
|
91
|
+
type: 'boolean',
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (ASTMatch.isNumber(typeAST)) {
|
|
96
|
+
return {
|
|
97
|
+
type: 'number',
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
if (ASTMatch.isInteger(typeAST)) {
|
|
102
|
+
return {
|
|
103
|
+
type: 'integer',
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (ASTMatch.isObject(typeAST)) {
|
|
108
|
+
return {
|
|
109
|
+
type: 'object',
|
|
110
|
+
properties: drilldown
|
|
111
|
+
? Object.fromEntries(
|
|
112
|
+
Object.entries(typeAST.properties).map(([key, value]) => [key, astToSchema(value)!])
|
|
113
|
+
)
|
|
114
|
+
: {},
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (ASTMatch.isArray(typeAST)) {
|
|
119
|
+
return {
|
|
120
|
+
type: 'array',
|
|
121
|
+
items: drilldown ? astToSchema(typeAST.items) : undefined,
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (ASTMatch.isMap(typeAST)) {
|
|
126
|
+
return {
|
|
127
|
+
type: 'map',
|
|
128
|
+
items: drilldown ? astToSchema(typeAST.valueType) : undefined,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (ASTMatch.isCustomType(typeAST)) {
|
|
133
|
+
return {
|
|
134
|
+
type: typeAST.typeName,
|
|
135
|
+
};
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return undefined;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Check if the AST type is match the JSON Schema
|
|
143
|
+
* @param typeAST
|
|
144
|
+
* @param schema
|
|
145
|
+
* @returns
|
|
146
|
+
*/
|
|
147
|
+
export function isASTMatchSchema(
|
|
148
|
+
typeAST: BaseType,
|
|
149
|
+
schema: IJsonSchema | IJsonSchema[]
|
|
150
|
+
): boolean {
|
|
151
|
+
if (Array.isArray(schema)) {
|
|
152
|
+
return typeAST.isTypeEqual(
|
|
153
|
+
ASTFactory.createUnion({
|
|
154
|
+
types: schema.map((_schema) => schemaToAST(_schema)!).filter(Boolean),
|
|
155
|
+
})
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return typeAST.isTypeEqual(schemaToAST(schema));
|
|
160
|
+
}
|
|
161
|
+
}
|