UNPKG

@wordpress/components

Version:
1 lines • 4.05 kB
{"version":3,"file":"utils.cjs","names":["__"],"sources":["../../src/alignment-matrix-control/utils.tsx"],"sourcesContent":["import { __ } from '@wordpress/i18n';\nimport type { AlignmentMatrixControlValue } from './types';\n\nexport const GRID: AlignmentMatrixControlValue[][] = [\n\t[ 'top left', 'top center', 'top right' ],\n\t[ 'center left', 'center center', 'center right' ],\n\t[ 'bottom left', 'bottom center', 'bottom right' ],\n];\n\n// Stored as map as i18n __() only accepts strings (not variables)\nexport const ALIGNMENT_LABEL: Record< AlignmentMatrixControlValue, string > = {\n\t'top left': __( 'Top Left' ),\n\t'top center': __( 'Top Center' ),\n\t'top right': __( 'Top Right' ),\n\t'center left': __( 'Center Left' ),\n\t'center center': __( 'Center' ),\n\tcenter: __( 'Center' ),\n\t'center right': __( 'Center Right' ),\n\t'bottom left': __( 'Bottom Left' ),\n\t'bottom center': __( 'Bottom Center' ),\n\t'bottom right': __( 'Bottom Right' ),\n};\n\n// Transforms GRID into a flat Array of values.\nexport const ALIGNMENTS = GRID.flat();\n\n/**\n * Normalizes and transforms an incoming value to better match the alignment values\n *\n * @param value An alignment value to parse.\n *\n * @return The parsed value.\n */\nfunction normalize( value?: string | null ) {\n\tconst normalized = value === 'center' ? 'center center' : value;\n\n\t// Strictly speaking, this could be `string | null | undefined`,\n\t// but will be validated shortly, so we're typecasting to an\n\t// `AlignmentMatrixControlValue` to keep TypeScript happy.\n\tconst transformed = normalized?.replace(\n\t\t'-',\n\t\t' '\n\t) as AlignmentMatrixControlValue;\n\n\treturn ALIGNMENTS.includes( transformed ) ? transformed : undefined;\n}\n\n/**\n * Creates an item ID based on a prefix ID and an alignment value.\n *\n * @param prefixId An ID to prefix.\n * @param value An alignment value.\n *\n * @return The item id.\n */\nexport function getItemId(\n\tprefixId: string,\n\tvalue?: AlignmentMatrixControlValue\n) {\n\tconst normalized = normalize( value );\n\tif ( ! normalized ) {\n\t\treturn;\n\t}\n\n\tconst id = normalized.replace( ' ', '-' );\n\treturn `${ prefixId }-${ id }`;\n}\n\n/**\n * Extracts an item value from its ID\n *\n * @param prefixId An ID prefix to remove\n * @param id An item ID\n * @return The item value\n */\nexport function getItemValue( prefixId: string, id?: string | null ) {\n\tconst value = id?.replace( prefixId + '-', '' );\n\treturn normalize( value );\n}\n\n/**\n * Retrieves the alignment index from a value.\n *\n * @param alignment Value to check.\n *\n * @return The index of a matching alignment.\n */\nexport function getAlignmentIndex(\n\talignment: AlignmentMatrixControlValue = 'center'\n) {\n\tconst normalized = normalize( alignment );\n\tif ( ! normalized ) {\n\t\treturn undefined;\n\t}\n\n\tconst index = ALIGNMENTS.indexOf( normalized );\n\treturn index > -1 ? index : undefined;\n}\n"],"mappings":";;;;;;;AAGA,MAAa,OAAwC;CACpD;EAAE;EAAY;EAAc;CAAY;CACxC;EAAE;EAAe;EAAiB;CAAe;CACjD;EAAE;EAAe;EAAiB;CAAe;AAClD;AAGA,MAAa,kBAAiE;CAC7E,aAAA,GAAYA,gBAAAA,GAAAA,CAAI,UAAW;CAC3B,eAAA,GAAcA,gBAAAA,GAAAA,CAAI,YAAa;CAC/B,cAAA,GAAaA,gBAAAA,GAAAA,CAAI,WAAY;CAC7B,gBAAA,GAAeA,gBAAAA,GAAAA,CAAI,aAAc;CACjC,kBAAA,GAAiBA,gBAAAA,GAAAA,CAAI,QAAS;CAC9B,SAAA,GAAQA,gBAAAA,GAAAA,CAAI,QAAS;CACrB,iBAAA,GAAgBA,gBAAAA,GAAAA,CAAI,cAAe;CACnC,gBAAA,GAAeA,gBAAAA,GAAAA,CAAI,aAAc;CACjC,kBAAA,GAAiBA,gBAAAA,GAAAA,CAAI,eAAgB;CACrC,iBAAA,GAAgBA,gBAAAA,GAAAA,CAAI,cAAe;AACpC;AAGA,MAAa,aAAa,KAAK,KAAK;;;;;;;;AASpC,SAAS,UAAW,OAAwB;CAM3C,MAAM,eALa,UAAU,WAAW,kBAAkB,MAAA,EAK1B,QAC/B,KACA,GACD;CAEA,OAA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