@wordpress/editor
Version:
Enhanced block editor for WordPress posts.
124 lines (123 loc) • 4.07 kB
JavaScript
// packages/editor/src/hooks/push-changes-to-global-styles/format-style-value.js
import { capitalCase } from "change-case";
var EMPTY_VALUE_LABEL = "—";
var PRESET_USER_PREFIX = "var:preset|";
var PRESET_CSS_VAR_REGEX = /^var\(\s*--wp--preset--[a-z0-9-]+?\s*(?:,[^)]*)?\)$/i;
function getPresetSlug(value) {
if (value.startsWith(PRESET_USER_PREFIX)) {
return value.split("|").pop();
}
if (PRESET_CSS_VAR_REGEX.test(value)) {
return value.replace(/^var\(\s*/, "").replace(/\s*(?:,[^)]*)?\)$/, "").split("--").pop();
}
return void 0;
}
var isSet = (value) => value !== void 0 && value !== null && value !== "";
var BORDER_SIDES = ["top", "right", "bottom", "left"];
function flattenBorder(border) {
if (isSet(border.width) || isSet(border.style) || isSet(border.color)) {
return border;
}
const setSides = BORDER_SIDES.filter((side) => border[side]);
if (!setSides.length) {
return border;
}
const collapse = (property) => {
const values = setSides.map((side) => border[side]?.[property]);
return values.every((value) => value === values[0]) ? values[0] : void 0;
};
return {
width: collapse("width"),
style: collapse("style"),
color: collapse("color")
};
}
function formatStyleValue(value) {
if (!isSet(value)) {
return EMPTY_VALUE_LABEL;
}
if (typeof value === "string") {
const presetSlug = getPresetSlug(value);
if (presetSlug) {
return capitalCase(presetSlug);
}
return value;
}
if (typeof value === "number") {
return String(value);
}
if (typeof value === "object") {
try {
const stringified = JSON.stringify(value);
return stringified && stringified !== "{}" && stringified !== "[]" ? stringified : EMPTY_VALUE_LABEL;
} catch {
return EMPTY_VALUE_LABEL;
}
}
return String(value);
}
function formatBorderShorthand(border) {
if (!border || typeof border !== "object") {
return formatStyleValue(border);
}
const { width, style, color } = flattenBorder(border);
const parts = [width, style, color].filter(isSet).map((part) => formatStyleValue(part));
return parts.length ? parts.join(" ") : EMPTY_VALUE_LABEL;
}
var RADIUS_CORNERS = ["topLeft", "topRight", "bottomRight", "bottomLeft"];
function formatBorderRadius(radius) {
if (radius && typeof radius === "object") {
const parts = RADIUS_CORNERS.map(
(corner) => radius[corner]
).filter(isSet);
return parts.length ? parts.join(" ") : EMPTY_VALUE_LABEL;
}
return formatStyleValue(radius);
}
function formatSpacingShorthand(spacing, resolve = (value) => value) {
const format = (value) => formatStyleValue(resolve(value));
if (!spacing || typeof spacing !== "object") {
return format(spacing);
}
const { top, right, bottom, left } = spacing;
if (isSet(top) && isSet(right) && isSet(bottom) && isSet(left)) {
if (top === right && right === bottom && bottom === left) {
return format(top);
}
if (top === bottom && left === right) {
return `${format(top)} ${format(left)}`;
}
return [top, right, bottom, left].map(format).join(" ");
}
if (!isSet(top) && !isSet(right) && !isSet(bottom) && !isSet(left)) {
return EMPTY_VALUE_LABEL;
}
return [top, right, bottom, left].map(
(value) => isSet(value) ? format(value) : EMPTY_VALUE_LABEL
).join(" ");
}
function formatBlockGap(gap, resolve = (value) => value) {
const format = (value) => formatStyleValue(resolve(value));
if (!gap || typeof gap !== "object") {
return format(gap);
}
const { top: row, left: column } = gap;
if (!isSet(row) && !isSet(column)) {
return EMPTY_VALUE_LABEL;
}
if (row === column) {
return format(row);
}
const rowLabel = isSet(row) ? format(row) : EMPTY_VALUE_LABEL;
const columnLabel = isSet(column) ? format(column) : EMPTY_VALUE_LABEL;
return `${rowLabel} - ${columnLabel}`;
}
export {
EMPTY_VALUE_LABEL,
formatBlockGap,
formatBorderRadius,
formatBorderShorthand,
formatSpacingShorthand,
formatStyleValue
};
//# sourceMappingURL=format-style-value.mjs.map