BlockControls.js
2 years ago
ButtonContentRenderer.js
2 years ago
ComponentCSS.js
1 year ago
ConditionalColors.js
2 years ago
InspectorAdvancedControls.js
3 years ago
ConditionalColors.js
106 lines
| 1 | import { __ } from '@wordpress/i18n'; |
| 2 | import { addFilter, applyFilters } from '@wordpress/hooks'; |
| 3 | |
| 4 | function shouldShowCurrentColors( props ) { |
| 5 | const { |
| 6 | attributes, |
| 7 | } = props; |
| 8 | |
| 9 | const { |
| 10 | useDynamicData, |
| 11 | dynamicContentType, |
| 12 | } = attributes; |
| 13 | |
| 14 | return applyFilters( |
| 15 | 'generateblocks.editor.addButtonCurrentColors', |
| 16 | useDynamicData && 'pagination-numbers' === dynamicContentType, |
| 17 | props |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | function AddColorItems( items, props ) { |
| 22 | const { |
| 23 | name, |
| 24 | } = props; |
| 25 | |
| 26 | if ( 'generateblocks/button' !== name ) { |
| 27 | return items; |
| 28 | } |
| 29 | |
| 30 | const addCurrentColors = shouldShowCurrentColors( props ); |
| 31 | |
| 32 | if ( addCurrentColors ) { |
| 33 | const newItems = [ |
| 34 | { |
| 35 | group: 'background', |
| 36 | attribute: 'backgroundColorCurrent', |
| 37 | tooltip: __( 'Current', 'generateblocks' ), |
| 38 | alpha: true, |
| 39 | }, |
| 40 | { |
| 41 | group: 'text', |
| 42 | attribute: 'textColorCurrent', |
| 43 | tooltip: __( 'Current', 'generateblocks' ), |
| 44 | alpha: false, |
| 45 | }, |
| 46 | ]; |
| 47 | |
| 48 | items.forEach( ( colorItem, index ) => { |
| 49 | newItems.forEach( ( newColorItem ) => { |
| 50 | if ( |
| 51 | newColorItem.group === colorItem.group && |
| 52 | ! colorItem.items.some( ( item ) => item.attribute === newColorItem.attribute ) |
| 53 | ) { |
| 54 | items[ index ].items.push( |
| 55 | { |
| 56 | tooltip: newColorItem.tooltip, |
| 57 | attribute: newColorItem.attribute, |
| 58 | alpha: newColorItem.alpha, |
| 59 | } |
| 60 | ); |
| 61 | } |
| 62 | } ); |
| 63 | } ); |
| 64 | } |
| 65 | |
| 66 | return items; |
| 67 | } |
| 68 | |
| 69 | addFilter( |
| 70 | 'generateblocks.editor.colorGroupItems', |
| 71 | 'generateblocks/button-colors/add-conditional-color-items', |
| 72 | AddColorItems |
| 73 | ); |
| 74 | |
| 75 | function addBorderCurrent( context, props ) { |
| 76 | const { |
| 77 | name, |
| 78 | } = props; |
| 79 | |
| 80 | if ( 'generateblocks/button' !== name ) { |
| 81 | return context; |
| 82 | } |
| 83 | |
| 84 | const addCurrentColors = shouldShowCurrentColors( props ); |
| 85 | |
| 86 | if ( addCurrentColors ) { |
| 87 | const existingColors = context.supports.borders.borderColors; |
| 88 | |
| 89 | if ( ! existingColors.some( ( e ) => 'Current' === e.state ) ) { |
| 90 | context.supports.borders.borderColors.push( { |
| 91 | state: 'Current', |
| 92 | tooltip: __( 'Border Current', 'generateblocks' ), |
| 93 | alpha: true, |
| 94 | } ); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | return context; |
| 99 | } |
| 100 | |
| 101 | addFilter( |
| 102 | 'generateblocks.editor.blockContext', |
| 103 | 'generateblocks/button-context/add-current-border-color', |
| 104 | addBorderCurrent |
| 105 | ); |
| 106 |