PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.5.2
GenerateBlocks v1.5.2
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / src / blocks / grid / edit.js
generateblocks / src / blocks / grid Last commit date
components 4 years ago css 4 years ago attributes.js 4 years ago block.js 4 years ago deprecated.js 5 years ago edit.js 4 years ago editor.scss 4 years ago
edit.js
133 lines
1 import { useDispatch } from '@wordpress/data';
2 import { Fragment, useEffect, useState } from '@wordpress/element';
3 import { InnerBlocks, useBlockProps } from '@wordpress/block-editor';
4 import GridLayoutSelector, { getColumnsFromLayout } from './components/LayoutSelector';
5 import { createBlock } from '@wordpress/blocks';
6 import BlockControls from './components/BlockControls';
7 import InspectorControls from './components/InspectorControls';
8 import InspectorAdvancedControls from './components/InspectorAdvancedControls';
9 import ComponentCSS from './components/ComponentCSS';
10 import classnames from 'classnames';
11 import { applyFilters } from '@wordpress/hooks';
12 import { compose } from '@wordpress/compose';
13 import { useDeviceType, useInnerBlocksCount } from '../../hooks';
14 import { withUniqueId, withGridLegacyMigration } from '../../hoc';
15 import withQueryLoop from '../query-loop/hoc/withQueryLoop';
16
17 const GridEdit = ( props ) => {
18 const {
19 clientId,
20 attributes,
21 setAttributes,
22 InnerBlocksRenderer = InnerBlocks,
23 LayoutSelector = GridLayoutSelector,
24 defaultLayout = false,
25 templateLock = false,
26 context,
27 } = props;
28
29 const [ selectedLayout, setSelectedLayout ] = useState( false );
30 const [ deviceType, setDeviceType ] = useDeviceType( 'Desktop' );
31 const innerBlocksCount = useInnerBlocksCount( clientId );
32
33 const { insertBlocks } = useDispatch( 'core/block-editor' );
34
35 useEffect( () => {
36 setAttributes( {
37 columns: innerBlocksCount,
38 } );
39 }, [ innerBlocksCount ] );
40
41 useEffect( () => {
42 const layout = defaultLayout || selectedLayout;
43
44 if ( ! attributes.isQueryLoop && layout ) {
45 const columnsData = getColumnsFromLayout( layout, attributes.uniqueId );
46
47 columnsData.forEach( ( colAttrs ) => {
48 insertBlocks(
49 createBlock( 'generateblocks/container', colAttrs ),
50 undefined,
51 props.clientId,
52 false
53 );
54 } );
55
56 setSelectedLayout( false );
57 }
58 }, [
59 selectedLayout,
60 defaultLayout,
61 attributes.uniqueId,
62 props.clientId,
63 attributes.isQueryLoop,
64 ] );
65
66 let htmlAttributes = {
67 className: classnames( {
68 'gb-grid-wrapper': true,
69 [ `gb-grid-wrapper-${ attributes.uniqueId }` ]: true,
70 [ `${ attributes.className }` ]: undefined !== attributes.className,
71 'gb-post-template': !! attributes.isQueryLoop,
72 [ `gb-post-template-${ attributes.uniqueId }` ]: !! attributes.isQueryLoop,
73 } ),
74 id: attributes.anchor ? attributes.anchor : null,
75 };
76
77 htmlAttributes = applyFilters( 'generateblocks.frontend.htmlAttributes', htmlAttributes, 'generateblocks/grid', attributes );
78
79 const blockProps = useBlockProps( htmlAttributes );
80
81 return (
82 <Fragment>
83 { ( ! attributes.isQueryLoop && ( attributes.columns > 0 || selectedLayout ) ) &&
84 <BlockControls uniqueId={ attributes.uniqueId } clientId={ props.clientId } />
85 }
86
87 <InspectorControls
88 { ...props }
89 state={ { selectedLayout, deviceType } }
90 deviceType={ deviceType }
91 setDeviceType={ setDeviceType }
92 blockDefaults={ generateBlocksDefaults.gridContainer }
93 />
94
95 <InspectorAdvancedControls
96 anchor={ attributes.anchor }
97 setAttributes={ setAttributes }
98 />
99
100 <ComponentCSS { ...props } deviceType={ deviceType } />
101
102 <div { ...blockProps }>
103 { ( attributes.isQueryLoop || attributes.columns > 0 || selectedLayout )
104 ? (
105 <InnerBlocksRenderer
106 templateLock={ templateLock }
107 allowedBlocks={ [ 'generateblocks/container' ] }
108 renderAppender={ false }
109 clientId={ clientId }
110 uniqueId={ attributes.uniqueId }
111 attributes={ attributes }
112 context={ context }
113 />
114 )
115 : (
116 <LayoutSelector
117 uniqueId={ attributes.uniqueId }
118 onClick={ setSelectedLayout }
119 isDisabled={ attributes?.isBlockPreview }
120 />
121 )
122 }
123 </div>
124 </Fragment>
125 );
126 };
127
128 export default compose(
129 withQueryLoop,
130 withUniqueId,
131 withGridLegacyMigration,
132 )( GridEdit );
133