@contentful/field-editor-shared
Advanced tools
Comparing version 0.6.2 to 0.7.0
@@ -13,2 +13,4 @@ 'use strict'; | ||
var emotion = require('emotion'); | ||
var get = _interopDefault(require('lodash/get')); | ||
var isObject = _interopDefault(require('lodash/isObject')); | ||
var isNumber = _interopDefault(require('lodash/isNumber')); | ||
@@ -199,2 +201,242 @@ | ||
// A type of promise-like that resolves synchronously and supports only one observer | ||
var _iteratorSymbol = | ||
/*#__PURE__*/ | ||
typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = | ||
/*#__PURE__*/ | ||
Symbol("Symbol.iterator")) : "@@iterator"; // Asynchronously iterate through an object's values | ||
var _asyncIteratorSymbol = | ||
/*#__PURE__*/ | ||
typeof Symbol !== "undefined" ? Symbol.asyncIterator || (Symbol.asyncIterator = | ||
/*#__PURE__*/ | ||
Symbol("Symbol.asyncIterator")) : "@@asyncIterator"; // Asynchronously iterate on a value using it's async iterator if present, or its synchronous iterator if missing | ||
function _catch(body, recover) { | ||
try { | ||
var result = body(); | ||
} catch (e) { | ||
return recover(e); | ||
} | ||
if (result && result.then) { | ||
return result.then(void 0, recover); | ||
} | ||
return result; | ||
} // Asynchronously await a promise and pass the result to a finally continuation | ||
function titleOrDefault(title, defaultTitle) { | ||
if (!title || title.match(/^\s*$/)) { | ||
return defaultTitle; | ||
} else { | ||
return title; | ||
} | ||
} | ||
function getFieldValue(_ref) { | ||
var entity = _ref.entity, | ||
fieldId = _ref.fieldId, | ||
localeCode = _ref.localeCode, | ||
defaultLocaleCode = _ref.defaultLocaleCode; | ||
var values = get(entity, ['fields', fieldId]); | ||
if (!isObject(values)) { | ||
return; | ||
} | ||
var firstLocaleCode = Object.keys(values)[0]; | ||
return values[localeCode] || values[defaultLocaleCode] || values[firstLocaleCode]; | ||
} | ||
function getAssetTitle(_ref2) { | ||
var asset = _ref2.asset, | ||
localeCode = _ref2.localeCode, | ||
defaultLocaleCode = _ref2.defaultLocaleCode, | ||
defaultTitle = _ref2.defaultTitle; | ||
var title = getFieldValue({ | ||
entity: asset, | ||
fieldId: 'title', | ||
localeCode: localeCode, | ||
defaultLocaleCode: defaultLocaleCode | ||
}); | ||
return titleOrDefault(title, defaultTitle); | ||
} | ||
function getEntityDescription(_ref3) { | ||
var entity = _ref3.entity, | ||
contentType = _ref3.contentType, | ||
localeCode = _ref3.localeCode, | ||
defaultLocaleCode = _ref3.defaultLocaleCode; | ||
if (!contentType) { | ||
return ''; | ||
} | ||
var isTextField = function isTextField(field) { | ||
return ['Symbol', 'Text'].includes(field.type); | ||
}; | ||
var isDisplayField = function isDisplayField(field) { | ||
return field.id === contentType.displayField; | ||
}; | ||
var isMaybeSlugField = function isMaybeSlugField(field) { | ||
return /\bslug\b/.test(field.name); | ||
}; | ||
var isDescriptionField = function isDescriptionField(field) { | ||
return isTextField(field) && !isDisplayField(field) && !isMaybeSlugField(field); | ||
}; | ||
var descriptionField = contentType.fields.find(isDescriptionField); | ||
if (!descriptionField) { | ||
return ''; | ||
} | ||
return getFieldValue({ | ||
entity: entity, | ||
fieldId: descriptionField.id, | ||
localeCode: localeCode, | ||
defaultLocaleCode: defaultLocaleCode | ||
}) || ''; | ||
} | ||
function getEntryTitle(_ref4) { | ||
var entry = _ref4.entry, | ||
contentType = _ref4.contentType, | ||
localeCode = _ref4.localeCode, | ||
defaultLocaleCode = _ref4.defaultLocaleCode, | ||
defaultTitle = _ref4.defaultTitle; | ||
var title; | ||
if (!contentType) { | ||
return defaultTitle; | ||
} | ||
var displayField = contentType.displayField; | ||
if (!displayField) { | ||
return defaultTitle; | ||
} | ||
var displayFieldInfo = contentType.fields.find(function (field) { | ||
return field.id === displayField; | ||
}); | ||
if (!displayFieldInfo) { | ||
return defaultTitle; | ||
} // when localization for a field is "turned off", | ||
// we don't clean up the "old" localized data, so it is still in the response. | ||
// Therefore, we're checking if displayField is localizable. | ||
if (displayFieldInfo.localized) { | ||
title = getFieldValue({ | ||
entity: entry, | ||
fieldId: displayField, | ||
localeCode: localeCode, | ||
defaultLocaleCode: defaultLocaleCode | ||
}); | ||
if (!title) { | ||
// Older content types may return id/apiName, but some entry lookup paths do not fetch raw data | ||
// In order to still return a title in this case, look for displayField as apiName in content type, | ||
// ...but still look for displayField as a field in the entry | ||
title = getFieldValue({ | ||
entity: entry, | ||
fieldId: displayFieldInfo.id, | ||
localeCode: localeCode, | ||
defaultLocaleCode: defaultLocaleCode | ||
}); | ||
} | ||
} else { | ||
title = getFieldValue({ | ||
entity: entry, | ||
fieldId: displayField, | ||
defaultLocaleCode: defaultLocaleCode, | ||
localeCode: '' | ||
}); | ||
if (!title) { | ||
title = getFieldValue({ | ||
entity: entry, | ||
fieldId: displayFieldInfo.id, | ||
defaultLocaleCode: defaultLocaleCode, | ||
localeCode: '' | ||
}); | ||
} | ||
} | ||
return titleOrDefault(title, defaultTitle); | ||
} | ||
function getEntryStatus(sys) { | ||
if (!sys || sys.type !== 'Entry' && sys.type !== 'Asset') { | ||
throw new TypeError('Invalid entity metadata object'); | ||
} | ||
if (sys.deletedVersion) { | ||
return 'deleted'; | ||
} else if (sys.archivedVersion) { | ||
return 'archived'; | ||
} else if (sys.publishedVersion) { | ||
if (sys.version > sys.publishedVersion + 1) { | ||
return 'changed'; | ||
} else { | ||
return 'published'; | ||
} | ||
} else { | ||
return 'draft'; | ||
} | ||
} | ||
/** | ||
* Gets a promise resolving with a localized asset image field representing a | ||
* given entities file. The promise may resolve with null. | ||
*/ | ||
var getEntryImage = function getEntryImage(_ref5, getAsset) { | ||
var entry = _ref5.entry, | ||
contentType = _ref5.contentType, | ||
localeCode = _ref5.localeCode; | ||
try { | ||
if (!contentType) { | ||
return Promise.resolve(null); | ||
} | ||
var assetLink = contentType.fields.find(function (field) { | ||
return field.type === 'Link' && field.linkType === 'Asset'; | ||
}); | ||
if (!assetLink) { | ||
return Promise.resolve(null); | ||
} | ||
var assetId = get(entry.fields, [assetLink.id, localeCode, 'sys', 'id']); | ||
if (!assetId) { | ||
return Promise.resolve(null); | ||
} | ||
return Promise.resolve(_catch(function () { | ||
return Promise.resolve(getAsset(assetId)).then(function (asset) { | ||
var file = get(asset, ['fields', 'file', localeCode]); | ||
var isImage = Boolean(get(file, ['details', 'image'], false)); | ||
return isImage ? file : null; | ||
}); | ||
}, function () { | ||
return null; | ||
})); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var entityHelpers = { | ||
__proto__: null, | ||
getFieldValue: getFieldValue, | ||
getAssetTitle: getAssetTitle, | ||
getEntityDescription: getEntityDescription, | ||
getEntryTitle: getEntryTitle, | ||
getEntryStatus: getEntryStatus, | ||
getEntryImage: getEntryImage | ||
}; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
@@ -259,2 +501,3 @@ function fromFieldValidations(validations, fieldType) { | ||
exports.PredefinedValuesError = PredefinedValuesError; | ||
exports.entityHelpers = entityHelpers; | ||
//# sourceMappingURL=field-editor-shared.cjs.development.js.map |
@@ -1,2 +0,2 @@ | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("react")),n=e(require("lodash/throttle")),r=e(require("lodash/isEqual")),a=require("@contentful/forma-36-react-components"),s=e(require("@contentful/forma-36-tokens")),i=require("emotion"),u=e(require("lodash/isNumber"));function o(){return(o=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var l=function(e){var t,r;function a(t){var r;(r=e.call(this,t)||this).unsubscribeErrors=null,r.unsubscribeDisabled=null,r.unsubscribeValue=null,r.setValue=n((function(e){r.props.isEmptyValue(void 0===e?null:e)?r.setState({lastSetValue:void 0},(function(){r.props.field.removeValue()})):r.setState({lastSetValue:e},(function(){r.props.field.setValue(e)}))}),r.props.throttle,{leading:0===r.props.throttle});var a=t.field.getValue();return r.state={isLocalValueChange:!1,externalReset:0,value:a,lastSetValue:a,lastRemoteValue:a,disabled:t.isInitiallyDisabled,errors:[]},r}r=e,(t=a).prototype=Object.create(r.prototype),t.prototype.constructor=t,t.__proto__=r;var s=a.prototype;return s.componentDidMount=function(){var e=this,t=this.props.field;this.unsubscribeErrors=t.onSchemaErrorsChanged((function(t){e.setState({errors:t||[]})})),this.unsubscribeDisabled=t.onIsDisabledChanged((function(t){e.setState({disabled:t})})),this.unsubscribeValue=t.onValueChanged((function(t){e.setState((function(n){var r=e.props.isEqualValues(t,n.lastSetValue);return{value:t,lastSetValue:t,lastRemoteValue:r?n.lastRemoteValue:t,isLocalValueChange:r,externalReset:n.externalReset+(r?0:1)}}))}))},s.componentWillUnmount=function(){"function"==typeof this.unsubscribeErrors&&this.unsubscribeErrors(),"function"==typeof this.unsubscribeDisabled&&this.unsubscribeDisabled(),"function"==typeof this.unsubscribeValue&&this.unsubscribeValue()},s.render=function(){var e=o({},this.state);return delete e.lastSetValue,this.props.children(o({},e,{setValue:this.setValue}))},a}(t.Component);l.defaultProps={children:function(){return null},isEmptyValue:function(e){return null===e||""===e},isEqualValues:function(e,t){return r(e,t)},throttle:300};var c=i.css({color:s.colorRedBase}),p={__proto__:null,fromFieldValidations:function(e,t){void 0===e&&(e=[]);var n=e.find((function(e){return"size"in e})),r=n&&n.size||{},a=r.min,s=r.max;return u(a)&&u(s)?{type:"min-max",min:a,max:s}:u(a)?{type:"min",min:a}:u(s)?{type:"max",max:s}:{type:"max",max:"Symbol"===t?256:5e4}},makeChecker:function(e){return function(t){return"max"===e.type?t<=e.max:"min"===e.type?t>=e.min:t>=e.min&&t<=e.max}}};exports.CharCounter=function(e){var n,r=0;e.value&&(r=e.value.length);var a=0===r||e.checkConstraint(r);return t.createElement("span",{"data-status-code":a?null:"invalid-size",className:i.cx((n={},n[c]=!a,n))},r," characters")},exports.CharValidation=function(e){var n=e.constraints;return"max"===n.type?t.createElement("span",null,"Maximum ",n.max," characters"):"min"===n.type?t.createElement("span",null,"Requires at least ",n.min," characters"):t.createElement("span",null,"Requires between ",n.min," and ",n.max," characters")},exports.ConstraintsUtils=p,exports.FieldConnector=l,exports.PredefinedValuesError=function(){return t.createElement(a.Note,{noteType:"warning",testId:"predefined-values-warning"},"The widget failed to initialize. You can fix the problem by providing predefined values under the validations tab in the field settings.")}; | ||
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}Object.defineProperty(exports,"__esModule",{value:!0});var t=e(require("react")),n=e(require("lodash/throttle")),r=e(require("lodash/isEqual")),i=require("@contentful/forma-36-react-components"),l=e(require("@contentful/forma-36-tokens")),a=require("emotion"),o=e(require("lodash/get")),u=e(require("lodash/isObject")),s=e(require("lodash/isNumber"));function d(){return(d=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}var c=function(e){var t,r;function i(t){var r;(r=e.call(this,t)||this).unsubscribeErrors=null,r.unsubscribeDisabled=null,r.unsubscribeValue=null,r.setValue=n((function(e){r.props.isEmptyValue(void 0===e?null:e)?r.setState({lastSetValue:void 0},(function(){r.props.field.removeValue()})):r.setState({lastSetValue:e},(function(){r.props.field.setValue(e)}))}),r.props.throttle,{leading:0===r.props.throttle});var i=t.field.getValue();return r.state={isLocalValueChange:!1,externalReset:0,value:i,lastSetValue:i,lastRemoteValue:i,disabled:t.isInitiallyDisabled,errors:[]},r}r=e,(t=i).prototype=Object.create(r.prototype),t.prototype.constructor=t,t.__proto__=r;var l=i.prototype;return l.componentDidMount=function(){var e=this,t=this.props.field;this.unsubscribeErrors=t.onSchemaErrorsChanged((function(t){e.setState({errors:t||[]})})),this.unsubscribeDisabled=t.onIsDisabledChanged((function(t){e.setState({disabled:t})})),this.unsubscribeValue=t.onValueChanged((function(t){e.setState((function(n){var r=e.props.isEqualValues(t,n.lastSetValue);return{value:t,lastSetValue:t,lastRemoteValue:r?n.lastRemoteValue:t,isLocalValueChange:r,externalReset:n.externalReset+(r?0:1)}}))}))},l.componentWillUnmount=function(){"function"==typeof this.unsubscribeErrors&&this.unsubscribeErrors(),"function"==typeof this.unsubscribeDisabled&&this.unsubscribeDisabled(),"function"==typeof this.unsubscribeValue&&this.unsubscribeValue()},l.render=function(){var e=d({},this.state);return delete e.lastSetValue,this.props.children(d({},e,{setValue:this.setValue}))},i}(t.Component);c.defaultProps={children:function(){return null},isEmptyValue:function(e){return null===e||""===e},isEqualValues:function(e,t){return r(e,t)},throttle:300};var f=a.css({color:l.colorRedBase});function p(e,t){return!e||e.match(/^\s*$/)?t:e}function y(e){var t=e.localeCode,n=e.defaultLocaleCode,r=o(e.entity,["fields",e.fieldId]);if(u(r)){var i=Object.keys(r)[0];return r[t]||r[n]||r[i]}}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var m={__proto__:null,getFieldValue:y,getAssetTitle:function(e){var t=e.defaultTitle;return p(y({entity:e.asset,fieldId:"title",localeCode:e.localeCode,defaultLocaleCode:e.defaultLocaleCode}),t)},getEntityDescription:function(e){var t=e.entity,n=e.contentType,r=e.localeCode,i=e.defaultLocaleCode;if(!n)return"";var l=n.fields.find((function(e){return function(e){return["Symbol","Text"].includes(e.type)}(e)&&!function(e){return e.id===n.displayField}(e)&&!function(e){return/\bslug\b/.test(e.name)}(e)}));return l&&y({entity:t,fieldId:l.id,localeCode:r,defaultLocaleCode:i})||""},getEntryTitle:function(e){var t,n=e.entry,r=e.contentType,i=e.localeCode,l=e.defaultLocaleCode,a=e.defaultTitle;if(!r)return a;var o=r.displayField;if(!o)return a;var u=r.fields.find((function(e){return e.id===o}));return u?(u.localized?(t=y({entity:n,fieldId:o,localeCode:i,defaultLocaleCode:l}))||(t=y({entity:n,fieldId:u.id,localeCode:i,defaultLocaleCode:l})):(t=y({entity:n,fieldId:o,defaultLocaleCode:l,localeCode:""}))||(t=y({entity:n,fieldId:u.id,defaultLocaleCode:l,localeCode:""})),p(t,a)):a},getEntryStatus:function(e){if(!e||"Entry"!==e.type&&"Asset"!==e.type)throw new TypeError("Invalid entity metadata object");return e.deletedVersion?"deleted":e.archivedVersion?"archived":e.publishedVersion?e.version>e.publishedVersion+1?"changed":"published":"draft"},getEntryImage:function(e,t){var n=e.entry,r=e.contentType,i=e.localeCode;try{if(!r)return Promise.resolve(null);var l=r.fields.find((function(e){return"Link"===e.type&&"Asset"===e.linkType}));if(!l)return Promise.resolve(null);var a=o(n.fields,[l.id,i,"sys","id"]);return Promise.resolve(a?function(e,n){try{var r=Promise.resolve(t(a)).then((function(e){var t=o(e,["fields","file",i]);return Boolean(o(t,["details","image"],!1))?t:null}))}catch(e){return null}return r&&r.then?r.then(void 0,(function(){return null})):r}():null)}catch(e){return Promise.reject(e)}}},b={__proto__:null,fromFieldValidations:function(e,t){void 0===e&&(e=[]);var n=e.find((function(e){return"size"in e})),r=n&&n.size||{},i=r.min,l=r.max;return s(i)&&s(l)?{type:"min-max",min:i,max:l}:s(i)?{type:"min",min:i}:s(l)?{type:"max",max:l}:{type:"max",max:"Symbol"===t?256:5e4}},makeChecker:function(e){return function(t){return"max"===e.type?t<=e.max:"min"===e.type?t>=e.min:t>=e.min&&t<=e.max}}};exports.CharCounter=function(e){var n,r=0;e.value&&(r=e.value.length);var i=0===r||e.checkConstraint(r);return t.createElement("span",{"data-status-code":i?null:"invalid-size",className:a.cx((n={},n[f]=!i,n))},r," characters")},exports.CharValidation=function(e){var n=e.constraints;return"max"===n.type?t.createElement("span",null,"Maximum ",n.max," characters"):"min"===n.type?t.createElement("span",null,"Requires at least ",n.min," characters"):t.createElement("span",null,"Requires between ",n.min," and ",n.max," characters")},exports.ConstraintsUtils=b,exports.FieldConnector=c,exports.PredefinedValuesError=function(){return t.createElement(i.Note,{noteType:"warning",testId:"predefined-values-warning"},"The widget failed to initialize. You can fix the problem by providing predefined values under the validations tab in the field settings.")},exports.entityHelpers=m; | ||
//# sourceMappingURL=field-editor-shared.cjs.production.min.js.map |
@@ -7,2 +7,4 @@ import React from 'react'; | ||
import { cx, css } from 'emotion'; | ||
import get from 'lodash-es/get'; | ||
import isObject from 'lodash-es/isObject'; | ||
import isNumber from 'lodash-es/isNumber'; | ||
@@ -193,2 +195,242 @@ | ||
// A type of promise-like that resolves synchronously and supports only one observer | ||
var _iteratorSymbol = | ||
/*#__PURE__*/ | ||
typeof Symbol !== "undefined" ? Symbol.iterator || (Symbol.iterator = | ||
/*#__PURE__*/ | ||
Symbol("Symbol.iterator")) : "@@iterator"; // Asynchronously iterate through an object's values | ||
var _asyncIteratorSymbol = | ||
/*#__PURE__*/ | ||
typeof Symbol !== "undefined" ? Symbol.asyncIterator || (Symbol.asyncIterator = | ||
/*#__PURE__*/ | ||
Symbol("Symbol.asyncIterator")) : "@@asyncIterator"; // Asynchronously iterate on a value using it's async iterator if present, or its synchronous iterator if missing | ||
function _catch(body, recover) { | ||
try { | ||
var result = body(); | ||
} catch (e) { | ||
return recover(e); | ||
} | ||
if (result && result.then) { | ||
return result.then(void 0, recover); | ||
} | ||
return result; | ||
} // Asynchronously await a promise and pass the result to a finally continuation | ||
function titleOrDefault(title, defaultTitle) { | ||
if (!title || title.match(/^\s*$/)) { | ||
return defaultTitle; | ||
} else { | ||
return title; | ||
} | ||
} | ||
function getFieldValue(_ref) { | ||
var entity = _ref.entity, | ||
fieldId = _ref.fieldId, | ||
localeCode = _ref.localeCode, | ||
defaultLocaleCode = _ref.defaultLocaleCode; | ||
var values = get(entity, ['fields', fieldId]); | ||
if (!isObject(values)) { | ||
return; | ||
} | ||
var firstLocaleCode = Object.keys(values)[0]; | ||
return values[localeCode] || values[defaultLocaleCode] || values[firstLocaleCode]; | ||
} | ||
function getAssetTitle(_ref2) { | ||
var asset = _ref2.asset, | ||
localeCode = _ref2.localeCode, | ||
defaultLocaleCode = _ref2.defaultLocaleCode, | ||
defaultTitle = _ref2.defaultTitle; | ||
var title = getFieldValue({ | ||
entity: asset, | ||
fieldId: 'title', | ||
localeCode: localeCode, | ||
defaultLocaleCode: defaultLocaleCode | ||
}); | ||
return titleOrDefault(title, defaultTitle); | ||
} | ||
function getEntityDescription(_ref3) { | ||
var entity = _ref3.entity, | ||
contentType = _ref3.contentType, | ||
localeCode = _ref3.localeCode, | ||
defaultLocaleCode = _ref3.defaultLocaleCode; | ||
if (!contentType) { | ||
return ''; | ||
} | ||
var isTextField = function isTextField(field) { | ||
return ['Symbol', 'Text'].includes(field.type); | ||
}; | ||
var isDisplayField = function isDisplayField(field) { | ||
return field.id === contentType.displayField; | ||
}; | ||
var isMaybeSlugField = function isMaybeSlugField(field) { | ||
return /\bslug\b/.test(field.name); | ||
}; | ||
var isDescriptionField = function isDescriptionField(field) { | ||
return isTextField(field) && !isDisplayField(field) && !isMaybeSlugField(field); | ||
}; | ||
var descriptionField = contentType.fields.find(isDescriptionField); | ||
if (!descriptionField) { | ||
return ''; | ||
} | ||
return getFieldValue({ | ||
entity: entity, | ||
fieldId: descriptionField.id, | ||
localeCode: localeCode, | ||
defaultLocaleCode: defaultLocaleCode | ||
}) || ''; | ||
} | ||
function getEntryTitle(_ref4) { | ||
var entry = _ref4.entry, | ||
contentType = _ref4.contentType, | ||
localeCode = _ref4.localeCode, | ||
defaultLocaleCode = _ref4.defaultLocaleCode, | ||
defaultTitle = _ref4.defaultTitle; | ||
var title; | ||
if (!contentType) { | ||
return defaultTitle; | ||
} | ||
var displayField = contentType.displayField; | ||
if (!displayField) { | ||
return defaultTitle; | ||
} | ||
var displayFieldInfo = contentType.fields.find(function (field) { | ||
return field.id === displayField; | ||
}); | ||
if (!displayFieldInfo) { | ||
return defaultTitle; | ||
} // when localization for a field is "turned off", | ||
// we don't clean up the "old" localized data, so it is still in the response. | ||
// Therefore, we're checking if displayField is localizable. | ||
if (displayFieldInfo.localized) { | ||
title = getFieldValue({ | ||
entity: entry, | ||
fieldId: displayField, | ||
localeCode: localeCode, | ||
defaultLocaleCode: defaultLocaleCode | ||
}); | ||
if (!title) { | ||
// Older content types may return id/apiName, but some entry lookup paths do not fetch raw data | ||
// In order to still return a title in this case, look for displayField as apiName in content type, | ||
// ...but still look for displayField as a field in the entry | ||
title = getFieldValue({ | ||
entity: entry, | ||
fieldId: displayFieldInfo.id, | ||
localeCode: localeCode, | ||
defaultLocaleCode: defaultLocaleCode | ||
}); | ||
} | ||
} else { | ||
title = getFieldValue({ | ||
entity: entry, | ||
fieldId: displayField, | ||
defaultLocaleCode: defaultLocaleCode, | ||
localeCode: '' | ||
}); | ||
if (!title) { | ||
title = getFieldValue({ | ||
entity: entry, | ||
fieldId: displayFieldInfo.id, | ||
defaultLocaleCode: defaultLocaleCode, | ||
localeCode: '' | ||
}); | ||
} | ||
} | ||
return titleOrDefault(title, defaultTitle); | ||
} | ||
function getEntryStatus(sys) { | ||
if (!sys || sys.type !== 'Entry' && sys.type !== 'Asset') { | ||
throw new TypeError('Invalid entity metadata object'); | ||
} | ||
if (sys.deletedVersion) { | ||
return 'deleted'; | ||
} else if (sys.archivedVersion) { | ||
return 'archived'; | ||
} else if (sys.publishedVersion) { | ||
if (sys.version > sys.publishedVersion + 1) { | ||
return 'changed'; | ||
} else { | ||
return 'published'; | ||
} | ||
} else { | ||
return 'draft'; | ||
} | ||
} | ||
/** | ||
* Gets a promise resolving with a localized asset image field representing a | ||
* given entities file. The promise may resolve with null. | ||
*/ | ||
var getEntryImage = function getEntryImage(_ref5, getAsset) { | ||
var entry = _ref5.entry, | ||
contentType = _ref5.contentType, | ||
localeCode = _ref5.localeCode; | ||
try { | ||
if (!contentType) { | ||
return Promise.resolve(null); | ||
} | ||
var assetLink = contentType.fields.find(function (field) { | ||
return field.type === 'Link' && field.linkType === 'Asset'; | ||
}); | ||
if (!assetLink) { | ||
return Promise.resolve(null); | ||
} | ||
var assetId = get(entry.fields, [assetLink.id, localeCode, 'sys', 'id']); | ||
if (!assetId) { | ||
return Promise.resolve(null); | ||
} | ||
return Promise.resolve(_catch(function () { | ||
return Promise.resolve(getAsset(assetId)).then(function (asset) { | ||
var file = get(asset, ['fields', 'file', localeCode]); | ||
var isImage = Boolean(get(file, ['details', 'image'], false)); | ||
return isImage ? file : null; | ||
}); | ||
}, function () { | ||
return null; | ||
})); | ||
} catch (e) { | ||
return Promise.reject(e); | ||
} | ||
}; | ||
var entityHelpers = { | ||
__proto__: null, | ||
getFieldValue: getFieldValue, | ||
getAssetTitle: getAssetTitle, | ||
getEntityDescription: getEntityDescription, | ||
getEntryTitle: getEntryTitle, | ||
getEntryStatus: getEntryStatus, | ||
getEntryImage: getEntryImage | ||
}; | ||
/* eslint-disable @typescript-eslint/no-explicit-any */ | ||
@@ -248,3 +490,3 @@ function fromFieldValidations(validations, fieldType) { | ||
export { CharCounter, CharValidation, constraints as ConstraintsUtils, FieldConnector, PredefinedValuesError }; | ||
export { CharCounter, CharValidation, constraints as ConstraintsUtils, FieldConnector, PredefinedValuesError, entityHelpers }; | ||
//# sourceMappingURL=field-editor-shared.esm.js.map |
@@ -6,3 +6,6 @@ export { FieldAPI, ParametersAPI, LocalesAPI, BaseExtensionSDK, OpenCustomWidgetOptions } from 'contentful-ui-extensions-sdk'; | ||
export { CharValidation } from './CharValidation'; | ||
import * as entityHelpers from './utils/entityHelpers'; | ||
export { entityHelpers }; | ||
import * as ConstraintsUtils from './utils/constraints'; | ||
export { ConstraintsUtils }; | ||
export { Entry, File, ReferenceEntityType, SingleReferenceValue } from './typesEntity'; |
{ | ||
"name": "@contentful/field-editor-shared", | ||
"version": "0.6.2", | ||
"version": "0.7.0", | ||
"main": "dist/index.js", | ||
@@ -35,4 +35,3 @@ "module": "dist/field-editor-shared.esm.js", | ||
] | ||
}, | ||
"gitHead": "27a662516c540754f9a8fd32c4f75e15f2ad4afc" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
177554
18
1048
1