UNPKG

@wordpress/global-styles-ui

Version:
205 lines (203 loc) • 7.48 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/global-styles-ui/src/utils.ts var utils_exports = {}; __export(utils_exports, { RESPONSIVE_STATES: () => RESPONSIVE_STATES, VALID_BLOCK_STATES: () => VALID_BLOCK_STATES, VALID_ELEMENT_STATES: () => VALID_ELEMENT_STATES, filterObjectByProperties: () => filterObjectByProperties, getFontFamilies: () => getFontFamilies, getNewIndexFromPresets: () => getNewIndexFromPresets, getValidPseudoStates: () => getValidPseudoStates, getValidViewportStates: () => getValidViewportStates, getVariationClassName: () => getVariationClassName, isVariationWithProperties: () => isVariationWithProperties, removePropertiesFromObject: () => removePropertiesFromObject }); module.exports = __toCommonJS(utils_exports); var import_global_styles_engine = require("@wordpress/global-styles-engine"); var import_i18n = require("@wordpress/i18n"); var import_lock_unlock = require("./lock-unlock.cjs"); var { getViewportBreakpoints } = (0, import_lock_unlock.unlock)(import_global_styles_engine.privateApis); var VALID_ELEMENT_STATES = { link: [ { value: ":link", label: (0, import_i18n.__)("Link") }, { value: ":any-link", label: (0, import_i18n.__)("Any Link") }, { value: ":visited", label: (0, import_i18n.__)("Visited") }, { value: ":hover", label: (0, import_i18n.__)("Hover") }, { value: ":focus", label: (0, import_i18n.__)("Focus") }, { value: ":focus-visible", label: (0, import_i18n.__)("Focus-visible") }, { value: ":active", label: (0, import_i18n.__)("Active") } ], button: [ { value: ":link", label: (0, import_i18n.__)("Link") }, { value: ":any-link", label: (0, import_i18n.__)("Any Link") }, { value: ":visited", label: (0, import_i18n.__)("Visited") }, { value: ":hover", label: (0, import_i18n.__)("Hover") }, { value: ":focus", label: (0, import_i18n.__)("Focus") }, { value: ":focus-visible", label: (0, import_i18n.__)("Focus-visible") }, { value: ":active", label: (0, import_i18n.__)("Active") } ] }; var VALID_BLOCK_STATES = { "core/button": [ { value: ":hover", label: (0, import_i18n.__)("Hover") }, { value: ":focus", label: (0, import_i18n.__)("Focus") }, { value: ":focus-visible", label: (0, import_i18n.__)("Focus-visible") }, { value: ":active", label: (0, import_i18n.__)("Active") } ] }; var RESPONSIVE_STATES = [ { value: "@tablet", label: (0, import_i18n.__)("Tablet") }, { value: "@mobile", label: (0, import_i18n.__)("Mobile") } ]; function getValidPseudoStates(name) { if (VALID_BLOCK_STATES[name]) { return VALID_BLOCK_STATES[name] ?? []; } if (VALID_ELEMENT_STATES[name]) { return VALID_ELEMENT_STATES[name]; } return []; } function getValidViewportStates(viewportSettings) { const breakpoints = getViewportBreakpoints(viewportSettings); return RESPONSIVE_STATES.filter( (state) => (state.value !== "@tablet" || breakpoints.tablet !== void 0) && (state.value !== "@mobile" || breakpoints.mobile !== void 0) ); } function removePropertiesFromObject(object, properties) { if (!properties?.length) { return object; } if (typeof object !== "object" || !object || !Object.keys(object).length) { return object; } for (const key in object) { if (properties.includes(key)) { delete object[key]; } else if (typeof object[key] === "object") { removePropertiesFromObject(object[key], properties); } } return object; } var filterObjectByProperties = (object, properties) => { if (!object || !properties?.length) { return {}; } const newObject = {}; Object.keys(object).forEach((key) => { if (properties.includes(key)) { newObject[key] = object[key]; } else if (typeof object[key] === "object") { const newFilter = filterObjectByProperties( object[key], properties ); if (Object.keys(newFilter).length) { newObject[key] = newFilter; } } }); return newObject; }; function isVariationWithProperties(variation, properties) { const variationWithProperties = filterObjectByProperties( structuredClone(variation), properties ); return (0, import_global_styles_engine.areGlobalStylesEqual)(variationWithProperties, variation); } function getFontFamilyFromSetting(fontFamilies, setting) { if (!Array.isArray(fontFamilies) || !setting) { return null; } const fontFamilyVariable = setting.replace("var(", "").replace(")", ""); const fontFamilySlug = fontFamilyVariable?.split("--").slice(-1)[0]; return fontFamilies.find( (fontFamily) => fontFamily.slug === fontFamilySlug ); } function getFontFamilies(themeJson) { const themeFontFamilies = themeJson?.settings?.typography?.fontFamilies?.theme; const customFontFamilies = themeJson?.settings?.typography?.fontFamilies?.custom; let fontFamilies = []; if (themeFontFamilies && customFontFamilies) { fontFamilies = [...themeFontFamilies, ...customFontFamilies]; } else if (themeFontFamilies) { fontFamilies = themeFontFamilies; } else if (customFontFamilies) { fontFamilies = customFontFamilies; } const bodyFontFamilySetting = themeJson?.styles?.typography?.fontFamily; const bodyFontFamily = getFontFamilyFromSetting( fontFamilies, bodyFontFamilySetting ); const headingFontFamilySetting = themeJson?.styles?.elements?.heading?.typography?.fontFamily; let headingFontFamily; if (!headingFontFamilySetting) { headingFontFamily = bodyFontFamily; } else { headingFontFamily = getFontFamilyFromSetting( fontFamilies, themeJson?.styles?.elements?.heading?.typography?.fontFamily ); } return [bodyFontFamily, headingFontFamily]; } function getVariationClassName(variation) { if (!variation) { return ""; } return `is-style-${variation}`; } function getNewIndexFromPresets(presets, slugPrefix) { const nameRegex = new RegExp(`^${slugPrefix}([\\d]+)$`); const highestPresetValue = presets.reduce((currentHighest, preset) => { if (typeof preset?.slug === "string") { const matches = preset?.slug.match(nameRegex); if (matches) { const id = parseInt(matches[1], 10); if (id > currentHighest) { return id; } } } return currentHighest; }, 0); return highestPresetValue + 1; } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { RESPONSIVE_STATES, VALID_BLOCK_STATES, VALID_ELEMENT_STATES, filterObjectByProperties, getFontFamilies, getNewIndexFromPresets, getValidPseudoStates, getValidViewportStates, getVariationClassName, isVariationWithProperties, removePropertiesFromObject }); //# sourceMappingURL=utils.cjs.map