@flowgram.ai/form-materials 0.1.31 → 0.2.0

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 (37) hide show
  1. package/bin/materials.js +21 -5
  2. package/dist/esm/index.js +453 -50
  3. package/dist/esm/index.js.map +1 -1
  4. package/dist/index.d.mts +161 -29
  5. package/dist/index.d.ts +161 -29
  6. package/dist/index.js +463 -54
  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 +18 -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/hooks.tsx +33 -22
  19. package/src/components/type-selector/index.tsx +5 -2
  20. package/src/components/type-selector/types.ts +4 -18
  21. package/src/components/variable-selector/config.json +1 -1
  22. package/src/components/variable-selector/index.tsx +76 -16
  23. package/src/components/variable-selector/styles.tsx +43 -0
  24. package/src/components/variable-selector/use-variable-tree.tsx +29 -7
  25. package/src/effects/index.ts +2 -0
  26. package/src/effects/provide-batch-input/config.json +5 -0
  27. package/src/effects/provide-batch-input/index.ts +38 -0
  28. package/src/effects/provide-batch-outputs/config.json +5 -0
  29. package/src/effects/provide-batch-outputs/index.ts +34 -0
  30. package/src/index.ts +3 -0
  31. package/src/typings/flow-value/config.json +5 -0
  32. package/src/typings/flow-value/index.ts +27 -0
  33. package/src/typings/index.ts +1 -0
  34. package/src/utils/format-legacy-refs/config.json +5 -0
  35. package/src/utils/format-legacy-refs/index.ts +153 -0
  36. package/src/utils/format-legacy-refs/readme.md +38 -0
  37. package/src/utils/index.ts +1 -0
@@ -1,3 +1,6 @@
1
- export { VariableSelector } from './variable-selector';
2
- export { TypeSelector, JsonSchema, VariableTypeIcons, ArrayIcons } from './type-selector';
3
- export { JsonSchemaEditor } from './json-schema-editor';
1
+ export * from './variable-selector';
2
+ export * from './type-selector';
3
+ export * from './json-schema-editor';
4
+ export * from './batch-variable-selector';
5
+ export * from './constant-input';
6
+ export * from './dynamic-value-input';
@@ -36,15 +36,21 @@ export function usePropertiesEdit(
36
36
  const initPropertyList = useMemo(
37
37
  () =>
38
38
  isDrilldownObject
39
- ? Object.entries(drilldown.schema?.properties || {}).map(
40
- ([name, _value]) =>
41
- ({
42
- key: genId(),
43
- name,
44
- isPropertyRequired: drilldown.schema?.required?.includes(name) || false,
45
- ..._value,
46
- } as PropertyValueType)
47
- )
39
+ ? Object.entries(drilldown.schema?.properties || {})
40
+ .sort(([, a], [, b]) => (a.extra?.index ?? 0) - (b.extra?.index ?? 0))
41
+ .map(
42
+ ([name, _value], index) =>
43
+ ({
44
+ key: genId(),
45
+ name,
46
+ isPropertyRequired: drilldown.schema?.required?.includes(name) || false,
47
+ ..._value,
48
+ extra: {
49
+ ...(_value.extra || {}),
50
+ index,
51
+ },
52
+ } as PropertyValueType)
53
+ )
48
54
  : [],
49
55
  [isDrilldownObject]
50
56
  );
@@ -65,23 +71,25 @@ export function usePropertiesEdit(
65
71
  nameMap.set(_property.name, _property);
66
72
  }
67
73
  }
68
- return Object.entries(drilldown.schema?.properties || {}).map(([name, _value]) => {
69
- const _property = nameMap.get(name);
70
- if (_property) {
74
+ return Object.entries(drilldown.schema?.properties || {})
75
+ .sort(([, a], [, b]) => (a.extra?.index ?? 0) - (b.extra?.index ?? 0))
76
+ .map(([name, _value]) => {
77
+ const _property = nameMap.get(name);
78
+ if (_property) {
79
+ return {
80
+ key: _property.key,
81
+ name,
82
+ isPropertyRequired: drilldown.schema?.required?.includes(name) || false,
83
+ ..._value,
84
+ };
85
+ }
71
86
  return {
72
- key: _property.key,
87
+ key: genId(),
73
88
  name,
74
89
  isPropertyRequired: drilldown.schema?.required?.includes(name) || false,
75
90
  ..._value,
76
91
  };
77
- }
78
- return {
79
- key: genId(),
80
- name,
81
- isPropertyRequired: drilldown.schema?.required?.includes(name) || false,
82
- ..._value,
83
- };
84
- });
92
+ });
85
93
  });
86
94
  }
87
95
  mountRef.current = true;
@@ -121,7 +129,10 @@ export function usePropertiesEdit(
121
129
  };
122
130
 
123
131
  const onAddProperty = () => {
124
- updatePropertyList((_list) => [..._list, { key: genId(), name: '', type: 'string' }]);
132
+ updatePropertyList((_list) => [
133
+ ..._list,
134
+ { key: genId(), name: '', type: 'string', extra: { index: _list.length + 1 } },
135
+ ]);
125
136
  };
126
137
 
127
138
  const onRemoveProperty = (key: number) => {
@@ -8,6 +8,8 @@ import { ArrayIcons, VariableTypeIcons, getSchemaIcon, options } from './constan
8
8
  interface PropTypes {
9
9
  value?: Partial<JsonSchema>;
10
10
  onChange: (value?: Partial<JsonSchema>) => void;
11
+ disabled?: boolean;
12
+ style?: React.CSSProperties;
11
13
  }
12
14
 
13
15
  export const getTypeSelectValue = (value?: Partial<JsonSchema>): string[] | undefined => {
@@ -29,15 +31,16 @@ export const parseTypeSelectValue = (value?: string[]): Partial<JsonSchema> | un
29
31
  };
30
32
 
31
33
  export function TypeSelector(props: PropTypes) {
32
- const { value, onChange } = props;
34
+ const { value, onChange, disabled, style } = props;
33
35
 
34
36
  const selectValue = useMemo(() => getTypeSelectValue(value), [value]);
35
37
 
36
38
  return (
37
39
  <Cascader
40
+ disabled={disabled}
38
41
  size="small"
39
42
  triggerRender={() => (
40
- <Button size="small" style={{ width: 50 }}>
43
+ <Button size="small" style={style}>
41
44
  {getSchemaIcon(value)}
42
45
  </Button>
43
46
  )}
@@ -1,19 +1,5 @@
1
- export type BasicType = 'boolean' | 'string' | 'integer' | 'number' | 'object' | 'array';
1
+ import { VarJSONSchema } from '@flowgram.ai/editor';
2
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
- }
3
+ export type BasicType = VarJSONSchema.BasicType;
4
+
5
+ export type JsonSchema = VarJSONSchema.ISchema;
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "variable-selector",
3
3
  "depMaterials": ["type-selector"],
4
- "depPackages": ["@douyinfe/semi-ui"]
4
+ "depPackages": ["@douyinfe/semi-ui", "styled-components"]
5
5
  }
@@ -1,47 +1,107 @@
1
- import React from 'react';
1
+ import React, { useMemo } from 'react';
2
2
 
3
- import { TreeSelect } from '@douyinfe/semi-ui';
3
+ import { VarJSONSchema } from '@flowgram.ai/editor';
4
+ import { TriggerRenderProps } from '@douyinfe/semi-ui/lib/es/treeSelect';
5
+ import { TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree';
6
+ import { IconChevronDownStroked, IconIssueStroked } from '@douyinfe/semi-icons';
4
7
 
5
8
  import { useVariableTree } from './use-variable-tree';
9
+ import { UIRootTitle, UITag, UITreeSelect } from './styles';
6
10
 
7
- export interface PropTypes {
8
- value?: string;
9
- config: {
11
+ interface PropTypes {
12
+ value?: string[];
13
+ config?: {
10
14
  placeholder?: string;
15
+ notFoundContent?: string;
11
16
  };
12
- onChange: (value?: string) => void;
17
+ onChange: (value?: string[]) => void;
18
+ includeSchema?: VarJSONSchema.ISchema | VarJSONSchema.ISchema[];
19
+ excludeSchema?: VarJSONSchema.ISchema | VarJSONSchema.ISchema[];
13
20
  readonly?: boolean;
14
21
  hasError?: boolean;
15
22
  style?: React.CSSProperties;
23
+ triggerRender?: (props: TriggerRenderProps) => React.ReactNode;
16
24
  }
17
25
 
26
+ export type VariableSelectorProps = PropTypes;
27
+
18
28
  export const VariableSelector = ({
19
29
  value,
20
30
  config,
21
31
  onChange,
22
32
  style,
23
33
  readonly = false,
34
+ includeSchema,
35
+ excludeSchema,
24
36
  hasError,
37
+ triggerRender,
25
38
  }: PropTypes) => {
26
- const treeData = useVariableTree();
39
+ const treeData = useVariableTree({ includeSchema, excludeSchema });
40
+
41
+ const treeValue = useMemo(() => {
42
+ if (typeof value === 'string') {
43
+ console.warn(
44
+ 'The Value of VariableSelector is a string, it should be an ARRAY. \n',
45
+ 'Please check the value of VariableSelector \n'
46
+ );
47
+ return value;
48
+ }
49
+ return value?.join('.');
50
+ }, [value]);
51
+
52
+ const renderIcon = (icon: string | JSX.Element) => {
53
+ if (typeof icon === 'string') {
54
+ return <img style={{ marginRight: 8 }} width={12} height={12} src={icon} />;
55
+ }
56
+
57
+ return icon;
58
+ };
27
59
 
28
60
  return (
29
61
  <>
30
- <TreeSelect
62
+ <UITreeSelect
31
63
  dropdownMatchSelectWidth={false}
32
64
  disabled={readonly}
33
65
  treeData={treeData}
34
66
  size="small"
35
- value={value}
36
- style={{
37
- ...style,
38
- outline: hasError ? '1px solid red' : undefined,
39
- }}
67
+ value={treeValue}
68
+ clearIcon={null}
69
+ $error={hasError}
70
+ style={style}
40
71
  validateStatus={hasError ? 'error' : undefined}
41
- onChange={(option) => {
42
- onChange(option as string);
72
+ onChange={(_, _config) => {
73
+ onChange((_config as TreeNodeData).keyPath as string[]);
74
+ }}
75
+ renderSelectedItem={(_option: TreeNodeData) => {
76
+ if (!_option?.keyPath) {
77
+ return (
78
+ <UITag
79
+ prefixIcon={<IconIssueStroked />}
80
+ color="amber"
81
+ closable={!readonly}
82
+ onClose={() => onChange(undefined)}
83
+ >
84
+ {config?.notFoundContent ?? 'Undefined'}
85
+ </UITag>
86
+ );
87
+ }
88
+
89
+ return (
90
+ <UITag
91
+ prefixIcon={renderIcon(_option.rootMeta?.icon || _option?.icon)}
92
+ closable={!readonly}
93
+ onClose={() => onChange(undefined)}
94
+ >
95
+ <UIRootTitle>
96
+ {_option.rootMeta?.title ? `${_option.rootMeta?.title} -` : null}
97
+ </UIRootTitle>
98
+ {_option.label}
99
+ </UITag>
100
+ );
43
101
  }}
44
- showClear
102
+ showClear={false}
103
+ arrowIcon={value ? null : <IconChevronDownStroked size="small" />}
104
+ triggerRender={triggerRender}
45
105
  placeholder={config?.placeholder ?? 'Select Variable...'}
46
106
  />
47
107
  </>
@@ -0,0 +1,43 @@
1
+ import styled from 'styled-components';
2
+ import { Tag, TreeSelect } from '@douyinfe/semi-ui';
3
+
4
+ export const UIRootTitle = styled.span`
5
+ margin-right: 4px;
6
+ color: var(--semi-color-text-2);
7
+ `;
8
+
9
+ export const UITag = styled(Tag)`
10
+ width: 100%;
11
+ display: flex;
12
+ align-items: center;
13
+ justify-content: flex-start;
14
+
15
+ & .semi-tag-content-center {
16
+ justify-content: flex-start;
17
+ }
18
+
19
+ &.semi-tag {
20
+ margin: 0;
21
+ }
22
+ `;
23
+
24
+ export const UITreeSelect = styled(TreeSelect)<{ $error?: boolean }>`
25
+ outline: ${({ $error }) => ($error ? '1px solid red' : 'none')};
26
+
27
+ height: 22px;
28
+ min-height: 22px;
29
+ line-height: 22px;
30
+
31
+ & .semi-tree-select-selection {
32
+ padding: 0 2px;
33
+ height: 22px;
34
+ }
35
+
36
+ & .semi-tree-select-selection-content {
37
+ width: 100%;
38
+ }
39
+
40
+ & .semi-tree-select-selection-placeholder {
41
+ padding-left: 10px;
42
+ }
43
+ `;
@@ -1,6 +1,6 @@
1
1
  import React, { useCallback } from 'react';
2
2
 
3
- import { useScopeAvailable, ASTMatch, BaseVariableField } from '@flowgram.ai/editor';
3
+ import { useScopeAvailable, ASTMatch, BaseVariableField, VarJSONSchema } from '@flowgram.ai/editor';
4
4
  import { TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree';
5
5
  import { Icon } from '@douyinfe/semi-ui';
6
6
 
@@ -8,11 +8,16 @@ import { ArrayIcons, VariableTypeIcons } from '../type-selector/constants';
8
8
 
9
9
  type VariableField = BaseVariableField<{ icon?: string | JSX.Element; title?: string }>;
10
10
 
11
- export function useVariableTree(): TreeNodeData[] {
11
+ export function useVariableTree(params: {
12
+ includeSchema?: VarJSONSchema.ISchema | VarJSONSchema.ISchema[];
13
+ excludeSchema?: VarJSONSchema.ISchema | VarJSONSchema.ISchema[];
14
+ }): TreeNodeData[] {
15
+ const { includeSchema, excludeSchema } = params;
16
+
12
17
  const available = useScopeAvailable();
13
18
 
14
19
  const getVariableTypeIcon = useCallback((variable: VariableField) => {
15
- if (variable.meta.icon) {
20
+ if (variable.meta?.icon) {
16
21
  if (typeof variable.meta.icon === 'string') {
17
22
  return <img style={{ marginRight: 8 }} width={12} height={12} src={variable.meta.icon} />;
18
23
  }
@@ -44,6 +49,10 @@ export function useVariableTree(): TreeNodeData[] {
44
49
  ): TreeNodeData | null => {
45
50
  let type = variable?.type;
46
51
 
52
+ if (!type) {
53
+ return null;
54
+ }
55
+
47
56
  let children: TreeNodeData[] | undefined;
48
57
 
49
58
  if (ASTMatch.isObject(type)) {
@@ -56,14 +65,27 @@ export function useVariableTree(): TreeNodeData[] {
56
65
  }
57
66
  }
58
67
 
59
- const currPath = [...parentFields.map((_field) => _field.key), variable.key].join('.');
68
+ const keyPath = [...parentFields.map((_field) => _field.key), variable.key];
69
+ const key = keyPath.join('.');
70
+
71
+ const isSchemaInclude = includeSchema ? type.isEqualWithJSONSchema(includeSchema) : true;
72
+ const isSchemaExclude = excludeSchema ? type.isEqualWithJSONSchema(excludeSchema) : false;
73
+ const isSchemaMatch = isSchemaInclude && !isSchemaExclude;
74
+
75
+ // If not match, and no children, return null
76
+ if (!isSchemaMatch && !children?.length) {
77
+ return null;
78
+ }
60
79
 
61
80
  return {
62
- key: currPath,
63
- label: variable.meta.title || variable.key,
64
- value: currPath,
81
+ key: key,
82
+ label: variable.meta?.title || variable.key,
83
+ value: key,
84
+ keyPath,
65
85
  icon: getVariableTypeIcon(variable),
66
86
  children,
87
+ disabled: !isSchemaMatch,
88
+ rootMeta: parentFields[0]?.meta,
67
89
  };
68
90
  };
69
91
 
@@ -0,0 +1,2 @@
1
+ export * from './provide-batch-input';
2
+ export * from './provide-batch-outputs';
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "provide-batch-input",
3
+ "depMaterials": ["flow-value"],
4
+ "depPackages": []
5
+ }
@@ -0,0 +1,38 @@
1
+ import {
2
+ ASTFactory,
3
+ EffectOptions,
4
+ FlowNodeRegistry,
5
+ createEffectFromVariableProvider,
6
+ getNodeForm,
7
+ } from '@flowgram.ai/editor';
8
+
9
+ import { IFlowRefValue } from '../../typings';
10
+
11
+ export const provideBatchInputEffect: EffectOptions[] = createEffectFromVariableProvider({
12
+ private: true,
13
+ parse: (value: IFlowRefValue, ctx) => [
14
+ ASTFactory.createVariableDeclaration({
15
+ key: `${ctx.node.id}_locals`,
16
+ meta: {
17
+ title: getNodeForm(ctx.node)?.getValueIn('title'),
18
+ icon: ctx.node.getNodeRegistry<FlowNodeRegistry>().info?.icon,
19
+ },
20
+ type: ASTFactory.createObject({
21
+ properties: [
22
+ ASTFactory.createProperty({
23
+ key: 'item',
24
+ initializer: ASTFactory.createEnumerateExpression({
25
+ enumerateFor: ASTFactory.createKeyPathExpression({
26
+ keyPath: value.content || [],
27
+ }),
28
+ }),
29
+ }),
30
+ ASTFactory.createProperty({
31
+ key: 'index',
32
+ type: ASTFactory.createNumber(),
33
+ }),
34
+ ],
35
+ }),
36
+ }),
37
+ ],
38
+ });
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "provide-batch-outputs",
3
+ "depMaterials": ["flow-value"],
4
+ "depPackages": []
5
+ }
@@ -0,0 +1,34 @@
1
+ import {
2
+ ASTFactory,
3
+ EffectOptions,
4
+ FlowNodeRegistry,
5
+ createEffectFromVariableProvider,
6
+ getNodeForm,
7
+ } from '@flowgram.ai/editor';
8
+
9
+ import { IFlowRefValue } from '../../typings';
10
+
11
+ export const provideBatchOutputsEffect: EffectOptions[] = createEffectFromVariableProvider({
12
+ private: true,
13
+ parse: (value: Record<string, IFlowRefValue>, ctx) => [
14
+ ASTFactory.createVariableDeclaration({
15
+ key: `${ctx.node.id}`,
16
+ meta: {
17
+ title: getNodeForm(ctx.node)?.getValueIn('title'),
18
+ icon: ctx.node.getNodeRegistry<FlowNodeRegistry>().info?.icon,
19
+ },
20
+ type: ASTFactory.createObject({
21
+ properties: Object.entries(value).map(([_key, value]) =>
22
+ ASTFactory.createProperty({
23
+ key: _key,
24
+ initializer: ASTFactory.createWrapArrayExpression({
25
+ wrapFor: ASTFactory.createKeyPathExpression({
26
+ keyPath: value.content || [],
27
+ }),
28
+ }),
29
+ })
30
+ ),
31
+ }),
32
+ }),
33
+ ],
34
+ });
package/src/index.ts CHANGED
@@ -1 +1,4 @@
1
1
  export * from './components';
2
+ export * from './effects';
3
+ export * from './utils';
4
+ export * from './typings';
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "flow-value",
3
+ "depMaterials": [],
4
+ "depPackages": []
5
+ }
@@ -0,0 +1,27 @@
1
+ export interface IFlowConstantValue {
2
+ type: 'constant';
3
+ content?: string | number | boolean;
4
+ }
5
+
6
+ export interface IFlowRefValue {
7
+ type: 'ref';
8
+ content?: string[];
9
+ }
10
+
11
+ export interface IFlowExpressionValue {
12
+ type: 'expression';
13
+ content?: string;
14
+ }
15
+
16
+ export interface IFlowTemplateValue {
17
+ type: 'template';
18
+ content?: string;
19
+ }
20
+
21
+ export type IFlowValue =
22
+ | IFlowConstantValue
23
+ | IFlowRefValue
24
+ | IFlowExpressionValue
25
+ | IFlowTemplateValue;
26
+
27
+ export type IFlowConstantRefValue = IFlowConstantValue | IFlowRefValue;
@@ -0,0 +1 @@
1
+ export * from './flow-value';
@@ -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
+ }