UNPKG

@wordpress/editor

Version:
8 lines (7 loc) 3.87 kB
{ "version": 3, "sources": ["../../../src/hooks/push-changes-to-global-styles/use-review-rows.js"], "sourcesContent": ["import { useMemo } from '@wordpress/element';\nimport {\n\tgetStyle,\n\tgetValueFromVariable,\n} from '@wordpress/global-styles-engine';\nimport { getStyleLabel } from './style-labels';\nimport {\n\tformatStyleValue,\n\tformatBorderShorthand,\n\tformatBorderRadius,\n\tformatSpacingShorthand,\n\tformatBlockGap,\n} from './format-style-value';\n\n/**\n * Turns a raw style value into readable text, using the row's `format` hint so\n * borders and spacing show as a single CSS value instead of a raw object.\n *\n * @param {string} format Optional format hint (`border`, `borderRadius`,\n * `spacing`, `blockGap`).\n * @param {*} value The raw style value.\n * @param {Function} resolve Callback to resolve preset values (used for spacing).\n *\n * @return {string} Readable text for the value.\n */\nfunction formatReviewValue( format, value, resolve ) {\n\tif ( format === 'border' ) {\n\t\treturn formatBorderShorthand( value );\n\t}\n\tif ( format === 'borderRadius' ) {\n\t\treturn formatBorderRadius( value );\n\t}\n\tif ( format === 'spacing' ) {\n\t\treturn formatSpacingShorthand( value, resolve );\n\t}\n\tif ( format === 'blockGap' ) {\n\t\treturn formatBlockGap( value, resolve );\n\t}\n\treturn formatStyleValue( value );\n}\n\n/**\n * Adds display details to each change row: a readable label, the block type's\n * current Global Styles value, and readable versions of the current and new\n * values.\n *\n * @param {Array} rows Grouped rows from `useChangesToPush`.\n * @param {Object} merged Merged Global Styles config.\n * @param {string} name Block name.\n *\n * @return {Array} Rows with `label`, `currentValue`, `formattedCurrentValue`\n * and `formattedNewValue` added.\n */\nexport function useReviewRows( rows, merged, name ) {\n\treturn useMemo( () => {\n\t\t// Swaps preset values (e.g. `var:preset|spacing|40`) for their real\n\t\t// size so spacing reads as a size rather than a preset name.\n\t\tconst resolve = ( value ) =>\n\t\t\tgetValueFromVariable( merged, name, value );\n\n\t\treturn rows.map( ( row ) => {\n\t\t\tconst currentValue = getStyle(\n\t\t\t\tmerged,\n\t\t\t\trow.primaryPath.join( '.' ),\n\t\t\t\tname,\n\t\t\t\t// Keep preset values encoded (e.g. `var:preset|color|vivid-red`)\n\t\t\t\t// so they show by name, matching the new value.\n\t\t\t\tfalse\n\t\t\t);\n\t\t\treturn {\n\t\t\t\t...row,\n\t\t\t\tlabel: getStyleLabel( row.primaryPath ),\n\t\t\t\tcurrentValue,\n\t\t\t\tformattedCurrentValue: formatReviewValue(\n\t\t\t\t\trow.format,\n\t\t\t\t\tcurrentValue,\n\t\t\t\t\tresolve\n\t\t\t\t),\n\t\t\t\tformattedNewValue: formatReviewValue(\n\t\t\t\t\trow.format,\n\t\t\t\t\trow.newValue,\n\t\t\t\t\tresolve\n\t\t\t\t),\n\t\t\t};\n\t\t} );\n\t}, [ rows, merged, name ] );\n}\n"], "mappings": ";AAAA,SAAS,eAAe;AACxB;AAAA,EACC;AAAA,EACA;AAAA,OACM;AACP,SAAS,qBAAqB;AAC9B;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAaP,SAAS,kBAAmB,QAAQ,OAAO,SAAU;AACpD,MAAK,WAAW,UAAW;AAC1B,WAAO,sBAAuB,KAAM;AAAA,EACrC;AACA,MAAK,WAAW,gBAAiB;AAChC,WAAO,mBAAoB,KAAM;AAAA,EAClC;AACA,MAAK,WAAW,WAAY;AAC3B,WAAO,uBAAwB,OAAO,OAAQ;AAAA,EAC/C;AACA,MAAK,WAAW,YAAa;AAC5B,WAAO,eAAgB,OAAO,OAAQ;AAAA,EACvC;AACA,SAAO,iBAAkB,KAAM;AAChC;AAcO,SAAS,cAAe,MAAM,QAAQ,MAAO;AACnD,SAAO,QAAS,MAAM;AAGrB,UAAM,UAAU,CAAE,UACjB,qBAAsB,QAAQ,MAAM,KAAM;AAE3C,WAAO,KAAK,IAAK,CAAE,QAAS;AAC3B,YAAM,eAAe;AAAA,QACpB;AAAA,QACA,IAAI,YAAY,KAAM,GAAI;AAAA,QAC1B;AAAA;AAAA;AAAA,QAGA;AAAA,MACD;AACA,aAAO;AAAA,QACN,GAAG;AAAA,QACH,OAAO,cAAe,IAAI,WAAY;AAAA,QACtC;AAAA,QACA,uBAAuB;AAAA,UACtB,IAAI;AAAA,UACJ;AAAA,UACA;AAAA,QACD;AAAA,QACA,mBAAmB;AAAA,UAClB,IAAI;AAAA,UACJ,IAAI;AAAA,UACJ;AAAA,QACD;AAAA,MACD;AAAA,IACD,CAAE;AAAA,EACH,GAAG,CAAE,MAAM,QAAQ,IAAK,CAAE;AAC3B;", "names": [] }