| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
?> |
| 6 |
<script> |
| 7 |
( function() { |
| 8 |
const handleDomReady = () => { |
| 9 |
if ( 'undefined' === typeof wp || 'undefined' === typeof wp.data || 'function' !== typeof wp.data.subscribe ) { |
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
const closeListener = wp.data.subscribe( |
| 14 |
() => { |
| 15 |
const editor = wp.data.select( 'core/editor' ); |
| 16 |
|
| 17 |
if ( 'function' !== typeof editor.__unstableIsEditorReady ) { |
| 18 |
closeListener(); |
| 19 |
return; |
| 20 |
} |
| 21 |
|
| 22 |
const isReady = editor.__unstableIsEditorReady(); |
| 23 |
if ( isReady ) { |
| 24 |
closeListener(); |
| 25 |
requestAnimationFrame( () => injectFormidableBlock() ); |
| 26 |
} |
| 27 |
} |
| 28 |
); |
| 29 |
} |
| 30 |
|
| 31 |
document.addEventListener( 'DOMContentLoaded', handleDomReady ); |
| 32 |
|
| 33 |
const injectFormidableBlock = () => { |
| 34 |
insertedBlock = wp.blocks.createBlock( |
| 35 |
'<?php echo esc_js( $block_name ); ?>', |
| 36 |
{ <?php echo esc_js( $object_key ); ?>: '<?php echo absint( $object_id ); ?>' } |
| 37 |
); |
| 38 |
|
| 39 |
const getBlocks = () => wp.data.select( 'core/editor' ).getBlocks(); |
| 40 |
const blockList = getBlocks(); |
| 41 |
|
| 42 |
const closeListener = wp.data.subscribe( |
| 43 |
() => { |
| 44 |
const currentBlocks = getBlocks(); |
| 45 |
if ( currentBlocks === blockList ) { |
| 46 |
return; |
| 47 |
} |
| 48 |
|
| 49 |
closeListener(); |
| 50 |
const block = currentBlocks[ currentBlocks.length - 1 ]; |
| 51 |
setTimeout( |
| 52 |
() => document.getElementById( 'block-' + block.clientId ).scrollIntoView({ behavior: 'smooth' }), |
| 53 |
1 |
| 54 |
); |
| 55 |
} |
| 56 |
); |
| 57 |
|
| 58 |
wp.data.dispatch( 'core/block-editor' ).insertBlocks( insertedBlock ); |
| 59 |
}; |
| 60 |
}() ); |
| 61 |
</script> |
| 62 |
|