components
1 year ago
block.json
1 year ago
edit.js
1 year ago
index.js
1 year ago
looper.js
1 year ago
query-parameters.js
1 year ago
save.js
1 year ago
templates.js
1 year ago
toolbar-appenders.js
1 year ago
edit.js
144 lines
| 1 | import { useBlockProps, InspectorControls, useInnerBlocksProps } from '@wordpress/block-editor'; |
| 2 | import { useEffect, useMemo } from '@wordpress/element'; |
| 3 | import { compose } from '@wordpress/compose'; |
| 4 | import { __ } from '@wordpress/i18n'; |
| 5 | |
| 6 | import { BlockStyles, withUniqueId } from '@edge22/block-styles'; |
| 7 | |
| 8 | import RootElement from '../../components/root-element/index.js'; |
| 9 | import { TemplateSelector } from '@components/template-selector'; |
| 10 | import { TEMPLATES } from './templates'; |
| 11 | import { BlockSettings } from './components/BlockSettings'; |
| 12 | import { selectorShortcuts } from '@utils/selectorShortcuts'; |
| 13 | import { withStyles } from '@hoc/withStyles'; |
| 14 | import { BlockAppender, BlockStylesBuilder } from '@components/index'; |
| 15 | import { withHtmlAttributes } from '@hoc/withHtmlAttributes.js'; |
| 16 | import { getBlockClasses } from '@utils/getBlockClasses.js'; |
| 17 | |
| 18 | function EditBlock( props ) { |
| 19 | const { |
| 20 | attributes, |
| 21 | setAttributes, |
| 22 | name, |
| 23 | clientId, |
| 24 | onStyleChange, |
| 25 | editorHtmlAttributes, |
| 26 | styles, |
| 27 | isSelected, |
| 28 | } = props; |
| 29 | |
| 30 | const { |
| 31 | tagName, |
| 32 | showTemplateSelector, |
| 33 | } = attributes; |
| 34 | |
| 35 | const classNames = getBlockClasses( |
| 36 | 'gb-query', |
| 37 | { |
| 38 | ...attributes, |
| 39 | styles, |
| 40 | } |
| 41 | ); |
| 42 | |
| 43 | useEffect( () => { |
| 44 | if ( ! tagName ) { |
| 45 | setAttributes( { tagName: 'div' } ); |
| 46 | } |
| 47 | }, [ tagName ] ); |
| 48 | |
| 49 | const blockProps = useBlockProps( |
| 50 | { |
| 51 | className: classNames.join( ' ' ).trim(), |
| 52 | ...editorHtmlAttributes, |
| 53 | } |
| 54 | ); |
| 55 | |
| 56 | const innerBlocksProps = useInnerBlocksProps( |
| 57 | blockProps, |
| 58 | { |
| 59 | allowedBlocks: [ |
| 60 | 'generateblocks/looper', |
| 61 | 'generateblocks/query-no-results', |
| 62 | 'generateblocks/query-page-numbers', |
| 63 | 'generateblocks/element', |
| 64 | ], |
| 65 | renderAppender: () => ( |
| 66 | <BlockAppender |
| 67 | clientId={ clientId } |
| 68 | isSelected={ isSelected } |
| 69 | attributes={ attributes } |
| 70 | /> |
| 71 | ), |
| 72 | } |
| 73 | ); |
| 74 | |
| 75 | const TagName = tagName || 'div'; |
| 76 | const shortcuts = useMemo( () => { |
| 77 | const visibleShortcuts = [ |
| 78 | { |
| 79 | label: __( 'Main', 'generateblocks' ), |
| 80 | value: '', |
| 81 | }, |
| 82 | { |
| 83 | label: __( 'Links', 'generateblocks' ), |
| 84 | value: 'a', |
| 85 | }, |
| 86 | ]; |
| 87 | |
| 88 | return { |
| 89 | selectorShortcuts, |
| 90 | visibleShortcuts, |
| 91 | }; |
| 92 | }, [] ); |
| 93 | |
| 94 | if ( showTemplateSelector ) { |
| 95 | return ( |
| 96 | <TemplateSelector |
| 97 | clientId={ clientId } |
| 98 | setAttributes={ setAttributes } |
| 99 | label={ __( 'Query', 'generateblocks' ) } |
| 100 | instructions={ __( 'Choose a layout to start with.', 'generateblocks' ) } |
| 101 | templates={ TEMPLATES } |
| 102 | /> |
| 103 | ); |
| 104 | } |
| 105 | |
| 106 | return ( |
| 107 | <> |
| 108 | <InspectorControls> |
| 109 | <BlockStyles |
| 110 | settingsTab={ ( |
| 111 | <BlockSettings |
| 112 | { ...props } |
| 113 | /> |
| 114 | ) } |
| 115 | stylesTab={ ( |
| 116 | <BlockStylesBuilder |
| 117 | attributes={ attributes } |
| 118 | setAttributes={ setAttributes } |
| 119 | shortcuts={ shortcuts } |
| 120 | onStyleChange={ onStyleChange } |
| 121 | name={ name } |
| 122 | /> |
| 123 | ) } |
| 124 | /> |
| 125 | </InspectorControls> |
| 126 | |
| 127 | <RootElement |
| 128 | name={ name } |
| 129 | clientId={ clientId } |
| 130 | > |
| 131 | <TagName { ...innerBlocksProps } /> |
| 132 | </RootElement> |
| 133 | </> |
| 134 | ); |
| 135 | } |
| 136 | |
| 137 | const Edit = compose( |
| 138 | withHtmlAttributes, |
| 139 | withStyles, |
| 140 | withUniqueId |
| 141 | )( EditBlock ); |
| 142 | |
| 143 | export { Edit }; |
| 144 |