| 1 |
/** |
| 2 |
* Editor registration for the standalone single-property section blocks. |
| 3 |
* |
| 4 |
* Plain ES5, no build step. Loops the localized MLSImportSections list and |
| 5 |
* registers one dynamic block per section. Each block renders server-side via |
| 6 |
* ServerSideRender (the PHP render_callback is the single source of markup). |
| 7 |
*/ |
| 8 |
( function ( blocks, element, serverSideRender, blockEditor ) { |
| 9 |
// Bail out if the block API or the localized section list isn't available |
| 10 |
if ( ! blocks || ! window.MLSImportSections ) { |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
// Local aliases and the block icon source |
| 15 |
var el = element.createElement; |
| 16 |
var useBlockProps = ( blockEditor || {} ).useBlockProps; |
| 17 |
var iconUrl = window.MLSImportSectionsIcon || ''; |
| 18 |
|
| 19 |
// Block toolbar/menu icon: the localized image URL, else a Dashicon fallback. |
| 20 |
// The PNG is 19x14, so it keeps its natural size (width/height auto beats the |
| 21 |
// width=24 height=24 attributes wp.components.Icon clones onto it) — a forced |
| 22 |
// square stretched it to 24x24 and blurred it. |
| 23 |
var icon = iconUrl |
| 24 |
? el( 'img', { src: iconUrl, alt: '', style: { width: 'auto', height: 'auto' } } ) |
| 25 |
: 'admin-home'; |
| 26 |
|
| 27 |
// Register one dynamic block per localized property section |
| 28 |
window.MLSImportSections.forEach( function ( section ) { |
| 29 |
// Namespaced block name, e.g. "mlsimport/property-gallery" |
| 30 |
var blockName = 'mlsimport/property-' + section.slug; |
| 31 |
|
| 32 |
blocks.registerBlockType( blockName, { |
| 33 |
apiVersion: 2, |
| 34 |
title: section.title, |
| 35 |
icon: icon, |
| 36 |
category: 'mlsimport-property', |
| 37 |
attributes: { |
| 38 |
// Optional explicit property id; 0 means "use the current post" |
| 39 |
id: { type: 'number', default: 0 } |
| 40 |
}, |
| 41 |
// Editor view: a server-rendered preview of the section |
| 42 |
edit: function ( props ) { |
| 43 |
// apiVersion 2 requires useBlockProps() on the edit root, or the |
| 44 |
// block has no selectable wrapper in the editor. |
| 45 |
var blockProps = useBlockProps ? useBlockProps() : {}; |
| 46 |
// Render the PHP markup for this block/attributes inside the wrapper |
| 47 |
return el( 'div', blockProps, el( serverSideRender, { |
| 48 |
block: blockName, |
| 49 |
attributes: props.attributes |
| 50 |
} ) ); |
| 51 |
}, |
| 52 |
// Dynamic block: markup comes from PHP, so nothing is saved to post content |
| 53 |
save: function () { |
| 54 |
return null; |
| 55 |
} |
| 56 |
} ); |
| 57 |
} ); |
| 58 |
} )( window.wp.blocks, window.wp.element, window.wp.serverSideRender, window.wp.blockEditor ); |
| 59 |
|