@wordpress/components
Version:
UI components for WordPress.
93 lines (92 loc) • 2.56 kB
JavaScript
;
Object.defineProperties(exports, {
__esModule: { value: true },
[Symbol.toStringTag]: { value: "Module" }
});
let _wordpress_i18n = require("@wordpress/i18n");
// packages/components/src/alignment-matrix-control/utils.tsx
const GRID = [
[
"top left",
"top center",
"top right"
],
[
"center left",
"center center",
"center right"
],
[
"bottom left",
"bottom center",
"bottom right"
]
];
const ALIGNMENT_LABEL = {
"top left": (0, _wordpress_i18n.__)("Top Left"),
"top center": (0, _wordpress_i18n.__)("Top Center"),
"top right": (0, _wordpress_i18n.__)("Top Right"),
"center left": (0, _wordpress_i18n.__)("Center Left"),
"center center": (0, _wordpress_i18n.__)("Center"),
center: (0, _wordpress_i18n.__)("Center"),
"center right": (0, _wordpress_i18n.__)("Center Right"),
"bottom left": (0, _wordpress_i18n.__)("Bottom Left"),
"bottom center": (0, _wordpress_i18n.__)("Bottom Center"),
"bottom right": (0, _wordpress_i18n.__)("Bottom Right")
};
const ALIGNMENTS = GRID.flat();
/**
* Normalizes and transforms an incoming value to better match the alignment values
*
* @param value An alignment value to parse.
*
* @return The parsed value.
*/
function normalize(value) {
const transformed = (value === "center" ? "center center" : value)?.replace("-", " ");
return ALIGNMENTS.includes(transformed) ? transformed : void 0;
}
/**
* Creates an item ID based on a prefix ID and an alignment value.
*
* @param prefixId An ID to prefix.
* @param value An alignment value.
*
* @return The item id.
*/
function getItemId(prefixId, value) {
const normalized = normalize(value);
if (!normalized) return;
return `${prefixId}-${normalized.replace(" ", "-")}`;
}
/**
* Extracts an item value from its ID
*
* @param prefixId An ID prefix to remove
* @param id An item ID
* @return The item value
*/
function getItemValue(prefixId, id) {
const value = id?.replace(prefixId + "-", "");
return normalize(value);
}
/**
* Retrieves the alignment index from a value.
*
* @param alignment Value to check.
*
* @return The index of a matching alignment.
*/
function getAlignmentIndex(alignment = "center") {
const normalized = normalize(alignment);
if (!normalized) return;
const index = ALIGNMENTS.indexOf(normalized);
return index > -1 ? index : void 0;
}
exports.ALIGNMENTS = ALIGNMENTS;
exports.ALIGNMENT_LABEL = ALIGNMENT_LABEL;
exports.GRID = GRID;
exports.getAlignmentIndex = getAlignmentIndex;
exports.getItemId = getItemId;
exports.getItemValue = getItemValue;
//# sourceMappingURL=utils.cjs.map