secure-custom-fields
/
assets
/
src
/
js
/
pro
/
blocks-v3
/
high-order-components
/
with-align-text.js
with-align-text.js
60 lines
| 1 | /** |
| 2 | * withAlignText Higher-Order Component |
| 3 | * Adds text alignment toolbar controls to ACF blocks |
| 4 | */ |
| 5 | |
| 6 | const { Fragment, Component } = wp.element; |
| 7 | const { BlockControls, AlignmentToolbar } = wp.blockEditor; |
| 8 | |
| 9 | /** |
| 10 | * Gets the default text alignment based on RTL setting |
| 11 | * |
| 12 | * @param {string} alignment - Current alignment value |
| 13 | * @returns {string} - Normalized alignment value (left, center, or right) |
| 14 | */ |
| 15 | const getDefaultAlignment = ( alignment ) => { |
| 16 | const defaultAlign = acf.get( 'rtl' ) ? 'right' : 'left'; |
| 17 | return [ 'left', 'center', 'right' ].includes( alignment ) |
| 18 | ? alignment |
| 19 | : defaultAlign; |
| 20 | }; |
| 21 | |
| 22 | /** |
| 23 | * Higher-order component that adds text alignment controls |
| 24 | * Wraps a block component and adds AlignmentToolbar to BlockControls |
| 25 | * |
| 26 | * @param {React.Component} BlockComponent - The component to wrap |
| 27 | * @param {Object} blockConfig - ACF block configuration |
| 28 | * @returns {React.Component} - Enhanced component with text alignment controls |
| 29 | */ |
| 30 | export const withAlignText = ( BlockComponent, blockConfig ) => { |
| 31 | const normalizeAlignment = getDefaultAlignment; |
| 32 | |
| 33 | // Set default alignment on block config |
| 34 | blockConfig.alignText = normalizeAlignment( blockConfig.alignText ); |
| 35 | |
| 36 | return class extends Component { |
| 37 | render() { |
| 38 | const { attributes, setAttributes } = this.props; |
| 39 | const { alignText } = attributes; |
| 40 | |
| 41 | return ( |
| 42 | <Fragment> |
| 43 | <BlockControls group="block"> |
| 44 | <AlignmentToolbar |
| 45 | value={ normalizeAlignment( alignText ) } |
| 46 | onChange={ function ( newAlignment ) { |
| 47 | setAttributes( { |
| 48 | alignText: |
| 49 | normalizeAlignment( newAlignment ), |
| 50 | } ); |
| 51 | } } |
| 52 | /> |
| 53 | </BlockControls> |
| 54 | <BlockComponent { ...this.props } /> |
| 55 | </Fragment> |
| 56 | ); |
| 57 | } |
| 58 | }; |
| 59 | }; |
| 60 |