@contentful/field-editor-shared
Advanced tools
Comparing version 0.23.0 to 0.24.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [0.24.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-shared@0.23.0...@contentful/field-editor-shared@0.24.0) (2021-09-17) | ||
### Features | ||
* add shortenStorageUnit util to shared ([#852](https://github.com/contentful/field-editors/issues/852)) ([142472b](https://github.com/contentful/field-editors/commit/142472b57fc1ca2843dd414c5320fdb5b5b11e62)) | ||
# [0.23.0](https://github.com/contentful/field-editors/compare/@contentful/field-editor-shared@0.22.1...@contentful/field-editor-shared@0.23.0) (2021-09-16) | ||
@@ -8,0 +19,0 @@ |
@@ -1394,2 +1394,68 @@ 'use strict'; | ||
/** | ||
* Transforms a number into a localized string (en-US) | ||
* toLocaleString(1000); // "1,000" | ||
* @param {Number} number | ||
*/ | ||
function toLocaleString(number) { | ||
return number.toLocaleString('en-US'); | ||
} | ||
function formatFloat(value, localize) { | ||
return localize ? toLocaleString(value) : value.toFixed(2) // remove floating point if not necessary | ||
.replace(/\.(0)*$|0*$/, ''); | ||
} | ||
/** | ||
* Make a storage unit number more readable by making them smaller | ||
* shortenStorageUnit(1000, 'GB'); // "1 TB" | ||
* shortenStorageUnit(0.001, 'TB'); // "1 GB" | ||
* @param value | ||
* @param uom Unit of measure | ||
* @returns | ||
*/ | ||
function shortenStorageUnit(value, uom) { | ||
if (value <= 0) { | ||
return '0 B'; | ||
} | ||
var units = ['PB', 'TB', 'GB', 'MB', 'KB', 'B']; | ||
var getBigger = function getBigger(unit) { | ||
return units[units.indexOf(unit) - 1]; | ||
}; | ||
var getSmaller = function getSmaller(unit) { | ||
return units[units.indexOf(unit) + 1]; | ||
}; | ||
var isBiggestUnit = function isBiggestUnit(unit) { | ||
return units.indexOf(unit) === 0; | ||
}; | ||
var isSmallestUnit = function isSmallestUnit(unit) { | ||
return units.indexOf(unit) === units.length - 1; | ||
}; | ||
var reduce = function reduce(number, unit) { | ||
if (number < 0.99 && !isSmallestUnit(unit)) { | ||
return reduce(number * 1000, getSmaller(unit)); | ||
} else if (number >= 1000 && !isBiggestUnit(unit)) { | ||
return reduce(number / 1000, getBigger(unit)); | ||
} else { | ||
return { | ||
number: number, | ||
unit: unit | ||
}; | ||
} | ||
}; | ||
var _reduce = reduce(value, uom), | ||
number = _reduce.number, | ||
unit = _reduce.unit; | ||
return formatFloat(number, false) + " " + unit; | ||
} | ||
exports.CharCounter = CharCounter; | ||
@@ -1403,2 +1469,4 @@ exports.CharValidation = CharValidation; | ||
exports.isValidImage = isValidImage; | ||
exports.shortenStorageUnit = shortenStorageUnit; | ||
exports.toLocaleString = toLocaleString; | ||
//# 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")),r=e(require("lodash/isEqual")),n=e(require("lodash/throttle")),o=require("@contentful/forma-36-react-components"),i=e(require("@contentful/forma-36-tokens")),a=require("emotion"),u=e(require("lodash/isNumber")),l=e(require("react-dom")),s=e(require("lodash/get")),c=e(require("lodash/isObject")),f=e(require("lodash/isString"));function d(e,t,r,n,o,i,a){try{var u=e[i](a),l=u.value}catch(e){return void r(e)}u.done?t(l):Promise.resolve(l).then(n,o)}function h(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var i=e.apply(t,r);function a(e){d(i,n,o,a,u,"next",e)}function u(e){d(i,n,o,a,u,"throw",e)}a(void 0)}))}}function p(){return(p=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function v(e,t){return(v=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function y(e,t){return e(t={exports:{}},t.exports),t.exports}var m=y((function(e){var t=function(e){var t=Object.prototype,r=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{u({},"")}catch(e){u=function(e,t,r){return e[t]=r}}function l(e,t,r,n){var o=Object.create((t&&t.prototype instanceof f?t:f).prototype),i=new C(n||[]);return o._invoke=function(e,t,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=w(a,r);if(u){if(u===c)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var l=s(e,t,r);if("normal"===l.type){if(n=r.done?"completed":"suspendedYield",l.arg===c)continue;return{value:l.arg,done:r.done}}"throw"===l.type&&(n="completed",r.method="throw",r.arg=l.arg)}}}(e,r,i),o}function s(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=l;var c={};function f(){}function d(){}function h(){}var p={};p[o]=function(){return this};var v=Object.getPrototypeOf,y=v&&v(v(L([])));y&&y!==t&&r.call(y,o)&&(p=y);var m=h.prototype=f.prototype=Object.create(p);function g(e){["next","throw","return"].forEach((function(t){u(e,t,(function(e){return this._invoke(t,e)}))}))}function b(e,t){var n;this._invoke=function(o,i){function a(){return new t((function(n,a){!function n(o,i,a,u){var l=s(e[o],e,i);if("throw"!==l.type){var c=l.arg,f=c.value;return f&&"object"==typeof f&&r.call(f,"__await")?t.resolve(f.__await).then((function(e){n("next",e,a,u)}),(function(e){n("throw",e,a,u)})):t.resolve(f).then((function(e){c.value=e,a(c)}),(function(e){return n("throw",e,a,u)}))}u(l.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function w(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,w(e,t),"throw"===t.method))return c;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return c}var n=s(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,c;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,c):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,c)}function x(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function C(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(x,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,i=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:_}}function _(){return{value:void 0,done:!0}}return d.prototype=m.constructor=h,h.constructor=d,d.displayName=u(h,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===d||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,h):(e.__proto__=h,u(e,a,"GeneratorFunction")),e.prototype=Object.create(m),e},e.awrap=function(e){return{__await:e}},g(b.prototype),b.prototype[i]=function(){return this},e.AsyncIterator=b,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new b(l(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},g(m),u(m,a,"Generator"),m[o]=function(){return this},m.toString=function(){return"[object Generator]"},e.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},e.values=L,C.prototype={constructor:C,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),l=r.call(i,"finallyLoc");if(u&&l){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!l)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,c):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),c},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),E(r),c}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;E(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:L(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),c}},e}(e.exports);try{regeneratorRuntime=t}catch(e){Function("r","regeneratorRuntime = r")(t)}})),g=function(e){var t,r;function o(t){var r;(r=e.call(this,t)||this).unsubscribeErrors=null,r.unsubscribeDisabled=null,r.unsubscribeValue=null,r.setValue=function(){var e=h(m.mark((function e(t){return m.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r.props.isEmptyValue(null!=t?t:null)?r.setState({value:void 0}):r.setState({value:t}),e.next=3,r.triggerSetValueCallbacks(t);case 3:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),r.triggerSetValueCallbacks=n((function(e){return new Promise((function(t,n){r.props.isEmptyValue(null!=e?e:null)?r.props.field.removeValue().then(t).catch(n):r.props.field.setValue(e).then(t).catch(n)}))}),r.props.throttle,{leading:0===r.props.throttle});var o=t.field.getValue();return r.state={isLocalValueChange:!1,externalReset:0,value:o,lastRemoteValue:o,disabled:t.isInitiallyDisabled,errors:[]},r}r=e,(t=o).prototype=Object.create(r.prototype),t.prototype.constructor=t,v(t,r);var i=o.prototype;return i.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(r){var n=e.props.isEqualValues(t,r.value);return{value:t,lastRemoteValue:n?r.lastRemoteValue:t,isLocalValueChange:n,externalReset:r.externalReset+(n?0:1)}}))}))},i.componentWillUnmount=function(){"function"==typeof this.unsubscribeErrors&&this.unsubscribeErrors(),"function"==typeof this.unsubscribeDisabled&&this.unsubscribeDisabled(),"function"==typeof this.unsubscribeValue&&this.unsubscribeValue()},i.render=function(){return this.props.children(p({},this.state,{setValue:this.setValue}))},o}(t.Component);g.defaultProps={children:function(){return null},isEmptyValue:function(e){return null===e||""===e},isEqualValues:function(e,t){return r(e,t)},throttle:300};var b=a.css({color:i.red600});function w(e){var t=null,r=function(){return null===t&&((t=document.createElement("div")).setAttribute("id","field-editor-modal-root"),document.body.appendChild(t)),t};return new Promise((function(t){var n={onClose:function(){o(n=p({},n,{isShown:!1})),t.apply(void 0,arguments),r().remove()},isShown:!0};function o(t){l.render(e({onClose:t.onClose,isShown:t.isShown}),r())}o(n)}))}function x(e,r){var n=Date.now(),i=u(e.width)?e.width+"px":e.width;return w((function(a){var u=a.onClose,l=function(){return u()};return t.createElement(o.Modal,{key:n,shouldCloseOnOverlayClick:e.shouldCloseOnOverlayClick||!1,shouldCloseOnEscapePress:e.shouldCloseOnEscapePress||!1,allowHeightOverflow:e.allowHeightOverflow||!1,position:e.position||"center",isShown:a.isShown,onClose:l,size:i||"700px"},(function(){return t.createElement(t.Fragment,null,e.title&&t.createElement(o.Modal.Header,{testId:"dialog-title",title:e.title,onClose:l}),t.createElement("div",{style:{minHeight:e.minHeight||"auto"}},t.createElement(r,{onClose:u})))}))}))}var E={__proto__:null,open:w,openDialog:x,default:{openDialog:x}};function C(e,t){if(!f(e))return t;if(e){var r=e.trim();return 0===r.length?t:r}return t}function L(e){var t=e.localeCode,r=e.defaultLocaleCode,n=s(e.entity,["fields",e.fieldId]);if(c(n)){var o=Object.keys(n)[0];return n[t]||n[r]||n[o]}}var _={__proto__:null,getFieldValue:L,getAssetTitle:function(e){var t=e.defaultTitle;return C(L({entity:e.asset,fieldId:"title",localeCode:e.localeCode,defaultLocaleCode:e.defaultLocaleCode}),t)},getEntityDescription:function(e){var t=e.entity,r=e.contentType,n=e.localeCode,o=e.defaultLocaleCode;if(!r)return"";var i=r.fields.find((function(e){return function(e){return["Symbol","Text"].includes(e.type)}(e)&&!function(e){return e.id===r.displayField}(e)&&!function(e){return/\bslug\b/.test(e.name)}(e)}));return i&&L({entity:t,fieldId:i.id,localeCode:n,defaultLocaleCode:o})||""},getEntryTitle:function(e){var t,r=e.entry,n=e.contentType,o=e.localeCode,i=e.defaultLocaleCode,a=e.defaultTitle;if(!n)return a;var u=n.displayField;if(!u)return a;var l=n.fields.find((function(e){return e.id===u}));return l?(l.localized?(t=L({entity:r,fieldId:u,localeCode:o,defaultLocaleCode:i}))||(t=L({entity:r,fieldId:l.id,localeCode:o,defaultLocaleCode:i})):(t=L({entity:r,fieldId:u,defaultLocaleCode:i,localeCode:""}))||(t=L({entity:r,fieldId:l.id,defaultLocaleCode:i,localeCode:""})),C(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(){var e=h(m.mark((function e(t,r){var n,o,i,a,u,l,c;return m.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.entry,i=t.localeCode,o=t.contentType){e.next=3;break}return e.abrupt("return",null);case 3:if(a=o.fields.find((function(e){return"Link"===e.type&&"Asset"===e.linkType}))){e.next=6;break}return e.abrupt("return",null);case 6:if(u=s(n.fields,[a.id,i,"sys","id"])){e.next=9;break}return e.abrupt("return",null);case 9:return e.prev=9,e.next=12,r(u);case 12:return l=s(e.sent,["fields","file",i]),c=Boolean(s(l,["details","image"],!1)),e.abrupt("return",c?l:null);case 18:return e.prev=18,e.t0=e.catch(9),e.abrupt("return",null);case 21:case"end":return e.stop()}}),e,null,[[9,18]])})));return function(t,r){return e.apply(this,arguments)}}()},V={__proto__:null,fromFieldValidations:function(e,t){void 0===e&&(e=[]);var r=e.find((function(e){return"size"in e})),n=r&&r.size||{},o=n.min,i=n.max;return u(o)&&u(i)?{type:"min-max",min:o,max:i}:u(o)?{type:"min",min:o}:u(i)?{type:"max",max:i}:{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 r,n=0;e.value&&(n=e.value.length);var o=0===n||e.checkConstraint(n);return t.createElement("span",{"data-status-code":o?null:"invalid-size",className:a.cx((r={},r[b]=!o,r))},n," characters")},exports.CharValidation=function(e){var r=e.constraints;return"max"===r.type?t.createElement("span",null,"Maximum ",r.max," characters"):"min"===r.type?t.createElement("span",null,"Requires at least ",r.min," characters"):t.createElement("span",null,"Requires between ",r.min," and ",r.max," characters")},exports.ConstraintsUtils=V,exports.FieldConnector=g,exports.ModalDialogLauncher=E,exports.PredefinedValuesError=function(){return t.createElement(o.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=_,exports.isValidImage=function(e){return["image/bmp","image/x-windows-bmp","image/gif","image/webp","image/jpg","image/jpeg","image/pjpeg","image/x-jps","image/png","image/svg+xml"].includes(e.contentType)}; | ||
"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")),r=e(require("lodash/isEqual")),n=e(require("lodash/throttle")),o=require("@contentful/forma-36-react-components"),i=e(require("@contentful/forma-36-tokens")),a=require("emotion"),u=e(require("lodash/isNumber")),l=e(require("react-dom")),c=e(require("lodash/get")),s=e(require("lodash/isObject")),f=e(require("lodash/isString"));function d(e,t,r,n,o,i,a){try{var u=e[i](a),l=u.value}catch(e){return void r(e)}u.done?t(l):Promise.resolve(l).then(n,o)}function p(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var i=e.apply(t,r);function a(e){d(i,n,o,a,u,"next",e)}function u(e){d(i,n,o,a,u,"throw",e)}a(void 0)}))}}function h(){return(h=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function v(e,t){return(v=Object.setPrototypeOf||function(e,t){return e.__proto__=t,e})(e,t)}function y(e,t){return e(t={exports:{}},t.exports),t.exports}var m=y((function(e){var t=function(e){var t=Object.prototype,r=t.hasOwnProperty,n="function"==typeof Symbol?Symbol:{},o=n.iterator||"@@iterator",i=n.asyncIterator||"@@asyncIterator",a=n.toStringTag||"@@toStringTag";function u(e,t,r){return Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}),e[t]}try{u({},"")}catch(e){u=function(e,t,r){return e[t]=r}}function l(e,t,r,n){var o=Object.create((t&&t.prototype instanceof f?t:f).prototype),i=new C(n||[]);return o._invoke=function(e,t,r){var n="suspendedStart";return function(o,i){if("executing"===n)throw new Error("Generator is already running");if("completed"===n){if("throw"===o)throw i;return{value:void 0,done:!0}}for(r.method=o,r.arg=i;;){var a=r.delegate;if(a){var u=x(a,r);if(u){if(u===s)continue;return u}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if("suspendedStart"===n)throw n="completed",r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);n="executing";var l=c(e,t,r);if("normal"===l.type){if(n=r.done?"completed":"suspendedYield",l.arg===s)continue;return{value:l.arg,done:r.done}}"throw"===l.type&&(n="completed",r.method="throw",r.arg=l.arg)}}}(e,r,i),o}function c(e,t,r){try{return{type:"normal",arg:e.call(t,r)}}catch(e){return{type:"throw",arg:e}}}e.wrap=l;var s={};function f(){}function d(){}function p(){}var h={};h[o]=function(){return this};var v=Object.getPrototypeOf,y=v&&v(v(L([])));y&&y!==t&&r.call(y,o)&&(h=y);var m=p.prototype=f.prototype=Object.create(h);function g(e){["next","throw","return"].forEach((function(t){u(e,t,(function(e){return this._invoke(t,e)}))}))}function b(e,t){var n;this._invoke=function(o,i){function a(){return new t((function(n,a){!function n(o,i,a,u){var l=c(e[o],e,i);if("throw"!==l.type){var s=l.arg,f=s.value;return f&&"object"==typeof f&&r.call(f,"__await")?t.resolve(f.__await).then((function(e){n("next",e,a,u)}),(function(e){n("throw",e,a,u)})):t.resolve(f).then((function(e){s.value=e,a(s)}),(function(e){return n("throw",e,a,u)}))}u(l.arg)}(o,i,n,a)}))}return n=n?n.then(a,a):a()}}function x(e,t){var r=e.iterator[t.method];if(void 0===r){if(t.delegate=null,"throw"===t.method){if(e.iterator.return&&(t.method="return",t.arg=void 0,x(e,t),"throw"===t.method))return s;t.method="throw",t.arg=new TypeError("The iterator does not provide a 'throw' method")}return s}var n=c(r,e.iterator,t.arg);if("throw"===n.type)return t.method="throw",t.arg=n.arg,t.delegate=null,s;var o=n.arg;return o?o.done?(t[e.resultName]=o.value,t.next=e.nextLoc,"return"!==t.method&&(t.method="next",t.arg=void 0),t.delegate=null,s):o:(t.method="throw",t.arg=new TypeError("iterator result is not an object"),t.delegate=null,s)}function w(e){var t={tryLoc:e[0]};1 in e&&(t.catchLoc=e[1]),2 in e&&(t.finallyLoc=e[2],t.afterLoc=e[3]),this.tryEntries.push(t)}function E(e){var t=e.completion||{};t.type="normal",delete t.arg,e.completion=t}function C(e){this.tryEntries=[{tryLoc:"root"}],e.forEach(w,this),this.reset(!0)}function L(e){if(e){var t=e[o];if(t)return t.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var n=-1,i=function t(){for(;++n<e.length;)if(r.call(e,n))return t.value=e[n],t.done=!1,t;return t.value=void 0,t.done=!0,t};return i.next=i}}return{next:O}}function O(){return{value:void 0,done:!0}}return d.prototype=m.constructor=p,p.constructor=d,d.displayName=u(p,a,"GeneratorFunction"),e.isGeneratorFunction=function(e){var t="function"==typeof e&&e.constructor;return!!t&&(t===d||"GeneratorFunction"===(t.displayName||t.name))},e.mark=function(e){return Object.setPrototypeOf?Object.setPrototypeOf(e,p):(e.__proto__=p,u(e,a,"GeneratorFunction")),e.prototype=Object.create(m),e},e.awrap=function(e){return{__await:e}},g(b.prototype),b.prototype[i]=function(){return this},e.AsyncIterator=b,e.async=function(t,r,n,o,i){void 0===i&&(i=Promise);var a=new b(l(t,r,n,o),i);return e.isGeneratorFunction(r)?a:a.next().then((function(e){return e.done?e.value:a.next()}))},g(m),u(m,a,"Generator"),m[o]=function(){return this},m.toString=function(){return"[object Generator]"},e.keys=function(e){var t=[];for(var r in e)t.push(r);return t.reverse(),function r(){for(;t.length;){var n=t.pop();if(n in e)return r.value=n,r.done=!1,r}return r.done=!0,r}},e.values=L,C.prototype={constructor:C,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=void 0,this.done=!1,this.delegate=null,this.method="next",this.arg=void 0,this.tryEntries.forEach(E),!e)for(var t in this)"t"===t.charAt(0)&&r.call(this,t)&&!isNaN(+t.slice(1))&&(this[t]=void 0)},stop:function(){this.done=!0;var e=this.tryEntries[0].completion;if("throw"===e.type)throw e.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var t=this;function n(r,n){return a.type="throw",a.arg=e,t.next=r,n&&(t.method="next",t.arg=void 0),!!n}for(var o=this.tryEntries.length-1;o>=0;--o){var i=this.tryEntries[o],a=i.completion;if("root"===i.tryLoc)return n("end");if(i.tryLoc<=this.prev){var u=r.call(i,"catchLoc"),l=r.call(i,"finallyLoc");if(u&&l){if(this.prev<i.catchLoc)return n(i.catchLoc,!0);if(this.prev<i.finallyLoc)return n(i.finallyLoc)}else if(u){if(this.prev<i.catchLoc)return n(i.catchLoc,!0)}else{if(!l)throw new Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return n(i.finallyLoc)}}}},abrupt:function(e,t){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var i=o;break}}i&&("break"===e||"continue"===e)&&i.tryLoc<=t&&t<=i.finallyLoc&&(i=null);var a=i?i.completion:{};return a.type=e,a.arg=t,i?(this.method="next",this.next=i.finallyLoc,s):this.complete(a)},complete:function(e,t){if("throw"===e.type)throw e.arg;return"break"===e.type||"continue"===e.type?this.next=e.arg:"return"===e.type?(this.rval=this.arg=e.arg,this.method="return",this.next="end"):"normal"===e.type&&t&&(this.next=t),s},finish:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.finallyLoc===e)return this.complete(r.completion,r.afterLoc),E(r),s}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var r=this.tryEntries[t];if(r.tryLoc===e){var n=r.completion;if("throw"===n.type){var o=n.arg;E(r)}return o}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,r){return this.delegate={iterator:L(e),resultName:t,nextLoc:r},"next"===this.method&&(this.arg=void 0),s}},e}(e.exports);try{regeneratorRuntime=t}catch(e){Function("r","regeneratorRuntime = r")(t)}})),g=function(e){var t,r;function o(t){var r;(r=e.call(this,t)||this).unsubscribeErrors=null,r.unsubscribeDisabled=null,r.unsubscribeValue=null,r.setValue=function(){var e=p(m.mark((function e(t){return m.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r.props.isEmptyValue(null!=t?t:null)?r.setState({value:void 0}):r.setState({value:t}),e.next=3,r.triggerSetValueCallbacks(t);case 3:case"end":return e.stop()}}),e)})));return function(t){return e.apply(this,arguments)}}(),r.triggerSetValueCallbacks=n((function(e){return new Promise((function(t,n){r.props.isEmptyValue(null!=e?e:null)?r.props.field.removeValue().then(t).catch(n):r.props.field.setValue(e).then(t).catch(n)}))}),r.props.throttle,{leading:0===r.props.throttle});var o=t.field.getValue();return r.state={isLocalValueChange:!1,externalReset:0,value:o,lastRemoteValue:o,disabled:t.isInitiallyDisabled,errors:[]},r}r=e,(t=o).prototype=Object.create(r.prototype),t.prototype.constructor=t,v(t,r);var i=o.prototype;return i.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(r){var n=e.props.isEqualValues(t,r.value);return{value:t,lastRemoteValue:n?r.lastRemoteValue:t,isLocalValueChange:n,externalReset:r.externalReset+(n?0:1)}}))}))},i.componentWillUnmount=function(){"function"==typeof this.unsubscribeErrors&&this.unsubscribeErrors(),"function"==typeof this.unsubscribeDisabled&&this.unsubscribeDisabled(),"function"==typeof this.unsubscribeValue&&this.unsubscribeValue()},i.render=function(){return this.props.children(h({},this.state,{setValue:this.setValue}))},o}(t.Component);g.defaultProps={children:function(){return null},isEmptyValue:function(e){return null===e||""===e},isEqualValues:function(e,t){return r(e,t)},throttle:300};var b=a.css({color:i.red600});function x(e){var t=null,r=function(){return null===t&&((t=document.createElement("div")).setAttribute("id","field-editor-modal-root"),document.body.appendChild(t)),t};return new Promise((function(t){var n={onClose:function(){o(n=h({},n,{isShown:!1})),t.apply(void 0,arguments),r().remove()},isShown:!0};function o(t){l.render(e({onClose:t.onClose,isShown:t.isShown}),r())}o(n)}))}function w(e,r){var n=Date.now(),i=u(e.width)?e.width+"px":e.width;return x((function(a){var u=a.onClose,l=function(){return u()};return t.createElement(o.Modal,{key:n,shouldCloseOnOverlayClick:e.shouldCloseOnOverlayClick||!1,shouldCloseOnEscapePress:e.shouldCloseOnEscapePress||!1,allowHeightOverflow:e.allowHeightOverflow||!1,position:e.position||"center",isShown:a.isShown,onClose:l,size:i||"700px"},(function(){return t.createElement(t.Fragment,null,e.title&&t.createElement(o.Modal.Header,{testId:"dialog-title",title:e.title,onClose:l}),t.createElement("div",{style:{minHeight:e.minHeight||"auto"}},t.createElement(r,{onClose:u})))}))}))}var E={__proto__:null,open:x,openDialog:w,default:{openDialog:w}};function C(e,t){if(!f(e))return t;if(e){var r=e.trim();return 0===r.length?t:r}return t}function L(e){var t=e.localeCode,r=e.defaultLocaleCode,n=c(e.entity,["fields",e.fieldId]);if(s(n)){var o=Object.keys(n)[0];return n[t]||n[r]||n[o]}}var O={__proto__:null,getFieldValue:L,getAssetTitle:function(e){var t=e.defaultTitle;return C(L({entity:e.asset,fieldId:"title",localeCode:e.localeCode,defaultLocaleCode:e.defaultLocaleCode}),t)},getEntityDescription:function(e){var t=e.entity,r=e.contentType,n=e.localeCode,o=e.defaultLocaleCode;if(!r)return"";var i=r.fields.find((function(e){return function(e){return["Symbol","Text"].includes(e.type)}(e)&&!function(e){return e.id===r.displayField}(e)&&!function(e){return/\bslug\b/.test(e.name)}(e)}));return i&&L({entity:t,fieldId:i.id,localeCode:n,defaultLocaleCode:o})||""},getEntryTitle:function(e){var t,r=e.entry,n=e.contentType,o=e.localeCode,i=e.defaultLocaleCode,a=e.defaultTitle;if(!n)return a;var u=n.displayField;if(!u)return a;var l=n.fields.find((function(e){return e.id===u}));return l?(l.localized?(t=L({entity:r,fieldId:u,localeCode:o,defaultLocaleCode:i}))||(t=L({entity:r,fieldId:l.id,localeCode:o,defaultLocaleCode:i})):(t=L({entity:r,fieldId:u,defaultLocaleCode:i,localeCode:""}))||(t=L({entity:r,fieldId:l.id,defaultLocaleCode:i,localeCode:""})),C(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(){var e=p(m.mark((function e(t,r){var n,o,i,a,u,l,s;return m.wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.entry,i=t.localeCode,o=t.contentType){e.next=3;break}return e.abrupt("return",null);case 3:if(a=o.fields.find((function(e){return"Link"===e.type&&"Asset"===e.linkType}))){e.next=6;break}return e.abrupt("return",null);case 6:if(u=c(n.fields,[a.id,i,"sys","id"])){e.next=9;break}return e.abrupt("return",null);case 9:return e.prev=9,e.next=12,r(u);case 12:return l=c(e.sent,["fields","file",i]),s=Boolean(c(l,["details","image"],!1)),e.abrupt("return",s?l:null);case 18:return e.prev=18,e.t0=e.catch(9),e.abrupt("return",null);case 21:case"end":return e.stop()}}),e,null,[[9,18]])})));return function(t,r){return e.apply(this,arguments)}}()},_={__proto__:null,fromFieldValidations:function(e,t){void 0===e&&(e=[]);var r=e.find((function(e){return"size"in e})),n=r&&r.size||{},o=n.min,i=n.max;return u(o)&&u(i)?{type:"min-max",min:o,max:i}:u(o)?{type:"min",min:o}:u(i)?{type:"max",max:i}:{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 r,n=0;e.value&&(n=e.value.length);var o=0===n||e.checkConstraint(n);return t.createElement("span",{"data-status-code":o?null:"invalid-size",className:a.cx((r={},r[b]=!o,r))},n," characters")},exports.CharValidation=function(e){var r=e.constraints;return"max"===r.type?t.createElement("span",null,"Maximum ",r.max," characters"):"min"===r.type?t.createElement("span",null,"Requires at least ",r.min," characters"):t.createElement("span",null,"Requires between ",r.min," and ",r.max," characters")},exports.ConstraintsUtils=_,exports.FieldConnector=g,exports.ModalDialogLauncher=E,exports.PredefinedValuesError=function(){return t.createElement(o.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=O,exports.isValidImage=function(e){return["image/bmp","image/x-windows-bmp","image/gif","image/webp","image/jpg","image/jpeg","image/pjpeg","image/x-jps","image/png","image/svg+xml"].includes(e.contentType)},exports.shortenStorageUnit=function(e,t){if(e<=0)return"0 B";var r=["PB","TB","GB","MB","KB","B"],n=function e(t,n){return t<.99&&!function(e){return r.indexOf(e)===r.length-1}(n)?e(1e3*t,function(e){return r[r.indexOf(e)+1]}(n)):t>=1e3&&!function(e){return 0===r.indexOf(e)}(n)?e(t/1e3,function(e){return r[r.indexOf(e)-1]}(n)):{number:t,unit:n}}(e,t),o=n.unit;return function(e,t){return e.toFixed(2).replace(/\.(0)*$|0*$/,"")}(n.number)+" "+o},exports.toLocaleString=function(e){return e.toLocaleString("en-US")}; | ||
//# sourceMappingURL=field-editor-shared.cjs.production.min.js.map |
@@ -1388,3 +1388,69 @@ import React from 'react'; | ||
export { CharCounter, CharValidation, constraints as ConstraintsUtils, FieldConnector, ModalDialogLauncher$1 as ModalDialogLauncher, PredefinedValuesError, entityHelpers, isValidImage }; | ||
/** | ||
* Transforms a number into a localized string (en-US) | ||
* toLocaleString(1000); // "1,000" | ||
* @param {Number} number | ||
*/ | ||
function toLocaleString(number) { | ||
return number.toLocaleString('en-US'); | ||
} | ||
function formatFloat(value, localize) { | ||
return localize ? toLocaleString(value) : value.toFixed(2) // remove floating point if not necessary | ||
.replace(/\.(0)*$|0*$/, ''); | ||
} | ||
/** | ||
* Make a storage unit number more readable by making them smaller | ||
* shortenStorageUnit(1000, 'GB'); // "1 TB" | ||
* shortenStorageUnit(0.001, 'TB'); // "1 GB" | ||
* @param value | ||
* @param uom Unit of measure | ||
* @returns | ||
*/ | ||
function shortenStorageUnit(value, uom) { | ||
if (value <= 0) { | ||
return '0 B'; | ||
} | ||
var units = ['PB', 'TB', 'GB', 'MB', 'KB', 'B']; | ||
var getBigger = function getBigger(unit) { | ||
return units[units.indexOf(unit) - 1]; | ||
}; | ||
var getSmaller = function getSmaller(unit) { | ||
return units[units.indexOf(unit) + 1]; | ||
}; | ||
var isBiggestUnit = function isBiggestUnit(unit) { | ||
return units.indexOf(unit) === 0; | ||
}; | ||
var isSmallestUnit = function isSmallestUnit(unit) { | ||
return units.indexOf(unit) === units.length - 1; | ||
}; | ||
var reduce = function reduce(number, unit) { | ||
if (number < 0.99 && !isSmallestUnit(unit)) { | ||
return reduce(number * 1000, getSmaller(unit)); | ||
} else if (number >= 1000 && !isBiggestUnit(unit)) { | ||
return reduce(number / 1000, getBigger(unit)); | ||
} else { | ||
return { | ||
number: number, | ||
unit: unit | ||
}; | ||
} | ||
}; | ||
var _reduce = reduce(value, uom), | ||
number = _reduce.number, | ||
unit = _reduce.unit; | ||
return formatFloat(number, false) + " " + unit; | ||
} | ||
export { CharCounter, CharValidation, constraints as ConstraintsUtils, FieldConnector, ModalDialogLauncher$1 as ModalDialogLauncher, PredefinedValuesError, entityHelpers, isValidImage, shortenStorageUnit, toLocaleString }; | ||
//# sourceMappingURL=field-editor-shared.esm.js.map |
@@ -13,2 +13,3 @@ export { FieldAPI, SpaceAPI, EntryFieldAPI, EntryAPI, DialogsAPI, IdsAPI, ContentType, ParametersAPI, LocalesAPI, NavigatorAPI, NotifierAPI, WindowAPI, AccessAPI, AppConfigAPI, LocationAPI, BaseExtensionSDK, FieldExtensionSDK, OpenCustomWidgetOptions, } from '@contentful/app-sdk'; | ||
export { isValidImage } from './utils/isValidImage'; | ||
export { shortenStorageUnit, toLocaleString } from './utils/shortenStorageUnit'; | ||
export { Entry, File, Asset } from './typesEntity'; |
{ | ||
"name": "@contentful/field-editor-shared", | ||
"version": "0.23.0", | ||
"version": "0.24.0", | ||
"main": "dist/index.js", | ||
@@ -41,3 +41,3 @@ "module": "dist/field-editor-shared.esm.js", | ||
}, | ||
"gitHead": "4020000939175ec415639fbc3b7b20e011c2a8a7" | ||
"gitHead": "f5450f62e92802984fb32a4dd28f266aabbdd5d7" | ||
} |
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
340489
23
2728