components
1 year ago
css
2 years ago
attributes.js
2 years ago
block-controls.js
2 years ago
block.js
1 year ago
deprecated.js
3 years ago
edit.js
2 years ago
editor.scss
1 year ago
transforms.js
1 year ago
edit.js
154 lines
| 1 | import BlockControls from './components/BlockControls'; |
| 2 | import InspectorAdvancedControls from './components/InspectorAdvancedControls'; |
| 3 | import GoogleFontLink from '../../components/google-font-link'; |
| 4 | import { applyFilters } from '@wordpress/hooks'; |
| 5 | import { Fragment, useEffect, useRef } from '@wordpress/element'; |
| 6 | import { compose } from '@wordpress/compose'; |
| 7 | import { withUniqueId, withContainerLegacyMigration, withDeviceType } from '../../hoc'; |
| 8 | import withDynamicContent from '../../extend/dynamic-content/hoc/withDynamicContent'; |
| 9 | import ContainerContentRenderer from './components/ContainerContentRenderer'; |
| 10 | import GenerateBlocksInspectorControls from '../../extend/inspector-control'; |
| 11 | import { withBlockContext } from '../../block-context'; |
| 12 | import { useSelect } from '@wordpress/data'; |
| 13 | import { withTemplateContext } from '../../extend/template-selector/templateContext'; |
| 14 | import getDeviceType from '../../utils/get-device-type'; |
| 15 | import withSetAttributes from '../../hoc/withSetAttributes'; |
| 16 | |
| 17 | const ContainerEdit = ( props ) => { |
| 18 | const { |
| 19 | clientId, |
| 20 | attributes, |
| 21 | setAttributes, |
| 22 | ContentRenderer = ContainerContentRenderer, |
| 23 | } = props; |
| 24 | |
| 25 | const { |
| 26 | anchor, |
| 27 | typography, |
| 28 | googleFont, |
| 29 | googleFontVariants, |
| 30 | isBlockPreview = false, |
| 31 | gridId, |
| 32 | isGrid, |
| 33 | isQueryLoopItem, |
| 34 | } = attributes; |
| 35 | |
| 36 | const ref = useRef( null ); |
| 37 | const deviceType = getDeviceType(); |
| 38 | |
| 39 | const allowedTagNames = applyFilters( |
| 40 | 'generateblocks.editor.allowedContainerTagNames', |
| 41 | [ |
| 42 | 'div', |
| 43 | 'article', |
| 44 | 'section', |
| 45 | 'header', |
| 46 | 'footer', |
| 47 | 'aside', |
| 48 | 'a', |
| 49 | ] |
| 50 | ); |
| 51 | |
| 52 | const filterTagName = ( tagValue ) => allowedTagNames.includes( tagValue ) ? tagValue : 'div'; |
| 53 | |
| 54 | const allShapes = []; |
| 55 | |
| 56 | Object.keys( generateBlocksInfo.svgShapes ).forEach( ( key ) => { |
| 57 | const shapes = generateBlocksInfo.svgShapes[ key ].svgs; |
| 58 | |
| 59 | Object.keys( shapes ).forEach( ( shapeName ) => { |
| 60 | allShapes[ shapeName ] = { |
| 61 | label: shapes[ shapeName ].label, |
| 62 | icon: shapes[ shapeName ].icon, |
| 63 | }; |
| 64 | } ); |
| 65 | } ); |
| 66 | |
| 67 | const { |
| 68 | getBlockParents, |
| 69 | getBlocksByClientId, |
| 70 | } = useSelect( ( select ) => select( 'core/block-editor' ), [] ); |
| 71 | |
| 72 | useEffect( () => { |
| 73 | const parentBlockId = getBlockParents( clientId, true ); |
| 74 | |
| 75 | if ( parentBlockId.length > 0 ) { |
| 76 | const parentBlocks = getBlocksByClientId( parentBlockId ); |
| 77 | |
| 78 | if ( parentBlocks.length > 0 ) { |
| 79 | if ( 'generateblocks/grid' === parentBlocks[ 0 ].name ) { |
| 80 | const parentGridId = parentBlocks[ 0 ].attributes.uniqueId; |
| 81 | |
| 82 | if ( parentGridId !== gridId ) { |
| 83 | setAttributes( { |
| 84 | isGrid: true, |
| 85 | gridId: parentGridId, |
| 86 | } ); |
| 87 | } |
| 88 | } else if ( isGrid && ! isQueryLoopItem ) { |
| 89 | // Grid block isn't the parent, can't be a grid item. |
| 90 | setAttributes( { |
| 91 | isGrid: false, |
| 92 | gridId: '', |
| 93 | } ); |
| 94 | } |
| 95 | } |
| 96 | } else if ( isGrid && ! isQueryLoopItem ) { |
| 97 | // No parent exists, can't be a grid item. |
| 98 | setAttributes( { |
| 99 | isGrid: false, |
| 100 | gridId: '', |
| 101 | } ); |
| 102 | } |
| 103 | } ); |
| 104 | |
| 105 | return ( |
| 106 | <Fragment> |
| 107 | <BlockControls |
| 108 | attributes={ attributes } |
| 109 | setAttributes={ setAttributes } |
| 110 | /> |
| 111 | |
| 112 | <GenerateBlocksInspectorControls |
| 113 | attributes={ attributes } |
| 114 | setAttributes={ setAttributes } |
| 115 | > |
| 116 | { applyFilters( 'generateblocks.editor.settingsPanel', undefined, { ...props, device: deviceType } ) } |
| 117 | </GenerateBlocksInspectorControls> |
| 118 | |
| 119 | <InspectorAdvancedControls |
| 120 | anchor={ anchor } |
| 121 | attributes={ attributes } |
| 122 | setAttributes={ setAttributes } |
| 123 | filterTagName={ filterTagName } |
| 124 | /> |
| 125 | |
| 126 | <GoogleFontLink |
| 127 | fontFamily={ typography.fontFamily } |
| 128 | googleFont={ googleFont } |
| 129 | googleFontVariants={ googleFontVariants } |
| 130 | isBlockPreview={ isBlockPreview } |
| 131 | /> |
| 132 | |
| 133 | <ContentRenderer |
| 134 | { ...props } |
| 135 | generateBlocksInfo={ generateBlocksInfo } |
| 136 | filterTagName={ filterTagName } |
| 137 | allShapes={ allShapes } |
| 138 | deviceType={ deviceType } |
| 139 | containerRef={ ref } |
| 140 | /> |
| 141 | </Fragment> |
| 142 | ); |
| 143 | }; |
| 144 | |
| 145 | export default compose( |
| 146 | withSetAttributes, |
| 147 | withDeviceType, |
| 148 | withTemplateContext, |
| 149 | withBlockContext, |
| 150 | withDynamicContent, |
| 151 | withUniqueId, |
| 152 | withContainerLegacyMigration, |
| 153 | )( ContainerEdit ); |
| 154 |