UNPKG

@wordpress/global-styles-ui

Version:
1 lines • 8.62 kB
{"version":3,"file":"preview-styles.cjs","names":[],"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;CAExD,IAAK,QAAQ,WAAW,GACvB,OAAO;CAGR,QAAQ,MAAQ,GAAG,MAAO,KAAK,IAAK,QAAQ,CAAE,IAAI,KAAK,IAAK,QAAQ,CAAE,CAAE;CAExE,OAAO,QAAS;AACjB;AAEA,MAAM,uBAA6D;CAClE,QAAQ;CACR,MAAM;AACP;AAEA,SAAS,cAAe,QAA+C;CACtE,OACC,WAAW,KAAA,KACX,OAAO,SAAU,MAAO,KACxB,UAAU,KACV,UAAU;AAEZ;AAMA,SAAS,kBAAmB,YAA+C;CAC1E,MAAM,UAAU,OAAQ,cAAc,EAAG,CAAC,CACxC,KAAK,CAAC,CACN,YAAY,CAAC,CACb,MAAO,KAAM,CAAC,CACd,OAAQ,OAAQ,CAAC,CACjB,KAAO,UAAW,qBAAsB,UAAW,OAAQ,KAAM,CAAE;CAErE,MAAM,CAAE,OAAO,OAAQ;CAEvB,IAAK,CAAE,cAAe,KAAM,GAC3B,OAAO;CAGR,IAAK,QAAQ,WAAW,KAAK,CAAE,cAAe,GAAI,GACjD,OAAO,OAAQ,KAAM;CAGtB,OAAO,OAAQ,KAAK,IAAK,KAAK,IAAK,KAAK,KAAM,GAAG,GAAI,CAAE;AACxD;AAmBA,SAAgB,iBAAkB,OAAgB;CAQjD,MAAM,QACL;CACD,MAAM,SAAS,MAAM,KAAK;CAE1B,MAAM,c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