@flowgram.ai/form-materials 0.1.31 → 0.2.1

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 (46) hide show
  1. package/bin/materials.js +21 -5
  2. package/dist/esm/index.js +570 -54
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/index.d.mts +206 -28
  5. package/dist/index.d.ts +206 -28
  6. package/dist/index.js +582 -59
  7. package/dist/index.js.map +1 -1
  8. package/package.json +4 -4
  9. package/src/components/batch-variable-selector/config.json +5 -0
  10. package/src/components/batch-variable-selector/index.tsx +19 -0
  11. package/src/components/constant-input/config.json +6 -0
  12. package/src/components/constant-input/index.tsx +81 -0
  13. package/src/components/constant-input/types.ts +18 -0
  14. package/src/components/dynamic-value-input/config.json +5 -0
  15. package/src/components/dynamic-value-input/index.tsx +77 -0
  16. package/src/components/dynamic-value-input/styles.tsx +19 -0
  17. package/src/components/index.ts +6 -3
  18. package/src/components/json-schema-editor/config.json +1 -1
  19. package/src/components/json-schema-editor/hooks.tsx +35 -24
  20. package/src/components/json-schema-editor/index.tsx +3 -3
  21. package/src/components/json-schema-editor/types.ts +3 -3
  22. package/src/components/type-selector/config.json +1 -1
  23. package/src/components/type-selector/constants.tsx +2 -2
  24. package/src/components/type-selector/index.tsx +11 -8
  25. package/src/components/variable-selector/config.json +2 -2
  26. package/src/components/variable-selector/index.tsx +76 -16
  27. package/src/components/variable-selector/styles.tsx +43 -0
  28. package/src/components/variable-selector/use-variable-tree.tsx +34 -6
  29. package/src/effects/index.ts +2 -0
  30. package/src/effects/provide-batch-input/config.json +5 -0
  31. package/src/effects/provide-batch-input/index.ts +38 -0
  32. package/src/effects/provide-batch-outputs/config.json +5 -0
  33. package/src/effects/provide-batch-outputs/index.ts +34 -0
  34. package/src/index.ts +3 -0
  35. package/src/typings/flow-value/config.json +5 -0
  36. package/src/typings/flow-value/index.ts +27 -0
  37. package/src/typings/index.ts +2 -0
  38. package/src/typings/json-schema/config.json +5 -0
  39. package/src/typings/json-schema/index.ts +31 -0
  40. package/src/utils/format-legacy-refs/config.json +5 -0
  41. package/src/utils/format-legacy-refs/index.ts +153 -0
  42. package/src/utils/format-legacy-refs/readme.md +38 -0
  43. package/src/utils/index.ts +2 -0
  44. package/src/utils/json-schema/config.json +5 -0
  45. package/src/utils/json-schema/index.ts +154 -0
  46. package/src/components/type-selector/types.ts +0 -19
@@ -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
+ }
@@ -0,0 +1,154 @@
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(typeAST: ASTNode): IJsonSchema | undefined {
78
+ if (ASTMatch.isString(typeAST)) {
79
+ return {
80
+ type: 'string',
81
+ };
82
+ }
83
+
84
+ if (ASTMatch.isBoolean(typeAST)) {
85
+ return {
86
+ type: 'boolean',
87
+ };
88
+ }
89
+
90
+ if (ASTMatch.isNumber(typeAST)) {
91
+ return {
92
+ type: 'number',
93
+ };
94
+ }
95
+
96
+ if (ASTMatch.isInteger(typeAST)) {
97
+ return {
98
+ type: 'integer',
99
+ };
100
+ }
101
+
102
+ if (ASTMatch.isObject(typeAST)) {
103
+ return {
104
+ type: 'object',
105
+ properties: Object.fromEntries(
106
+ Object.entries(typeAST.properties).map(([key, value]) => [key, astToSchema(value)!])
107
+ ),
108
+ };
109
+ }
110
+
111
+ if (ASTMatch.isArray(typeAST)) {
112
+ return {
113
+ type: 'array',
114
+ items: astToSchema(typeAST.items),
115
+ };
116
+ }
117
+
118
+ if (ASTMatch.isMap(typeAST)) {
119
+ return {
120
+ type: 'map',
121
+ items: astToSchema(typeAST.valueType),
122
+ };
123
+ }
124
+
125
+ if (ASTMatch.isCustomType(typeAST)) {
126
+ return {
127
+ type: typeAST.typeName,
128
+ };
129
+ }
130
+
131
+ return undefined;
132
+ }
133
+
134
+ /**
135
+ * Check if the AST type is match the JSON Schema
136
+ * @param typeAST
137
+ * @param schema
138
+ * @returns
139
+ */
140
+ export function isASTMatchSchema(
141
+ typeAST: BaseType,
142
+ schema: IJsonSchema | IJsonSchema[]
143
+ ): boolean {
144
+ if (Array.isArray(schema)) {
145
+ return typeAST.isTypeEqual(
146
+ ASTFactory.createUnion({
147
+ types: schema.map((_schema) => schemaToAST(_schema)!).filter(Boolean),
148
+ })
149
+ );
150
+ }
151
+
152
+ return typeAST.isTypeEqual(schemaToAST(schema));
153
+ }
154
+ }
@@ -1,19 +0,0 @@
1
- export type BasicType = 'boolean' | 'string' | 'integer' | 'number' | 'object' | 'array';
2
-
3
- export interface JsonSchema<T = string> {
4
- type?: T;
5
- default?: any;
6
- title?: string;
7
- description?: string;
8
- enum?: (string | number)[];
9
- properties?: Record<string, JsonSchema>;
10
- additionalProperties?: JsonSchema;
11
- items?: JsonSchema;
12
- required?: string[];
13
- $ref?: string;
14
- extra?: {
15
- order?: number;
16
- literal?: boolean; // is literal type
17
- formComponent?: string; // Set the render component
18
- };
19
- }