@flowgram.ai/form-materials 0.4.0 → 0.4.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.
Files changed (41) hide show
  1. package/dist/esm/index.js +792 -423
  2. package/dist/esm/index.js.map +1 -1
  3. package/dist/index.d.mts +136 -30
  4. package/dist/index.d.ts +136 -30
  5. package/dist/index.js +902 -525
  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/display-flow-value/index.tsx +2 -12
  10. package/src/components/display-inputs-values/index.tsx +44 -6
  11. package/src/components/dynamic-value-input/hooks.ts +24 -3
  12. package/src/components/dynamic-value-input/index.tsx +2 -2
  13. package/src/components/dynamic-value-input/styles.tsx +14 -4
  14. package/src/components/index.ts +2 -0
  15. package/src/components/inputs-values/index.tsx +3 -3
  16. package/src/components/inputs-values/styles.tsx +1 -1
  17. package/src/components/inputs-values-tree/hooks/use-child-list.tsx +76 -0
  18. package/src/components/inputs-values-tree/index.tsx +56 -0
  19. package/src/components/inputs-values-tree/row.tsx +173 -0
  20. package/src/components/inputs-values-tree/styles.tsx +128 -0
  21. package/src/components/inputs-values-tree/types.ts +21 -0
  22. package/src/components/json-schema-editor/default-value.tsx +1 -3
  23. package/src/components/json-schema-editor/hooks.tsx +13 -2
  24. package/src/components/json-schema-editor/index.tsx +17 -57
  25. package/src/components/json-schema-editor/styles.tsx +12 -55
  26. package/src/components/json-schema-editor/types.ts +0 -1
  27. package/src/components/prompt-editor/index.tsx +10 -3
  28. package/src/effects/auto-rename-ref/index.ts +7 -54
  29. package/src/form-plugins/infer-inputs-plugin/index.ts +3 -75
  30. package/src/hooks/use-object-list/index.tsx +34 -6
  31. package/src/plugins/json-schema-preset/manager.ts +1 -0
  32. package/src/plugins/json-schema-preset/type-definition/string.tsx +18 -9
  33. package/src/shared/flow-value/index.ts +6 -0
  34. package/src/shared/flow-value/schema.ts +38 -0
  35. package/src/shared/flow-value/utils.ts +201 -0
  36. package/src/shared/index.ts +1 -0
  37. package/src/typings/flow-value/index.ts +2 -0
  38. package/src/components/json-schema-editor/components/blur-input.tsx +0 -27
  39. package/src/plugins/disable-declaration-plugin/config.json +0 -5
  40. package/src/plugins/json-schema-preset/config.json +0 -9
  41. /package/src/components/{inputs-values/components/blur-input.tsx → blur-input/index.tsx} +0 -0
@@ -4,15 +4,9 @@
4
4
  */
5
5
 
6
6
  import { get, set } from 'lodash';
7
- import { JsonSchemaUtils, IJsonSchema } from '@flowgram.ai/json-schema';
8
- import {
9
- defineFormPluginCreator,
10
- getNodePrivateScope,
11
- getNodeScope,
12
- Scope,
13
- } from '@flowgram.ai/editor';
7
+ import { defineFormPluginCreator, getNodePrivateScope, getNodeScope } from '@flowgram.ai/editor';
14
8
 
15
- import { IFlowConstantValue, IFlowRefValue, IFlowTemplateValue } from '@/typings';
9
+ import { FlowValueUtils } from '@/shared';
16
10
 
17
11
  interface InputConfig {
18
12
  sourceKey: string;
@@ -30,7 +24,7 @@ export const createInferInputsPlugin = defineFormPluginCreator<InputConfig>({
30
24
  set(
31
25
  formData,
32
26
  targetKey,
33
- infer(
27
+ FlowValueUtils.inferJsonSchema(
34
28
  get(formData, sourceKey),
35
29
  scope === 'private' ? getNodePrivateScope(ctx.node) : getNodeScope(ctx.node)
36
30
  )
@@ -40,69 +34,3 @@ export const createInferInputsPlugin = defineFormPluginCreator<InputConfig>({
40
34
  });
41
35
  },
42
36
  });
43
-
44
- function isRef(value: any): value is IFlowRefValue {
45
- return (
46
- value?.type === 'ref' && Array.isArray(value?.content) && typeof value?.content[0] === 'string'
47
- );
48
- }
49
-
50
- function isTemplate(value: any): value is IFlowTemplateValue {
51
- return value?.type === 'template' && typeof value?.content === 'string';
52
- }
53
-
54
- function isConstant(value: any): value is IFlowConstantValue {
55
- return value?.type === 'constant' && typeof value?.content !== 'undefined';
56
- }
57
-
58
- const infer = (values: any, scope: Scope): IJsonSchema | undefined => {
59
- if (typeof values === 'object') {
60
- if (isConstant(values)) {
61
- if (values?.schema) {
62
- return values.schema;
63
- }
64
-
65
- if (typeof values.content === 'string') {
66
- return {
67
- type: 'string',
68
- };
69
- }
70
-
71
- if (typeof values.content === 'number') {
72
- return {
73
- type: 'number',
74
- };
75
- }
76
-
77
- if (typeof values.content === 'boolean') {
78
- return {
79
- type: 'boolean',
80
- };
81
- }
82
- }
83
-
84
- if (isRef(values)) {
85
- const variable = scope.available.getByKeyPath(values?.content);
86
- const schema = variable?.type ? JsonSchemaUtils.astToSchema(variable?.type) : undefined;
87
-
88
- return schema;
89
- }
90
-
91
- if (isTemplate(values)) {
92
- return {
93
- type: 'string',
94
- };
95
- }
96
-
97
- return {
98
- type: 'object',
99
- properties: Object.keys(values).reduce((acc, key) => {
100
- const schema = infer(values[key], scope);
101
- if (schema) {
102
- acc[key] = schema;
103
- }
104
- return acc;
105
- }, {} as Record<string, IJsonSchema>),
106
- };
107
- }
108
- };
@@ -3,7 +3,7 @@
3
3
  * SPDX-License-Identifier: MIT
4
4
  */
5
5
 
6
- import { useEffect, useState } from 'react';
6
+ import { useEffect, useRef, useState } from 'react';
7
7
 
8
8
  import { nanoid } from 'nanoid';
9
9
  import { difference, get, isObject, set } from 'lodash';
@@ -27,14 +27,30 @@ export function useObjectList<ValueType>({
27
27
  }: {
28
28
  value?: ObjectType<ValueType>;
29
29
  onChange: (value?: ObjectType<ValueType>) => void;
30
- sortIndexKey?: string;
30
+ sortIndexKey?: string | ((item: ValueType | undefined) => string);
31
31
  }) {
32
32
  const [list, setList] = useState<ListItem<ValueType>[]>([]);
33
33
 
34
+ const effectVersion = useRef(0);
35
+ const changeVersion = useRef(0);
36
+
37
+ const getSortIndex = (value?: ValueType) => {
38
+ if (typeof sortIndexKey === 'function') {
39
+ return get(value, sortIndexKey(value)) || 0;
40
+ }
41
+ return get(value, sortIndexKey || '') || 0;
42
+ };
43
+
34
44
  useEffect(() => {
45
+ effectVersion.current = effectVersion.current + 1;
46
+ if (effectVersion.current === changeVersion.current) {
47
+ return;
48
+ }
49
+ effectVersion.current = changeVersion.current;
50
+
35
51
  setList((_prevList) => {
36
52
  const newKeys = Object.entries(value || {})
37
- .sort((a, b) => get(a[1], sortIndexKey || 0) - get(b[1], sortIndexKey || 0))
53
+ .sort((a, b) => getSortIndex(a[1]) - getSortIndex(b[1]))
38
54
  .map(([key]) => key);
39
55
 
40
56
  const oldKeys = _prevList.map((item) => item.key).filter(Boolean) as string[];
@@ -57,16 +73,19 @@ export function useObjectList<ValueType>({
57
73
  });
58
74
  }, [value]);
59
75
 
60
- const add = () => {
76
+ const add = (defaultValue?: ValueType) => {
61
77
  setList((prevList) => [
62
78
  ...prevList,
63
79
  {
64
80
  id: genId(),
81
+ value: defaultValue,
65
82
  },
66
83
  ]);
67
84
  };
68
85
 
69
86
  const updateValue = (itemId: string, value: ValueType) => {
87
+ changeVersion.current = changeVersion.current + 1;
88
+
70
89
  setList((prevList) => {
71
90
  const nextList = prevList.map((_item) => {
72
91
  if (_item.id === itemId) {
@@ -84,8 +103,13 @@ export function useObjectList<ValueType>({
84
103
  .filter((item) => item.key)
85
104
  .map((item) => [item.key!, item.value])
86
105
  .map((_res, idx) => {
87
- if (isObject(_res[1]) && sortIndexKey) {
88
- set(_res[1], sortIndexKey, idx);
106
+ const indexKey =
107
+ typeof sortIndexKey === 'function'
108
+ ? sortIndexKey(_res[1] as ValueType | undefined)
109
+ : sortIndexKey;
110
+
111
+ if (isObject(_res[1]) && indexKey) {
112
+ set(_res[1], indexKey, idx);
89
113
  }
90
114
  return _res;
91
115
  })
@@ -97,6 +121,8 @@ export function useObjectList<ValueType>({
97
121
  };
98
122
 
99
123
  const updateKey = (itemId: string, key: string) => {
124
+ changeVersion.current = changeVersion.current + 1;
125
+
100
126
  setList((prevList) => {
101
127
  const nextList = prevList.map((_item) => {
102
128
  if (_item.id === itemId) {
@@ -119,6 +145,8 @@ export function useObjectList<ValueType>({
119
145
  };
120
146
 
121
147
  const remove = (itemId: string) => {
148
+ changeVersion.current = changeVersion.current + 1;
149
+
122
150
  setList((prevList) => {
123
151
  const nextList = prevList.filter((_item) => _item.id !== itemId);
124
152
 
@@ -9,6 +9,7 @@ export interface ConstantRendererProps<Value = any> {
9
9
  value?: Value;
10
10
  onChange?: (value: Value) => void;
11
11
  readonly?: boolean;
12
+ [key: string]: any;
12
13
  }
13
14
  export interface JsonSchemaTypeRegistry<Value = any> extends OriginJsonSchemaTypeRegistry {
14
15
  /**
@@ -7,18 +7,27 @@
7
7
  import React from 'react';
8
8
 
9
9
  import { I18n } from '@flowgram.ai/editor';
10
- import { Input } from '@douyinfe/semi-ui';
10
+ import { Input, TextArea } from '@douyinfe/semi-ui';
11
11
 
12
12
  import { type JsonSchemaTypeRegistry } from '../manager';
13
13
 
14
14
  export const stringRegistry: Partial<JsonSchemaTypeRegistry> = {
15
15
  type: 'string',
16
- ConstantRenderer: (props) => (
17
- <Input
18
- placeholder={I18n.t('Please Input String')}
19
- size="small"
20
- disabled={props.readonly}
21
- {...props}
22
- />
23
- ),
16
+ ConstantRenderer: (props) =>
17
+ props?.enableMultiLineStr ? (
18
+ <TextArea
19
+ autosize
20
+ rows={1}
21
+ placeholder={I18n.t('Please Input String')}
22
+ disabled={props.readonly}
23
+ {...props}
24
+ />
25
+ ) : (
26
+ <Input
27
+ size="small"
28
+ placeholder={I18n.t('Please Input String')}
29
+ disabled={props.readonly}
30
+ {...props}
31
+ />
32
+ ),
24
33
  };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
3
+ * SPDX-License-Identifier: MIT
4
+ */
5
+
6
+ export * from './utils';
@@ -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.any().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
- }