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