index.js
54 lines
| 1 | /** |
| 2 | * WordPress dependencies |
| 3 | */ |
| 4 | import { __ } from '@wordpress/i18n' |
| 5 | const {InnerBlocks} = wp.blockEditor; |
| 6 | const {useEffect} = wp.element; |
| 7 | const {select, dispatch} = wp.data; |
| 8 | |
| 9 | const edit = ({isSelected, clientId}) => { |
| 10 | // When adding a new Multi-Form Goal block, select the inner Progress Bar block by default |
| 11 | useEffect(() => { |
| 12 | if (isSelected) { |
| 13 | selectProgressBar(); |
| 14 | } |
| 15 | }, []); |
| 16 | |
| 17 | const selectProgressBar = () => { |
| 18 | const parentBlock = select('core/editor').getBlocksByClientId(clientId)[0]; |
| 19 | const progressBarBlock = parentBlock.innerBlocks[parentBlock.innerBlocks.length - 1]; |
| 20 | dispatch('core/block-editor').selectBlock(progressBarBlock.clientId); |
| 21 | }; |
| 22 | |
| 23 | const blockTemplate = [ |
| 24 | [ |
| 25 | 'core/media-text', |
| 26 | { |
| 27 | imageFill: true, |
| 28 | }, |
| 29 | [ |
| 30 | [ |
| 31 | 'core/heading', |
| 32 | { |
| 33 | placeholder: __('Heading', 'give'), |
| 34 | }, |
| 35 | ], |
| 36 | [ |
| 37 | 'core/paragraph', |
| 38 | { |
| 39 | placeholder: __('Summary', 'give'), |
| 40 | }, |
| 41 | ], |
| 42 | ], |
| 43 | ], |
| 44 | ['give/progress-bar', {}], |
| 45 | ]; |
| 46 | |
| 47 | return ( |
| 48 | <div className="give-multi-form-goal-block"> |
| 49 | <InnerBlocks template={blockTemplate} templateLock="all" /> |
| 50 | </div> |
| 51 | ); |
| 52 | }; |
| 53 | export default edit; |
| 54 |