| 1 |
/** |
| 2 |
* Form Block specific functions for Gutenberg. |
| 3 |
* |
| 4 |
* @since 1.9.6.5 |
| 5 |
* |
| 6 |
* @package ConvertKit |
| 7 |
* @author ConvertKit |
| 8 |
*/ |
| 9 |
|
| 10 |
/** |
| 11 |
* Custom callback function to render the ConvertKit Form Block preview in the Gutenberg Editor. |
| 12 |
* |
| 13 |
* @since 1.9.6.5 |
| 14 |
*/ |
| 15 |
function convertKitGutenbergFormBlockRenderPreview( block, props ) { |
| 16 |
|
| 17 |
var form = block.fields.form.data.forms[ props.attributes.form ]; |
| 18 |
|
| 19 |
// If no Form has been selected for display, return a prompt to tell the editor |
| 20 |
// what to do. |
| 21 |
if ( typeof form === 'undefined' ) { |
| 22 |
return wp.element.createElement( |
| 23 |
'div', |
| 24 |
{ |
| 25 |
// convertkit-no-content class allows resources/backend/css/gutenberg.css |
| 26 |
// to apply styling/branding to the block. |
| 27 |
className: 'convertkit-' + block.name + ' convertkit-no-content' |
| 28 |
}, |
| 29 |
block.gutenberg_help_description |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
// If the Form is a <script> embed, use the SandBox because the Gutenberg editor |
| 34 |
// will not execute inline scripts. |
| 35 |
if ( typeof form.uid !== 'undefined' ) { |
| 36 |
// Determine the Form's format (inline, sticky bar etc). |
| 37 |
// This isn't available in API responses prior to Feb 2022, so check the Form object contains this property. |
| 38 |
var format = ( ( typeof form.format !== 'undefined' ) ? form.format : 'inline' ), |
| 39 |
html = '<script async data-uid="' + form.uid + '" src="' + form.embed_js + '"></script>', |
| 40 |
className = [ 'convertkit-' + block.name ]; |
| 41 |
|
| 42 |
// If the format isn't inline, define the Gutenberg Block preview's HTML to explain why the Form won't be |
| 43 |
// rendered in the editor i.e because it is a Sticky Bar that is displayed at the top/bottom of the document. |
| 44 |
// Also add a CSS class to the block in the editor, so that resources/backend/css/gutenberg-block-form.css |
| 45 |
// can apply styling/branding to the block. |
| 46 |
switch ( format ) { |
| 47 |
case 'modal': |
| 48 |
html = block.i18n.gutenberg_form_modal.replace( '%s', form.name ); |
| 49 |
className.push( 'convertkit--no-content' ); |
| 50 |
break; |
| 51 |
|
| 52 |
case 'slide in': |
| 53 |
html = block.i18n.gutenberg_form_slide_in.replace( '%s', form.name ); |
| 54 |
className.push( 'convertkit-no-content' ); |
| 55 |
break; |
| 56 |
|
| 57 |
case 'sticky bar': |
| 58 |
html = block.i18n.gutenberg_form_sticky_bar.replace( '%s', form.name ); |
| 59 |
className.push( 'convertkit-no-content' ); |
| 60 |
break; |
| 61 |
} |
| 62 |
|
| 63 |
// Return SandBox. |
| 64 |
return wp.element.createElement( |
| 65 |
'div', |
| 66 |
{ |
| 67 |
className: className.join( ' ' ) |
| 68 |
}, |
| 69 |
wp.components.SandBox( |
| 70 |
{ |
| 71 |
html: html, |
| 72 |
title: block.name, |
| 73 |
styles: [ |
| 74 |
'body{font-family:-apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif; text-align:center;}', |
| 75 |
], |
| 76 |
} |
| 77 |
) |
| 78 |
); |
| 79 |
} |
| 80 |
|
| 81 |
// This is a Legacy Form. |
| 82 |
// Use the block's PHP's render() function by calling the ServerSideRender component. |
| 83 |
return wp.element.createElement( |
| 84 |
wp.components.ServerSideRender, |
| 85 |
{ |
| 86 |
block: 'convertkit/' + block.name, |
| 87 |
attributes: props.attributes, |
| 88 |
className: 'convertkit-' + block.name, |
| 89 |
} |
| 90 |
); |
| 91 |
|
| 92 |
} |
| 93 |
|