@wordpress/components
Version:
UI components for WordPress.
83 lines (82 loc) • 2.09 kB
JavaScript
import { __ } from "@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": __("Top Left"),
"top center": __("Top Center"),
"top right": __("Top Right"),
"center left": __("Center Left"),
"center center": __("Center"),
center: __("Center"),
"center right": __("Center Right"),
"bottom left": __("Bottom Left"),
"bottom center": __("Bottom Center"),
"bottom right": __("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;
}
export { ALIGNMENTS, ALIGNMENT_LABEL, GRID, getAlignmentIndex, getItemId, getItemValue };
//# sourceMappingURL=utils.mjs.map