frontblocks-faq-schema.js
88 lines
| 1 | "use strict"; |
| 2 | |
| 3 | var addFilter = wp.hooks.addFilter; |
| 4 | var Fragment = wp.element.Fragment; |
| 5 | var InspectorControls = wp.blockEditor.InspectorControls; |
| 6 | var _wp$components = wp.components, |
| 7 | PanelBody = _wp$components.PanelBody, |
| 8 | ToggleControl = _wp$components.ToggleControl, |
| 9 | SelectControl = _wp$components.SelectControl; |
| 10 | var __ = wp.i18n.__; |
| 11 | var FAQ_BLOCKS = ['core/accordion', 'generateblocks/container']; |
| 12 | var SCHEMA_TYPES = [{ |
| 13 | label: __('FAQPage (questions & answers)', 'frontblocks'), |
| 14 | value: 'FAQPage' |
| 15 | }, { |
| 16 | label: __('HowTo (steps / how-to guide)', 'frontblocks'), |
| 17 | value: 'HowTo' |
| 18 | }]; |
| 19 | |
| 20 | // Register attributes on supported blocks. |
| 21 | addFilter('blocks.registerBlockType', 'frontblocks/faq-schema-attribute', function (settings, name) { |
| 22 | if (!FAQ_BLOCKS.includes(name)) { |
| 23 | return settings; |
| 24 | } |
| 25 | settings.attributes = Object.assign({}, settings.attributes, { |
| 26 | frblFaqSchema: { |
| 27 | type: 'boolean', |
| 28 | default: false |
| 29 | }, |
| 30 | frblSchemaType: { |
| 31 | type: 'string', |
| 32 | default: 'FAQPage' |
| 33 | } |
| 34 | }); |
| 35 | return settings; |
| 36 | }); |
| 37 | |
| 38 | // Add inspector controls to supported blocks. |
| 39 | addFilter('editor.BlockEdit', 'frontblocks/faq-schema-controls', function (BlockEdit) { |
| 40 | return function (props) { |
| 41 | if (!FAQ_BLOCKS.includes(props.name)) { |
| 42 | return wp.element.createElement(BlockEdit, props); |
| 43 | } |
| 44 | if (props.name === 'generateblocks/container' && props.attributes.variantRole !== 'accordion') { |
| 45 | return wp.element.createElement(BlockEdit, props); |
| 46 | } |
| 47 | var _props$attributes = props.attributes, |
| 48 | frblFaqSchema = _props$attributes.frblFaqSchema === undefined ? false : _props$attributes.frblFaqSchema, |
| 49 | frblSchemaType = _props$attributes.frblSchemaType === undefined ? 'FAQPage' : _props$attributes.frblSchemaType; |
| 50 | return wp.element.createElement( |
| 51 | Fragment, |
| 52 | null, |
| 53 | wp.element.createElement(BlockEdit, props), |
| 54 | wp.element.createElement( |
| 55 | InspectorControls, |
| 56 | null, |
| 57 | wp.element.createElement( |
| 58 | PanelBody, |
| 59 | { |
| 60 | title: __('FrontBlocks - Schema', 'frontblocks'), |
| 61 | initialOpen: false |
| 62 | }, |
| 63 | wp.element.createElement(ToggleControl, { |
| 64 | label: __('Add Schema (JSON-LD)', 'frontblocks'), |
| 65 | help: __('Include this block\'s content in the page structured data.', 'frontblocks'), |
| 66 | checked: frblFaqSchema, |
| 67 | onChange: function (value) { |
| 68 | return props.setAttributes({ |
| 69 | frblFaqSchema: value |
| 70 | }); |
| 71 | } |
| 72 | }), |
| 73 | frblFaqSchema && wp.element.createElement(SelectControl, { |
| 74 | label: __('Schema type', 'frontblocks'), |
| 75 | value: frblSchemaType, |
| 76 | options: SCHEMA_TYPES, |
| 77 | onChange: function (value) { |
| 78 | return props.setAttributes({ |
| 79 | frblSchemaType: value |
| 80 | }); |
| 81 | } |
| 82 | }) |
| 83 | ) |
| 84 | ) |
| 85 | ); |
| 86 | }; |
| 87 | }); |
| 88 |