Skip to content

Commit 2433172

Browse files
committed
Refactor field type definitions and usage
1 parent d359286 commit 2433172

6 files changed

Lines changed: 72 additions & 50 deletions

File tree

src/lib/components/contents/details/editor/field-editor.svelte

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* NumberField,
4040
* StringField,
4141
* TextField,
42+
* VisibleField,
4243
* } from '$lib/types/public';
4344
*/
4445
@@ -89,16 +90,15 @@
8990
);
9091
9192
const inEditorComponent = $derived(widgetContext === 'markdown-editor-component');
93+
const { name: fieldName, widget: widgetName = 'string', i18n = false } = $derived(fieldConfig);
9294
const {
93-
name: fieldName,
9495
label = '',
9596
comment = '',
9697
hint = '',
97-
widget: widgetName = 'string',
98-
i18n = false,
98+
// @ts-ignore Some field types don’t have `pattern` property
9999
pattern = /** @type {string[]} */ ([]),
100100
readonly: readonlyOption = false,
101-
} = $derived(fieldConfig);
101+
} = $derived(/** @type {VisibleField} */ (fieldConfig));
102102
const required = $derived(isFieldRequired({ fieldConfig, locale }));
103103
const { hasSubFields } = $derived(
104104
widgetName === 'list'

src/lib/components/contents/details/preview/field-preview.svelte

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
1010
/**
1111
* @import { InternalLocaleCode, TypedFieldKeyPath } from '$lib/types/private';
12-
* @import { Field, FieldKeyPath } from '$lib/types/public';
12+
* @import { Field, FieldKeyPath, VisibleField } from '$lib/types/public';
1313
*/
1414
1515
/**
@@ -30,13 +30,8 @@
3030
/* eslint-enable prefer-const */
3131
} = $props();
3232
33-
const {
34-
name: fieldName,
35-
label = '',
36-
widget: widgetName = 'string',
37-
preview = true,
38-
i18n = false,
39-
} = $derived(fieldConfig);
33+
const { name: fieldName, widget: widgetName = 'string', i18n = false } = $derived(fieldConfig);
34+
const { label = '', preview = true } = $derived(/** @type {VisibleField} */ (fieldConfig));
4035
const multiple = $derived(isFieldMultiple(fieldConfig));
4136
const isList = $derived(widgetName === 'list' || multiple);
4237
const isIndexFile = $derived($entryDraft?.isIndexFile ?? false);

src/lib/services/contents/collection/view/sort-keys.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,14 @@ export const getSortKeyLabel = ({ collection, key }) => {
219219

220220
const keyPath = arr.slice(0, index + 1).join('.');
221221

222+
// @ts-ignore Hidden field doesn't have `label` property
222223
return getField({ collectionName: collection.name, keyPath })?.label || _key;
223224
})
224225
.filter(Boolean)
225226
.join(' – ');
226227
}
227228

229+
// @ts-ignore Hidden field doesn't have `label` property
228230
return collection.fields?.find(({ name }) => name === key)?.label || key;
229231
};
230232

src/lib/services/contents/draft/validate.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ export const validateAnyField = (args) => {
107107
return undefined;
108108
}
109109

110+
// @ts-ignore Some field types don’t have `pattern` property
110111
const { widget: widgetName = 'string', i18n = false, pattern: validation } = fieldConfig;
111112
const multiple = isFieldMultiple(fieldConfig);
112113

src/lib/services/contents/entry/fields.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ export const getField = (args) => {
264264
* @param {InternalLocaleCode} args.locale Current pane’s locale.
265265
* @returns {boolean} Result.
266266
*/
267+
// @ts-ignore Hidden field doesn’t have `required` property
267268
export const isFieldRequired = ({ fieldConfig: { required = true }, locale }) =>
268269
Array.isArray(required) ? required.includes(locale) : !!required;
269270

src/lib/types/public.js

Lines changed: 61 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@
196196
* Common field properties that are shared among all field types.
197197
* @typedef {object} CommonFieldProps
198198
* @property {string} name Unique identifier for the field. It cannot include periods and spaces.
199+
*/
200+
201+
/**
202+
* Standard field properties for a built-in widget.
203+
* @typedef {object} StandardFieldProps
199204
* @property {string} [label] Label of the field to be displayed in the editor UI. Default: `name`
200205
* field value.
201206
* @property {string} [comment] Short description of the field to be displayed in the editor UI.
@@ -207,22 +212,27 @@
207212
* non-default like `false` but automatically copies the default locale’s value to other locales.
208213
* `translate` and `none` are aliases of `true` and `false`, respectively. This option only works
209214
* when i18n is set up with the global and collection-level `i18n` option.
210-
* @see https://decapcms.org/docs/configuration-options/#fields
211-
* @see https://decapcms.org/docs/widgets/
212-
*/
213-
214-
/**
215-
* Standard field properties for a built-in widget.
216-
* @typedef {object} StandardFieldProps
217215
* @property {boolean | LocaleCode[]} [required] Whether to make data input on the field required.
218216
* Default: `true`. This option also affects data output if the `omit_empty_optional_fields` global
219217
* output option is `true`. If i18n is enabled and the field doesn’t require input in all locales,
220218
* required locale codes can be passed as an array like `[en, fr]` instead of a boolean.
221219
* @property {boolean} [readonly] Whether to make the field read-only. Default: `false`. This is
222220
* useful when a `default` value is provided and the field should not be editable by users.
221+
* @see https://decapcms.org/docs/configuration-options/#fields
222+
* @see https://decapcms.org/docs/widgets/
223+
*/
224+
225+
/**
226+
* Properties for a field that is visible in the editor UI.
227+
* @typedef {CommonFieldProps & StandardFieldProps} VisibleFieldProps
228+
*/
229+
230+
/**
231+
* Field validation properties.
232+
* @typedef {object} FieldValidationProps
223233
* @property {[string | RegExp, string]} [pattern] Validation format. The first argument is a
224234
* regular expression matching pattern for a valid input value, and the second argument is an error
225-
* message. This option has no effect on a List or Object field with subfields.
235+
* message to be displayed when the input value does not match the pattern.
226236
*/
227237

228238
/**
@@ -331,8 +341,7 @@
331341

332342
/**
333343
* Boolean field definition.
334-
* @typedef {CommonFieldProps & StandardFieldProps & BooleanFieldProps & AdjacentLabelProps
335-
* } BooleanField
344+
* @typedef {VisibleFieldProps & BooleanFieldProps & AdjacentLabelProps} BooleanField
336345
*/
337346

338347
/**
@@ -354,7 +363,7 @@
354363

355364
/**
356365
* Code field definition.
357-
* @typedef {CommonFieldProps & StandardFieldProps & CodeFieldProps} CodeField
366+
* @typedef {VisibleFieldProps & FieldValidationProps & CodeFieldProps} CodeField
358367
*/
359368

360369
/**
@@ -371,7 +380,7 @@
371380

372381
/**
373382
* Color field definition.
374-
* @typedef {CommonFieldProps & StandardFieldProps & ColorFieldProps} ColorField
383+
* @typedef {VisibleFieldProps & FieldValidationProps & ColorFieldProps} ColorField
375384
*/
376385

377386
/**
@@ -384,7 +393,7 @@
384393

385394
/**
386395
* Compute field definition.
387-
* @typedef {CommonFieldProps & StandardFieldProps & ComputeFieldProps} ComputeField
396+
* @typedef {VisibleFieldProps & ComputeFieldProps} ComputeField
388397
*/
389398

390399
/**
@@ -409,7 +418,7 @@
409418

410419
/**
411420
* DateTime field definition.
412-
* @typedef {CommonFieldProps & StandardFieldProps & DateTimeFieldProps} DateTimeField
421+
* @typedef {VisibleFieldProps & FieldValidationProps & DateTimeFieldProps} DateTimeField
413422
*/
414423

415424
/**
@@ -421,7 +430,7 @@
421430

422431
/**
423432
* File field definition.
424-
* @typedef {CommonFieldProps & StandardFieldProps & MediaFieldProps & FileFieldProps} FileField
433+
* @typedef {VisibleFieldProps & FieldValidationProps & MediaFieldProps & FileFieldProps } FileField
425434
*/
426435

427436
/**
@@ -430,12 +439,17 @@
430439
* @property {'hidden'} widget Widget name.
431440
* @property {any} [default] Default value. Accepts any data type that can be stored with the
432441
* configured file format.
442+
* @property {false | 'duplicate'} [i18n] Whether to enable the field in locales other than the
443+
* default. Unlike other visible fields, hidden fields can be configured to be enabled in
444+
* non-default locales only when set to `duplicate`. In that case, the default locale’s value is
445+
* automatically copied to other locales. Default: `false`. This option only works when i18n is set
446+
* up with the global and collection-level `i18n` option.
433447
* @see https://decapcms.org/docs/widgets/#Hidden
434448
*/
435449

436450
/**
437451
* Hidden field definition.
438-
* @typedef {CommonFieldProps & StandardFieldProps & HiddenFieldProps} HiddenField
452+
* @typedef {CommonFieldProps & HiddenFieldProps} HiddenField
439453
*/
440454

441455
/**
@@ -447,7 +461,8 @@
447461

448462
/**
449463
* Image field definition.
450-
* @typedef {CommonFieldProps & StandardFieldProps & MediaFieldProps & ImageFieldProps} ImageField
464+
* @typedef {VisibleFieldProps & FieldValidationProps & MediaFieldProps & ImageFieldProps
465+
* } ImageField
451466
*/
452467

453468
/**
@@ -462,8 +477,7 @@
462477

463478
/**
464479
* KeyValue field definition.
465-
* @typedef {CommonFieldProps & StandardFieldProps & KeyValueFieldProps & MultiValueFieldProps
466-
* } KeyValueField
480+
* @typedef {VisibleFieldProps & KeyValueFieldProps & MultiValueFieldProps} KeyValueField
467481
*/
468482

469483
/**
@@ -476,6 +490,16 @@
476490
* @see https://decapcms.org/docs/widgets/#List
477491
*/
478492

493+
/**
494+
* Base properties for a List field.
495+
* @typedef {VisibleFieldProps & ListFieldProps & MultiValueFieldProps} ListFieldBaseProps
496+
*/
497+
498+
/**
499+
* Simple List field definition with primitive item types.
500+
* @typedef {ListFieldBaseProps & FieldValidationProps} SimpleListField
501+
*/
502+
479503
/**
480504
* Base properties for a complex List field with subfields or variable types.
481505
* @typedef {object} ComplexListFieldBaseProps
@@ -506,15 +530,9 @@
506530
* details.
507531
*/
508532

509-
/**
510-
* Simple List field definition with primitive item types.
511-
* @typedef {CommonFieldProps & StandardFieldProps & ListFieldProps & MultiValueFieldProps
512-
* } SimpleListField
513-
*/
514-
515533
/**
516534
* Properties for a complex List field with subfields or variable types.
517-
* @typedef {SimpleListField & ComplexListFieldBaseProps} ComplexListFieldProps
535+
* @typedef {ListFieldBaseProps & ComplexListFieldBaseProps} ComplexListFieldProps
518536
*/
519537

520538
/**
@@ -568,7 +586,7 @@
568586

569587
/**
570588
* Map field definition.
571-
* @typedef {CommonFieldProps & StandardFieldProps & MapFieldProps} MapField
589+
* @typedef {VisibleFieldProps & FieldValidationProps & MapFieldProps} MapField
572590
*/
573591

574592
/**
@@ -618,7 +636,7 @@
618636

619637
/**
620638
* Markdown field definition.
621-
* @typedef {CommonFieldProps & StandardFieldProps & MarkdownFieldProps} MarkdownField
639+
* @typedef {VisibleFieldProps & FieldValidationProps & MarkdownFieldProps} MarkdownField
622640
*/
623641

624642
/**
@@ -635,7 +653,7 @@
635653

636654
/**
637655
* Number field definition.
638-
* @typedef {CommonFieldProps & StandardFieldProps & NumberFieldProps & AdjacentLabelProps
656+
* @typedef {VisibleFieldProps & FieldValidationProps & NumberFieldProps & AdjacentLabelProps
639657
* } NumberField
640658
*/
641659

@@ -653,7 +671,7 @@
653671

654672
/**
655673
* Base properties for a complex Object field with subfields or variable types.
656-
* @typedef {CommonFieldProps & StandardFieldProps & ObjectFieldProps} ComplexObjectFieldProps
674+
* @typedef {VisibleFieldProps & ObjectFieldProps} ComplexObjectFieldProps
657675
*/
658676

659677
/**
@@ -708,7 +726,7 @@
708726

709727
/**
710728
* Relation field definition.
711-
* @typedef {CommonFieldProps & StandardFieldProps & RelationFieldProps & MultiOptionFieldProps
729+
* @typedef {VisibleFieldProps & FieldValidationProps & RelationFieldProps & MultiOptionFieldProps
712730
* } RelationField
713731
*/
714732

@@ -729,7 +747,7 @@
729747

730748
/**
731749
* Select field definition.
732-
* @typedef {CommonFieldProps & StandardFieldProps & SelectFieldProps & MultiOptionFieldProps
750+
* @typedef {VisibleFieldProps & FieldValidationProps & SelectFieldProps & MultiOptionFieldProps
733751
* } SelectField
734752
*/
735753

@@ -747,7 +765,7 @@
747765

748766
/**
749767
* String field definition.
750-
* @typedef {CommonFieldProps & StandardFieldProps & StringFieldProps & AdjacentLabelProps &
768+
* @typedef {VisibleFieldProps & FieldValidationProps & StringFieldProps & AdjacentLabelProps &
751769
* CharCountProps} StringField
752770
*/
753771

@@ -761,7 +779,7 @@
761779

762780
/**
763781
* Text field definition.
764-
* @typedef {CommonFieldProps & StandardFieldProps & TextFieldProps & CharCountProps} TextField
782+
* @typedef {VisibleFieldProps & FieldValidationProps & TextFieldProps & CharCountProps } TextField
765783
*/
766784

767785
/**
@@ -779,14 +797,19 @@
779797

780798
/**
781799
* UUID field definition.
782-
* @typedef {CommonFieldProps & StandardFieldProps & UuidFieldProps} UuidField
800+
* @typedef {VisibleFieldProps & UuidFieldProps} UuidField
783801
*/
784802

785803
/**
786-
* Entry field using a built-in widget.
804+
* Visible field types.
787805
* @typedef {BooleanField | CodeField | ColorField | ComputeField | DateTimeField | FileField |
788-
* HiddenField | ImageField | KeyValueField | ListField | MapField | MarkdownField | NumberField |
789-
* ObjectField | RelationField | SelectField | StringField | TextField | UuidField} StandardField
806+
* ImageField | KeyValueField | ListField | MapField | MarkdownField | NumberField | ObjectField |
807+
* RelationField | SelectField | StringField | TextField | UuidField} VisibleField
808+
*/
809+
810+
/**
811+
* Entry field using a built-in widget.
812+
* @typedef {VisibleField | HiddenField} StandardField
790813
* @see https://decapcms.org/docs/widgets/
791814
*/
792815

0 commit comments

Comments
 (0)