@flowgram.ai/form-materials 0.3.6 → 0.4.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 (39) hide show
  1. package/dist/esm/index.js +723 -401
  2. package/dist/esm/index.js.map +1 -1
  3. package/dist/index.d.mts +132 -29
  4. package/dist/index.d.ts +132 -29
  5. package/dist/index.js +839 -510
  6. package/dist/index.js.map +1 -1
  7. package/package.json +6 -5
  8. package/src/components/batch-outputs/index.tsx +3 -2
  9. package/src/components/dynamic-value-input/hooks.ts +1 -1
  10. package/src/components/dynamic-value-input/index.tsx +1 -1
  11. package/src/components/dynamic-value-input/styles.tsx +14 -4
  12. package/src/components/index.ts +2 -0
  13. package/src/components/inputs-values/index.tsx +3 -3
  14. package/src/components/inputs-values/styles.tsx +1 -1
  15. package/src/components/inputs-values-tree/hooks/use-child-list.tsx +71 -0
  16. package/src/components/inputs-values-tree/index.tsx +56 -0
  17. package/src/components/inputs-values-tree/row.tsx +163 -0
  18. package/src/components/inputs-values-tree/styles.tsx +128 -0
  19. package/src/components/inputs-values-tree/types.ts +21 -0
  20. package/src/components/json-schema-editor/default-value.tsx +1 -3
  21. package/src/components/json-schema-editor/hooks.tsx +13 -2
  22. package/src/components/json-schema-editor/index.tsx +17 -57
  23. package/src/components/json-schema-editor/styles.tsx +12 -55
  24. package/src/components/json-schema-editor/types.ts +0 -1
  25. package/src/components/prompt-editor/index.tsx +10 -3
  26. package/src/effects/auto-rename-ref/index.ts +7 -54
  27. package/src/form-plugins/infer-inputs-plugin/index.ts +3 -75
  28. package/src/hooks/use-object-list/index.tsx +34 -6
  29. package/src/plugins/json-schema-preset/manager.ts +1 -0
  30. package/src/plugins/json-schema-preset/type-definition/string.tsx +18 -9
  31. package/src/shared/flow-value/index.ts +6 -0
  32. package/src/shared/flow-value/schema.ts +38 -0
  33. package/src/shared/flow-value/utils.ts +201 -0
  34. package/src/shared/index.ts +1 -0
  35. package/src/typings/flow-value/index.ts +2 -0
  36. package/src/components/json-schema-editor/components/blur-input.tsx +0 -27
  37. package/src/plugins/disable-declaration-plugin/config.json +0 -5
  38. package/src/plugins/json-schema-preset/config.json +0 -9
  39. /package/src/components/{inputs-values/components/blur-input.tsx → blur-input/index.tsx} +0 -0
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import z from 'zod';
7
+
8
+ // Shared extra schema for flow value types
9
+ export const extraSchema = z
10
+ .object({
11
+ index: z.number().optional(),
12
+ })
13
+ .optional();
14
+
15
+ export const constantSchema = z.object({
16
+ type: z.literal('constant'),
17
+ content: z.union([z.string(), z.number(), z.boolean()]).optional(),
18
+ schema: z.any().optional(),
19
+ extra: extraSchema,
20
+ });
21
+
22
+ export const refSchema = z.object({
23
+ type: z.literal('ref'),
24
+ content: z.array(z.string()).optional(),
25
+ extra: extraSchema,
26
+ });
27
+
28
+ export const expressionSchema = z.object({
29
+ type: z.literal('expression'),
30
+ content: z.string().optional(),
31
+ extra: extraSchema,
32
+ });
33
+
34
+ export const templateSchema = z.object({
35
+ type: z.literal('template'),
36
+ content: z.string().optional(),
37
+ extra: extraSchema,
38
+ });
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ import { isArray, isObject, isPlainObject, uniq } from 'lodash';
7
+ import { IJsonSchema, JsonSchemaUtils } from '@flowgram.ai/json-schema';
8
+ import { Scope } from '@flowgram.ai/editor';
9
+
10
+ import {
11
+ IFlowConstantValue,
12
+ IFlowRefValue,
13
+ IFlowExpressionValue,
14
+ IFlowTemplateValue,
15
+ IFlowValue,
16
+ IFlowConstantRefValue,
17
+ FlowValueType,
18
+ } from '@/typings';
19
+
20
+ import { constantSchema, refSchema, expressionSchema, templateSchema } from './schema';
21
+
22
+ export namespace FlowValueUtils {
23
+ /**
24
+ * Check if the value is a constant type
25
+ */
26
+ export function isConstant(value: any): value is IFlowConstantValue {
27
+ return constantSchema.safeParse(value).success;
28
+ }
29
+
30
+ /**
31
+ * Check if the value is a reference type
32
+ */
33
+ export function isRef(value: any): value is IFlowRefValue {
34
+ return refSchema.safeParse(value).success;
35
+ }
36
+
37
+ /**
38
+ * Check if the value is an expression type
39
+ */
40
+ export function isExpression(value: any): value is IFlowExpressionValue {
41
+ return expressionSchema.safeParse(value).success;
42
+ }
43
+
44
+ /**
45
+ * Check if the value is a template type
46
+ */
47
+ export function isTemplate(value: any): value is IFlowTemplateValue {
48
+ return templateSchema.safeParse(value).success;
49
+ }
50
+
51
+ /**
52
+ * Check if the value is either a constant or reference type
53
+ */
54
+ export function isConstantOrRef(value: any): value is IFlowConstantRefValue {
55
+ return isConstant(value) || isRef(value);
56
+ }
57
+
58
+ /**
59
+ * Check if the value is a valid flow value type
60
+ */
61
+ export function isFlowValue(value: any): value is IFlowValue {
62
+ return isConstant(value) || isRef(value) || isExpression(value) || isTemplate(value);
63
+ }
64
+
65
+ /**
66
+ * Traverse all flow values in the given value
67
+ * @param value The value to traverse
68
+ * @param options The options to traverse
69
+ * @returns A generator of flow values
70
+ */
71
+ export function* traverse(
72
+ value: any,
73
+ options: {
74
+ includeTypes?: FlowValueType[];
75
+ path?: string;
76
+ }
77
+ ): Generator<{ value: IFlowValue; path: string }> {
78
+ const { includeTypes = ['ref', 'template'], path = '' } = options;
79
+
80
+ if (isPlainObject(value)) {
81
+ if (isRef(value) && includeTypes.includes('ref')) {
82
+ yield { value, path };
83
+ return;
84
+ }
85
+
86
+ if (isTemplate(value) && includeTypes.includes('template')) {
87
+ yield { value, path };
88
+ return;
89
+ }
90
+
91
+ if (isExpression(value) && includeTypes.includes('expression')) {
92
+ yield { value, path };
93
+ return;
94
+ }
95
+
96
+ if (isConstant(value) && includeTypes.includes('constant')) {
97
+ yield { value, path };
98
+ return;
99
+ }
100
+
101
+ for (const [_key, _value] of Object.entries(value)) {
102
+ yield* traverse(_value, { ...options, path: `${path}.${_key}` });
103
+ }
104
+ return;
105
+ }
106
+
107
+ if (isArray(value)) {
108
+ for (const [_idx, _value] of value.entries()) {
109
+ yield* traverse(_value, { ...options, path: `${path}[${_idx}]` });
110
+ }
111
+ return;
112
+ }
113
+
114
+ return;
115
+ }
116
+
117
+ /**
118
+ * Get all key paths in the template value
119
+ * @param value The template value
120
+ * @returns A list of key paths
121
+ */
122
+ export function getTemplateKeyPaths(value: IFlowTemplateValue) {
123
+ // find all keyPath wrapped in {{}}
124
+ const keyPathReg = /{{(.*?)}}/g;
125
+ return uniq(value.content?.match(keyPathReg) || []).map((_keyPath) =>
126
+ _keyPath.slice(2, -2).split('.')
127
+ );
128
+ }
129
+
130
+ /**
131
+ * Infer the schema of the constant value
132
+ * @param value
133
+ * @returns
134
+ */
135
+ export function inferConstantJsonSchema(value: IFlowConstantValue): IJsonSchema | undefined {
136
+ if (value?.schema) {
137
+ return value.schema;
138
+ }
139
+
140
+ if (typeof value.content === 'string') {
141
+ return {
142
+ type: 'string',
143
+ };
144
+ }
145
+
146
+ if (typeof value.content === 'number') {
147
+ return {
148
+ type: 'number',
149
+ };
150
+ }
151
+
152
+ if (typeof value.content === 'boolean') {
153
+ return {
154
+ type: 'boolean',
155
+ };
156
+ }
157
+
158
+ if (isObject(value.content)) {
159
+ return {
160
+ type: 'object',
161
+ };
162
+ }
163
+ return undefined;
164
+ }
165
+
166
+ /**
167
+ * Infer the schema of the flow value
168
+ * @param values The flow value or object contains flow value
169
+ * @param scope
170
+ * @returns
171
+ */
172
+ export function inferJsonSchema(values: any, scope: Scope): IJsonSchema | undefined {
173
+ if (isPlainObject(values)) {
174
+ if (isConstant(values)) {
175
+ return inferConstantJsonSchema(values);
176
+ }
177
+
178
+ if (isRef(values)) {
179
+ const variable = scope.available.getByKeyPath(values?.content);
180
+ const schema = variable?.type ? JsonSchemaUtils.astToSchema(variable?.type) : undefined;
181
+
182
+ return schema;
183
+ }
184
+
185
+ if (isTemplate(values)) {
186
+ return { type: 'string' };
187
+ }
188
+
189
+ return {
190
+ type: 'object',
191
+ properties: Object.keys(values).reduce((acc, key) => {
192
+ const schema = inferJsonSchema((values as any)[key], scope);
193
+ if (schema) {
194
+ acc[key] = schema;
195
+ }
196
+ return acc;
197
+ }, {} as Record<string, IJsonSchema>),
198
+ };
199
+ }
200
+ }
201
+ }
@@ -5,3 +5,4 @@
5
5
 
6
6
  export * from './format-legacy-refs';
7
7
  export * from './inject-material';
8
+ export * from './flow-value';
@@ -9,6 +9,8 @@ export interface IFlowValueExtra {
9
9
  index?: number;
10
10
  }
11
11
 
12
+ export type FlowValueType = 'constant' | 'ref' | 'expression' | 'template';
13
+
12
14
  export interface IFlowConstantValue {
13
15
  type: 'constant';
14
16
  content?: string | number | boolean;
@@ -1,27 +0,0 @@
1
- /**
2
- * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
- * SPDX-License-Identifier: MIT
4
- */
5
-
6
- import React, { useEffect, useState } from 'react';
7
-
8
- import Input, { InputProps } from '@douyinfe/semi-ui/lib/es/input';
9
-
10
- export function BlurInput(props: InputProps) {
11
- const [value, setValue] = useState('');
12
-
13
- useEffect(() => {
14
- setValue(props.value as string);
15
- }, [props.value]);
16
-
17
- return (
18
- <Input
19
- {...props}
20
- value={value}
21
- onChange={(value) => {
22
- setValue(value);
23
- }}
24
- onBlur={(e) => props.onChange?.(value, e)}
25
- />
26
- );
27
- }
@@ -1,5 +0,0 @@
1
- {
2
- "name": "disable-declaration-plugin",
3
- "depMaterials": [],
4
- "depPackages": []
5
- }
@@ -1,9 +0,0 @@
1
- {
2
- "name": "json-schema-preset",
3
- "depMaterials": [
4
- "components/code-editor-mini"
5
- ],
6
- "depPackages": [
7
- "@flowgram.ai/json-schema"
8
- ]
9
- }