UNPKG

@wordpress/editor

Version:
154 lines (152 loc) 5.41 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; 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 __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // packages/editor/src/hooks/push-changes-to-global-styles/format-style-value.js var format_style_value_exports = {}; __export(format_style_value_exports, { EMPTY_VALUE_LABEL: () => EMPTY_VALUE_LABEL, formatBlockGap: () => formatBlockGap, formatBorderRadius: () => formatBorderRadius, formatBorderShorthand: () => formatBorderShorthand, formatSpacingShorthand: () => formatSpacingShorthand, formatStyleValue: () => formatStyleValue }); module.exports = __toCommonJS(format_style_value_exports); var import_change_case = require("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 (0, import_change_case.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}`; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { EMPTY_VALUE_LABEL, formatBlockGap, formatBorderRadius, formatBorderShorthand, formatSpacingShorthand, formatStyleValue }); //# sourceMappingURL=format-style-value.cjs.map