UNPKG

@wordpress/components

Version:
308 lines (307 loc) • 6.75 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); let _wordpress_i18n = require("@wordpress/i18n"); let ___unit_control_utils_ts = require("../unit-control/utils.cjs"); // packages/components/src/box-control/utils.ts const 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: .1 }, rm: { max: 10, step: .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 }, cqw: { max: 100, step: 1 }, cqh: { max: 100, step: 1 }, cqi: { max: 100, step: 1 }, cqb: { max: 100, step: 1 }, cqmin: { max: 100, step: 1 }, cqmax: { max: 100, step: 1 } }; const LABELS = { all: (0, _wordpress_i18n.__)("All sides"), top: (0, _wordpress_i18n.__)("Top side"), bottom: (0, _wordpress_i18n.__)("Bottom side"), left: (0, _wordpress_i18n.__)("Left side"), right: (0, _wordpress_i18n.__)("Right side"), vertical: (0, _wordpress_i18n.__)("Top and bottom sides"), horizontal: (0, _wordpress_i18n.__)("Left and right sides") }; const DEFAULT_VALUES = { top: void 0, right: void 0, bottom: void 0, left: void 0 }; const ALL_SIDES = [ "top", "right", "bottom", "left" ]; /** * Gets the merged input value and unit from values data. * * @param values Box values. * @param availableSides Available box sides to evaluate. * * @return A value + unit for the 'all' input. */ function getMergedValue(values = {}, availableSides = ALL_SIDES) { const sides = normalizeSides(availableSides); if (sides.every((side) => values[side] === values[sides[0]])) return values[sides[0]]; } /** * Checks if the values are mixed. * * @param values Box values. * @param availableSides Available box sides to evaluate. * @return Whether the values are mixed. */ function isValueMixed(values = {}, availableSides = ALL_SIDES) { const sides = normalizeSides(availableSides); return sides.some((side) => values[side] !== values[sides[0]]); } /** * Checks to determine if values are defined. * * @param values Box values. * * @return Whether values are mixed. */ function isValuesDefined(values) { return values && Object.values(values).filter((value) => !!value && /\d/.test(value)).length > 0; } /** * Extracts the unit of each side from the given box values. * * @param values Box values. * * @return Units for each side. */ function getSelectedUnits(values) { return { top: (0, ___unit_control_utils_ts.parseQuantityAndUnitFromRawValue)(values?.top)[1], right: (0, ___unit_control_utils_ts.parseQuantityAndUnitFromRawValue)(values?.right)[1], bottom: (0, ___unit_control_utils_ts.parseQuantityAndUnitFromRawValue)(values?.bottom)[1], left: (0, ___unit_control_utils_ts.parseQuantityAndUnitFromRawValue)(values?.left)[1] }; } /** * Normalizes provided sides configuration to an array containing only top, * right, bottom and left. This essentially just maps `horizontal` or `vertical` * to their appropriate sides to facilitate correctly determining value for * all input control. * * @param sides Available sides for box control. * @return Normalized sides configuration. */ 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; } /** * Return the allowed sides based on the sides configuration. * * @param sides Sides configuration. * @return Allowed sides. */ 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; } /** * Checks if a value is a preset value. * * @param value The value to check. * @param presetKey The preset key to check against. * @return Whether the value is a preset value. */ function isValuePreset(value, presetKey) { return value.startsWith(`var:preset|${presetKey}|`); } /** * Returns the index of the preset value in the presets array. * * @param value The value to check. * @param presetKey The preset key to check against. * @param presets The array of presets to search. * @return The index of the preset value in the presets array. */ function getPresetIndexFromValue(value, presetKey, presets) { if (!isValuePreset(value, presetKey)) return; const match = value.match(new RegExp(`^var:preset\\|${presetKey}\\|(.+)$`)); if (!match) return; const slug = match[1]; const index = presets.findIndex((preset) => { return preset.slug === slug; }); return index !== -1 ? index : void 0; } /** * Returns the preset value from the index. * * @param index The index of the preset value in the presets array. * @param presetKey The preset key to check against. * @param presets The array of presets to search. * @return The preset value from the index. */ function getPresetValueFromIndex(index, presetKey, presets) { return `var:preset|${presetKey}|${presets[index].slug}`; } exports.ALL_SIDES = ALL_SIDES; exports.CUSTOM_VALUE_SETTINGS = CUSTOM_VALUE_SETTINGS; exports.DEFAULT_VALUES = DEFAULT_VALUES; exports.LABELS = LABELS; exports.getAllowedSides = getAllowedSides; exports.getMergedValue = getMergedValue; exports.getPresetIndexFromValue = getPresetIndexFromValue; exports.getPresetValueFromIndex = getPresetValueFromIndex; exports.getSelectedUnits = getSelectedUnits; exports.isValueMixed = isValueMixed; exports.isValuePreset = isValuePreset; exports.isValuesDefined = isValuesDefined; exports.normalizeSides = normalizeSides; //# sourceMappingURL=utils.cjs.map