UNPKG

contentful-management

Version:
255 lines (252 loc) 6.3 kB
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } import { in_ } from './types'; 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' } } }; export 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 */ export 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; }); } export 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' } }; export 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 = _objectSpread(_objectSpread({}, DEFAULTS_WIDGET[field]), {}, { settings: { helpText: null }, widgetNamespace: 'builtin', fieldId }); if (in_(field, DEFAULTS_SETTINGS)) { defaultWidget.settings = _objectSpread(_objectSpread({}, defaultWidget.settings), DEFAULTS_SETTINGS[field]); } return defaultWidget; } /** * Given our internal identifier returns a minimal API field object. */ export function toApiFieldType(internal) { return INTERNAL_TO_API[internal]; } /* * 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. */ export default 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); }