block.json
1 year ago
edit.js
1 year ago
index-min.js
1 year ago
index.js
1 year ago
save.js
1 year ago
edit.js
378 lines
| 1 | /** |
| 2 | * External dependencies |
| 3 | */ |
| 4 | import styled from '@emotion/styled'; |
| 5 | |
| 6 | /** |
| 7 | * WordPress components that create the necessary UI elements for the block |
| 8 | * |
| 9 | * @see https://developer.wordpress.org/block-editor/packages/packages-components/ |
| 10 | */ |
| 11 | import {registerBlockType} from '@wordpress/blocks'; |
| 12 | import {__} from '@wordpress/i18n'; |
| 13 | import { |
| 14 | Panel, |
| 15 | PanelBody, |
| 16 | PanelRow, |
| 17 | TextControl, |
| 18 | SelectControl, |
| 19 | ToggleControl, |
| 20 | TabPanel, |
| 21 | __experimentalToggleGroupControl as ToggleGroupControl, |
| 22 | __experimentalToggleGroupControlOption as ToggleGroupControlOption |
| 23 | } from '@wordpress/components'; |
| 24 | |
| 25 | import {useEffect, useState} from '@wordpress/element'; |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * React hook that is used to mark the block wrapper element. |
| 30 | * It provides all the necessary props like the class name. |
| 31 | * |
| 32 | * @see https://developer.wordpress.org/block-editor/reference-guides/packages/packages-block-editor/#useblockprops |
| 33 | */ |
| 34 | import { |
| 35 | useBlockProps, |
| 36 | InspectorControls, |
| 37 | } from '@wordpress/block-editor'; |
| 38 | |
| 39 | import React from "react"; |
| 40 | import TypographyControl from "../../controls/TypographyControl"; |
| 41 | import ColorSelectorControl from "../../controls/ColorSelectorControl"; |
| 42 | import BorderControl from "../../controls/BorderControl"; |
| 43 | import PaddingBoxControl from "../../controls/PaddingBoxControl"; |
| 44 | import {SettingsIcon, StylesIcon} from "../../controls/LatepointIcons"; |
| 45 | |
| 46 | const LatepointBookButtonWrapper = styled.div` |
| 47 | `; |
| 48 | |
| 49 | const LatepointBookButton = styled.div` |
| 50 | `; |
| 51 | |
| 52 | const PanelRowBlock = styled(PanelRow)` |
| 53 | display: block; |
| 54 | margin-bottom: 20px; |
| 55 | `; |
| 56 | |
| 57 | /** |
| 58 | * The edit function describes the structure of your block in the context of the |
| 59 | * editor. This represents what the editor will render when the block is used. |
| 60 | * |
| 61 | * @see https://developer.wordpress.org/block-editor/reference-guides/block-api/block-edit-save/#edit |
| 62 | * |
| 63 | * @param {Object} props Properties passed to the function. |
| 64 | * @param {Object} props.attributes Available block attributes. |
| 65 | * @param {Function} props.setAttributes Function that updates individual attributes. |
| 66 | * |
| 67 | * @return {WPElement} Element to render. |
| 68 | */ |
| 69 | export default function Edit({attributes, setAttributes}) { |
| 70 | const blockProps = useBlockProps(); |
| 71 | |
| 72 | useEffect(() => { |
| 73 | if (!attributes.id) { |
| 74 | setAttributes({ id: Math.random().toString(36).substr(2, 9) + '-' + Date.now() }); |
| 75 | } |
| 76 | }, []); |
| 77 | |
| 78 | const [isHovered, setIsHovered] = useState(false); |
| 79 | const [activeTab, setActiveTab] = useState('settings'); |
| 80 | |
| 81 | const Separator = styled.div` |
| 82 | height: 1px; |
| 83 | background-color: #e0e0e0; |
| 84 | `; |
| 85 | |
| 86 | const generateStyles = () => { |
| 87 | let styles = {} |
| 88 | if (attributes.is_inherit) return styles; |
| 89 | |
| 90 | if (attributes.font_weight) styles.fontWeight = attributes.font_weight |
| 91 | if (attributes.font_size) styles.fontSize = attributes.font_size |
| 92 | if (attributes.text_transform) styles.textTransform = attributes.text_transform |
| 93 | if (attributes.font_family) styles.fontFamily = attributes.font_family |
| 94 | if (attributes.line_height) styles.lineHeight = attributes.line_height |
| 95 | if (attributes.letter_spacing) styles.letterSpacing = attributes.letter_spacing |
| 96 | |
| 97 | if (attributes.border_radius) styles.borderRadius = attributes.border_radius |
| 98 | if (attributes.bg_color) styles.backgroundColor = isHovered && attributes.bg_color_hover ? attributes.bg_color_hover : attributes.bg_color; |
| 99 | if (attributes.text_color) styles.color = isHovered && attributes.text_color_hover ? attributes.text_color_hover : attributes.text_color; |
| 100 | if (attributes.border_color) styles.borderColor = isHovered && attributes.border_color_hover ? attributes.border_color_hover : attributes.border_color; |
| 101 | if (attributes.border_style && attributes.border_style !== 'default') styles.borderStyle = attributes.border_style; |
| 102 | if (attributes.border_width && attributes.border_style !== 'default') styles.borderWidth = attributes.border_width; |
| 103 | if (attributes.padding) styles.padding = attributes.padding; |
| 104 | return styles |
| 105 | } |
| 106 | |
| 107 | const getGeneralTabs = () => { |
| 108 | let tabs = [ |
| 109 | {name: 'settings', title: <div className="lb-tab-head"><SettingsIcon/>Settings</div>} |
| 110 | ]; |
| 111 | if(!attributes.is_inherit){ |
| 112 | tabs.push({name: 'styles', title: <div className="lb-tab-head"><StylesIcon/>Style</div>}); |
| 113 | } |
| 114 | return tabs; |
| 115 | } |
| 116 | |
| 117 | return ( |
| 118 | <div {...blockProps}> |
| 119 | |
| 120 | <InspectorControls> |
| 121 | <TabPanel |
| 122 | className="lb-general-tabs" |
| 123 | activeClass="active-tab" |
| 124 | tabs={ getGeneralTabs() } |
| 125 | onSelect={(tabName) => setActiveTab(tabName)} |
| 126 | > |
| 127 | {(tab) => { |
| 128 | if (tab.name === 'settings') { |
| 129 | return ( |
| 130 | <> |
| 131 | <PanelBody title="Button Settings" initialOpen={true}> |
| 132 | <TextControl |
| 133 | label="Caption" |
| 134 | value={attributes.caption || ''} |
| 135 | onChange={(value) => setAttributes({caption: value})} |
| 136 | /> |
| 137 | <ToggleControl |
| 138 | label="Inherit From Theme" |
| 139 | checked={attributes.is_inherit} |
| 140 | onChange={(value) => setAttributes({is_inherit: value})} |
| 141 | /> |
| 142 | </PanelBody> |
| 143 | <PanelBody title="Booking Form Settings" initialOpen={false}> |
| 144 | <ToggleControl |
| 145 | label="Hide Summary Panel" |
| 146 | checked={attributes.hide_summary} |
| 147 | onChange={(value) => setAttributes({hide_summary: value})} |
| 148 | /> |
| 149 | <ToggleControl |
| 150 | label="Hide Side Panel" |
| 151 | checked={attributes.hide_side_panel} |
| 152 | onChange={(value) => setAttributes({hide_side_panel: value})} |
| 153 | /> |
| 154 | </PanelBody> |
| 155 | |
| 156 | <PanelBody title="Step Settings" initialOpen={false}> |
| 157 | <SelectControl |
| 158 | value={attributes.selected_agent} |
| 159 | label={__('Preselected Agent', 'latepoint')} |
| 160 | onChange={(value) => setAttributes({selected_agent: value})} |
| 161 | options={latepoint_helper.selected_agents_options} |
| 162 | /> |
| 163 | <SelectControl |
| 164 | value={attributes.selected_service} |
| 165 | label={__('Preselected Service', 'latepoint')} |
| 166 | onChange={(value) => setAttributes({selected_service: value})} |
| 167 | options={latepoint_helper.selected_services_options} |
| 168 | /> |
| 169 | <SelectControl |
| 170 | value={attributes.selected_service_category} |
| 171 | label={__('Preselected Service Category', 'latepoint')} |
| 172 | onChange={(value) => setAttributes({selected_service_category: value})} |
| 173 | options={latepoint_helper.selected_service_categories_options} |
| 174 | /> |
| 175 | <SelectControl |
| 176 | value={attributes.selected_bundle} |
| 177 | label={__('Preselected Bundle', 'latepoint')} |
| 178 | onChange={(value) => setAttributes({selected_bundle: value})} |
| 179 | options={latepoint_helper.selected_bundles_options} |
| 180 | /> |
| 181 | <SelectControl |
| 182 | value={attributes.selected_location} |
| 183 | label={__('Preselected Location', 'latepoint')} |
| 184 | onChange={(value) => setAttributes({selected_location: value})} |
| 185 | options={latepoint_helper.selected_locations_options} |
| 186 | /> |
| 187 | <TextControl |
| 188 | label={__('Preselected Booking Start Date', 'latepoint')} |
| 189 | value={attributes.selected_start_date || ''} |
| 190 | placeholder="YYYY-MM-DD" |
| 191 | onChange={(value) => setAttributes({selected_start_date: value})} |
| 192 | /> |
| 193 | <TextControl |
| 194 | label={__('Preselected Booking Start Time', 'latepoint')} |
| 195 | value={attributes.selected_start_time || ''} |
| 196 | placeholder="Minutes" |
| 197 | onChange={(value) => setAttributes({selected_start_time: value})} |
| 198 | /> |
| 199 | <TextControl |
| 200 | label={__('Preselected Duration', 'latepoint')} |
| 201 | value={attributes.selected_duration || ''} |
| 202 | placeholder="Minutes" |
| 203 | onChange={(value) => setAttributes({selected_duration: value})} |
| 204 | /> |
| 205 | <TextControl |
| 206 | label={__('Preselected Total Attendees', 'latepoint')} |
| 207 | value={attributes.selected_total_attendees || ''} |
| 208 | placeholder="Number" |
| 209 | onChange={(value) => setAttributes({selected_total_attendees: value})} |
| 210 | /> |
| 211 | </PanelBody> |
| 212 | |
| 213 | <PanelBody title="Other Settings" initialOpen={false}> |
| 214 | <TextControl |
| 215 | label="Source ID" |
| 216 | value={attributes.source_id || ''} |
| 217 | onChange={(value) => setAttributes({source_id: value})} |
| 218 | /> |
| 219 | <TextControl |
| 220 | label="Calendar Start Date" |
| 221 | value={attributes.calendar_start_date || ''} |
| 222 | placeholder="YYYY-MM-DD" |
| 223 | onChange={(value) => setAttributes({calendar_start_date: value})} |
| 224 | /> |
| 225 | <TextControl |
| 226 | label="Show Services" |
| 227 | placeholder="Comma separated service IDs" |
| 228 | value={attributes.show_services || ''} |
| 229 | onChange={(value) => setAttributes({show_services: value})} |
| 230 | /> |
| 231 | <TextControl |
| 232 | label="Show Service Categories" |
| 233 | placeholder="Comma separated category IDs" |
| 234 | value={attributes.show_service_categories || ''} |
| 235 | onChange={(value) => setAttributes({show_service_categories: value})} |
| 236 | /> |
| 237 | <TextControl |
| 238 | label="Show Agents" |
| 239 | placeholder="Comma separated agent IDs" |
| 240 | value={attributes.show_agents || ''} |
| 241 | onChange={(value) => setAttributes({show_agents: value})} |
| 242 | /> |
| 243 | |
| 244 | <TextControl |
| 245 | label="Show Locations" |
| 246 | placeholder="Comma separated location IDs" |
| 247 | value={attributes.show_locations || ''} |
| 248 | onChange={(value) => setAttributes({show_locations: value})} |
| 249 | /> |
| 250 | </PanelBody> |
| 251 | </> |
| 252 | ); |
| 253 | } |
| 254 | if (tab.name === 'styles') { |
| 255 | return ( |
| 256 | <> |
| 257 | <Panel> |
| 258 | <PanelBody> |
| 259 | <ToggleGroupControl |
| 260 | className="lb-toggle-group" |
| 261 | isBlock |
| 262 | isDeselectable={true} |
| 263 | value={attributes.align} |
| 264 | label={__('Alignment', 'latepoint')} |
| 265 | onChange={(value) => { |
| 266 | setAttributes({align: value}) |
| 267 | }} |
| 268 | > |
| 269 | <ToggleGroupControlOption label="Left" value="left"/> |
| 270 | <ToggleGroupControlOption label="Center" value="center"/> |
| 271 | <ToggleGroupControlOption label="Right" value="right"/> |
| 272 | <ToggleGroupControlOption label="Justify" value="justify"/> |
| 273 | </ToggleGroupControl> |
| 274 | </PanelBody> |
| 275 | </Panel> |
| 276 | |
| 277 | {!attributes.is_inherit && ( |
| 278 | <Panel> |
| 279 | <PanelBody> |
| 280 | <PaddingBoxControl |
| 281 | label={__('Padding', 'latepoint')} |
| 282 | paddingAttribute="padding" |
| 283 | attributes={attributes} |
| 284 | setAttributes={setAttributes} |
| 285 | /> |
| 286 | </PanelBody> |
| 287 | <PanelBody title="Color" initialOpen={false}> |
| 288 | <TabPanel |
| 289 | className="lb-tabs" |
| 290 | activeClass="active-tab" |
| 291 | tabs={[ |
| 292 | {name: 'tab-normal', title: 'Normal',}, |
| 293 | {name: 'tab-hover', title: 'Hover',}, |
| 294 | ]} |
| 295 | > |
| 296 | {(tab) => { |
| 297 | if (tab.name === 'tab-normal') { |
| 298 | return ( |
| 299 | <> |
| 300 | <PanelRow> |
| 301 | <ColorSelectorControl |
| 302 | attributes={attributes} |
| 303 | setAttributes={setAttributes} |
| 304 | colorAttribute="bg_color" |
| 305 | label={__('Background Color', 'latepoint')} |
| 306 | ></ColorSelectorControl> |
| 307 | </PanelRow> |
| 308 | <PanelRow> |
| 309 | <ColorSelectorControl |
| 310 | attributes={attributes} |
| 311 | setAttributes={setAttributes} |
| 312 | colorAttribute="text_color" |
| 313 | label={__('Text Color', 'latepoint')} |
| 314 | ></ColorSelectorControl> |
| 315 | </PanelRow> |
| 316 | </> |
| 317 | ); |
| 318 | } |
| 319 | if (tab.name === 'tab-hover') { |
| 320 | return ( |
| 321 | <> |
| 322 | <PanelRow> |
| 323 | <ColorSelectorControl |
| 324 | attributes={attributes} |
| 325 | setAttributes={setAttributes} |
| 326 | colorAttribute="bg_color_hover" |
| 327 | label={__('Background Color', 'latepoint')} |
| 328 | ></ColorSelectorControl> |
| 329 | </PanelRow> |
| 330 | <PanelRow> |
| 331 | <ColorSelectorControl |
| 332 | attributes={attributes} |
| 333 | setAttributes={setAttributes} |
| 334 | colorAttribute="text_color_hover" |
| 335 | label={__('Text Color', 'latepoint')} |
| 336 | ></ColorSelectorControl> |
| 337 | </PanelRow> |
| 338 | </> |
| 339 | ); |
| 340 | } |
| 341 | }} |
| 342 | </TabPanel> |
| 343 | </PanelBody> |
| 344 | |
| 345 | <PanelBody title="Text" initialOpen={false}> |
| 346 | <TypographyControl attributes={attributes} setAttributes={setAttributes} fontSizeAttr="font_size"></TypographyControl> |
| 347 | </PanelBody> |
| 348 | <PanelBody title="Border" initialOpen={false}> |
| 349 | <BorderControl attributes={attributes} setAttributes={setAttributes} borderRadiusAttr="border_radius"></BorderControl> |
| 350 | </PanelBody> |
| 351 | </Panel> |
| 352 | )} |
| 353 | </> |
| 354 | ); |
| 355 | } |
| 356 | }} |
| 357 | </TabPanel> |
| 358 | |
| 359 | <Panel> |
| 360 | <Separator></Separator> |
| 361 | </Panel> |
| 362 | |
| 363 | </InspectorControls> |
| 364 | |
| 365 | <LatepointBookButtonWrapper |
| 366 | className={'latepoint-book-button-wrapper ' + 'wp-block-button ' + (attributes.align ? `latepoint-book-button-align-${attributes.align}` : '')}> |
| 367 | <LatepointBookButton |
| 368 | onMouseEnter={() => setIsHovered(true)} |
| 369 | onMouseLeave={() => setIsHovered(false)} |
| 370 | style={generateStyles()} |
| 371 | className="wp-block-button__link latepoint-book-button"> |
| 372 | {attributes.caption} |
| 373 | </LatepointBookButton> |
| 374 | </LatepointBookButtonWrapper> |
| 375 | </div> |
| 376 | ); |
| 377 | } |
| 378 |