@elementor/editor-props 3.33.0-271 → 3.33.0-272

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.
@@ -0,0 +1,190 @@
1
+ import { type PropsSchema, type PropType } from '../types';
2
+ import { type JsonSchema7 } from './prop-json-schema';
3
+
4
+ export function jsonSchemaToPropType( schema: JsonSchema7, key = < string >schema.key ): PropType {
5
+ const meta: Record< string, unknown > = {};
6
+
7
+ if ( schema.description ) {
8
+ meta.description = schema.description;
9
+ }
10
+
11
+ // Handle union types (anyOf)
12
+ if ( schema.anyOf && Array.isArray( schema.anyOf ) ) {
13
+ return convertJsonSchemaToUnionPropType( schema, meta );
14
+ }
15
+
16
+ // Handle object types
17
+ if ( schema.type === 'object' && schema.properties ) {
18
+ return convertJsonSchemaToObjectPropType( schema, meta, key );
19
+ }
20
+
21
+ // Handle array types
22
+ if ( schema.type === 'array' && schema.items ) {
23
+ return convertJsonSchemaToArrayPropType( schema, meta, key );
24
+ }
25
+
26
+ // Handle plain types (string, number, boolean)
27
+ return convertJsonSchemaToPlainPropType( schema, meta, key );
28
+ }
29
+
30
+ function convertJsonSchemaToPlainPropType(
31
+ schema: JsonSchema7,
32
+ meta: Record< string, unknown >,
33
+ key = < string >schema.key
34
+ ): PropType {
35
+ const settings: Record< string, unknown > = {};
36
+
37
+ // Determine the key based on type
38
+ let propKey = key || 'string';
39
+
40
+ if ( schema.type === 'number' ) {
41
+ propKey = 'number';
42
+ } else if ( schema.type === 'boolean' ) {
43
+ propKey = 'boolean';
44
+ } else if ( schema.type === 'string' ) {
45
+ propKey = 'string';
46
+ }
47
+
48
+ // Handle enum values
49
+ if ( Array.isArray( schema.enum ) ) {
50
+ settings.enum = schema.enum;
51
+ }
52
+
53
+ return {
54
+ kind: 'plain',
55
+ key: propKey,
56
+ settings,
57
+ meta,
58
+ } as PropType;
59
+ }
60
+
61
+ /**
62
+ * Converts a JSON Schema anyOf to a union PropType
63
+ * @param schema
64
+ * @param meta
65
+ */
66
+ function convertJsonSchemaToUnionPropType( schema: JsonSchema7, meta: Record< string, unknown > ): PropType {
67
+ const propTypes: Record< string, PropType > = {};
68
+
69
+ if ( ! schema.anyOf || ! Array.isArray( schema.anyOf ) ) {
70
+ throw new Error( 'Invalid anyOf schema' );
71
+ }
72
+
73
+ // Process each variant in the anyOf array
74
+ for ( const variantSchema of schema.anyOf ) {
75
+ // Each variant should be an object with $$type and value properties
76
+ if (
77
+ variantSchema.type === 'object' &&
78
+ variantSchema.properties &&
79
+ variantSchema.properties.$$type &&
80
+ variantSchema.properties.value
81
+ ) {
82
+ const typeProperty = variantSchema.properties.$$type;
83
+
84
+ // Extract the type key from the enum
85
+ let typeKey: string;
86
+ if ( typeProperty.enum && Array.isArray( typeProperty.enum ) && typeProperty.enum.length > 0 ) {
87
+ typeKey = typeProperty.enum[ 0 ] as string;
88
+ } else {
89
+ continue;
90
+ }
91
+
92
+ // Convert the value schema to a PropType
93
+ const valuePropType = convertJsonSchemaToPropType( variantSchema.properties.value );
94
+ propTypes[ typeKey ] = valuePropType;
95
+ }
96
+ }
97
+
98
+ return {
99
+ kind: 'union',
100
+ prop_types: propTypes,
101
+ settings: {},
102
+ meta,
103
+ } as PropType;
104
+ }
105
+
106
+ function convertJsonSchemaToObjectPropType(
107
+ schema: JsonSchema7,
108
+ meta: Record< string, unknown >,
109
+ key = < string >schema.key
110
+ ): PropType {
111
+ const shape: Record< string, PropType > = {};
112
+
113
+ if ( ! schema.properties ) {
114
+ return {
115
+ kind: 'object',
116
+ key,
117
+ shape: {},
118
+ settings: {},
119
+ meta,
120
+ } as PropType;
121
+ }
122
+
123
+ const requiredFields = Array.isArray( schema.required ) ? schema.required : [];
124
+
125
+ // Convert each property
126
+ for ( const [ propKey, propSchema ] of Object.entries( schema.properties ) ) {
127
+ const subPropType = convertJsonSchemaToPropType( propSchema, key );
128
+
129
+ // Mark as required if it's in the required array
130
+ if ( requiredFields.includes( propKey ) ) {
131
+ subPropType.settings = {
132
+ ...subPropType.settings,
133
+ required: true,
134
+ };
135
+ }
136
+
137
+ shape[ propKey ] = subPropType;
138
+ }
139
+
140
+ return {
141
+ kind: 'object',
142
+ key: key || 'object',
143
+ shape,
144
+ settings: {},
145
+ meta,
146
+ } as PropType;
147
+ }
148
+
149
+ function convertJsonSchemaToArrayPropType(
150
+ schema: JsonSchema7,
151
+ meta: Record< string, unknown >,
152
+ key = < string >schema.key
153
+ ): PropType {
154
+ if ( ! schema.items ) {
155
+ throw new Error( 'Array schema must have items property' );
156
+ }
157
+
158
+ const itemPropType = convertJsonSchemaToPropType( schema.items );
159
+
160
+ return {
161
+ kind: 'array',
162
+ key: key || 'array',
163
+ item_prop_type: itemPropType,
164
+ settings: {},
165
+ meta,
166
+ } as PropType;
167
+ }
168
+
169
+ function convertJsonSchemaToPropType( schema: JsonSchema7, key?: string ): PropType {
170
+ return jsonSchemaToPropType( schema, key );
171
+ }
172
+
173
+ /**
174
+ * Converts a complete JSON Schema object back to a PropsSchema
175
+ *
176
+ * @param jsonSchema The JSON Schema to convert
177
+ */
178
+ export function jsonSchemaToPropsSchema( jsonSchema: JsonSchema7 ): PropsSchema {
179
+ const propsSchema: PropsSchema = {};
180
+
181
+ if ( jsonSchema.type !== 'object' || ! jsonSchema.properties ) {
182
+ throw new Error( 'Root schema must be an object with properties' );
183
+ }
184
+
185
+ for ( const [ key, propSchema ] of Object.entries( jsonSchema.properties ) ) {
186
+ propsSchema[ key ] = convertJsonSchemaToPropType( propSchema, key );
187
+ }
188
+
189
+ return propsSchema;
190
+ }
@@ -0,0 +1,17 @@
1
+ export type JsonSchema7 = {
2
+ type?: string | string[];
3
+ properties?: Record< string, JsonSchema7 >;
4
+ items?: JsonSchema7;
5
+ enum?: unknown[];
6
+ anyOf?: JsonSchema7[];
7
+ oneOf?: JsonSchema7[];
8
+ allOf?: JsonSchema7[];
9
+ required?: string[];
10
+ description?: string;
11
+ default?: unknown;
12
+ if?: JsonSchema7;
13
+ then?: JsonSchema7;
14
+ else?: JsonSchema7;
15
+ key?: string;
16
+ [ key: string ]: unknown;
17
+ };
@@ -0,0 +1,172 @@
1
+ import { type PropsSchema, type PropType } from '../types';
2
+ import { type JsonSchema7 } from './prop-json-schema';
3
+
4
+ export function propTypeToJsonSchema( propType: PropType, key?: string ): JsonSchema7 {
5
+ const description = propType.meta?.description;
6
+
7
+ const schema: JsonSchema7 = {};
8
+
9
+ if ( description ) {
10
+ schema.description = description;
11
+ }
12
+
13
+ if ( key ) {
14
+ schema.key = key;
15
+ }
16
+
17
+ // Handle different kinds of prop types
18
+ switch ( propType.kind ) {
19
+ case 'plain':
20
+ return convertPlainPropType( propType, schema );
21
+ case 'union':
22
+ return convertUnionPropType( propType, schema );
23
+ case 'object':
24
+ return convertObjectPropType( propType, schema );
25
+ case 'array':
26
+ return convertArrayPropType( propType, schema );
27
+ default:
28
+ return convertPlainPropType( propType, schema );
29
+ }
30
+
31
+ return schema;
32
+ }
33
+
34
+ function convertPlainPropType( propType: PropType & { kind: 'plain' }, baseSchema: JsonSchema7 ): JsonSchema7 {
35
+ const schema = { ...baseSchema };
36
+ schema.key = propType.key;
37
+
38
+ // Determine type based on key
39
+ const key = propType.key.toLowerCase();
40
+
41
+ switch ( key ) {
42
+ case 'number':
43
+ schema.type = 'number';
44
+ break;
45
+ case 'boolean':
46
+ schema.type = 'boolean';
47
+ break;
48
+ default:
49
+ schema.type = 'string';
50
+ }
51
+
52
+ // Handle enum from settings
53
+ if ( Array.isArray( propType.settings?.enum ) ) {
54
+ schema.enum = propType.settings.enum;
55
+ }
56
+
57
+ return schema;
58
+ }
59
+
60
+ /**
61
+ * Converts a union prop type to JSON Schema ( электричество anyOf)
62
+ *
63
+ * @param propType The union prop type to convert
64
+ * @param baseSchema Base schema to extend
65
+ */
66
+ function convertUnionPropType( propType: PropType & { kind: 'union' }, baseSchema: JsonSchema7 ): JsonSchema7 {
67
+ const schema = structuredClone( baseSchema );
68
+
69
+ const propTypes = propType.prop_types || {};
70
+ const schemas: JsonSchema7[] = [];
71
+
72
+ // Convert each prop type in the union
73
+ for ( const [ typeKey, subPropType ] of Object.entries( propTypes ) ) {
74
+ const subSchema = convertPropTypeToJsonSchema( subPropType );
75
+
76
+ schemas.push( {
77
+ type: 'object',
78
+ required: [ '$$type', 'value' ],
79
+ properties: {
80
+ $$type: {
81
+ type: 'string',
82
+ const: typeKey,
83
+ description: `Discriminator for union type variant: ${ typeKey }`,
84
+ },
85
+ value: subSchema,
86
+ },
87
+ } );
88
+ }
89
+
90
+ if ( schemas.length > 0 ) {
91
+ schema.anyOf = schemas;
92
+ }
93
+
94
+ const propTypeDescription = propType.meta?.description;
95
+ if ( propTypeDescription ) {
96
+ schema.description = propTypeDescription;
97
+ }
98
+ return schema;
99
+ }
100
+
101
+ function convertObjectPropType( propType: PropType & { kind: 'object' }, baseSchema: JsonSchema7 ): JsonSchema7 {
102
+ const schema = structuredClone( baseSchema );
103
+
104
+ schema.type = 'object';
105
+ schema.properties = {};
106
+
107
+ const required: string[] = [];
108
+
109
+ const shape = propType.shape || {};
110
+
111
+ // Convert each property in the object shape
112
+ for ( const [ key, subPropType ] of Object.entries( shape ) ) {
113
+ const propSchema = convertPropTypeToJsonSchema( subPropType );
114
+
115
+ // Check if this property is required
116
+ if ( subPropType.settings?.required === true ) {
117
+ required.push( key );
118
+ }
119
+
120
+ schema.properties[ key ] = propSchema;
121
+ }
122
+
123
+ // Add required array if there are required fields
124
+ if ( required.length > 0 ) {
125
+ schema.required = required;
126
+ }
127
+
128
+ return schema;
129
+ }
130
+
131
+ function convertArrayPropType( propType: PropType & { kind: 'array' }, baseSchema: JsonSchema7 ): JsonSchema7 {
132
+ const schema = structuredClone( baseSchema );
133
+
134
+ schema.type = 'array';
135
+ schema.key = propType.key;
136
+
137
+ const itemPropType = propType.item_prop_type;
138
+
139
+ if ( itemPropType ) {
140
+ schema.items = convertPropTypeToJsonSchema( itemPropType );
141
+ }
142
+
143
+ return schema;
144
+ }
145
+
146
+ function convertPropTypeToJsonSchema( propType: PropType ): JsonSchema7 {
147
+ return propTypeToJsonSchema( propType );
148
+ }
149
+
150
+ export function propsSchemaToJsonSchema( schema: PropsSchema ): JsonSchema7 {
151
+ const jsonSchema: JsonSchema7 = {
152
+ type: 'object',
153
+ properties: {},
154
+ };
155
+
156
+ for ( const [ key, propType ] of Object.entries( schema ) ) {
157
+ // Skip internal properties
158
+ if ( key === '_cssid' || key === 'classes' || key === 'attributes' ) {
159
+ continue;
160
+ }
161
+
162
+ const propSchema = convertPropTypeToJsonSchema( propType );
163
+ if ( jsonSchema.properties ) {
164
+ jsonSchema.properties[ key ] = propSchema;
165
+ }
166
+
167
+ // Handle required fields at root level if needed
168
+ // (typically props are optional unless specified)
169
+ }
170
+
171
+ return jsonSchema;
172
+ }