|
| 1 | +/** |
| 2 | + * WordPress dependencies. |
| 3 | + */ |
| 4 | +import { __ } from '@wordpress/i18n'; |
| 5 | + |
| 6 | +import { createBlocksFromInnerBlocksTemplate } from '@wordpress/blocks'; |
| 7 | + |
| 8 | +import { |
| 9 | + store as blockEditorStore, |
| 10 | + BlockPreview, |
| 11 | +} from '@wordpress/block-editor'; |
| 12 | + |
| 13 | +import { Modal } from '@wordpress/components'; |
| 14 | + |
| 15 | +import { useDispatch, useSelect } from '@wordpress/data'; |
| 16 | + |
| 17 | +/** |
| 18 | + * Internal dependencies. |
| 19 | + */ |
| 20 | +import variations from '../variations'; |
| 21 | + |
| 22 | +const PatternSelector = ({ setOpen }) => { |
| 23 | + const currentBlock = useSelect((select) => |
| 24 | + select(blockEditorStore).getSelectedBlock() |
| 25 | + ); |
| 26 | + |
| 27 | + const { clearSelectedBlock, replaceBlock } = useDispatch(blockEditorStore); |
| 28 | + |
| 29 | + return ( |
| 30 | + <Modal |
| 31 | + title={__('Choose a Pattern', 'feedzy-rss-feeds')} |
| 32 | + onRequestClose={() => setOpen(false)} |
| 33 | + size="fill" |
| 34 | + > |
| 35 | + <div className="fz-pattern-selector"> |
| 36 | + {variations.map((variation) => { |
| 37 | + const block = { |
| 38 | + ...currentBlock, |
| 39 | + attributes: { |
| 40 | + feed: currentBlock?.attributes?.feed, |
| 41 | + ...variation?.attributes, |
| 42 | + }, |
| 43 | + innerBlocks: createBlocksFromInnerBlocksTemplate( |
| 44 | + variation?.innerBlocks |
| 45 | + ), |
| 46 | + }; |
| 47 | + |
| 48 | + const onClick = () => { |
| 49 | + replaceBlock(currentBlock.clientId, block); |
| 50 | + clearSelectedBlock(); |
| 51 | + setOpen(false); |
| 52 | + }; |
| 53 | + |
| 54 | + return ( |
| 55 | + <div |
| 56 | + key={variation.name} |
| 57 | + onClick={onClick} |
| 58 | + onKeyDown={(e) => { |
| 59 | + if (e.key === 'Enter' || e.key === ' ') { |
| 60 | + onClick(); |
| 61 | + } |
| 62 | + }} |
| 63 | + role="button" |
| 64 | + tabIndex="0" |
| 65 | + className="fz-pattern" |
| 66 | + > |
| 67 | + <BlockPreview blocks={block} viewportWidth={1400} /> |
| 68 | + |
| 69 | + <div>{variation.title}</div> |
| 70 | + </div> |
| 71 | + ); |
| 72 | + })} |
| 73 | + </div> |
| 74 | + </Modal> |
| 75 | + ); |
| 76 | +}; |
| 77 | + |
| 78 | +export default PatternSelector; |
0 commit comments