UNPKG

@wordpress/global-styles-ui

Version:
80 lines (79 loc) • 3.05 kB
"use strict"; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } }); // packages/global-styles-ui/src/font-library/utils/preview-styles.ts function findNearest(input, numbers) { if (numbers.length === 0) return null; numbers.sort((a, b) => Math.abs(input - a) - Math.abs(input - b)); return numbers[0]; } const FONT_WEIGHT_KEYWORDS = { normal: 400, bold: 700 }; function isValidWeight(weight) { return weight !== void 0 && Number.isFinite(weight) && weight >= 1 && weight <= 1e3; } function resolveFontWeight(fontWeight) { const weights = String(fontWeight ?? "").trim().toLowerCase().split(/\s+/).filter(Boolean).map((value) => FONT_WEIGHT_KEYWORDS[value] ?? Number(value)); const [start, end] = weights; if (!isValidWeight(start)) return "400"; if (weights.length !== 2 || !isValidWeight(end)) return String(start); return String(Math.min(Math.max(400, start), end)); } function formatFontFamily(input) { const regex = /^(?!generic\([ a-zA-Z\-]+\)$)(?!var\(\s*--[\w-]+\s*\)$)(?!^[a-zA-Z\-]+$).+/; const output = input.trim(); const formatItem = (item) => { item = item.trim(); if (item.match(regex)) { item = item.replace(/^["']|["']$/g, ""); return `"${item}"`; } return item; }; if (output.includes(",")) return output.split(",").map(formatItem).filter((item) => item !== "").join(", "); return formatItem(output); } function formatFontFaceName(input) { if (!input) return ""; let output = input.trim(); if (output.includes(",")) output = (output.split(",").find((item) => item.trim() !== "") ?? "").trim(); output = output.replace(/^["']|["']$/g, ""); if (window.navigator.userAgent.toLowerCase().includes("firefox")) output = `"${output}"`; return output; } function getFamilyPreviewStyle(family) { const style = { fontFamily: formatFontFamily(family.fontFamily) }; if (!("fontFace" in family) || !Array.isArray(family.fontFace)) { style.fontWeight = "400"; style.fontStyle = "normal"; return style; } if (family.fontFace) { const normalFaces = family.fontFace.filter((face) => face?.fontStyle && face.fontStyle.toLowerCase() === "normal"); if (normalFaces.length > 0) { style.fontStyle = "normal"; const normalWeights = normalFaces.map((face) => Number(resolveFontWeight(face.fontWeight))); style.fontWeight = String(findNearest(400, normalWeights) ?? 400); } else { style.fontStyle = family.fontFace.length && family.fontFace[0].fontStyle || "normal"; style.fontWeight = family.fontFace.length && resolveFontWeight(family.fontFace[0].fontWeight) || "400"; } } return style; } function getFacePreviewStyle(face) { return { fontFamily: formatFontFamily(face.fontFamily), fontStyle: face.fontStyle || "normal", fontWeight: resolveFontWeight(face.fontWeight) }; } exports.formatFontFaceName = formatFontFaceName; exports.formatFontFamily = formatFontFamily; exports.getFacePreviewStyle = getFacePreviewStyle; exports.getFamilyPreviewStyle = getFamilyPreviewStyle; //# sourceMappingURL=preview-styles.cjs.map