@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,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
+ }