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