@wordpress/global-styles-ui
Version:
Global Styles UI components for WordPress.
8 lines (7 loc) • 8.8 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/font-library/utils/preview-styles.ts"],
"sourcesContent": ["import type { CSSProperties } from 'react';\nimport type { FontFace, FontFamily } from '@wordpress/core-data';\n\nfunction findNearest( input: number, numbers: number[] ) {\n\t// If the numbers array is empty, return null\n\tif ( numbers.length === 0 ) {\n\t\treturn null;\n\t}\n\t// Sort the array based on the absolute difference with the input\n\tnumbers.sort( ( a, b ) => Math.abs( input - a ) - Math.abs( input - b ) );\n\t// Return the first element (which will be the nearest) from the sorted array\n\treturn numbers[ 0 ];\n}\n\nconst FONT_WEIGHT_KEYWORDS: Record< string, number | undefined > = {\n\tnormal: 400,\n\tbold: 700,\n};\n\nfunction isValidWeight( weight: number | undefined ): weight is number {\n\treturn (\n\t\tweight !== undefined &&\n\t\tNumber.isFinite( weight ) &&\n\t\tweight >= 1 &&\n\t\tweight <= 1000\n\t);\n}\n\n/*\n * Resolve a font-weight range (e.g. \"200 900\") to a single value.\n * Ranges that cover 400 resolve to 400; otherwise to the closest end.\n */\nfunction resolveFontWeight( fontWeight: FontFace[ 'fontWeight' ] ): string {\n\tconst weights = String( fontWeight ?? '' )\n\t\t.trim()\n\t\t.toLowerCase()\n\t\t.split( /\\s+/ )\n\t\t.filter( Boolean )\n\t\t.map( ( value ) => FONT_WEIGHT_KEYWORDS[ value ] ?? Number( value ) );\n\n\tconst [ start, end ] = weights;\n\n\tif ( ! isValidWeight( start ) ) {\n\t\treturn '400';\n\t}\n\n\tif ( weights.length !== 2 || ! isValidWeight( end ) ) {\n\t\treturn String( start );\n\t}\n\n\treturn String( Math.min( Math.max( 400, start ), end ) );\n}\n\n/*\n * Format the font family to use in the CSS font-family property of a CSS rule.\n *\n * The input can be a string with the font family name or a string with multiple font family names separated by commas.\n * It follows the recommendations from the CSS Fonts Module Level 4.\n * https://www.w3.org/TR/css-fonts-4/#font-family-prop\n *\n * @param {string} input - The font family.\n * @return {string} The formatted font family.\n *\n * Example:\n * formatFontFamily( \"Open Sans, Font+Name, sans-serif\" ) => '\"Open Sans\", \"Font+Name\", sans-serif'\n * formatFontFamily( \"'Open Sans', generic(kai), sans-serif\" ) => '\"Open Sans\", generic(kai), sans-serif'\n * formatFontFamily( \"DotGothic16, Slabo 27px, serif\" ) => '\"DotGothic16\", \"Slabo 27px\", serif'\n * formatFontFamily( \"Mine's, Moe's Typography\" ) => `\"Mine's\", \"Moe's Typography\"`\n * formatFontFamily( \"var(--my-font), sans-serif\" ) => 'var(--my-font), sans-serif'\n */\nexport function formatFontFamily( input: string ) {\n\t// Matches anything that has to be quoted to be a valid font family name.\n\t// Left alone: a bare run of letters and hyphens, which covers the generic\n\t// keywords such as `sans-serif`; `generic(kai)`; and a reference to a\n\t// custom property such as `var(--wp--preset--font-family--body)`. The last\n\t// two are CSS function calls rather than names, so quoting either one\n\t// would stop it resolving.\n\t// TODO: The regex was scoped to `var(--name )` and not things like `var(--name, fallback)`. That'll require more string parsing.\n\tconst regex =\n\t\t/^(?!generic\\([ a-zA-Z\\-]+\\)$)(?!var\\(\\s*--[\\w-]+\\s*\\)$)(?!^[a-zA-Z\\-]+$).+/;\n\tconst output = input.trim();\n\n\tconst formatItem = ( item: string ) => {\n\t\titem = item.trim();\n\t\tif ( item.match( regex ) ) {\n\t\t\t// removes leading and trailing quotes.\n\t\t\titem = item.replace( /^[\"']|[\"']$/g, '' );\n\t\t\treturn `\"${ item }\"`;\n\t\t}\n\t\treturn item;\n\t};\n\n\tif ( output.includes( ',' ) ) {\n\t\treturn output\n\t\t\t.split( ',' )\n\t\t\t.map( formatItem )\n\t\t\t.filter( ( item ) => item !== '' )\n\t\t\t.join( ', ' );\n\t}\n\n\treturn formatItem( output );\n}\n\n/*\n * Format the font face name to use in the font-family property of a font face.\n *\n * The input can be a string with the font face name or a string with multiple font face names separated by commas.\n * It removes the leading and trailing quotes from the font face name.\n *\n * @param {string} input - The font face name.\n * @return {string} The formatted font face name.\n *\n * Example:\n * formatFontFaceName(\"Open Sans\") => \"Open Sans\"\n * formatFontFaceName(\"'Open Sans', sans-serif\") => \"Open Sans\"\n * formatFontFaceName(\", 'Open Sans', 'Helvetica Neue', sans-serif\") => \"Open Sans\"\n */\nexport function formatFontFaceName( input: string ) {\n\tif ( ! input ) {\n\t\treturn '';\n\t}\n\n\tlet output = input.trim();\n\tif ( output.includes( ',' ) ) {\n\t\toutput = (\n\t\t\toutput\n\t\t\t\t.split( ',' )\n\t\t\t\t// finds the first item that is not an empty string.\n\t\t\t\t.find( ( item ) => item.trim() !== '' ) ?? ''\n\t\t).trim();\n\t}\n\t// removes leading and trailing quotes.\n\toutput = output.replace( /^[\"']|[\"']$/g, '' );\n\n\t// Firefox needs the font name to be wrapped in double quotes meanwhile other browsers don't.\n\tif ( window.navigator.userAgent.toLowerCase().includes( 'firefox' ) ) {\n\t\toutput = `\"${ output }\"`;\n\t}\n\treturn output;\n}\n\nexport function getFamilyPreviewStyle(\n\tfamily: FontFamily | FontFace\n): CSSProperties {\n\tconst style: CSSProperties = {\n\t\tfontFamily: formatFontFamily( family.fontFamily ),\n\t};\n\n\tif ( ! ( 'fontFace' in family ) || ! Array.isArray( family.fontFace ) ) {\n\t\tstyle.fontWeight = '400';\n\t\tstyle.fontStyle = 'normal';\n\t\treturn style;\n\t}\n\n\tif ( family.fontFace ) {\n\t\t//get all the font faces with normal style\n\t\tconst normalFaces = family.fontFace.filter(\n\t\t\t( face ) =>\n\t\t\t\tface?.fontStyle && face.fontStyle.toLowerCase() === 'normal'\n\t\t);\n\t\tif ( normalFaces.length > 0 ) {\n\t\t\tstyle.fontStyle = 'normal';\n\t\t\tconst normalWeights = normalFaces.map( ( face ) =>\n\t\t\t\tNumber( resolveFontWeight( face.fontWeight ) )\n\t\t\t);\n\t\t\tstyle.fontWeight = String(\n\t\t\t\tfindNearest( 400, normalWeights ) ?? 400\n\t\t\t);\n\t\t} else {\n\t\t\tstyle.fontStyle =\n\t\t\t\t( family.fontFace.length && family.fontFace[ 0 ].fontStyle ) ||\n\t\t\t\t'normal';\n\t\t\tstyle.fontWeight =\n\t\t\t\t( family.fontFace.length &&\n\t\t\t\t\tresolveFontWeight( family.fontFace[ 0 ].fontWeight ) ) ||\n\t\t\t\t'400';\n\t\t}\n\t}\n\n\treturn style;\n}\n\nexport function getFacePreviewStyle( face: FontFace ): CSSProperties {\n\treturn {\n\t\tfontFamily: formatFontFamily( face.fontFamily ),\n\t\tfontStyle: face.fontStyle || 'normal',\n\t\tfontWeight: resolveFontWeight( face.fontWeight ),\n\t};\n}\n"],
"mappings": ";AAGA,SAAS,YAAa,OAAe,SAAoB;AAExD,MAAK,QAAQ,WAAW,GAAI;AAC3B,WAAO;AAAA,EACR;AAEA,UAAQ,KAAM,CAAE,GAAG,MAAO,KAAK,IAAK,QAAQ,CAAE,IAAI,KAAK,IAAK,QAAQ,CAAE,CAAE;AAExE,SAAO,QAAS,CAAE;AACnB;AAEA,IAAM,uBAA6D;AAAA,EAClE,QAAQ;AAAA,EACR,MAAM;AACP;AAEA,SAAS,cAAe,QAA+C;AACtE,SACC,WAAW,UACX,OAAO,SAAU,MAAO,KACxB,UAAU,KACV,UAAU;AAEZ;AAMA,SAAS,kBAAmB,YAA+C;AAC1E,QAAM,UAAU,OAAQ,cAAc,EAAG,EACvC,KAAK,EACL,YAAY,EACZ,MAAO,KAAM,EACb,OAAQ,OAAQ,EAChB,IAAK,CAAE,UAAW,qBAAsB,KAAM,KAAK,OAAQ,KAAM,CAAE;AAErE,QAAM,CAAE,OAAO,GAAI,IAAI;AAEvB,MAAK,CAAE,cAAe,KAAM,GAAI;AAC/B,WAAO;AAAA,EACR;AAEA,MAAK,QAAQ,WAAW,KAAK,CAAE,cAAe,GAAI,GAAI;AACrD,WAAO,OAAQ,KAAM;AAAA,EACtB;AAEA,SAAO,OAAQ,KAAK,IAAK,KAAK,IAAK,KAAK,KAAM,GAAG,GAAI,CAAE;AACxD;AAmBO,SAAS,iBAAkB,O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