@flowgram.ai/form-materials 0.1.0-alpha.8

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.
Files changed (60) hide show
  1. package/bin/index.ts +94 -0
  2. package/bin/materials.ts +137 -0
  3. package/bin/project.ts +86 -0
  4. package/dist/esm/index.js +1978 -0
  5. package/dist/esm/index.js.map +1 -0
  6. package/dist/index.d.mts +285 -0
  7. package/dist/index.d.ts +285 -0
  8. package/dist/index.js +2018 -0
  9. package/dist/index.js.map +1 -0
  10. package/package.json +72 -0
  11. package/src/components/batch-variable-selector/config.json +5 -0
  12. package/src/components/batch-variable-selector/index.tsx +19 -0
  13. package/src/components/condition-row/config.json +5 -0
  14. package/src/components/condition-row/constants.ts +123 -0
  15. package/src/components/condition-row/hooks/useOp.tsx +45 -0
  16. package/src/components/condition-row/hooks/useRule.ts +26 -0
  17. package/src/components/condition-row/index.tsx +71 -0
  18. package/src/components/condition-row/styles.tsx +25 -0
  19. package/src/components/condition-row/types.ts +37 -0
  20. package/src/components/constant-input/config.json +6 -0
  21. package/src/components/constant-input/index.tsx +81 -0
  22. package/src/components/constant-input/types.ts +18 -0
  23. package/src/components/dynamic-value-input/config.json +5 -0
  24. package/src/components/dynamic-value-input/index.tsx +85 -0
  25. package/src/components/dynamic-value-input/styles.tsx +19 -0
  26. package/src/components/index.ts +7 -0
  27. package/src/components/json-schema-editor/components/blur-input.tsx +22 -0
  28. package/src/components/json-schema-editor/config.json +5 -0
  29. package/src/components/json-schema-editor/default-value.tsx +130 -0
  30. package/src/components/json-schema-editor/hooks.tsx +161 -0
  31. package/src/components/json-schema-editor/index.tsx +256 -0
  32. package/src/components/json-schema-editor/styles.tsx +235 -0
  33. package/src/components/json-schema-editor/types.ts +21 -0
  34. package/src/components/json-schema-editor/utils.ts +24 -0
  35. package/src/components/type-selector/config.json +5 -0
  36. package/src/components/type-selector/constants.tsx +359 -0
  37. package/src/components/type-selector/index.tsx +57 -0
  38. package/src/components/variable-selector/config.json +5 -0
  39. package/src/components/variable-selector/index.tsx +109 -0
  40. package/src/components/variable-selector/styles.tsx +43 -0
  41. package/src/components/variable-selector/use-variable-tree.tsx +101 -0
  42. package/src/effects/auto-rename-ref/config.json +5 -0
  43. package/src/effects/auto-rename-ref/index.ts +104 -0
  44. package/src/effects/index.ts +3 -0
  45. package/src/effects/provide-batch-input/config.json +5 -0
  46. package/src/effects/provide-batch-input/index.ts +38 -0
  47. package/src/effects/provide-batch-outputs/config.json +5 -0
  48. package/src/effects/provide-batch-outputs/index.ts +34 -0
  49. package/src/index.ts +4 -0
  50. package/src/typings/flow-value/config.json +5 -0
  51. package/src/typings/flow-value/index.ts +27 -0
  52. package/src/typings/index.ts +2 -0
  53. package/src/typings/json-schema/config.json +5 -0
  54. package/src/typings/json-schema/index.ts +31 -0
  55. package/src/utils/format-legacy-refs/config.json +5 -0
  56. package/src/utils/format-legacy-refs/index.ts +153 -0
  57. package/src/utils/format-legacy-refs/readme.md +38 -0
  58. package/src/utils/index.ts +2 -0
  59. package/src/utils/json-schema/config.json +5 -0
  60. package/src/utils/json-schema/index.ts +161 -0
@@ -0,0 +1,101 @@
1
+ import React, { useCallback } from 'react';
2
+
3
+ import { useScopeAvailable, ASTMatch, BaseVariableField } from '@flowgram.ai/editor';
4
+ import { TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree';
5
+ import { Icon } from '@douyinfe/semi-ui';
6
+
7
+ import { ArrayIcons, VariableTypeIcons } from '../type-selector/constants';
8
+ import { JsonSchemaUtils } from '../../utils/json-schema';
9
+ import { IJsonSchema } from '../../typings/json-schema';
10
+
11
+ type VariableField = BaseVariableField<{ icon?: string | JSX.Element; title?: string }>;
12
+
13
+ export function useVariableTree(params: {
14
+ includeSchema?: IJsonSchema | IJsonSchema[];
15
+ excludeSchema?: IJsonSchema | IJsonSchema[];
16
+ }): TreeNodeData[] {
17
+ const { includeSchema, excludeSchema } = params;
18
+
19
+ const available = useScopeAvailable();
20
+
21
+ const getVariableTypeIcon = useCallback((variable: VariableField) => {
22
+ if (variable.meta?.icon) {
23
+ if (typeof variable.meta.icon === 'string') {
24
+ return <img style={{ marginRight: 8 }} width={12} height={12} src={variable.meta.icon} />;
25
+ }
26
+
27
+ return variable.meta.icon;
28
+ }
29
+
30
+ const _type = variable.type;
31
+
32
+ if (ASTMatch.isArray(_type)) {
33
+ return (
34
+ <Icon
35
+ size="small"
36
+ svg={ArrayIcons[_type.items?.kind.toLowerCase()] || VariableTypeIcons.array}
37
+ />
38
+ );
39
+ }
40
+
41
+ if (ASTMatch.isCustomType(_type)) {
42
+ return <Icon size="small" svg={VariableTypeIcons[_type.typeName.toLowerCase()]} />;
43
+ }
44
+
45
+ return <Icon size="small" svg={VariableTypeIcons[variable.type?.kind.toLowerCase()]} />;
46
+ }, []);
47
+
48
+ const renderVariable = (
49
+ variable: VariableField,
50
+ parentFields: VariableField[] = []
51
+ ): TreeNodeData | null => {
52
+ let type = variable?.type;
53
+
54
+ if (!type) {
55
+ return null;
56
+ }
57
+
58
+ let children: TreeNodeData[] | undefined;
59
+
60
+ if (ASTMatch.isObject(type)) {
61
+ children = (type.properties || [])
62
+ .map((_property) => renderVariable(_property as VariableField, [...parentFields, variable]))
63
+ .filter(Boolean) as TreeNodeData[];
64
+
65
+ if (!children?.length) {
66
+ return null;
67
+ }
68
+ }
69
+
70
+ const keyPath = [...parentFields.map((_field) => _field.key), variable.key];
71
+ const key = keyPath.join('.');
72
+
73
+ const isSchemaInclude = includeSchema
74
+ ? JsonSchemaUtils.isASTMatchSchema(type, includeSchema)
75
+ : true;
76
+ const isSchemaExclude = excludeSchema
77
+ ? JsonSchemaUtils.isASTMatchSchema(type, excludeSchema)
78
+ : false;
79
+ const isSchemaMatch = isSchemaInclude && !isSchemaExclude;
80
+
81
+ // If not match, and no children, return null
82
+ if (!isSchemaMatch && !children?.length) {
83
+ return null;
84
+ }
85
+
86
+ return {
87
+ key: key,
88
+ label: variable.meta?.title || variable.key,
89
+ value: key,
90
+ keyPath,
91
+ icon: getVariableTypeIcon(variable),
92
+ children,
93
+ disabled: !isSchemaMatch,
94
+ rootMeta: parentFields[0]?.meta,
95
+ };
96
+ };
97
+
98
+ return [...available.variables.slice(0).reverse()]
99
+ .map((_variable) => renderVariable(_variable as VariableField))
100
+ .filter(Boolean) as TreeNodeData[];
101
+ }
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "auto-rename-ref",
3
+ "depMaterials": ["flow-value"],
4
+ "depPackages": ["lodash"]
5
+ }
@@ -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
+ }
@@ -0,0 +1,3 @@
1
+ export * from './provide-batch-input';
2
+ export * from './provide-batch-outputs';
3
+ export * from './auto-rename-ref';
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "provide-batch-input",
3
+ "depMaterials": ["flow-value"],
4
+ "depPackages": []
5
+ }
@@ -0,0 +1,38 @@
1
+ import {
2
+ ASTFactory,
3
+ EffectOptions,
4
+ FlowNodeRegistry,
5
+ createEffectFromVariableProvider,
6
+ getNodeForm,
7
+ } from '@flowgram.ai/editor';
8
+
9
+ import { IFlowRefValue } from '../../typings';
10
+
11
+ export const provideBatchInputEffect: EffectOptions[] = createEffectFromVariableProvider({
12
+ private: true,
13
+ parse: (value: IFlowRefValue, ctx) => [
14
+ ASTFactory.createVariableDeclaration({
15
+ key: `${ctx.node.id}_locals`,
16
+ meta: {
17
+ title: getNodeForm(ctx.node)?.getValueIn('title'),
18
+ icon: ctx.node.getNodeRegistry<FlowNodeRegistry>().info?.icon,
19
+ },
20
+ type: ASTFactory.createObject({
21
+ properties: [
22
+ ASTFactory.createProperty({
23
+ key: 'item',
24
+ initializer: ASTFactory.createEnumerateExpression({
25
+ enumerateFor: ASTFactory.createKeyPathExpression({
26
+ keyPath: value.content || [],
27
+ }),
28
+ }),
29
+ }),
30
+ ASTFactory.createProperty({
31
+ key: 'index',
32
+ type: ASTFactory.createNumber(),
33
+ }),
34
+ ],
35
+ }),
36
+ }),
37
+ ],
38
+ });
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "provide-batch-outputs",
3
+ "depMaterials": ["flow-value"],
4
+ "depPackages": []
5
+ }
@@ -0,0 +1,34 @@
1
+ import {
2
+ ASTFactory,
3
+ EffectOptions,
4
+ FlowNodeRegistry,
5
+ createEffectFromVariableProvider,
6
+ getNodeForm,
7
+ } from '@flowgram.ai/editor';
8
+
9
+ import { IFlowRefValue } from '../../typings';
10
+
11
+ export const provideBatchOutputsEffect: EffectOptions[] = createEffectFromVariableProvider({
12
+ private: true,
13
+ parse: (value: Record<string, IFlowRefValue>, ctx) => [
14
+ ASTFactory.createVariableDeclaration({
15
+ key: `${ctx.node.id}`,
16
+ meta: {
17
+ title: getNodeForm(ctx.node)?.getValueIn('title'),
18
+ icon: ctx.node.getNodeRegistry<FlowNodeRegistry>().info?.icon,
19
+ },
20
+ type: ASTFactory.createObject({
21
+ properties: Object.entries(value).map(([_key, value]) =>
22
+ ASTFactory.createProperty({
23
+ key: _key,
24
+ initializer: ASTFactory.createWrapArrayExpression({
25
+ wrapFor: ASTFactory.createKeyPathExpression({
26
+ keyPath: value.content || [],
27
+ }),
28
+ }),
29
+ })
30
+ ),
31
+ }),
32
+ }),
33
+ ],
34
+ });
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from './components';
2
+ export * from './effects';
3
+ export * from './utils';
4
+ export * from './typings';
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "flow-value",
3
+ "depMaterials": [],
4
+ "depPackages": []
5
+ }
@@ -0,0 +1,27 @@
1
+ export interface IFlowConstantValue {
2
+ type: 'constant';
3
+ content?: string | number | boolean;
4
+ }
5
+
6
+ export interface IFlowRefValue {
7
+ type: 'ref';
8
+ content?: string[];
9
+ }
10
+
11
+ export interface IFlowExpressionValue {
12
+ type: 'expression';
13
+ content?: string;
14
+ }
15
+
16
+ export interface IFlowTemplateValue {
17
+ type: 'template';
18
+ content?: string;
19
+ }
20
+
21
+ export type IFlowValue =
22
+ | IFlowConstantValue
23
+ | IFlowRefValue
24
+ | IFlowExpressionValue
25
+ | IFlowTemplateValue;
26
+
27
+ export type IFlowConstantRefValue = IFlowConstantValue | IFlowRefValue;
@@ -0,0 +1,2 @@
1
+ export * from './flow-value';
2
+ export * from './json-schema';
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "json-schema",
3
+ "depMaterials": [],
4
+ "depPackages": []
5
+ }
@@ -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>;
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "format-legacy-ref",
3
+ "depMaterials": [],
4
+ "depPackages": []
5
+ }
@@ -0,0 +1,153 @@
1
+ import { isObject } from 'lodash';
2
+
3
+ interface LegacyFlowRefValueSchema {
4
+ type: 'ref';
5
+ content: string;
6
+ }
7
+
8
+ interface NewFlowRefValueSchema {
9
+ type: 'ref';
10
+ content: string[];
11
+ }
12
+
13
+ /**
14
+ * In flowgram 0.2.0, for introducing Loop variable functionality,
15
+ * the FlowRefValueSchema type definition is updated:
16
+ *
17
+ * interface LegacyFlowRefValueSchema {
18
+ * type: 'ref';
19
+ * content: string;
20
+ * }
21
+ *
22
+ * interface NewFlowRefValueSchema {
23
+ * type: 'ref';
24
+ * content: string[];
25
+ * }
26
+ *
27
+ *
28
+ * For making sure backend json will not be changed, we provide format legacy ref utils for updating the formData
29
+ *
30
+ * How to use:
31
+ *
32
+ * 1. Call formatLegacyRefOnSubmit on the formData before submitting
33
+ * 2. Call formatLegacyRefOnInit on the formData after submitting
34
+ *
35
+ * Example:
36
+ * import { formatLegacyRefOnSubmit, formatLegacyRefOnInit } from '@flowgram.ai/form-materials';
37
+ * formMeta: {
38
+ * formatOnSubmit: (data) => formatLegacyRefOnSubmit(data),
39
+ * formatOnInit: (data) => formatLegacyRefOnInit(data),
40
+ * }
41
+ */
42
+ export function formatLegacyRefOnSubmit(value: any): any {
43
+ if (isObject(value)) {
44
+ if (isLegacyFlowRefValueSchema(value)) {
45
+ return formatLegacyRefToNewRef(value);
46
+ }
47
+
48
+ return Object.fromEntries(
49
+ Object.entries(value).map(([key, value]: [string, any]) => [
50
+ key,
51
+ formatLegacyRefOnSubmit(value),
52
+ ])
53
+ );
54
+ }
55
+
56
+ if (Array.isArray(value)) {
57
+ return value.map(formatLegacyRefOnSubmit);
58
+ }
59
+
60
+ return value;
61
+ }
62
+
63
+ /**
64
+ * In flowgram 0.2.0, for introducing Loop variable functionality,
65
+ * the FlowRefValueSchema type definition is updated:
66
+ *
67
+ * interface LegacyFlowRefValueSchema {
68
+ * type: 'ref';
69
+ * content: string;
70
+ * }
71
+ *
72
+ * interface NewFlowRefValueSchema {
73
+ * type: 'ref';
74
+ * content: string[];
75
+ * }
76
+ *
77
+ *
78
+ * For making sure backend json will not be changed, we provide format legacy ref utils for updating the formData
79
+ *
80
+ * How to use:
81
+ *
82
+ * 1. Call formatLegacyRefOnSubmit on the formData before submitting
83
+ * 2. Call formatLegacyRefOnInit on the formData after submitting
84
+ *
85
+ * Example:
86
+ * import { formatLegacyRefOnSubmit, formatLegacyRefOnInit } from '@flowgram.ai/form-materials';
87
+ *
88
+ * formMeta: {
89
+ * formatOnSubmit: (data) => formatLegacyRefOnSubmit(data),
90
+ * formatOnInit: (data) => formatLegacyRefOnInit(data),
91
+ * }
92
+ */
93
+ export function formatLegacyRefOnInit(value: any): any {
94
+ if (isObject(value)) {
95
+ if (isNewFlowRefValueSchema(value)) {
96
+ return formatNewRefToLegacyRef(value);
97
+ }
98
+
99
+ return Object.fromEntries(
100
+ Object.entries(value).map(([key, value]: [string, any]) => [
101
+ key,
102
+ formatLegacyRefOnInit(value),
103
+ ])
104
+ );
105
+ }
106
+
107
+ if (Array.isArray(value)) {
108
+ return value.map(formatLegacyRefOnInit);
109
+ }
110
+
111
+ return value;
112
+ }
113
+
114
+ export function isLegacyFlowRefValueSchema(value: any): value is LegacyFlowRefValueSchema {
115
+ return (
116
+ isObject(value) &&
117
+ Object.keys(value).length === 2 &&
118
+ (value as any).type === 'ref' &&
119
+ typeof (value as any).content === 'string'
120
+ );
121
+ }
122
+
123
+ export function isNewFlowRefValueSchema(value: any): value is NewFlowRefValueSchema {
124
+ return (
125
+ isObject(value) &&
126
+ Object.keys(value).length === 2 &&
127
+ (value as any).type === 'ref' &&
128
+ Array.isArray((value as any).content)
129
+ );
130
+ }
131
+
132
+ export function formatLegacyRefToNewRef(value: LegacyFlowRefValueSchema) {
133
+ const keyPath = value.content.split('.');
134
+
135
+ if (keyPath[1] === 'outputs') {
136
+ return {
137
+ type: 'ref',
138
+ content: [`${keyPath[0]}.${keyPath[1]}`, ...(keyPath.length > 2 ? keyPath.slice(2) : [])],
139
+ };
140
+ }
141
+
142
+ return {
143
+ type: 'ref',
144
+ content: keyPath,
145
+ };
146
+ }
147
+
148
+ export function formatNewRefToLegacyRef(value: NewFlowRefValueSchema) {
149
+ return {
150
+ type: 'ref',
151
+ content: value.content.join('.'),
152
+ };
153
+ }
@@ -0,0 +1,38 @@
1
+ # Notice
2
+
3
+ In `@flowgram.ai/form-materials@0.2.0`, for introducing loop-related materials,
4
+
5
+ The FlowRefValueSchema type definition is updated:
6
+
7
+ ```typescript
8
+ interface LegacyFlowRefValueSchema {
9
+ type: 'ref';
10
+ content: string;
11
+ }
12
+
13
+ interface NewFlowRefValueSchema {
14
+ type: 'ref';
15
+ content: string[];
16
+ }
17
+ ```
18
+
19
+
20
+
21
+ For making sure backend json will not be changed in your application, we provide `format-legacy-ref` utils for upgrading
22
+
23
+
24
+ How to use:
25
+
26
+ 1. Call formatLegacyRefOnSubmit on the formData before submitting
27
+ 2. Call formatLegacyRefOnInit on the formData after submitting
28
+
29
+ Example:
30
+
31
+ ```typescript
32
+ import { formatLegacyRefOnSubmit, formatLegacyRefOnInit } from '@flowgram.ai/form-materials';
33
+
34
+ formMeta: {
35
+ formatOnSubmit: (data) => formatLegacyRefOnSubmit(data),
36
+ formatOnInit: (data) => formatLegacyRefOnInit(data),
37
+ }
38
+ ```
@@ -0,0 +1,2 @@
1
+ export * from './format-legacy-refs';
2
+ export * from './json-schema';
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "json-schema",
3
+ "depMaterials": ["typings/json-schema"],
4
+ "depPackages": ["lodash"]
5
+ }