| 1 |
/** |
| 2 |
* Custom field icons + inserter hover previews via registerBlockType filter. |
| 3 |
* Preview short-circuits edit before BaseFieldEdit (no ID allocation REST). |
| 4 |
* Must load before blocks register. |
| 5 |
*/ |
| 6 |
import { addFilter } from '@wordpress/hooks'; |
| 7 |
import { useBlockProps } from '@wordpress/block-editor'; |
| 8 |
import { FIELD_ICONS } from './icons'; |
| 9 |
import { FIELD_PREVIEWS } from './previews'; |
| 10 |
|
| 11 |
// Example-only marker; never serialized on real blocks. |
| 12 |
const PREVIEW_ATTR = '__wppbPreview'; |
| 13 |
|
| 14 |
const FieldPreview = ( { name } ) => { |
| 15 |
// Inline geometry: inserter iframe styles load late and would reflow. |
| 16 |
const blockProps = useBlockProps( { |
| 17 |
className: 'wppb-fb-field-art-preview', |
| 18 |
style: { width: '224px', maxWidth: 'none', margin: '6px auto', padding: 0, color: '#1e1e1e' }, |
| 19 |
} ); |
| 20 |
return <div { ...blockProps }>{ FIELD_PREVIEWS[ name ] || null }</div>; |
| 21 |
}; |
| 22 |
|
| 23 |
addFilter( |
| 24 |
'blocks.registerBlockType', |
| 25 |
'profile-builder/field-art', |
| 26 |
( settings, name ) => { |
| 27 |
if ( typeof name !== 'string' || ! name.startsWith( 'profile-builder/' ) ) { |
| 28 |
return settings; |
| 29 |
} |
| 30 |
|
| 31 |
let next = settings; |
| 32 |
|
| 33 |
if ( FIELD_ICONS[ name ] ) { |
| 34 |
next = { ...next, icon: FIELD_ICONS[ name ] }; |
| 35 |
} |
| 36 |
|
| 37 |
if ( FIELD_PREVIEWS[ name ] ) { |
| 38 |
const OriginalEdit = next.edit; |
| 39 |
next = { |
| 40 |
...next, |
| 41 |
attributes: { |
| 42 |
...( next.attributes || {} ), |
| 43 |
[ PREVIEW_ATTR ]: { type: 'boolean', default: false }, |
| 44 |
}, |
| 45 |
example: { |
| 46 |
attributes: { [ PREVIEW_ATTR ]: true }, |
| 47 |
viewportWidth: 248, |
| 48 |
}, |
| 49 |
edit: ( props ) => |
| 50 |
props?.attributes?.[ PREVIEW_ATTR ] |
| 51 |
? <FieldPreview name={ name } /> |
| 52 |
: <OriginalEdit { ...props } />, |
| 53 |
}; |
| 54 |
} |
| 55 |
|
| 56 |
return next; |
| 57 |
} |
| 58 |
); |
| 59 |
|