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