components
1 year ago
block.json
1 year ago
edit.js
1 year ago
editor.scss
1 year ago
index.js
1 year ago
save.js
1 year ago
edit.js
111 lines
| 1 | import { useBlockProps, InspectorControls } 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 { LoopInnerBlocksRenderer } from './components/LoopInnerBlocksRenderer'; |
| 9 | import { BlockSettings } from './components/BlockSettings'; |
| 10 | import { selectorShortcuts as defaultSelectorShortcuts } from '@utils/selectorShortcuts.js'; |
| 11 | import { withStyles } from '@hoc/withStyles'; |
| 12 | import { BlockStylesBuilder } from '@components/index'; |
| 13 | |
| 14 | import './editor.scss'; |
| 15 | import { withHtmlAttributes } from '@hoc/withHtmlAttributes.js'; |
| 16 | import { getBlockClasses } from '@utils/getBlockClasses'; |
| 17 | |
| 18 | function EditBlock( props ) { |
| 19 | const { |
| 20 | attributes, |
| 21 | setAttributes, |
| 22 | onStyleChange, |
| 23 | editorHtmlAttributes, |
| 24 | styles, |
| 25 | } = props; |
| 26 | |
| 27 | const { |
| 28 | tagName, |
| 29 | } = attributes; |
| 30 | |
| 31 | const classNames = getBlockClasses( |
| 32 | 'gb-looper', |
| 33 | { |
| 34 | ...attributes, |
| 35 | styles, |
| 36 | } |
| 37 | ); |
| 38 | |
| 39 | useEffect( () => { |
| 40 | if ( ! tagName ) { |
| 41 | setAttributes( { tagName: 'div' } ); |
| 42 | } |
| 43 | }, [ tagName ] ); |
| 44 | |
| 45 | const blockProps = useBlockProps( |
| 46 | { |
| 47 | className: classNames.join( ' ' ).trim(), |
| 48 | ...editorHtmlAttributes, |
| 49 | } |
| 50 | ); |
| 51 | |
| 52 | const TagName = tagName || 'div'; |
| 53 | const shortcuts = useMemo( () => { |
| 54 | const selectorShortcuts = { |
| 55 | ...defaultSelectorShortcuts, |
| 56 | default: { |
| 57 | items: [ |
| 58 | { label: __( 'First item', 'generateblocks' ), value: '> .gb-loop-item:first-child' }, |
| 59 | ...defaultSelectorShortcuts.default.items, |
| 60 | ], |
| 61 | }, |
| 62 | }; |
| 63 | |
| 64 | const visibleSelectors = [ |
| 65 | { |
| 66 | label: __( 'Main', 'generateblocks' ), |
| 67 | value: '', |
| 68 | }, |
| 69 | ]; |
| 70 | |
| 71 | return { |
| 72 | selectorShortcuts, |
| 73 | visibleShortcuts: visibleSelectors, |
| 74 | }; |
| 75 | }, [] ); |
| 76 | |
| 77 | return ( |
| 78 | <> |
| 79 | <InspectorControls> |
| 80 | <BlockStyles |
| 81 | settingsTab={ ( |
| 82 | <BlockSettings |
| 83 | { ...props } |
| 84 | /> |
| 85 | ) } |
| 86 | stylesTab={ ( |
| 87 | <BlockStylesBuilder |
| 88 | attributes={ attributes } |
| 89 | setAttributes={ setAttributes } |
| 90 | shortcuts={ shortcuts } |
| 91 | onStyleChange={ onStyleChange } |
| 92 | name={ name } |
| 93 | /> |
| 94 | ) } |
| 95 | /> |
| 96 | </InspectorControls> |
| 97 | <TagName { ...blockProps }> |
| 98 | <LoopInnerBlocksRenderer { ...props } /> |
| 99 | </TagName> |
| 100 | </> |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | const Edit = compose( |
| 105 | withHtmlAttributes, |
| 106 | withStyles, |
| 107 | withUniqueId |
| 108 | )( EditBlock ); |
| 109 | |
| 110 | export { Edit }; |
| 111 |