| 1 |
(function(wp) { |
| 2 |
'use strict'; |
| 3 |
|
| 4 |
/** |
| 5 |
* Helper function used to convert a VikBooking widget into a block. |
| 6 |
* |
| 7 |
* @param object manifest |
| 8 |
* |
| 9 |
* @return void |
| 10 |
*/ |
| 11 |
window.vboRegisterBlockEditor = (manifest) => { |
| 12 |
/** |
| 13 |
* Registers a new block provided a unique name and an object defining its behavior. |
| 14 |
* @link https://github.com/WordPress/gutenberg/tree/master/blocks#api |
| 15 |
*/ |
| 16 |
const registerBlockType = wp.blocks.registerBlockType; |
| 17 |
|
| 18 |
/** |
| 19 |
* Returns a new element of given type. Element is an abstraction layer atop React. |
| 20 |
* @link https://github.com/WordPress/gutenberg/tree/master/packages/element#element |
| 21 |
*/ |
| 22 |
const el = wp.element.createElement; |
| 23 |
|
| 24 |
/** |
| 25 |
* Retrieves the translation of text. |
| 26 |
* @link https://github.com/WordPress/gutenberg/tree/master/i18n#api |
| 27 |
*/ |
| 28 |
const __ = wp.i18n.__; |
| 29 |
|
| 30 |
/** |
| 31 |
* Every block starts by registering a new block type definition. |
| 32 |
* @link https://wordpress.org/gutenberg/handbook/block-api/ |
| 33 |
*/ |
| 34 |
registerBlockType(manifest.id, { |
| 35 |
/** |
| 36 |
* This is the block display title, which can be translated with `i18n` functions. |
| 37 |
* The block inserter will show this name. |
| 38 |
*/ |
| 39 |
title: manifest.title, |
| 40 |
|
| 41 |
/** |
| 42 |
* This is the block description, which is displayed within the right sidebar. |
| 43 |
*/ |
| 44 |
description: manifest.description, |
| 45 |
|
| 46 |
/** |
| 47 |
* The icon can be a DASHICON or a SVG entity. |
| 48 |
* NOTE: we need to use a different icon because Gutenberg seems |
| 49 |
* to have problems in displaying the coffee icon. |
| 50 |
*/ |
| 51 |
icon: manifest.icon, |
| 52 |
|
| 53 |
/** |
| 54 |
* Blocks are grouped into categories to help users browse and discover them. |
| 55 |
* The categories provided by core are `common`, `embed`, `formatting`, `layout` and `widgets`. |
| 56 |
*/ |
| 57 |
category: manifest.category, |
| 58 |
|
| 59 |
/** |
| 60 |
* Sometimes a block could have aliases that help users discover it while searching. |
| 61 |
* You can do so by providing an array of terms (which can be translated). |
| 62 |
* It is only allowed to add as much as three terms per block. |
| 63 |
*/ |
| 64 |
keywords: manifest.keywords, |
| 65 |
|
| 66 |
/** |
| 67 |
* Optional block extended support features. |
| 68 |
*/ |
| 69 |
supports: manifest.supports, |
| 70 |
|
| 71 |
/** |
| 72 |
* Attributes provide the structured data needs of a block. |
| 73 |
* They can exist in different forms when they are serialized, |
| 74 |
* but they are declared together under a common interface. |
| 75 |
*/ |
| 76 |
attributes: manifest.attributes, |
| 77 |
|
| 78 |
/** |
| 79 |
* The API version of the block. |
| 80 |
* |
| 81 |
* @link https://developer.wordpress.org/block-editor/reference-guides/block-api/block-api-versions/ |
| 82 |
*/ |
| 83 |
apiVersion: 3, |
| 84 |
|
| 85 |
/** |
| 86 |
* The edit function describes the structure of your block in the context of the editor. |
| 87 |
* This represents what the editor will render when the block is used. |
| 88 |
* @link https://wordpress.org/gutenberg/handbook/block-edit-save/#edit |
| 89 |
* |
| 90 |
* @param Object props Properties passed from the editor. |
| 91 |
* |
| 92 |
* @return Element Element to render. |
| 93 |
*/ |
| 94 |
edit: (props) => { |
| 95 |
// use the new version of InspectorControls |
| 96 |
// provided by WordPress 5.4, if supported |
| 97 |
let InspectorControls, InspectorAdvancedControls; |
| 98 |
|
| 99 |
if (wp.blockEditor && wp.blockEditor.InspectorControls) { |
| 100 |
// use new version |
| 101 |
InspectorControls = wp.blockEditor.InspectorControls; |
| 102 |
InspectorAdvancedControls = wp.blockEditor.InspectorAdvancedControls; |
| 103 |
} else { |
| 104 |
// fallback to the older one |
| 105 |
InspectorControls = wp.editor.InspectorControls; |
| 106 |
InspectorAdvancedControls = wp.editor.InspectorAdvancedControls; |
| 107 |
} |
| 108 |
|
| 109 |
let navBar = [], advancedFields = []; |
| 110 |
|
| 111 |
// iterate all the form fieldsets |
| 112 |
manifest.form.forEach((fieldset, index) => { |
| 113 |
let fields = []; |
| 114 |
|
| 115 |
// iterate all the fields under this tab |
| 116 |
fieldset.fields.forEach((field) => { |
| 117 |
// create element according to the provided fields |
| 118 |
let fieldControl = window.vboCreateControlElement(field, props); |
| 119 |
|
| 120 |
// register the element within the list of fields |
| 121 |
fields.push(fieldControl); |
| 122 |
}); |
| 123 |
|
| 124 |
if (fieldset.name !== 'advanced') { |
| 125 |
// add accordion |
| 126 |
navBar.push( |
| 127 |
el( |
| 128 |
wp.components.PanelBody, |
| 129 |
{ |
| 130 |
title: fieldset.title, |
| 131 |
initialOpen: index === 0, |
| 132 |
key: fieldset.name, |
| 133 |
}, |
| 134 |
fields |
| 135 |
) |
| 136 |
); |
| 137 |
} else { |
| 138 |
advancedFields.push(fields); |
| 139 |
} |
| 140 |
|
| 141 |
}); |
| 142 |
|
| 143 |
// setup inspector (right-side area) |
| 144 |
let controls = el( |
| 145 |
// create InspectorControls element |
| 146 |
InspectorControls, |
| 147 |
// define inspector properties |
| 148 |
{ |
| 149 |
key: 'controls', |
| 150 |
}, |
| 151 |
navBar |
| 152 |
); |
| 153 |
|
| 154 |
// setup advanced tab |
| 155 |
let advancedControls = el( |
| 156 |
// create InspectorAdvancedControls element |
| 157 |
InspectorAdvancedControls, |
| 158 |
// define inspector properties |
| 159 |
{ |
| 160 |
key: 'controls', |
| 161 |
}, |
| 162 |
advancedFields |
| 163 |
); |
| 164 |
|
| 165 |
return [ |
| 166 |
controls, |
| 167 |
advancedControls, |
| 168 |
el( |
| 169 |
wp.serverSideRender, |
| 170 |
{ |
| 171 |
block: manifest.id, |
| 172 |
attributes: props.attributes, |
| 173 |
} |
| 174 |
) |
| 175 |
]; |
| 176 |
} |
| 177 |
}); |
| 178 |
} |
| 179 |
})(window.wp); |