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