PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 1.7.2
GenerateBlocks v1.7.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 3 years ago css 3 years ago attributes.js 3 years ago block.js 3 years ago deprecated.js 5 years ago edit.js 3 years ago editor.scss 4 years ago
edit.js
144 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 { useInnerBlocksCount } from '../../hooks';
14 import { withUniqueId, withGridLegacyMigration, withDeviceType } from '../../hoc';
15 import withQueryLoop from '../query-loop/hoc/withQueryLoop';
16 import { withBlockContext } from '../../block-context';
17 import GenerateBlocksInspectorControls from '../../extend/inspector-control';
18 import './components/WidthControls';
19 import getDeviceType from '../../utils/get-device-type';
20
21 const GridEdit = ( props ) => {
22 const {
23 clientId,
24 attributes,
25 setAttributes,
26 InnerBlocksRenderer = InnerBlocks,
27 LayoutSelector = GridLayoutSelector,
28 defaultLayout = false,
29 templateLock = false,
30 context,
31 } = props;
32
33 const [ selectedLayout, setSelectedLayout ] = useState( false );
34 const deviceType = getDeviceType();
35 const innerBlocksCount = useInnerBlocksCount( clientId );
36
37 const { insertBlocks } = useDispatch( 'core/block-editor' );
38
39 useEffect( () => {
40 setAttributes( {
41 columns: innerBlocksCount,
42 } );
43 }, [ innerBlocksCount ] );
44
45 useEffect( () => {
46 const layout = defaultLayout || selectedLayout;
47
48 if ( ! attributes.isQueryLoop && layout ) {
49 const columnsData = getColumnsFromLayout( layout, attributes.uniqueId );
50
51 columnsData.forEach( ( colAttrs ) => {
52 insertBlocks(
53 createBlock( 'generateblocks/container', colAttrs ),
54 undefined,
55 props.clientId,
56 false
57 );
58 } );
59
60 setSelectedLayout( false );
61 }
62 }, [
63 selectedLayout,
64 defaultLayout,
65 attributes.uniqueId,
66 props.clientId,
67 attributes.isQueryLoop,
68 ] );
69
70 let htmlAttributes = {
71 className: classnames( {
72 'gb-grid-wrapper': true,
73 [ `gb-grid-wrapper-${ attributes.uniqueId }` ]: true,
74 [ `${ attributes.className }` ]: undefined !== attributes.className,
75 'gb-post-template': !! attributes.isQueryLoop,
76 [ `gb-post-template-${ attributes.uniqueId }` ]: !! attributes.isQueryLoop,
77 } ),
78 id: attributes.anchor ? attributes.anchor : null,
79 };
80
81 htmlAttributes = applyFilters( 'generateblocks.frontend.htmlAttributes', htmlAttributes, 'generateblocks/grid', attributes );
82
83 const blockProps = useBlockProps( htmlAttributes );
84
85 return (
86 <Fragment>
87 { ( ! attributes.isQueryLoop && ( attributes.columns > 0 || selectedLayout ) ) &&
88 <BlockControls uniqueId={ attributes.uniqueId } clientId={ props.clientId } />
89 }
90
91 <GenerateBlocksInspectorControls
92 attributes={ attributes }
93 setAttributes={ setAttributes }
94 >
95 { applyFilters( 'generateblocks.editor.settingsPanel', undefined, { ...props, device: deviceType } ) }
96
97 <InspectorControls
98 attributes={ attributes }
99 setAttributes={ setAttributes }
100 deviceType={ deviceType }
101 />
102 </GenerateBlocksInspectorControls>
103
104 <InspectorAdvancedControls
105 anchor={ attributes.anchor }
106 setAttributes={ setAttributes }
107 />
108
109 <ComponentCSS { ...props } deviceType={ deviceType } />
110
111 <div { ...blockProps }>
112 { ( attributes.isQueryLoop || attributes.columns > 0 || selectedLayout )
113 ? (
114 <InnerBlocksRenderer
115 templateLock={ templateLock }
116 allowedBlocks={ [ 'generateblocks/container' ] }
117 renderAppender={ false }
118 clientId={ clientId }
119 uniqueId={ attributes.uniqueId }
120 attributes={ attributes }
121 context={ context }
122 />
123 )
124 : (
125 <LayoutSelector
126 uniqueId={ attributes.uniqueId }
127 onClick={ setSelectedLayout }
128 isDisabled={ attributes?.isBlockPreview }
129 />
130 )
131 }
132 </div>
133 </Fragment>
134 );
135 };
136
137 export default compose(
138 withDeviceType,
139 withBlockContext,
140 withQueryLoop,
141 withUniqueId,
142 withGridLegacyMigration,
143 )( GridEdit );
144