@flowgram.ai/form-materials 0.1.0-alpha.11 → 0.1.0-alpha.13

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 (133) hide show
  1. package/bin/index.ts +14 -22
  2. package/bin/materials.ts +41 -90
  3. package/bin/project.ts +4 -0
  4. package/bin/utils/import.ts +127 -0
  5. package/bin/utils/traverse-file.ts +60 -0
  6. package/dist/esm/index.js +2073 -1601
  7. package/dist/esm/index.js.map +1 -1
  8. package/dist/index.d.mts +392 -131
  9. package/dist/index.d.ts +392 -131
  10. package/dist/index.js +2175 -1710
  11. package/dist/index.js.map +1 -1
  12. package/package.json +5 -4
  13. package/src/components/assign-row/components/blur-input.tsx +27 -0
  14. package/src/components/assign-row/index.tsx +84 -0
  15. package/src/components/assign-row/types.ts +25 -0
  16. package/src/components/assign-rows/index.tsx +59 -0
  17. package/src/components/batch-outputs/index.tsx +7 -14
  18. package/src/components/batch-outputs/types.ts +1 -1
  19. package/src/components/batch-variable-selector/index.tsx +2 -2
  20. package/src/components/code-editor/index.tsx +7 -0
  21. package/src/components/code-editor/language-features.ts +22 -1
  22. package/src/components/code-editor/theme/light.ts +1 -1
  23. package/src/components/code-editor-mini/index.tsx +31 -0
  24. package/src/components/condition-row/constants.ts +8 -10
  25. package/src/components/condition-row/hooks/useOp.tsx +15 -9
  26. package/src/components/condition-row/hooks/useRule.ts +9 -5
  27. package/src/components/condition-row/index.tsx +28 -10
  28. package/src/components/condition-row/types.ts +5 -5
  29. package/src/components/constant-input/index.tsx +20 -61
  30. package/src/components/constant-input/types.ts +6 -9
  31. package/src/components/display-flow-value/index.tsx +59 -0
  32. package/src/components/display-inputs-values/index.tsx +28 -0
  33. package/src/components/display-inputs-values/styles.ts +12 -0
  34. package/src/components/display-outputs/index.tsx +65 -0
  35. package/src/components/display-outputs/styles.ts +12 -0
  36. package/src/components/display-schema-tag/index.tsx +45 -0
  37. package/src/components/display-schema-tag/styles.ts +28 -0
  38. package/src/components/display-schema-tree/index.tsx +75 -0
  39. package/src/components/display-schema-tree/styles.tsx +90 -0
  40. package/src/components/dynamic-value-input/hooks.ts +53 -0
  41. package/src/components/dynamic-value-input/index.tsx +74 -19
  42. package/src/components/dynamic-value-input/styles.tsx +28 -2
  43. package/src/components/index.ts +9 -0
  44. package/src/components/inputs-values/components/blur-input.tsx +27 -0
  45. package/src/components/inputs-values/index.tsx +75 -0
  46. package/src/components/inputs-values/styles.tsx +19 -0
  47. package/src/components/inputs-values/types.ts +22 -0
  48. package/src/components/json-editor-with-variables/extensions/variable-tree.tsx +1 -1
  49. package/src/components/json-editor-with-variables/index.tsx +2 -1
  50. package/src/components/json-schema-editor/default-value.tsx +12 -106
  51. package/src/components/json-schema-editor/hooks.tsx +53 -94
  52. package/src/components/json-schema-editor/index.tsx +32 -13
  53. package/src/components/json-schema-editor/styles.tsx +0 -29
  54. package/src/components/json-schema-editor/types.ts +1 -1
  55. package/src/components/prompt-editor/types.tsx +1 -1
  56. package/src/components/prompt-editor-with-inputs/extensions/inputs-tree.tsx +2 -1
  57. package/src/components/prompt-editor-with-inputs/index.tsx +3 -2
  58. package/src/components/prompt-editor-with-inputs/inputs-picker.tsx +2 -2
  59. package/src/components/prompt-editor-with-variables/extensions/variable-tag.tsx +6 -3
  60. package/src/components/prompt-editor-with-variables/extensions/variable-tree.tsx +1 -1
  61. package/src/components/prompt-editor-with-variables/index.tsx +2 -1
  62. package/src/components/type-selector/index.tsx +58 -13
  63. package/src/components/variable-selector/index.tsx +42 -17
  64. package/src/components/variable-selector/styles.tsx +18 -8
  65. package/src/components/variable-selector/use-variable-tree.tsx +19 -22
  66. package/src/effects/auto-rename-ref/index.ts +1 -1
  67. package/src/effects/index.ts +3 -1
  68. package/src/effects/listen-ref-schema-change/index.ts +56 -0
  69. package/src/effects/listen-ref-value-change/index.ts +53 -0
  70. package/src/effects/provide-batch-input/index.ts +1 -1
  71. package/src/effects/provide-json-schema-outputs/index.ts +1 -3
  72. package/src/effects/sync-variable-title/index.ts +1 -0
  73. package/src/effects/validate-when-variable-sync/index.ts +35 -0
  74. package/src/form-plugins/batch-outputs-plugin/index.ts +1 -1
  75. package/src/form-plugins/index.ts +3 -1
  76. package/src/form-plugins/infer-assign-plugin/index.ts +90 -0
  77. package/src/form-plugins/infer-inputs-plugin/index.ts +108 -0
  78. package/src/hooks/index.tsx +6 -0
  79. package/src/hooks/use-object-list/index.tsx +136 -0
  80. package/src/index.ts +3 -1
  81. package/src/{utils/format-legacy-refs → plugins/disable-declaration-plugin}/config.json +1 -1
  82. package/src/plugins/disable-declaration-plugin/create-disable-declaration-plugin.ts +31 -0
  83. package/src/plugins/disable-declaration-plugin/index.tsx +6 -0
  84. package/src/plugins/index.ts +7 -0
  85. package/src/plugins/json-schema-preset/config.json +9 -0
  86. package/src/plugins/json-schema-preset/create-type-preset-plugin.tsx +28 -0
  87. package/src/plugins/json-schema-preset/index.tsx +41 -0
  88. package/src/plugins/json-schema-preset/manager.ts +18 -0
  89. package/src/plugins/json-schema-preset/type-definition/array.tsx +26 -0
  90. package/src/plugins/json-schema-preset/type-definition/boolean.tsx +33 -0
  91. package/src/plugins/json-schema-preset/type-definition/index.tsx +24 -0
  92. package/src/plugins/json-schema-preset/type-definition/integer.tsx +25 -0
  93. package/src/plugins/json-schema-preset/type-definition/number.tsx +25 -0
  94. package/src/plugins/json-schema-preset/type-definition/object.tsx +26 -0
  95. package/src/plugins/json-schema-preset/type-definition/string.tsx +24 -0
  96. package/src/{utils → shared}/index.ts +1 -1
  97. package/src/shared/inject-material/README.md +170 -0
  98. package/src/shared/inject-material/README.zh.md +174 -0
  99. package/src/shared/inject-material/index.tsx +87 -0
  100. package/src/typings/flow-value/index.ts +11 -0
  101. package/src/typings/index.ts +0 -1
  102. package/src/validate/index.tsx +6 -0
  103. package/src/validate/validate-flow-value/index.tsx +73 -0
  104. package/src/components/batch-outputs/config.json +0 -12
  105. package/src/components/batch-outputs/use-list.ts +0 -86
  106. package/src/components/batch-variable-selector/config.json +0 -5
  107. package/src/components/code-editor/config.json +0 -10
  108. package/src/components/condition-row/config.json +0 -5
  109. package/src/components/constant-input/config.json +0 -6
  110. package/src/components/dynamic-value-input/config.json +0 -5
  111. package/src/components/json-editor-with-variables/config.json +0 -13
  112. package/src/components/json-schema-editor/config.json +0 -13
  113. package/src/components/json-schema-editor/utils.ts +0 -29
  114. package/src/components/prompt-editor/config.json +0 -9
  115. package/src/components/prompt-editor-with-inputs/config.json +0 -13
  116. package/src/components/prompt-editor-with-variables/config.json +0 -13
  117. package/src/components/type-selector/config.json +0 -5
  118. package/src/components/type-selector/constants.tsx +0 -364
  119. package/src/components/variable-selector/config.json +0 -5
  120. package/src/effects/auto-rename-ref/config.json +0 -5
  121. package/src/effects/provide-batch-input/config.json +0 -5
  122. package/src/effects/provide-batch-outputs/config.json +0 -5
  123. package/src/effects/provide-batch-outputs/index.ts +0 -38
  124. package/src/effects/provide-json-schema-outputs/config.json +0 -8
  125. package/src/effects/sync-variable-title/config.json +0 -5
  126. package/src/form-plugins/batch-outputs-plugin/config.json +0 -7
  127. package/src/typings/flow-value/config.json +0 -5
  128. package/src/typings/json-schema/config.json +0 -5
  129. package/src/typings/json-schema/index.ts +0 -36
  130. package/src/utils/json-schema/config.json +0 -5
  131. package/src/utils/json-schema/index.ts +0 -180
  132. /package/src/{utils → shared}/format-legacy-refs/index.ts +0 -0
  133. /package/src/{utils → shared}/format-legacy-refs/readme.md +0 -0
@@ -3,11 +3,12 @@
3
3
  * SPDX-License-Identifier: MIT
4
4
  */
5
5
 
6
- import { useEffect, useMemo, useRef, useState } from 'react';
6
+ import { useEffect, useState } from 'react';
7
7
 
8
- import { omit } from 'lodash';
8
+ import { difference, omit } from 'lodash';
9
+ import { produce } from 'immer';
10
+ import { IJsonSchema, type JsonSchemaTypeManager, useTypeManager } from '@flowgram.ai/json-schema';
9
11
 
10
- import { IJsonSchema } from '../../typings';
11
12
  import { PropertyValueType } from './types';
12
13
 
13
14
  let _id = 0;
@@ -15,92 +16,46 @@ function genId() {
15
16
  return _id++;
16
17
  }
17
18
 
18
- function getDrilldownSchema(
19
- value?: PropertyValueType,
20
- path?: (keyof PropertyValueType)[]
21
- ): { schema?: PropertyValueType | null; path?: (keyof PropertyValueType)[] } {
22
- if (!value) {
23
- return {};
24
- }
25
-
26
- if (value.type === 'array' && value.items) {
27
- return getDrilldownSchema(value.items, [...(path || []), 'items']);
28
- }
29
-
30
- return { schema: value, path };
31
- }
32
-
33
19
  export function usePropertiesEdit(
34
20
  value?: PropertyValueType,
35
21
  onChange?: (value: PropertyValueType) => void
36
22
  ) {
37
- // Get drilldown (array.items.items...)
38
- const drilldown = useMemo(() => getDrilldownSchema(value), [value, value?.type, value?.items]);
39
-
40
- const isDrilldownObject = drilldown.schema?.type === 'object';
41
-
42
- // Generate Init Property List
43
- const initPropertyList = useMemo(
44
- () =>
45
- isDrilldownObject
46
- ? Object.entries(drilldown.schema?.properties || {})
47
- .sort(([, a], [, b]) => (a.extra?.index ?? 0) - (b.extra?.index ?? 0))
48
- .map(
49
- ([name, _value], index) =>
50
- ({
51
- key: genId(),
52
- name,
53
- isPropertyRequired: drilldown.schema?.required?.includes(name) || false,
54
- ..._value,
55
- extra: {
56
- ...(_value.extra || {}),
57
- index,
58
- },
59
- } as PropertyValueType)
60
- )
61
- : [],
62
- [isDrilldownObject]
63
- );
64
-
65
- const [propertyList, setPropertyList] = useState<PropertyValueType[]>(initPropertyList);
66
-
67
- const mountRef = useRef(false);
23
+ const typeManager = useTypeManager() as JsonSchemaTypeManager;
24
+
25
+ // Get drilldown properties (array.items.items.properties...)
26
+ const drilldownSchema = typeManager.getPropertiesParent(value || {});
27
+ const canAddField = typeManager.canAddField(value || {});
28
+
29
+ const [propertyList, setPropertyList] = useState<PropertyValueType[]>([]);
68
30
 
69
31
  useEffect(() => {
70
- // If initRef is true, it means the component has been mounted
71
- if (mountRef.current) {
72
- // If the value is changed, update the property list
73
- setPropertyList((_list) => {
74
- const nameMap = new Map<string, PropertyValueType>();
75
-
76
- for (const _property of _list) {
77
- if (_property.name) {
78
- nameMap.set(_property.name, _property);
79
- }
80
- }
81
- return Object.entries(drilldown.schema?.properties || {})
82
- .sort(([, a], [, b]) => (a.extra?.index ?? 0) - (b.extra?.index ?? 0))
83
- .map(([name, _value]) => {
84
- const _property = nameMap.get(name);
85
- if (_property) {
86
- return {
87
- key: _property.key,
88
- name,
89
- isPropertyRequired: drilldown.schema?.required?.includes(name) || false,
90
- ..._value,
91
- };
92
- }
93
- return {
94
- key: genId(),
95
- name,
96
- isPropertyRequired: drilldown.schema?.required?.includes(name) || false,
97
- ..._value,
98
- };
99
- });
100
- });
101
- }
102
- mountRef.current = true;
103
- }, [drilldown.schema]);
32
+ // If the value is changed, update the property list
33
+ setPropertyList((_list) => {
34
+ const newNames = Object.entries(drilldownSchema?.properties || {})
35
+ .sort(([, a], [, b]) => (a.extra?.index ?? 0) - (b.extra?.index ?? 0))
36
+ .map(([key]) => key);
37
+
38
+ const oldNames = _list.map((item) => item.name).filter(Boolean) as string[];
39
+ const addNames = difference(newNames, oldNames);
40
+
41
+ return _list
42
+ .filter((item) => !item.name || newNames.includes(item.name))
43
+ .map((item) => ({
44
+ key: item.key,
45
+ name: item.name,
46
+ isPropertyRequired: drilldownSchema?.required?.includes(item.name || '') || false,
47
+ ...item,
48
+ }))
49
+ .concat(
50
+ addNames.map((_name) => ({
51
+ key: genId(),
52
+ name: _name,
53
+ isPropertyRequired: drilldownSchema?.required?.includes(_name) || false,
54
+ ...(drilldownSchema?.properties?.[_name] || {}),
55
+ }))
56
+ );
57
+ });
58
+ }, [drilldownSchema]);
104
59
 
105
60
  const updatePropertyList = (updater: (list: PropertyValueType[]) => PropertyValueType[]) => {
106
61
  setPropertyList((_list) => {
@@ -122,21 +77,25 @@ export function usePropertiesEdit(
122
77
  }
123
78
  }
124
79
 
125
- let drilldownSchema = value || {};
126
- if (drilldown.path) {
127
- drilldownSchema = drilldown.path.reduce((acc, key) => acc[key], value || {});
128
- }
129
- drilldownSchema.properties = nextProperties;
130
- drilldownSchema.required = nextRequired;
80
+ onChange?.(
81
+ produce(value || {}, (draft) => {
82
+ const propertiesParent = typeManager.getPropertiesParent(draft);
131
83
 
132
- onChange?.(value || {});
84
+ if (propertiesParent) {
85
+ propertiesParent.properties = nextProperties;
86
+ propertiesParent.required = nextRequired;
87
+ return;
88
+ }
89
+ })
90
+ );
133
91
 
134
92
  return next;
135
93
  });
136
94
  };
137
95
 
138
96
  const onAddProperty = () => {
139
- updatePropertyList((_list) => [
97
+ // set property list only, not trigger updatePropertyList
98
+ setPropertyList((_list) => [
140
99
  ..._list,
141
100
  { key: genId(), name: '', type: 'string', extra: { index: _list.length + 1 } },
142
101
  ]);
@@ -153,14 +112,14 @@ export function usePropertiesEdit(
153
112
  };
154
113
 
155
114
  useEffect(() => {
156
- if (!isDrilldownObject) {
115
+ if (!canAddField) {
157
116
  setPropertyList([]);
158
117
  }
159
- }, [isDrilldownObject]);
118
+ }, [canAddField]);
160
119
 
161
120
  return {
162
121
  propertyList,
163
- isDrilldownObject,
122
+ canAddField,
164
123
  onAddProperty,
165
124
  onRemoveProperty,
166
125
  onEditProperty,
@@ -5,6 +5,8 @@
5
5
 
6
6
  import React, { useMemo, useState } from 'react';
7
7
 
8
+ import { IJsonSchema } from '@flowgram.ai/json-schema';
9
+ import { I18n } from '@flowgram.ai/editor';
8
10
  import { Button, Checkbox, IconButton } from '@douyinfe/semi-ui';
9
11
  import {
10
12
  IconExpand,
@@ -15,8 +17,8 @@ import {
15
17
  IconMinus,
16
18
  } from '@douyinfe/semi-icons';
17
19
 
18
- import { TypeSelector } from '../type-selector';
19
- import { IJsonSchema } from '../../typings';
20
+ import { InjectTypeSelector } from '@/components/type-selector';
21
+
20
22
  import { ConfigType, PropertyValueType } from './types';
21
23
  import {
22
24
  IconAddChildren,
@@ -39,13 +41,16 @@ import { usePropertiesEdit } from './hooks';
39
41
  import { DefaultValue } from './default-value';
40
42
  import { BlurInput } from './components/blur-input';
41
43
 
44
+ const DEFAULT = { type: 'object' };
45
+
42
46
  export function JsonSchemaEditor(props: {
43
47
  value?: IJsonSchema;
44
48
  onChange?: (value: IJsonSchema) => void;
45
49
  config?: ConfigType;
46
50
  className?: string;
51
+ readonly?: boolean;
47
52
  }) {
48
- const { value = { type: 'object' }, config = {}, onChange: onChangeProps } = props;
53
+ const { value = DEFAULT, config = {}, onChange: onChangeProps, readonly } = props;
49
54
  const { propertyList, onAddProperty, onRemoveProperty, onEditProperty } = usePropertiesEdit(
50
55
  value,
51
56
  onChangeProps
@@ -56,6 +61,7 @@ export function JsonSchemaEditor(props: {
56
61
  <UIProperties>
57
62
  {propertyList.map((_property, index) => (
58
63
  <PropertyEdit
64
+ readonly={readonly}
59
65
  key={_property.key}
60
66
  value={_property}
61
67
  config={config}
@@ -70,12 +76,13 @@ export function JsonSchemaEditor(props: {
70
76
  ))}
71
77
  </UIProperties>
72
78
  <Button
79
+ disabled={readonly}
73
80
  size="small"
74
81
  style={{ marginTop: 10, marginLeft: 16 }}
75
82
  icon={<IconPlus />}
76
83
  onClick={onAddProperty}
77
84
  >
78
- {config?.addButtonText ?? 'Add'}
85
+ {config?.addButtonText ?? I18n.t('Add')}
79
86
  </Button>
80
87
  </UIContainer>
81
88
  );
@@ -86,6 +93,7 @@ function PropertyEdit(props: {
86
93
  config?: ConfigType;
87
94
  onChange?: (value: PropertyValueType) => void;
88
95
  onRemove?: () => void;
96
+ readonly?: boolean;
89
97
  $isLast?: boolean;
90
98
  $index?: number;
91
99
  $isFirst?: boolean;
@@ -97,6 +105,7 @@ function PropertyEdit(props: {
97
105
  const {
98
106
  value,
99
107
  config,
108
+ readonly,
100
109
  $level = 0,
101
110
  onChange: onChangeProps,
102
111
  onRemove,
@@ -115,7 +124,7 @@ function PropertyEdit(props: {
115
124
 
116
125
  const typeSelectorValue = useMemo(() => ({ type, items }), [type, items]);
117
126
 
118
- const { propertyList, isDrilldownObject, onAddProperty, onRemoveProperty, onEditProperty } =
127
+ const { propertyList, canAddField, onAddProperty, onRemoveProperty, onEditProperty } =
119
128
  usePropertiesEdit(value, onChangeProps);
120
129
 
121
130
  const onChange = (key: string, _value: any) => {
@@ -125,7 +134,7 @@ function PropertyEdit(props: {
125
134
  });
126
135
  };
127
136
 
128
- const showCollapse = isDrilldownObject && propertyList.length > 0;
137
+ const showCollapse = canAddField && propertyList.length > 0;
129
138
 
130
139
  return (
131
140
  <>
@@ -155,6 +164,7 @@ function PropertyEdit(props: {
155
164
  <UIRow>
156
165
  <UIName>
157
166
  <BlurInput
167
+ disabled={readonly}
158
168
  placeholder={config?.placeholder ?? 'Input Variable Name'}
159
169
  size="small"
160
170
  value={name}
@@ -162,8 +172,9 @@ function PropertyEdit(props: {
162
172
  />
163
173
  </UIName>
164
174
  <UIType>
165
- <TypeSelector
175
+ <InjectTypeSelector
166
176
  value={typeSelectorValue}
177
+ readonly={readonly}
167
178
  onChange={(_value) => {
168
179
  onChangeProps?.({
169
180
  ...(value || {}),
@@ -174,12 +185,14 @@ function PropertyEdit(props: {
174
185
  </UIType>
175
186
  <UIRequired>
176
187
  <Checkbox
188
+ disabled={readonly}
177
189
  checked={isPropertyRequired}
178
190
  onChange={(e) => onChange('isPropertyRequired', e.target.checked)}
179
191
  />
180
192
  </UIRequired>
181
193
  <UIActions>
182
194
  <IconButton
195
+ disabled={readonly}
183
196
  size="small"
184
197
  theme="borderless"
185
198
  icon={expand ? <IconShrink size="small" /> : <IconExpand size="small" />}
@@ -187,8 +200,9 @@ function PropertyEdit(props: {
187
200
  setExpand((_expand) => !_expand);
188
201
  }}
189
202
  />
190
- {isDrilldownObject && (
203
+ {canAddField && (
191
204
  <IconButton
205
+ disabled={readonly}
192
206
  size="small"
193
207
  theme="borderless"
194
208
  icon={<IconAddChildren />}
@@ -199,6 +213,7 @@ function PropertyEdit(props: {
199
213
  />
200
214
  )}
201
215
  <IconButton
216
+ disabled={readonly}
202
217
  size="small"
203
218
  theme="borderless"
204
219
  icon={<IconMinus size="small" />}
@@ -208,24 +223,27 @@ function PropertyEdit(props: {
208
223
  </UIRow>
209
224
  {expand && (
210
225
  <UIExpandDetail>
211
- <UILabel>{config?.descTitle ?? 'Description'}</UILabel>
226
+ <UILabel>{config?.descTitle ?? I18n.t('Description')}</UILabel>
212
227
  <BlurInput
228
+ disabled={readonly}
213
229
  size="small"
214
230
  value={description}
215
231
  onChange={(value) => onChange('description', value)}
216
- placeholder={config?.descPlaceholder ?? 'Help LLM to understand the property'}
232
+ placeholder={
233
+ config?.descPlaceholder ?? I18n.t('Help LLM to understand the property')
234
+ }
217
235
  />
218
- {$level === 0 && type && type !== 'array' && (
236
+ {$level === 0 && (
219
237
  <>
220
238
  <UILabel style={{ marginTop: 10 }}>
221
- {config?.defaultValueTitle ?? 'Default Value'}
239
+ {config?.defaultValueTitle ?? I18n.t('Default Value')}
222
240
  </UILabel>
223
241
  <DefaultValueWrapper>
224
242
  <DefaultValue
225
243
  value={defaultValue}
226
244
  schema={value}
227
245
  type={type}
228
- placeholder={config?.defaultValuePlaceholder}
246
+ placeholder={config?.defaultValuePlaceholder ?? I18n.t('Default Value')}
229
247
  jsonFormatText={config?.jsonFormatText}
230
248
  onChange={(value) => onChange('default', value)}
231
249
  />
@@ -240,6 +258,7 @@ function PropertyEdit(props: {
240
258
  <UIProperties $shrink={true}>
241
259
  {propertyList.map((_property, index) => (
242
260
  <PropertyEdit
261
+ readonly={readonly}
243
262
  key={_property.key}
244
263
  value={_property}
245
264
  config={config}
@@ -200,35 +200,6 @@ export const DefaultValueWrapper = styled.div`
200
200
  margin: 0;
201
201
  `;
202
202
 
203
- export const JSONViewerWrapper = styled.div`
204
- padding: 0 0 24px;
205
- &:first-child {
206
- margin-top: 0px;
207
- }
208
- `;
209
-
210
- export const JSONHeader = styled.div`
211
- display: flex;
212
- justify-content: space-between;
213
- align-items: center;
214
- background-color: var(--semi-color-fill-0);
215
- border-radius: 6px 6px 0 0;
216
- height: 36px;
217
- padding: 0 8px 0 12px;
218
- `;
219
-
220
- export const JSONHeaderLeft = styled.div`
221
- display: flex;
222
- align-items: center;
223
- gap: 10px;
224
- `;
225
-
226
- export const JSONHeaderRight = styled.div`
227
- display: flex;
228
- align-items: center;
229
- gap: 10px;
230
- `;
231
-
232
203
  export const ConstantInputWrapper = styled.div`
233
204
  flex-grow: 1;
234
205
 
@@ -3,7 +3,7 @@
3
3
  * SPDX-License-Identifier: MIT
4
4
  */
5
5
 
6
- import { IJsonSchema } from '../../typings';
6
+ import { IJsonSchema } from '@flowgram.ai/json-schema';
7
7
 
8
8
  export interface PropertyValueType extends IJsonSchema {
9
9
  name?: string;
@@ -5,7 +5,7 @@
5
5
 
6
6
  import React from 'react';
7
7
 
8
- import { IFlowTemplateValue } from '../../typings';
8
+ import { IFlowTemplateValue } from '@/typings';
9
9
 
10
10
  export type PropsType = React.PropsWithChildren<{
11
11
  value?: IFlowTemplateValue;
@@ -15,8 +15,9 @@ import {
15
15
  } from '@coze-editor/editor/react';
16
16
  import { EditorAPI } from '@coze-editor/editor/preset-prompt';
17
17
 
18
+ import { IFlowValue } from '@/typings';
19
+
18
20
  import { InputsPicker } from '../inputs-picker';
19
- import { IFlowValue } from '../../../typings';
20
21
 
21
22
  export function InputsTree({ inputsValues }: { inputsValues: Record<string, IFlowValue> }) {
22
23
  const [posKey, setPosKey] = useState('');
@@ -5,9 +5,10 @@
5
5
 
6
6
  import React from 'react';
7
7
 
8
+ import { IFlowValue } from '@/typings';
9
+ import { PromptEditor, PromptEditorPropsType } from '@/components/prompt-editor';
10
+
8
11
  import { InputsTree } from './extensions/inputs-tree';
9
- import { PromptEditor, PromptEditorPropsType } from '../prompt-editor';
10
- import { IFlowValue } from '../../typings';
11
12
 
12
13
  interface PropsType extends PromptEditorPropsType {
13
14
  inputsValues: Record<string, IFlowValue>;
@@ -16,7 +16,7 @@ import {
16
16
  import { TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree';
17
17
  import { Tree } from '@douyinfe/semi-ui';
18
18
 
19
- import { IFlowValue } from '../../typings';
19
+ import { IFlowValue } from '@/typings';
20
20
 
21
21
  type VariableField = BaseVariableField<{ icon?: string | JSX.Element; title?: string }>;
22
22
 
@@ -79,7 +79,7 @@ export function InputsPicker({
79
79
  const treeData: TreeNodeData[] = useMemo(
80
80
  () =>
81
81
  Object.entries(inputsValues).map(([key, value]) => {
82
- if (value.type === 'ref') {
82
+ if (value?.type === 'ref') {
83
83
  const variable = available.getByKeyPath(value.content || []);
84
84
 
85
85
  if (variable) {
@@ -67,10 +67,13 @@ class VariableTagWidget extends WidgetType {
67
67
  return;
68
68
  }
69
69
 
70
- const rootField = last(v.parentFields);
70
+ const rootField = last(v.parentFields) || v;
71
+ const isRoot = v.parentFields.length === 0;
71
72
 
72
73
  const rootTitle = (
73
- <UIRootTitle>{rootField?.meta.title ? `${rootField.meta.title} -` : ''}</UIRootTitle>
74
+ <UIRootTitle>
75
+ {rootField?.meta.title ? `${rootField.meta.title} ${isRoot ? '' : '-'} ` : ''}
76
+ </UIRootTitle>
74
77
  );
75
78
  const rootIcon = this.renderIcon(rootField?.meta.icon);
76
79
 
@@ -86,7 +89,7 @@ class VariableTagWidget extends WidgetType {
86
89
  >
87
90
  <UITag prefixIcon={rootIcon}>
88
91
  {rootTitle}
89
- <UIVarName>{v?.key}</UIVarName>
92
+ {!isRoot && <UIVarName>{v?.key}</UIVarName>}
90
93
  </UITag>
91
94
  </Popover>
92
95
  );
@@ -15,7 +15,7 @@ import {
15
15
  } from '@coze-editor/editor/react';
16
16
  import { EditorAPI } from '@coze-editor/editor/preset-prompt';
17
17
 
18
- import { useVariableTree } from '../../variable-selector';
18
+ import { useVariableTree } from '@/components/variable-selector';
19
19
 
20
20
  export function VariableTree() {
21
21
  const [posKey, setPosKey] = useState('');
@@ -5,9 +5,10 @@
5
5
 
6
6
  import React from 'react';
7
7
 
8
+ import { PromptEditor, PromptEditorPropsType } from '@/components/prompt-editor';
9
+
8
10
  import { VariableTree } from './extensions/variable-tree';
9
11
  import { VariableTagInject } from './extensions/variable-tag';
10
- import { PromptEditor, PromptEditorPropsType } from '../prompt-editor';
11
12
 
12
13
  export function PromptEditorWithVariables(props: PromptEditorPropsType) {
13
14
  return (
@@ -5,18 +5,25 @@
5
5
 
6
6
  import React, { useMemo } from 'react';
7
7
 
8
- import { Button, Cascader } from '@douyinfe/semi-ui';
8
+ import { IJsonSchema } from '@flowgram.ai/json-schema';
9
+ import { Cascader, Icon, IconButton } from '@douyinfe/semi-ui';
9
10
 
10
- import { IJsonSchema } from '../../typings';
11
- import { ArrayIcons, VariableTypeIcons, getSchemaIcon, options } from './constants';
11
+ import { createInjectMaterial } from '@/shared/inject-material';
12
+ import { useTypeManager } from '@/plugins';
12
13
 
13
- interface PropTypes {
14
+ export interface TypeSelectorProps {
14
15
  value?: Partial<IJsonSchema>;
15
- onChange: (value?: Partial<IJsonSchema>) => void;
16
+ onChange?: (value?: Partial<IJsonSchema>) => void;
17
+ readonly?: boolean;
18
+ /**
19
+ * @deprecated use readonly instead
20
+ */
16
21
  disabled?: boolean;
17
22
  style?: React.CSSProperties;
18
23
  }
19
24
 
25
+ const labelStyle: React.CSSProperties = { display: 'flex', alignItems: 'center', gap: 5 };
26
+
20
27
  export const getTypeSelectValue = (value?: Partial<IJsonSchema>): string[] | undefined => {
21
28
  if (value?.type === 'array' && value?.items) {
22
29
  return [value.type, ...(getTypeSelectValue(value.items) || [])];
@@ -35,28 +42,66 @@ export const parseTypeSelectValue = (value?: string[]): Partial<IJsonSchema> | u
35
42
  return { type };
36
43
  };
37
44
 
38
- export function TypeSelector(props: PropTypes) {
39
- const { value, onChange, disabled, style } = props;
45
+ export function TypeSelector(props: TypeSelectorProps) {
46
+ const { value, onChange, readonly, disabled, style } = props;
40
47
 
41
48
  const selectValue = useMemo(() => getTypeSelectValue(value), [value]);
42
49
 
50
+ const typeManager = useTypeManager();
51
+
52
+ const icon = typeManager.getDisplayIcon(value || {});
53
+
54
+ const options = useMemo(
55
+ () =>
56
+ typeManager.getTypeRegistriesWithParentType().map((_type) => {
57
+ const isArray = _type.type === 'array';
58
+
59
+ return {
60
+ label: (
61
+ <div style={labelStyle}>
62
+ <Icon size="small" svg={_type.icon} />
63
+ {typeManager.getTypeBySchema(_type)?.label || _type.type}
64
+ </div>
65
+ ),
66
+ value: _type.type,
67
+ children: isArray
68
+ ? typeManager.getTypeRegistriesWithParentType('array').map((_type) => ({
69
+ label: (
70
+ <div style={labelStyle}>
71
+ <Icon
72
+ size="small"
73
+ svg={typeManager.getDisplayIcon({
74
+ type: 'array',
75
+ items: { type: _type.type },
76
+ })}
77
+ />
78
+ {typeManager.getTypeBySchema(_type)?.label || _type.type}
79
+ </div>
80
+ ),
81
+ value: _type.type,
82
+ }))
83
+ : [],
84
+ };
85
+ }),
86
+ []
87
+ );
88
+
43
89
  return (
44
90
  <Cascader
45
- disabled={disabled}
91
+ disabled={readonly || disabled}
46
92
  size="small"
47
93
  triggerRender={() => (
48
- <Button size="small" style={style}>
49
- {getSchemaIcon(value)}
50
- </Button>
94
+ <IconButton size="small" style={style} disabled={readonly || disabled} icon={icon} />
51
95
  )}
52
96
  treeData={options}
53
97
  value={selectValue}
54
98
  leafOnly={true}
55
99
  onChange={(value) => {
56
- onChange(parseTypeSelectValue(value as string[]));
100
+ onChange?.(parseTypeSelectValue(value as string[]));
57
101
  }}
58
102
  />
59
103
  );
60
104
  }
61
105
 
62
- export { VariableTypeIcons, ArrayIcons, getSchemaIcon };
106
+ TypeSelector.renderKey = 'type-selector-render-key';
107
+ export const InjectTypeSelector = createInjectMaterial(TypeSelector);