@elementor/editor-props 3.35.0-340 → 3.35.0-341

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.
@@ -10,10 +10,13 @@ export function propTypeToJsonSchema( propType: PropType ): JsonSchema7 {
10
10
  schema.description = description;
11
11
  }
12
12
 
13
+ // Add example from initial_value if it exists
14
+ if ( propType.initial_value !== null && propType.initial_value !== undefined ) {
15
+ schema.examples = [ propType.initial_value ];
16
+ }
17
+
13
18
  // Handle different kinds of prop types
14
19
  switch ( propType.kind ) {
15
- case 'plain':
16
- return convertPlainPropType( propType, schema );
17
20
  case 'union':
18
21
  return convertUnionPropType( propType, schema );
19
22
  case 'object':
@@ -23,33 +26,50 @@ export function propTypeToJsonSchema( propType: PropType ): JsonSchema7 {
23
26
  default:
24
27
  return convertPlainPropType( propType, schema );
25
28
  }
26
-
27
- return schema;
28
29
  }
29
30
 
30
- function convertPlainPropType( propType: PropType & { kind: 'plain' }, baseSchema: JsonSchema7 ): JsonSchema7 {
31
+ function convertPlainPropType(
32
+ propType: PropType & { key: string; kind: string },
33
+ baseSchema: JsonSchema7
34
+ ): JsonSchema7 {
31
35
  const schema = { ...baseSchema };
32
36
 
33
- // Determine type based on key
34
- const key = propType.key.toLowerCase();
37
+ // This could happen when data is malformed due to a bug, added this as a safeguard.
38
+ if ( ! Object.hasOwn( propType, 'kind' ) ) {
39
+ throw new Error( `PropType kind is undefined for propType with key: ${ propType.key ?? '[unknown key]' }` );
40
+ }
41
+
42
+ const enumValues = ( propType.settings?.enum || [] ) as string[] | number[];
35
43
 
36
- switch ( key ) {
44
+ switch ( propType.kind ) {
45
+ case 'string':
37
46
  case 'number':
38
- schema.type = 'number';
39
- break;
40
47
  case 'boolean':
41
- schema.type = 'boolean';
42
- break;
48
+ return {
49
+ ...schema,
50
+ type: 'object',
51
+ properties: {
52
+ $$type: {
53
+ type: 'string',
54
+ const: propType.key ?? propType.kind,
55
+ },
56
+ value: {
57
+ type: propType.kind,
58
+ ...( enumValues.length > 0 ? { enum: enumValues } : {} ),
59
+ },
60
+ },
61
+ required: [ '$$type', 'value' ],
62
+ };
43
63
  default:
44
- schema.type = 'string';
45
- }
46
-
47
- // Handle enum from settings
48
- if ( Array.isArray( propType.settings?.enum ) ) {
49
- schema.enum = propType.settings.enum;
64
+ return {
65
+ ...schema,
66
+ type: 'object',
67
+ $$type: propType.kind,
68
+ value: {
69
+ type: propType.kind,
70
+ },
71
+ };
50
72
  }
51
-
52
- return schema;
53
73
  }
54
74
 
55
75
  /**
@@ -66,21 +86,11 @@ function convertUnionPropType( propType: PropType & { kind: 'union' }, baseSchem
66
86
 
67
87
  // Convert each prop type in the union
68
88
  for ( const [ typeKey, subPropType ] of Object.entries( propTypes ) ) {
89
+ if ( typeKey === 'dynamic' || typeKey === 'overridable' ) {
90
+ continue;
91
+ }
69
92
  const subSchema = convertPropTypeToJsonSchema( subPropType );
70
-
71
- schemas.push( {
72
- type: 'object',
73
- required: [ '$$type', 'value' ],
74
- properties: {
75
- $$type: {
76
- type: 'string',
77
- const: typeKey,
78
- description: subPropType.meta?.description,
79
- $comment: `Discriminator for union type variant: ${ typeKey }`,
80
- },
81
- value: subSchema,
82
- },
83
- } );
93
+ schemas.push( subSchema );
84
94
  }
85
95
 
86
96
  if ( schemas.length > 0 ) {
@@ -98,43 +108,77 @@ function convertObjectPropType( propType: PropType & { kind: 'object' }, baseSch
98
108
  const schema = structuredClone( baseSchema );
99
109
 
100
110
  schema.type = 'object';
101
- schema.properties = {};
111
+ const internalStructure: {
112
+ properties: {
113
+ $$type: JsonSchema7;
114
+ value: JsonSchema7;
115
+ };
116
+ } = {
117
+ properties: {
118
+ $$type: {
119
+ type: 'string',
120
+ const: propType.key,
121
+ },
122
+ value: {
123
+ type: 'object',
124
+ properties: {} as Record< string, JsonSchema7 >,
125
+ additionalProperties: false,
126
+ },
127
+ },
128
+ };
102
129
 
103
- const required: string[] = [];
130
+ const required: string[] = [ '$$type', 'value' ];
131
+ const valueRequired: string[] = [];
104
132
 
105
133
  const shape = propType.shape || {};
106
134
 
107
135
  // Convert each property in the object shape
108
136
  for ( const [ key, subPropType ] of Object.entries( shape ) ) {
109
- const propSchema = convertPropTypeToJsonSchema( subPropType );
137
+ const propSchema = propTypeToJsonSchema( subPropType );
110
138
 
111
139
  // Check if this property is required
112
140
  if ( subPropType.settings?.required === true ) {
113
- required.push( key );
141
+ valueRequired.push( key );
114
142
  }
115
143
 
116
- schema.properties[ key ] = propSchema;
144
+ if ( internalStructure.properties.value.properties ) {
145
+ internalStructure.properties.value.properties[ key ] = propSchema;
146
+ }
117
147
  }
118
148
 
119
- // Add required array if there are required fields
120
- if ( required.length > 0 ) {
121
- schema.required = required;
149
+ schema.required = required;
150
+ if ( valueRequired.length > 0 ) {
151
+ internalStructure.properties.value.required = valueRequired;
122
152
  }
123
153
 
124
- return schema;
154
+ return {
155
+ ...schema,
156
+ ...internalStructure,
157
+ };
125
158
  }
126
159
 
127
160
  function convertArrayPropType( propType: PropType & { kind: 'array' }, baseSchema: JsonSchema7 ): JsonSchema7 {
128
161
  const schema = structuredClone( baseSchema );
129
162
 
130
- schema.type = 'array';
163
+ schema.type = 'object';
164
+ let items: unknown;
131
165
 
132
166
  const itemPropType = propType.item_prop_type;
133
167
 
134
168
  if ( itemPropType ) {
135
- schema.items = convertPropTypeToJsonSchema( itemPropType );
169
+ items = convertPropTypeToJsonSchema( itemPropType );
136
170
  }
137
171
 
172
+ schema.properties = {
173
+ $$type: {
174
+ type: 'string',
175
+ const: propType.key,
176
+ },
177
+ value: {
178
+ type: 'array',
179
+ ...( items ? { items } : {} ),
180
+ } as JsonSchema7,
181
+ };
138
182
  return schema;
139
183
  }
140
184