@flowgram.ai/form-materials 0.2.0 → 0.2.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.
@@ -3,7 +3,7 @@ import React from 'react';
3
3
  import { CascaderData } from '@douyinfe/semi-ui/lib/es/cascader';
4
4
  import Icon from '@douyinfe/semi-icons';
5
5
 
6
- import { JsonSchema } from './types';
6
+ import { IJsonSchema } from '../../typings';
7
7
 
8
8
  export const VariableTypeIcons: { [key: string]: React.ReactNode } = {
9
9
  custom: (
@@ -271,7 +271,7 @@ export const ArrayIcons: { [key: string]: React.ReactNode } = {
271
271
  ),
272
272
  };
273
273
 
274
- export const getSchemaIcon = (value?: Partial<JsonSchema>) => {
274
+ export const getSchemaIcon = (value?: Partial<IJsonSchema>) => {
275
275
  if (value?.type === 'array') {
276
276
  return ArrayIcons[value.items?.type || 'object'];
277
277
  }
@@ -2,17 +2,17 @@ import React, { useMemo } from 'react';
2
2
 
3
3
  import { Button, Cascader } from '@douyinfe/semi-ui';
4
4
 
5
- import { JsonSchema } from './types';
5
+ import { IJsonSchema } from '../../typings';
6
6
  import { ArrayIcons, VariableTypeIcons, getSchemaIcon, options } from './constants';
7
7
 
8
8
  interface PropTypes {
9
- value?: Partial<JsonSchema>;
10
- onChange: (value?: Partial<JsonSchema>) => void;
9
+ value?: Partial<IJsonSchema>;
10
+ onChange: (value?: Partial<IJsonSchema>) => void;
11
11
  disabled?: boolean;
12
12
  style?: React.CSSProperties;
13
13
  }
14
14
 
15
- export const getTypeSelectValue = (value?: Partial<JsonSchema>): string[] | undefined => {
15
+ export const getTypeSelectValue = (value?: Partial<IJsonSchema>): string[] | undefined => {
16
16
  if (value?.type === 'array' && value?.items) {
17
17
  return [value.type, ...(getTypeSelectValue(value.items) || [])];
18
18
  }
@@ -20,7 +20,7 @@ export const getTypeSelectValue = (value?: Partial<JsonSchema>): string[] | unde
20
20
  return value?.type ? [value.type] : undefined;
21
21
  };
22
22
 
23
- export const parseTypeSelectValue = (value?: string[]): Partial<JsonSchema> | undefined => {
23
+ export const parseTypeSelectValue = (value?: string[]): Partial<IJsonSchema> | undefined => {
24
24
  const [type, ...subTypes] = value || [];
25
25
 
26
26
  if (type === 'array') {
@@ -54,4 +54,4 @@ export function TypeSelector(props: PropTypes) {
54
54
  );
55
55
  }
56
56
 
57
- export { JsonSchema, VariableTypeIcons, ArrayIcons, getSchemaIcon };
57
+ export { VariableTypeIcons, ArrayIcons, getSchemaIcon };
@@ -1,5 +1,5 @@
1
1
  {
2
2
  "name": "variable-selector",
3
- "depMaterials": ["type-selector"],
3
+ "depMaterials": ["type-selector", "utils/json-schema", "typings/json-schema"],
4
4
  "depPackages": ["@douyinfe/semi-ui", "styled-components"]
5
5
  }
@@ -1,10 +1,10 @@
1
1
  import React, { useMemo } from 'react';
2
2
 
3
- import { VarJSONSchema } from '@flowgram.ai/editor';
4
3
  import { TriggerRenderProps } from '@douyinfe/semi-ui/lib/es/treeSelect';
5
4
  import { TreeNodeData } from '@douyinfe/semi-ui/lib/es/tree';
6
5
  import { IconChevronDownStroked, IconIssueStroked } from '@douyinfe/semi-icons';
7
6
 
7
+ import { IJsonSchema } from '../../typings/json-schema';
8
8
  import { useVariableTree } from './use-variable-tree';
9
9
  import { UIRootTitle, UITag, UITreeSelect } from './styles';
10
10
 
@@ -15,8 +15,8 @@ interface PropTypes {
15
15
  notFoundContent?: string;
16
16
  };
17
17
  onChange: (value?: string[]) => void;
18
- includeSchema?: VarJSONSchema.ISchema | VarJSONSchema.ISchema[];
19
- excludeSchema?: VarJSONSchema.ISchema | VarJSONSchema.ISchema[];
18
+ includeSchema?: IJsonSchema | IJsonSchema[];
19
+ excludeSchema?: IJsonSchema | IJsonSchema[];
20
20
  readonly?: boolean;
21
21
  hasError?: boolean;
22
22
  style?: React.CSSProperties;
@@ -1,16 +1,18 @@
1
1
  import React, { useCallback } from 'react';
2
2
 
3
- import { useScopeAvailable, ASTMatch, BaseVariableField, VarJSONSchema } from '@flowgram.ai/editor';
3
+ import { useScopeAvailable, ASTMatch, BaseVariableField } 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
 
7
7
  import { ArrayIcons, VariableTypeIcons } from '../type-selector/constants';
8
+ import { JsonSchemaUtils } from '../../utils/json-schema';
9
+ import { IJsonSchema } from '../../typings/json-schema';
8
10
 
9
11
  type VariableField = BaseVariableField<{ icon?: string | JSX.Element; title?: string }>;
10
12
 
11
13
  export function useVariableTree(params: {
12
- includeSchema?: VarJSONSchema.ISchema | VarJSONSchema.ISchema[];
13
- excludeSchema?: VarJSONSchema.ISchema | VarJSONSchema.ISchema[];
14
+ includeSchema?: IJsonSchema | IJsonSchema[];
15
+ excludeSchema?: IJsonSchema | IJsonSchema[];
14
16
  }): TreeNodeData[] {
15
17
  const { includeSchema, excludeSchema } = params;
16
18
 
@@ -68,8 +70,12 @@ export function useVariableTree(params: {
68
70
  const keyPath = [...parentFields.map((_field) => _field.key), variable.key];
69
71
  const key = keyPath.join('.');
70
72
 
71
- const isSchemaInclude = includeSchema ? type.isEqualWithJSONSchema(includeSchema) : true;
72
- const isSchemaExclude = excludeSchema ? type.isEqualWithJSONSchema(excludeSchema) : false;
73
+ const isSchemaInclude = includeSchema
74
+ ? JsonSchemaUtils.isASTMatchSchema(type, includeSchema)
75
+ : true;
76
+ const isSchemaExclude = excludeSchema
77
+ ? JsonSchemaUtils.isASTMatchSchema(type, excludeSchema)
78
+ : false;
73
79
  const isSchemaMatch = isSchemaInclude && !isSchemaExclude;
74
80
 
75
81
  // If not match, and no children, return null
@@ -1 +1,2 @@
1
1
  export * from './flow-value';
2
+ export * from './json-schema';
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "json-schema",
3
+ "depMaterials": [],
4
+ "depPackages": []
5
+ }
@@ -0,0 +1,31 @@
1
+ export type JsonSchemaBasicType =
2
+ | 'boolean'
3
+ | 'string'
4
+ | 'integer'
5
+ | 'number'
6
+ | 'object'
7
+ | 'array'
8
+ | 'map';
9
+
10
+ export interface IJsonSchema<T = string> {
11
+ type?: T;
12
+ default?: any;
13
+ title?: string;
14
+ description?: string;
15
+ enum?: (string | number)[];
16
+ properties?: Record<string, IJsonSchema<T>>;
17
+ additionalProperties?: IJsonSchema<T>;
18
+ items?: IJsonSchema<T>;
19
+ required?: string[];
20
+ $ref?: string;
21
+ extra?: {
22
+ index?: number;
23
+ // Used in BaseType.isEqualWithJSONSchema, the type comparison will be weak
24
+ weak?: boolean;
25
+ // Set the render component
26
+ formComponent?: string;
27
+ [key: string]: any;
28
+ };
29
+ }
30
+
31
+ export type IBasicJsonSchema = IJsonSchema<JsonSchemaBasicType>;
@@ -1 +1,2 @@
1
1
  export * from './format-legacy-refs';
2
+ export * from './json-schema';
@@ -0,0 +1,5 @@
1
+ {
2
+ "name": "json-schema",
3
+ "depMaterials": ["typings/json-schema"],
4
+ "depPackages": ["lodash"]
5
+ }
@@ -0,0 +1,154 @@
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(typeAST: ASTNode): IJsonSchema | undefined {
78
+ if (ASTMatch.isString(typeAST)) {
79
+ return {
80
+ type: 'string',
81
+ };
82
+ }
83
+
84
+ if (ASTMatch.isBoolean(typeAST)) {
85
+ return {
86
+ type: 'boolean',
87
+ };
88
+ }
89
+
90
+ if (ASTMatch.isNumber(typeAST)) {
91
+ return {
92
+ type: 'number',
93
+ };
94
+ }
95
+
96
+ if (ASTMatch.isInteger(typeAST)) {
97
+ return {
98
+ type: 'integer',
99
+ };
100
+ }
101
+
102
+ if (ASTMatch.isObject(typeAST)) {
103
+ return {
104
+ type: 'object',
105
+ properties: Object.fromEntries(
106
+ Object.entries(typeAST.properties).map(([key, value]) => [key, astToSchema(value)!])
107
+ ),
108
+ };
109
+ }
110
+
111
+ if (ASTMatch.isArray(typeAST)) {
112
+ return {
113
+ type: 'array',
114
+ items: astToSchema(typeAST.items),
115
+ };
116
+ }
117
+
118
+ if (ASTMatch.isMap(typeAST)) {
119
+ return {
120
+ type: 'map',
121
+ items: astToSchema(typeAST.valueType),
122
+ };
123
+ }
124
+
125
+ if (ASTMatch.isCustomType(typeAST)) {
126
+ return {
127
+ type: typeAST.typeName,
128
+ };
129
+ }
130
+
131
+ return undefined;
132
+ }
133
+
134
+ /**
135
+ * Check if the AST type is match the JSON Schema
136
+ * @param typeAST
137
+ * @param schema
138
+ * @returns
139
+ */
140
+ export function isASTMatchSchema(
141
+ typeAST: BaseType,
142
+ schema: IJsonSchema | IJsonSchema[]
143
+ ): boolean {
144
+ if (Array.isArray(schema)) {
145
+ return typeAST.isTypeEqual(
146
+ ASTFactory.createUnion({
147
+ types: schema.map((_schema) => schemaToAST(_schema)!).filter(Boolean),
148
+ })
149
+ );
150
+ }
151
+
152
+ return typeAST.isTypeEqual(schemaToAST(schema));
153
+ }
154
+ }
@@ -1,5 +0,0 @@
1
- import { VarJSONSchema } from '@flowgram.ai/editor';
2
-
3
- export type BasicType = VarJSONSchema.BasicType;
4
-
5
- export type JsonSchema = VarJSONSchema.ISchema;