UNPKG

@wordpress/components

Version:
1 lines • 3.92 kB
{"version":3,"file":"serializer.cjs","names":[],"sources":["../../src/custom-gradient-picker/serializer.ts"],"sourcesContent":["import type gradientParser from 'gradient-parser';\nimport type { GradientAST } from './types';\n\nexport function serializeGradientColor( {\n\ttype,\n\tvalue,\n}: gradientParser.ColorStop ) {\n\tif ( type === 'literal' ) {\n\t\treturn value;\n\t}\n\tif ( type === 'hex' ) {\n\t\treturn `#${ value }`;\n\t}\n\tif ( type === 'var' ) {\n\t\treturn `var(${ value })`;\n\t}\n\tif ( type === 'hsl' ) {\n\t\tconst [ hue, saturation, lightness ] = value;\n\t\treturn `hsl(${ hue },${ saturation }%,${ lightness }%)`;\n\t}\n\tif ( type === 'hsla' ) {\n\t\tconst [ hue, saturation, lightness, alpha = '1' ] = value;\n\t\treturn `hsla(${ hue },${ saturation }%,${ lightness }%,${ alpha })`;\n\t}\n\treturn `${ type }(${ value.join( ',' ) })`;\n}\n\nexport function serializeGradientPosition(\n\tposition: gradientParser.ColorStop[ 'length' ]\n) {\n\tif ( ! position ) {\n\t\treturn '';\n\t}\n\tconst { value, type } = position;\n\tif ( type === 'calc' ) {\n\t\treturn `calc(${ value })`;\n\t}\n\treturn `${ value }${ type }`;\n}\n\nexport function serializeGradientColorStop( {\n\ttype,\n\tvalue,\n\tlength,\n}: gradientParser.ColorStop ) {\n\treturn `${ serializeGradientColor( {\n\t\ttype,\n\t\tvalue,\n\t} as gradientParser.ColorStop ) } ${ serializeGradientPosition( length ) }`;\n}\n\nexport function serializeGradientOrientation(\n\torientation: gradientParser.GradientNode[ 'orientation' ]\n) {\n\tif (\n\t\tArray.isArray( orientation ) ||\n\t\t! orientation ||\n\t\torientation.type !== 'angular'\n\t) {\n\t\treturn;\n\t}\n\treturn `${ orientation.value }${ orientation.unit }`;\n}\n\nexport function serializeGradient( {\n\ttype,\n\tcolorInterpolation,\n\torientation,\n\tcolorStops,\n}: GradientAST ) {\n\t// The color interpolation method shares the first argument with the\n\t// orientation, e.g. `linear-gradient(in oklch 160deg, …)`.\n\tconst serializedOrientation = [\n\t\tcolorInterpolation,\n\t\tserializeGradientOrientation( orientation ),\n\t]\n\t\t.filter( Boolean )\n\t\t.join( ' ' );\n\tconst serializedColorStops = colorStops\n\t\t.sort( ( colorStop1, colorStop2 ) => {\n\t\t\tconst getNumericStopValue = (\n\t\t\t\tcolorStop: gradientParser.ColorStop\n\t\t\t) => {\n\t\t\t\treturn colorStop?.length?.value === undefined\n\t\t\t\t\t? 0\n\t\t\t\t\t: parseInt( colorStop.length.value );\n\t\t\t};\n\n\t\t\treturn (\n\t\t\t\tgetNumericStopValue( colorStop1 ) -\n\t\t\t\tgetNumericStopValue( colorStop2 )\n\t\t\t);\n\t\t} )\n\t\t.map( serializeGradientColorStop );\n\treturn `${ type }(${ [ serializedOrientation, ...serializedColorStops ]\n\t\t.filter( Boolean )\n\t\t.join( ',' ) })`;\n}\n"],"mappings":";;;;;;AAGA,SAAgB,uBAAwB,EACvC,MACA,SAC6B;CAC7B,IAAK,SAAS,WACb,OAAO;CAER,IAAK,SAAS,OACb,OAAO,IAAK;CAEb,IAAK,SAAS,OACb,OAAO,OAAQ,MAAO;CAEvB,IAAK,SAAS,OAAQ;EACrB,MAAM,CAAE,KAAK,YAAY,aAAc;EACvC,OAAO,OAAQ,IAAK,GAAI,WAAY,IAAK,UAAW;CACrD;CACA,IAAK,SAAS,QAAS;EACtB,MAAM,CAAE,KAAK,YAAY,WAAW,QAAQ,OAAQ;EACpD,OAAO,QAAS,IAAK,GAAI,WAAY,IAAK,UAAW,IAAK,MAAO;CAClE;CACA,OAAO,GAAI,KAAM,GAAI,MAAM,KAAM,GAAI,EAAG;AACzC;AAEA,SAAgB,0BACf,UACC;CACD,IAAK,CAAE,UACN,OAAO;CAER,MAAM,EAAE,OAAO,SAAS;CACxB,IAAK,SAAS,QACb,OAAO,QAAS,MAAO;CAExB,OAAO,GAAI,QAAU;AACtB;AAEA,SAAgB,2BAA4B,EAC3C,MACA,O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