contentful-management
Version:
Client for Contentful's Content Management API
163 lines (160 loc) • 5.01 kB
JavaScript
import { in_ } from './types.js';
const DROPDOWN_TYPES = ['Text', 'Symbol', 'Integer', 'Number', 'Boolean'];
const INTERNAL_TO_API = {
Symbol: { type: 'Symbol' },
Text: { type: 'Text' },
RichText: { type: 'RichText' },
Integer: { type: 'Integer' },
Number: { type: 'Number' },
Boolean: { type: 'Boolean' },
Date: { type: 'Date' },
Location: { type: 'Location' },
Object: { type: 'Object' },
File: { type: 'File' },
Entry: { type: 'Link', linkType: 'Entry' },
Asset: { type: 'Link', linkType: 'Asset' },
Resource: { type: 'ResourceLink' },
Symbols: { type: 'Array', items: { type: 'Symbol' } },
Entries: { type: 'Array', items: { type: 'Link', linkType: 'Entry' } },
Assets: { type: 'Array', items: { type: 'Link', linkType: 'Asset' } },
Resources: { type: 'Array', items: { type: 'ResourceLink' } },
};
const FIELD_TYPES = Object.keys(INTERNAL_TO_API);
/**
* Returns an internal string identifier for an API field object.
*
* We use this string as a simplified reference to field types.
* Possible values are:
*
* - Symbol
* - Symbols
* - Text
* - RichText
* - Integer
* - Number
* - Boolean
* - Date
* - Location
* - Object
* - Entry
* - Entries
* - Asset
* - Assets
* - File
*/
function toInternalFieldType(api) {
return FIELD_TYPES.find((key) => {
const internalApi = INTERNAL_TO_API[key];
const stripped = {
type: api.type,
linkType: api.linkType,
items: api.items,
};
if (stripped.items) {
stripped.items = { type: stripped.items.type, linkType: stripped.items.linkType };
}
if (internalApi.type === 'Link') {
return internalApi.linkType === stripped.linkType;
}
if (internalApi.type === 'Array' && internalApi.items && stripped.items) {
if (internalApi.items.type === 'Link') {
return internalApi.items.linkType === stripped.items.linkType;
}
return internalApi.items.type === stripped.items.type;
}
return internalApi.type === stripped.type;
});
}
const DEFAULTS_WIDGET = {
Text: { widgetId: 'markdown' },
Symbol: { widgetId: 'singleLine' },
Integer: { widgetId: 'numberEditor' },
Number: { widgetId: 'numberEditor' },
Boolean: { widgetId: 'boolean' },
Date: { widgetId: 'datePicker' },
Location: { widgetId: 'locationEditor' },
Object: { widgetId: 'objectEditor' },
RichText: { widgetId: 'richTextEditor' },
Entry: { widgetId: 'entryLinkEditor' },
Asset: { widgetId: 'assetLinkEditor' },
Symbols: { widgetId: 'tagEditor' },
Entries: { widgetId: 'entryLinksEditor' },
Assets: { widgetId: 'assetLinksEditor' },
File: { widgetId: 'fileEditor' },
Resource: { widgetId: 'resourceLinkEditor' },
Resources: { widgetId: 'resourceLinksEditor' },
};
const DEFAULTS_SETTINGS = {
Boolean: {
falseLabel: 'No',
helpText: null,
trueLabel: 'Yes',
},
Date: {
helpText: null,
ampm: '24',
format: 'timeZ',
},
Entry: {
helpText: null,
showCreateEntityAction: true,
showLinkEntityAction: true,
},
Asset: {
helpText: null,
showCreateEntityAction: true,
showLinkEntityAction: true,
},
Entries: {
helpText: null,
bulkEditing: false,
showCreateEntityAction: true,
showLinkEntityAction: true,
},
Assets: {
helpText: null,
showCreateEntityAction: true,
showLinkEntityAction: true,
},
};
function getDefaultWidget(field, fieldId) {
const defaultWidget = {
...DEFAULTS_WIDGET[field],
settings: {
helpText: null,
},
widgetNamespace: 'builtin',
fieldId,
};
if (in_(field, DEFAULTS_SETTINGS)) {
defaultWidget.settings = {
...defaultWidget.settings,
...DEFAULTS_SETTINGS[field],
};
}
return defaultWidget;
}
/*
* Gets the default widget ID for a field:
* - If a field allows predefined values then `dropdown` widget is used
* in the presence of the `in` validation.
* - If a Text field is a title then the `singleLine` widget is used.
* - Otherwise a simple type-to-editor mapping is used.
*/
function getDefaultControlOfField(field) {
const fieldType = toInternalFieldType(field);
if (!fieldType) {
throw new Error('Invalid field type');
}
const hasInValidation = (field.validations || []).find((v) => 'in' in v);
if (hasInValidation && DROPDOWN_TYPES.includes(fieldType)) {
return {
widgetId: 'dropdown',
fieldId: field.id,
widgetNamespace: 'builtin',
};
}
return getDefaultWidget(fieldType, field.id);
}
export { DEFAULTS_SETTINGS, DEFAULTS_WIDGET, FIELD_TYPES, getDefaultControlOfField as default, toInternalFieldType };
//# sourceMappingURL=controls-defaults.js.map