@dotcms/uve 1.2.1 → 1.2.2

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.
package/index.cjs.js CHANGED
@@ -1,10 +1,627 @@
1
1
  'use strict';
2
2
 
3
3
  var _public = require('./public.cjs.js');
4
- require('@dotcms/types');
4
+ var types = require('@dotcms/types');
5
5
  require('@dotcms/types/internal');
6
6
 
7
+ /**
8
+ * Normalizes a field definition into the schema format expected by UVE.
9
+ *
10
+ * Converts developer-friendly field definitions into a normalized structure where
11
+ * all type-specific configuration properties are moved into a `config` object.
12
+ * This transformation ensures consistency in the schema format sent to UVE.
13
+ *
14
+ * **Field Type Handling:**
15
+ * - **Input fields**: Extracts `inputType` and `placeholder` into config
16
+ * - **Dropdown fields**: Normalizes options (strings become `{ label, value }` objects)
17
+ * - **Radio fields**: Normalizes options (preserves image properties like `imageURL`, `width`, `height`),
18
+ * extracts `columns` into config
19
+ * - **Checkbox group fields**: Normalizes options (converts to format expected by UVE)
20
+ *
21
+ * @experimental This method is experimental and may be subject to change.
22
+ *
23
+ * @param field - The field definition to normalize. Must be one of: input, dropdown, radio, or checkboxGroup
24
+ * @returns The normalized field schema with type, label, and config properties ready to be sent to UVE
25
+ *
26
+ * @example
27
+ * ```typescript
28
+ * // Input field normalization
29
+ * normalizeField({
30
+ * type: 'input',
31
+ * id: 'font-size',
32
+ * label: 'Font Size',
33
+ * inputType: 'number',
34
+ * placeholder: 'Enter size'
35
+ * })
36
+ * // Returns: {
37
+ * // type: 'input',
38
+ * // id: 'font-size',
39
+ * // label: 'Font Size',
40
+ * // config: { inputType: 'number', placeholder: 'Enter size' }
41
+ * // }
42
+ *
43
+ * // Dropdown field with string options normalization
44
+ * normalizeField({
45
+ * type: 'dropdown',
46
+ * label: 'Font Family',
47
+ * options: ['Arial', 'Helvetica']
48
+ * })
49
+ * // Returns: {
50
+ * // type: 'dropdown',
51
+ * // label: 'Font Family',
52
+ * // config: { options: [{ label: 'Arial', value: 'Arial' }, { label: 'Helvetica', value: 'Helvetica' }] }
53
+ * // }
54
+ * ```
55
+ */
56
+ function normalizeField(field) {
57
+ const base = {
58
+ type: field.type,
59
+ label: field.label,
60
+ id: field.id
61
+ };
62
+ const config = {};
63
+ // Handle type-specific properties
64
+ if (field.type === 'input') {
65
+ config.inputType = field.inputType;
66
+ config.placeholder = field.placeholder;
67
+ }
68
+ if (field.type === 'dropdown' || field.type === 'radio') {
69
+ // Normalize options to consistent format
70
+ config.options = field.options.map(opt => typeof opt === 'string' ? {
71
+ label: opt,
72
+ value: opt
73
+ } : opt);
74
+ // Handle radio-specific properties
75
+ if (field.type === 'radio') {
76
+ config.columns = field.columns;
77
+ }
78
+ }
79
+ if (field.type === 'checkboxGroup') {
80
+ // Normalize checkbox options - convert to format expected by UVE
81
+ // Options have label and key - convert key to 'value' for UVE format
82
+ config.options = field.options.map(opt => ({
83
+ label: opt.label,
84
+ value: opt.key // UVE expects 'value' to be the key identifier
85
+ }));
86
+ }
87
+ return {
88
+ ...base,
89
+ config
90
+ };
91
+ }
92
+ /**
93
+ * Normalizes a section definition into the schema format expected by UVE.
94
+ *
95
+ * Converts a section with a flat array of fields into the normalized schema format
96
+ * where fields are organized as a multi-dimensional array (array of column arrays).
97
+ * Currently, all sections are normalized to a single-column layout structure.
98
+ *
99
+ * **Normalization Process:**
100
+ * 1. Normalizes each field in the section using `normalizeField`
101
+ * 2. Wraps the normalized fields array in an outer array to create the column structure
102
+ * 3. Preserves the section title
103
+ *
104
+ * The output format always uses a multi-dimensional array structure (`fields: StyleEditorFieldSchema[][]`),
105
+ * even for single-column layouts, ensuring consistency in the UVE schema format.
106
+ *
107
+ * @experimental This method is experimental and may be subject to change.
108
+ *
109
+ * @param section - The section definition to normalize, containing a title and array of fields
110
+ * @param section.title - The section title displayed to users
111
+ * @param section.fields - Array of field definitions to normalize
112
+ * @returns The normalized section schema with fields organized as a single-column array structure
113
+ *
114
+ * @example
115
+ * ```typescript
116
+ * normalizeSection({
117
+ * title: 'Typography',
118
+ * fields: [
119
+ * { type: 'input', label: 'Font Size', inputType: 'number' },
120
+ * { type: 'dropdown', label: 'Font Family', options: ['Arial'] }
121
+ * ]
122
+ * })
123
+ * // Returns: {
124
+ * // title: 'Typography',
125
+ * // fields: [
126
+ * // [
127
+ * // { type: 'input', label: 'Font Size', config: { inputType: 'number' } },
128
+ * // { type: 'dropdown', label: 'Font Family', config: { options: [...] } }
129
+ * // ]
130
+ * // ]
131
+ * // }
132
+ * ```
133
+ */
134
+ function normalizeSection(section) {
135
+ // Determine if fields is multi-column or single column
136
+ const normalizedFields = section.fields.map(normalizeField);
137
+ return {
138
+ title: section.title,
139
+ fields: normalizedFields
140
+ };
141
+ }
142
+ /**
143
+ * Normalizes a complete form definition into the schema format expected by UVE.
144
+ *
145
+ * This is the main entry point for converting a developer-friendly form definition
146
+ * into the normalized schema structure that UVE (Universal Visual Editor) can consume.
147
+ * The normalization process transforms the entire form hierarchy:
148
+ *
149
+ * **Normalization Process:**
150
+ * 1. Preserves the `contentType` identifier
151
+ * 2. Processes each section using `normalizeSection`, which:
152
+ * - Normalizes all fields in the section using `normalizeField`
153
+ * - Organizes fields into the required multi-dimensional array structure
154
+ * 3. Returns a fully normalized schema with consistent structure across all sections
155
+ *
156
+ * The resulting schema has all field-specific properties moved into `config` objects
157
+ * and all sections using the consistent single-column array structure, regardless
158
+ * of the input format.
159
+ *
160
+ * @experimental This method is experimental and may be subject to change.
161
+ *
162
+ * @param form - The complete form definition to normalize
163
+ * @param form.contentType - The content type identifier this form is associated with
164
+ * @param form.sections - Array of section definitions, each containing a title and fields
165
+ * @returns The normalized form schema ready to be sent to UVE, with all fields and sections normalized
166
+ *
167
+ * @example
168
+ * ```typescript
169
+ * const schema = normalizeForm({
170
+ * contentType: 'my-content-type',
171
+ * sections: [
172
+ * {
173
+ * title: 'Typography',
174
+ * fields: [
175
+ * { type: 'input', id: 'font-size', label: 'Font Size', inputType: 'number' },
176
+ * { type: 'dropdown', id: 'font-family', label: 'Font Family', options: ['Arial', 'Helvetica'] }
177
+ * ]
178
+ * },
179
+ * {
180
+ * title: 'Colors',
181
+ * fields: [
182
+ * { type: 'input', id: 'primary-color', label: 'Primary Color', inputType: 'text' }
183
+ * ]
184
+ * }
185
+ * ]
186
+ * });
187
+ * // Returns: {
188
+ * // contentType: 'my-content-type',
189
+ * // sections: [
190
+ * // {
191
+ * // title: 'Typography',
192
+ * // fields: [
193
+ * // [
194
+ * // { type: 'input', id: 'font-size', label: 'Font Size', config: { inputType: 'number' } },
195
+ * // { type: 'dropdown', id: 'font-family', label: 'Font Family', config: { options: [...] } }
196
+ * // ]
197
+ * // ]
198
+ * // },
199
+ * // {
200
+ * // title: 'Colors',
201
+ * // fields: [
202
+ * // [
203
+ * // { type: 'input', id: 'primary-color', label: 'Primary Color', config: { inputType: 'text' } }
204
+ * // ]
205
+ * // ]
206
+ * // }
207
+ * // ]
208
+ * // }
209
+ * ```
210
+ */
211
+ function normalizeForm(form) {
212
+ return {
213
+ contentType: form.contentType,
214
+ sections: form.sections.map(normalizeSection)
215
+ };
216
+ }
7
217
 
218
+ /**
219
+ * Helper functions for creating style editor field definitions.
220
+ *
221
+ * Provides type-safe factory functions for creating different types of form fields
222
+ * used in the style editor. Each function creates a field definition with the
223
+ * appropriate `type` property automatically set, eliminating the need to manually
224
+ * specify the type discriminator.
225
+ *
226
+ * **Available Field Types:**
227
+ * - `input`: Text or number input fields
228
+ * - `dropdown`: Single-value selection from a dropdown list
229
+ * - `radio`: Single-value selection from radio button options (supports visual options with images)
230
+ * - `checkboxGroup`: Multiple-value selection from checkbox options
231
+ *
232
+ * These factory functions ensure type safety by inferring the correct field type
233
+ * based on the configuration provided, and they automatically set the `type` property
234
+ * to match the factory function used.
235
+ *
236
+ * @experimental This API is experimental and may be subject to change.
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * const form = defineStyleEditorSchema({
241
+ * contentType: 'my-content-type',
242
+ * sections: [
243
+ * {
244
+ * title: 'Typography',
245
+ * fields: [
246
+ * styleEditorField.input({
247
+ * id: 'font-size',
248
+ * label: 'Font Size',
249
+ * inputType: 'number'
250
+ * }),
251
+ * styleEditorField.dropdown({
252
+ * id: 'font-family',
253
+ * label: 'Font Family',
254
+ * options: ['Arial', 'Helvetica']
255
+ * }),
256
+ * styleEditorField.radio({
257
+ * id: 'alignment',
258
+ * label: 'Alignment',
259
+ * options: ['Left', 'Center', 'Right']
260
+ * })
261
+ * ]
262
+ * }
263
+ * ]
264
+ * });
265
+ * ```
266
+ */
267
+ const styleEditorField = {
268
+ /**
269
+ * Creates an input field definition.
270
+ *
271
+ * Supports both text and number input types for different value types.
272
+ *
273
+ * @experimental This method is experimental and may be subject to change.
274
+ *
275
+ * @typeParam T - The input type ('text' or 'number'), inferred from `config.inputType`
276
+ * @param config - Input field configuration
277
+ * @param config.id - The unique identifier for this field
278
+ * @param config.label - The label displayed for this input field
279
+ * @param config.inputType - The type of input ('text' or 'number')
280
+ * @param config.placeholder - Optional placeholder text for the input
281
+ * @returns A complete input field definition with type 'input'
282
+ *
283
+ * @example
284
+ * ```typescript
285
+ * // Number input
286
+ * styleEditorField.input({
287
+ * id: 'font-size',
288
+ * label: 'Font Size',
289
+ * inputType: 'number',
290
+ * placeholder: 'Enter font size'
291
+ * })
292
+ *
293
+ * // Text input
294
+ * styleEditorField.input({
295
+ * id: 'font-name',
296
+ * label: 'Font Name',
297
+ * inputType: 'text',
298
+ * placeholder: 'Enter font name'
299
+ * })
300
+ * ```
301
+ */
302
+ input: config => ({
303
+ type: 'input',
304
+ ...config
305
+ }),
306
+ /**
307
+ * Creates a dropdown field definition.
308
+ *
309
+ * Allows users to select a single value from a list of options.
310
+ * Options can be provided as simple strings or as objects with label and value.
311
+ *
312
+ * **Best Practice:** Use `as const` when defining options for better type safety:
313
+ * ```typescript
314
+ * const OPTIONS = [
315
+ * { label: '18', value: '18px' },
316
+ * { label: '24', value: '24px' }
317
+ * ] as const;
318
+ *
319
+ * styleEditorField.dropdown({
320
+ * id: 'size',
321
+ * label: 'Size',
322
+ * options: OPTIONS
323
+ * });
324
+ * ```
325
+ *
326
+ * @experimental This method is experimental and may be subject to change.
327
+ *
328
+ * @param config - Dropdown field configuration (without the 'type' property)
329
+ * @param config.id - The unique identifier for this field
330
+ * @param config.label - The label displayed for this dropdown field
331
+ * @param config.options - Array of options. Can be strings or objects with label and value. Use `as const` for best type safety.
332
+ * @returns A complete dropdown field definition with type 'dropdown'
333
+ *
334
+ * @example
335
+ * ```typescript
336
+ * // Simple string options
337
+ * styleEditorField.dropdown({
338
+ * id: 'font-family',
339
+ * label: 'Font Family',
340
+ * options: ['Arial', 'Helvetica', 'Times New Roman']
341
+ * })
342
+ *
343
+ * // Object options with custom labels (recommended: use 'as const')
344
+ * const OPTIONS = [
345
+ * { label: 'Light Theme', value: 'light' },
346
+ * { label: 'Dark Theme', value: 'dark' }
347
+ * ] as const;
348
+ *
349
+ * styleEditorField.dropdown({
350
+ * id: 'theme',
351
+ * label: 'Theme',
352
+ * options: OPTIONS
353
+ * })
354
+ * ```
355
+ */
356
+ dropdown: config => ({
357
+ type: 'dropdown',
358
+ ...config,
359
+ options: config.options
360
+ }),
361
+ /**
362
+ * Creates a radio button field definition.
363
+ *
364
+ * Allows users to select a single option from a list. Supports visual
365
+ * options with background images for enhanced UI. Options can be provided
366
+ * as simple strings or as objects with label, value, and optional image properties.
367
+ *
368
+ * **Layout Options:**
369
+ * - `columns: 1` (default): Single column list layout
370
+ * - `columns: 2`: Two-column grid layout, ideal for visual options with images
371
+ *
372
+ * **Best Practice:** Use `as const` when defining options for better type safety:
373
+ * ```typescript
374
+ * const RADIO_OPTIONS = [
375
+ * { label: 'Left', value: 'left' },
376
+ * { label: 'Right', value: 'right' }
377
+ * ] as const;
378
+ *
379
+ * styleEditorField.radio({
380
+ * id: 'layout',
381
+ * label: 'Layout',
382
+ * options: RADIO_OPTIONS
383
+ * });
384
+ * ```
385
+ *
386
+ * @experimental This method is experimental and may be subject to change.
387
+ *
388
+ * @param config - Radio field configuration (without the 'type' property)
389
+ * @param config.id - The unique identifier for this field
390
+ * @param config.label - The label displayed for this radio group
391
+ * @param config.options - Array of options. Can be strings or objects with label, value, and optional image properties. Use `as const` for best type safety.
392
+ * @param config.columns - Optional number of columns (1 or 2). Defaults to 1 (single column)
393
+ * @returns A complete radio field definition with type 'radio'
394
+ *
395
+ * @example
396
+ * ```typescript
397
+ * // Simple string options (single column)
398
+ * styleEditorField.radio({
399
+ * id: 'alignment',
400
+ * label: 'Alignment',
401
+ * options: ['Left', 'Center', 'Right']
402
+ * })
403
+ *
404
+ * // Two-column grid layout with images (recommended: use 'as const')
405
+ * const LAYOUT_OPTIONS = [
406
+ * {
407
+ * label: 'Left',
408
+ * value: 'left',
409
+ * imageURL: 'https://example.com/layout-left.png',
410
+ * },
411
+ * {
412
+ * label: 'Right',
413
+ * value: 'right',
414
+ * imageURL: 'https://example.com/layout-right.png',
415
+ * },
416
+ * { label: 'Center', value: 'center' },
417
+ * { label: 'Overlap', value: 'overlap' }
418
+ * ] as const;
419
+ *
420
+ * styleEditorField.radio({
421
+ * id: 'layout',
422
+ * label: 'Layout',
423
+ * columns: 2,
424
+ * options: LAYOUT_OPTIONS
425
+ * })
426
+ * ```
427
+ */
428
+ radio: config => ({
429
+ type: 'radio',
430
+ ...config,
431
+ options: config.options
432
+ }),
433
+ /**
434
+ * Creates a checkbox group field definition.
435
+ *
436
+ * Allows users to select multiple options simultaneously. Each option
437
+ * can be independently checked or unchecked. The default checked state
438
+ * is defined directly in each option's `value` property (boolean).
439
+ *
440
+ * **Key Differences from Other Field Types:**
441
+ * - Uses `key` instead of `value` for the identifier (to avoid confusion)
442
+ * - Checked state is managed by the form system, not stored in the option definition
443
+ *
444
+ * **Why `key` instead of `value`?**
445
+ * In dropdown and radio fields, `value` represents the actual selected value (string).
446
+ * In checkbox groups, the actual value is boolean (checked/unchecked), so we use
447
+ * `key` for the identifier to avoid confusion. This makes it clear that `value`
448
+ * is the boolean state, not just an identifier.
449
+ *
450
+ * @experimental This method is experimental and may be subject to change.
451
+ *
452
+ * @param config - Checkbox group field configuration (without the 'type' property)
453
+ * @param config.id - The unique identifier for this field
454
+ * @param config.label - The label displayed for this checkbox group
455
+ * @param config.options - Array of checkbox options with label and key
456
+ * @returns A complete checkbox group field definition with type 'checkboxGroup'
457
+ *
458
+ * @example
459
+ * ```typescript
460
+ * styleEditorField.checkboxGroup({
461
+ * id: 'text-decoration',
462
+ * label: 'Text Decoration',
463
+ * options: [
464
+ * { label: 'Underline', key: 'underline' },
465
+ * { label: 'Overline', key: 'overline' },
466
+ * { label: 'Line Through', key: 'line-through' }
467
+ * ]
468
+ * })
469
+ *
470
+ * // Example with type settings
471
+ * styleEditorField.checkboxGroup({
472
+ * id: 'type-settings',
473
+ * label: 'Type settings',
474
+ * options: [
475
+ * { label: 'Bold', key: 'bold' },
476
+ * { label: 'Italic', key: 'italic' },
477
+ * { label: 'Underline', key: 'underline' },
478
+ * { label: 'Strikethrough', key: 'strikethrough' }
479
+ * ]
480
+ * })
481
+ * ```
482
+ */
483
+ checkboxGroup: config => ({
484
+ type: 'checkboxGroup',
485
+ ...config
486
+ })
487
+ };
488
+ /**
489
+ * Normalizes and validates a style editor form definition.
490
+ *
491
+ * Converts the developer-friendly form structure into the schema format
492
+ * expected by UVE (Universal Visual Editor). This function processes the
493
+ * form definition and transforms it into the normalized schema format where:
494
+ *
495
+ * - All field-specific properties are moved into `config` objects
496
+ * - String options are normalized to `{ label, value }` objects
497
+ * - Sections are organized into the multi-dimensional array structure required by UVE
498
+ *
499
+ * The normalization process ensures consistency and type safety in the schema
500
+ * format sent to UVE. After normalization, use `registerStyleEditorSchemas`
501
+ * to register the schema with the UVE editor.
502
+ *
503
+ * @experimental This method is experimental and may be subject to change.
504
+ *
505
+ * @param form - The style editor form definition containing contentType and sections
506
+ * @param form.contentType - The content type identifier for this form
507
+ * @param form.sections - Array of sections, each containing a title and fields array
508
+ * @returns The normalized form schema ready to be sent to UVE
509
+ *
510
+ * @example
511
+ * ```typescript
512
+ * const formSchema = defineStyleEditorSchema({
513
+ * contentType: 'my-content-type',
514
+ * sections: [
515
+ * {
516
+ * title: 'Typography',
517
+ * fields: [
518
+ * styleEditorField.input({
519
+ * id: 'font-size',
520
+ * label: 'Font Size',
521
+ * inputType: 'number'
522
+ * }),
523
+ * styleEditorField.dropdown({
524
+ * id: 'font-family',
525
+ * label: 'Font Family',
526
+ * options: ['Arial', 'Helvetica']
527
+ * })
528
+ * ]
529
+ * },
530
+ * {
531
+ * title: 'Colors',
532
+ * fields: [
533
+ * styleEditorField.input({
534
+ * id: 'primary-color',
535
+ * label: 'Primary Color',
536
+ * inputType: 'text'
537
+ * }),
538
+ * styleEditorField.input({
539
+ * id: 'secondary-color',
540
+ * label: 'Secondary Color',
541
+ * inputType: 'text'
542
+ * })
543
+ * ]
544
+ * }
545
+ * ]
546
+ * });
547
+ *
548
+ * // Register the schema with UVE
549
+ * registerStyleEditorSchemas([formSchema]);
550
+ * ```
551
+ */
552
+ function defineStyleEditorSchema(form) {
553
+ return normalizeForm(form);
554
+ }
555
+ /**
556
+ * Registers style editor form schemas with the UVE editor.
557
+ *
558
+ * Sends normalized style editor schemas to the UVE (Universal Visual Editor)
559
+ * for registration. The schemas must be normalized using `defineStyleEditorSchema`
560
+ * before being passed to this function.
561
+ *
562
+ * **Behavior:**
563
+ * - Only registers schemas when UVE is in EDIT mode
564
+ * - Validates that each schema has a `contentType` property
565
+ * - Skips schemas without `contentType` and logs a warning
566
+ * - Sends the validated schemas to UVE via the `REGISTER_STYLE_SCHEMAS` action
567
+ *
568
+ * **Note:** This function will silently return early if UVE is not in EDIT mode,
569
+ * so it's safe to call even when the editor is not active.
570
+ *
571
+ * @experimental This method is experimental and may be subject to change.
572
+ *
573
+ * @param schemas - Array of normalized style editor form schemas to register with UVE
574
+ * @returns void - This function does not return a value
575
+ *
576
+ * @example
577
+ * ```typescript
578
+ * // Create and normalize a form schema
579
+ * const formSchema = defineStyleEditorSchema({
580
+ * contentType: 'my-content-type',
581
+ * sections: [
582
+ * {
583
+ * title: 'Typography',
584
+ * fields: [
585
+ * styleEditorField.input({
586
+ * id: 'font-size',
587
+ * label: 'Font Size',
588
+ * inputType: 'number'
589
+ * })
590
+ * ]
591
+ * }
592
+ * ]
593
+ * });
594
+ *
595
+ * // Register the schema with UVE
596
+ * registerStyleEditorSchemas([formSchema]);
597
+ *
598
+ * // Register multiple schemas at once
599
+ * const schema1 = defineStyleEditorSchema({ ... });
600
+ * const schema2 = defineStyleEditorSchema({ ... });
601
+ * registerStyleEditorSchemas([schema1, schema2]);
602
+ * ```
603
+ */
604
+ function registerStyleEditorSchemas(schemas) {
605
+ const {
606
+ mode
607
+ } = _public.getUVEState() || {};
608
+ if (!mode || mode !== types.UVE_MODE.EDIT) {
609
+ return;
610
+ }
611
+ const validatedSchemas = schemas.filter((schema, index) => {
612
+ if (!schema.contentType) {
613
+ console.warn(`[registerStyleEditorSchemas] Skipping schema with index [${index}] for not having a contentType`);
614
+ return false;
615
+ }
616
+ return true;
617
+ });
618
+ _public.sendMessageToUVE({
619
+ action: types.DotCMSUVEAction.REGISTER_STYLE_SCHEMAS,
620
+ payload: {
621
+ schemas: validatedSchemas
622
+ }
623
+ });
624
+ }
8
625
 
9
626
  exports.createUVESubscription = _public.createUVESubscription;
10
627
  exports.editContentlet = _public.editContentlet;
@@ -12,7 +629,9 @@ exports.enableBlockEditorInline = _public.enableBlockEditorInline;
12
629
  exports.getUVEState = _public.getUVEState;
13
630
  exports.initInlineEditing = _public.initInlineEditing;
14
631
  exports.initUVE = _public.initUVE;
15
- exports.isAnalyticsActive = _public.isAnalyticsActive;
16
632
  exports.reorderMenu = _public.reorderMenu;
17
633
  exports.sendMessageToUVE = _public.sendMessageToUVE;
18
634
  exports.updateNavigation = _public.updateNavigation;
635
+ exports.defineStyleEditorSchema = defineStyleEditorSchema;
636
+ exports.registerStyleEditorSchemas = registerStyleEditorSchemas;
637
+ exports.styleEditorField = styleEditorField;