frontblocks-gf-inline-option.js
7 months ago
frontblocks-gf-inline-option.jsx
1 month ago
frontblocks-gf-inline.css
4 months ago
frontblocks-gf-inline.js
7 months ago
frontblocks-gf-inline-option.jsx
119 lines
| 1 | (function() { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const { __ } = wp.i18n; |
| 5 | const { createElement: el } = wp.element; |
| 6 | const { InspectorControls } = wp.blockEditor; |
| 7 | const { PanelBody, ToggleControl, RangeControl } = wp.components; |
| 8 | const { addFilter } = wp.hooks; |
| 9 | const { createHigherOrderComponent } = wp.compose; |
| 10 | const { apiFetch } = wp; |
| 11 | |
| 12 | // Intercept Gravity Forms API requests and remove custom attributes. |
| 13 | if (apiFetch && apiFetch.use) { |
| 14 | apiFetch.use(function(options, next) { |
| 15 | if (options.path && options.path.includes('/wp/v2/block-renderer/gravityforms/form')) { |
| 16 | // Remove our custom attributes from the URL. |
| 17 | options.path = options.path |
| 18 | .replace(/&attributes%5BfrblGfInlineEnabled%5D=[^&]*/g, '') |
| 19 | .replace(/&attributes%5BfrblGfInlineGap%5D=[^&]*/g, '') |
| 20 | .replace(/attributes%5BfrblGfInlineEnabled%5D=[^&]*&/g, '') |
| 21 | .replace(/attributes%5BfrblGfInlineGap%5D=[^&]*&/g, ''); |
| 22 | } |
| 23 | return next(options); |
| 24 | }); |
| 25 | } |
| 26 | |
| 27 | // Add inline layout controls to the Gravity Forms block. |
| 28 | addFilter( |
| 29 | 'editor.BlockEdit', |
| 30 | 'frontblocks/gf-inline-controls', |
| 31 | function(BlockEdit) { |
| 32 | return function(props) { |
| 33 | // Only add controls to Gravity Forms block. |
| 34 | if (props.name !== 'gravityforms/form') { |
| 35 | return el(BlockEdit, props); |
| 36 | } |
| 37 | |
| 38 | // Check if a form is selected. |
| 39 | const { attributes, setAttributes } = props; |
| 40 | const { |
| 41 | frblGfInlineEnabled = false, |
| 42 | frblGfInlineGap = 10, |
| 43 | formId = 0 |
| 44 | } = attributes; |
| 45 | |
| 46 | // Only show options if a form is selected. |
| 47 | const showInlineOptions = formId && formId > 0; |
| 48 | |
| 49 | return el( |
| 50 | 'div', |
| 51 | {}, |
| 52 | el(BlockEdit, props), |
| 53 | showInlineOptions && el( |
| 54 | InspectorControls, |
| 55 | {}, |
| 56 | el( |
| 57 | PanelBody, |
| 58 | { |
| 59 | title: __('FrontBlocks for GravityForms', 'frontblocks'), |
| 60 | initialOpen: false |
| 61 | }, |
| 62 | el(ToggleControl, { |
| 63 | label: __('Enable Inline Layout', 'frontblocks'), |
| 64 | checked: frblGfInlineEnabled, |
| 65 | onChange: (value) => setAttributes({ frblGfInlineEnabled: value }), |
| 66 | help: __('Display form fields and button on the same line', 'frontblocks') |
| 67 | }), |
| 68 | frblGfInlineEnabled && el( |
| 69 | RangeControl, |
| 70 | { |
| 71 | label: __('Gap between elements (px)', 'frontblocks'), |
| 72 | value: frblGfInlineGap, |
| 73 | onChange: (value) => setAttributes({ frblGfInlineGap: value }), |
| 74 | min: 0, |
| 75 | max: 50, |
| 76 | step: 1, |
| 77 | help: __('Space between form fields and button', 'frontblocks') |
| 78 | } |
| 79 | ) |
| 80 | ) |
| 81 | ) |
| 82 | ); |
| 83 | }; |
| 84 | } |
| 85 | ); |
| 86 | |
| 87 | // Add custom class to block wrapper in editor. |
| 88 | addFilter( |
| 89 | 'editor.BlockListBlock', |
| 90 | 'frontblocks/gf-inline-wrapper-class', |
| 91 | createHigherOrderComponent(function(BlockListBlock) { |
| 92 | return function(props) { |
| 93 | if (props.name === 'gravityforms/form') { |
| 94 | const { attributes } = props; |
| 95 | if (attributes.frblGfInlineEnabled) { |
| 96 | // Apply gap CSS variable to editor preview. |
| 97 | if (typeof window !== 'undefined') { |
| 98 | setTimeout(function() { |
| 99 | const blockElement = document.querySelector('[data-block="' + props.clientId + '"]'); |
| 100 | if (blockElement) { |
| 101 | blockElement.style.setProperty('--gf-inline-gap', (attributes.frblGfInlineGap || 10) + 'px'); |
| 102 | } |
| 103 | }, 100); |
| 104 | } |
| 105 | |
| 106 | return el(BlockListBlock, { |
| 107 | ...props, |
| 108 | className: 'frontblocks-gf-inline-preview' |
| 109 | }); |
| 110 | } |
| 111 | } |
| 112 | return el(BlockListBlock, props); |
| 113 | }; |
| 114 | }, 'withInlineClass') |
| 115 | ); |
| 116 | |
| 117 | })(); |
| 118 | |
| 119 |