@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
173 lines (172 loc) • 5.53 kB
JavaScript
// packages/global-styles-ui/src/utils.ts
import {
areGlobalStylesEqual,
privateApis as globalStylesEnginePrivateApis
} from "@wordpress/global-styles-engine";
import { __ } from "@wordpress/i18n";
import { unlock } from "./lock-unlock.mjs";
var { getViewportBreakpoints } = unlock(globalStylesEnginePrivateApis);
var VALID_ELEMENT_STATES = {
link: [
{ value: ":link", label: __("Link") },
{ value: ":any-link", label: __("Any Link") },
{ value: ":visited", label: __("Visited") },
{ value: ":hover", label: __("Hover") },
{ value: ":focus", label: __("Focus") },
{ value: ":focus-visible", label: __("Focus-visible") },
{ value: ":active", label: __("Active") }
],
button: [
{ value: ":link", label: __("Link") },
{ value: ":any-link", label: __("Any Link") },
{ value: ":visited", label: __("Visited") },
{ value: ":hover", label: __("Hover") },
{ value: ":focus", label: __("Focus") },
{ value: ":focus-visible", label: __("Focus-visible") },
{ value: ":active", label: __("Active") }
]
};
var VALID_BLOCK_STATES = {
"core/button": [
{ value: ":hover", label: __("Hover") },
{ value: ":focus", label: __("Focus") },
{ value: ":focus-visible", label: __("Focus-visible") },
{ value: ":active", label: __("Active") }
]
};
var RESPONSIVE_STATES = [
{ value: "@tablet", label: __("Tablet") },
{ value: "@mobile", label: __("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 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;
}
export {
RESPONSIVE_STATES,
VALID_BLOCK_STATES,
VALID_ELEMENT_STATES,
filterObjectByProperties,
getFontFamilies,
getNewIndexFromPresets,
getValidPseudoStates,
getValidViewportStates,
getVariationClassName,
isVariationWithProperties,
removePropertiesFromObject
};
//# sourceMappingURL=utils.mjs.map