UNPKG

@wordpress/components

Version:
313 lines (311 loc) 7.75 kB
"use strict"; var __create = Object.create; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __getProtoOf = Object.getPrototypeOf; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps( // If the importer is in node compatibility mode or this is not an ESM // file that has been converted to a CommonJS file using a Babel- // compatible transform (i.e. "__esModule" has not been set), then set // "default" to the CommonJS "module.exports" for node compatibility. isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod )); var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/components/src/box-control/utils.ts var utils_exports = {}; __export(utils_exports, { ALL_SIDES: () => ALL_SIDES, CUSTOM_VALUE_SETTINGS: () => CUSTOM_VALUE_SETTINGS, DEFAULT_VALUES: () => DEFAULT_VALUES, LABELS: () => LABELS, applyValueToSides: () => applyValueToSides, getAllUnitFallback: () => getAllUnitFallback, getAllowedSides: () => getAllowedSides, getInitialSide: () => getInitialSide, getMergedValue: () => getMergedValue, getPresetIndexFromValue: () => getPresetIndexFromValue, getPresetValueFromIndex: () => getPresetValueFromIndex, isValueMixed: () => isValueMixed, isValuePreset: () => isValuePreset, isValuesDefined: () => isValuesDefined, normalizeSides: () => normalizeSides }); module.exports = __toCommonJS(utils_exports); var import_i18n = require("@wordpress/i18n"); var import_deprecated = __toESM(require("@wordpress/deprecated")); var CUSTOM_VALUE_SETTINGS = { px: { max: 300, step: 1 }, "%": { max: 100, step: 1 }, vw: { max: 100, step: 1 }, vh: { max: 100, step: 1 }, em: { max: 10, step: 0.1 }, rm: { max: 10, step: 0.1 }, svw: { max: 100, step: 1 }, lvw: { max: 100, step: 1 }, dvw: { max: 100, step: 1 }, svh: { max: 100, step: 1 }, lvh: { max: 100, step: 1 }, dvh: { max: 100, step: 1 }, vi: { max: 100, step: 1 }, svi: { max: 100, step: 1 }, lvi: { max: 100, step: 1 }, dvi: { max: 100, step: 1 }, vb: { max: 100, step: 1 }, svb: { max: 100, step: 1 }, lvb: { max: 100, step: 1 }, dvb: { max: 100, step: 1 }, vmin: { max: 100, step: 1 }, svmin: { max: 100, step: 1 }, lvmin: { max: 100, step: 1 }, dvmin: { max: 100, step: 1 }, vmax: { max: 100, step: 1 }, svmax: { max: 100, step: 1 }, lvmax: { max: 100, step: 1 }, dvmax: { max: 100, step: 1 } }; var LABELS = { all: (0, import_i18n.__)("All sides"), top: (0, import_i18n.__)("Top side"), bottom: (0, import_i18n.__)("Bottom side"), left: (0, import_i18n.__)("Left side"), right: (0, import_i18n.__)("Right side"), vertical: (0, import_i18n.__)("Top and bottom sides"), horizontal: (0, import_i18n.__)("Left and right sides") }; var DEFAULT_VALUES = { top: void 0, right: void 0, bottom: void 0, left: void 0 }; var ALL_SIDES = ["top", "right", "bottom", "left"]; function mode(arr) { return arr.sort((a, b) => arr.filter((v) => v === a).length - arr.filter((v) => v === b).length).pop(); } function getMergedValue(values = {}, availableSides = ALL_SIDES) { const sides = normalizeSides(availableSides); if (sides.every((side) => values[side] === values[sides[0]])) { return values[sides[0]]; } return void 0; } function isValueMixed(values = {}, availableSides = ALL_SIDES) { const sides = normalizeSides(availableSides); return sides.some((side) => values[side] !== values[sides[0]]); } function getAllUnitFallback(selectedUnits) { if (!selectedUnits || typeof selectedUnits !== "object") { return void 0; } const filteredUnits = Object.values(selectedUnits).filter(Boolean); return mode(filteredUnits); } function isValuesDefined(values) { return values && Object.values(values).filter( // Switching units when input is empty causes values only // containing units. This gives false positive on mixed values // unless filtered. (value) => !!value && /\d/.test(value) ).length > 0; } function getInitialSide(isLinked, splitOnAxis) { let initialSide = "all"; if (!isLinked) { initialSide = splitOnAxis ? "vertical" : "top"; } return initialSide; } function normalizeSides(sides) { const filteredSides = []; if (!sides?.length) { return ALL_SIDES; } if (sides.includes("vertical")) { filteredSides.push(...["top", "bottom"]); } else if (sides.includes("horizontal")) { filteredSides.push(...["left", "right"]); } else { const newSides = ALL_SIDES.filter((side) => sides.includes(side)); filteredSides.push(...newSides); } return filteredSides; } function applyValueToSides(currentValues, newValue, sides) { (0, import_deprecated.default)("applyValueToSides", { since: "6.8", version: "7.0" }); const newValues = { ...currentValues }; if (sides?.length) { sides.forEach((side) => { if (side === "vertical") { newValues.top = newValue; newValues.bottom = newValue; } else if (side === "horizontal") { newValues.left = newValue; newValues.right = newValue; } else { newValues[side] = newValue; } }); } else { ALL_SIDES.forEach((side) => newValues[side] = newValue); } return newValues; } function getAllowedSides(sides) { const allowedSides = new Set(!sides ? ALL_SIDES : []); sides?.forEach((allowedSide) => { if (allowedSide === "vertical") { allowedSides.add("top"); allowedSides.add("bottom"); } else if (allowedSide === "horizontal") { allowedSides.add("right"); allowedSides.add("left"); } else { allowedSides.add(allowedSide); } }); return allowedSides; } function isValuePreset(value, presetKey) { return value.startsWith(`var:preset|${presetKey}|`); } function getPresetIndexFromValue(value, presetKey, presets) { if (!isValuePreset(value, presetKey)) { return void 0; } const match = value.match(new RegExp(`^var:preset\\|${presetKey}\\|(.+)$`)); if (!match) { return void 0; } const slug = match[1]; const index = presets.findIndex((preset) => { return preset.slug === slug; }); return index !== -1 ? index : void 0; } function getPresetValueFromIndex(index, presetKey, presets) { const preset = presets[index]; return `var:preset|${presetKey}|${preset.slug}`; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { ALL_SIDES, CUSTOM_VALUE_SETTINGS, DEFAULT_VALUES, LABELS, applyValueToSides, getAllUnitFallback, getAllowedSides, getInitialSide, getMergedValue, getPresetIndexFromValue, getPresetValueFromIndex, isValueMixed, isValuePreset, isValuesDefined, normalizeSides }); //# sourceMappingURL=utils.js.map