| 1 |
import { __, sprintf } from '@wordpress/i18n' |
| 2 |
import { registerBlockType } from '@wordpress/blocks' |
| 3 |
import { useDispatch } from '@wordpress/data' |
| 4 |
import { useEffect } from '@wordpress/element' |
| 5 |
import { brandBlockIcon } from '../../components/icons' |
| 6 |
import { setModalVisibility } from '../../util/general' |
| 7 |
import metadata from './block.json' |
| 8 |
|
| 9 |
export const openModal = (source) => setModalVisibility(source, 'open') |
| 10 |
|
| 11 |
registerBlockType(metadata, { |
| 12 |
icon: brandBlockIcon, |
| 13 |
example: { |
| 14 |
attributes: { |
| 15 |
preview: window.extendifyData.asset_path + '/preview.png', |
| 16 |
}, |
| 17 |
}, |
| 18 |
edit: function Edit({ clientId, attributes }) { |
| 19 |
const { removeBlock } = useDispatch('core/block-editor') |
| 20 |
useEffect(() => { |
| 21 |
if (attributes.preview) { |
| 22 |
return |
| 23 |
} |
| 24 |
openModal('library-block') |
| 25 |
removeBlock(clientId) |
| 26 |
}, [clientId, attributes, removeBlock]) |
| 27 |
return ( |
| 28 |
<img |
| 29 |
style={{ display: 'block', maxWidth: '100%' }} |
| 30 |
src={attributes.preview} |
| 31 |
alt={sprintf( |
| 32 |
__('%s Pattern Library', 'extendify'), |
| 33 |
'Extendify', |
| 34 |
)} |
| 35 |
/> |
| 36 |
) |
| 37 |
}, |
| 38 |
}) |
| 39 |
|