@gechiui/block-editor
Version:
125 lines (105 loc) • 3.67 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.getAllUnit = getAllUnit;
exports.getAllValue = getAllValue;
exports.hasDefinedValues = hasDefinedValues;
exports.hasMixedValues = hasMixedValues;
exports.mode = mode;
var _components = require("@gechiui/components");
/**
* GeChiUI dependencies
*/
/**
* Gets the (non-undefined) item with the highest occurrence within an array
* Based in part on: https://stackoverflow.com/a/20762713
*
* Undefined values are always sorted to the end by `sort`, so this function
* returns the first element, to always prioritize real values over undefined
* values.
*
* See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#description
*
* @param {Array<any>} inputArray Array of items to check.
* @return {any} The item with the most occurrences.
*/
function mode(inputArray) {
const arr = [...inputArray];
return arr.sort((a, b) => inputArray.filter(v => v === b).length - inputArray.filter(v => v === a).length).shift();
}
/**
* Returns the most common CSS unit in the radius values.
* Falls back to `px` as a default unit.
*
* @param {Object|string} values Radius values.
* @return {string} Most common CSS unit in values. Default: `px`.
*/
function getAllUnit() {
let values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
if (typeof values === 'string') {
const [, unit] = (0, _components.__experimentalParseUnit)(values);
return unit || 'px';
}
const allUnits = Object.values(values).map(value => {
const [, unit] = (0, _components.__experimentalParseUnit)(value);
return unit;
});
return mode(allUnits) || 'px';
}
/**
* Gets the 'all' input value and unit from values data.
*
* @param {Object|string} values Radius values.
* @return {string} A value + unit for the 'all' input.
*/
function getAllValue() {
let values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
/**
* Border radius support was originally a single pixel value.
*
* To maintain backwards compatibility treat this case as the all value.
*/
if (typeof values === 'string') {
return values;
}
const parsedValues = Object.values(values).map(value => (0, _components.__experimentalParseUnit)(value));
const allValues = parsedValues.map(value => value[0]);
const allUnits = parsedValues.map(value => value[1]);
const value = allValues.every(v => v === allValues[0]) ? allValues[0] : '';
const unit = mode(allUnits);
const allValue = value === 0 || value ? `${value}${unit}` : null;
return allValue;
}
/**
* Checks to determine if values are mixed.
*
* @param {Object} values Radius values.
* @return {boolean} Whether values are mixed.
*/
function hasMixedValues() {
let values = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
const allValue = getAllValue(values);
const isMixed = isNaN(parseFloat(allValue));
return isMixed;
}
/**
* Checks to determine if values are defined.
*
* @param {Object} values Radius values.
* @return {boolean} Whether values are mixed.
*/
function hasDefinedValues(values) {
if (!values) {
return false;
} // A string value represents a shorthand value.
if (typeof values === 'string') {
return true;
} // An object represents longhand border radius values, if any are set
// flag values as being defined.
const filteredValues = Object.values(values).filter(value => {
return !!value || value === 0;
});
return !!filteredValues.length;
}
//# sourceMappingURL=utils.js.map