PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.2.0
GenerateBlocks v2.2.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 / element / edit.js
generateblocks / src / blocks / element Last commit date
components 1 year ago utils 1 year ago block.json 1 year ago edit.js 1 year ago editor.scss 1 year ago index.js 1 year ago save.js 1 year ago
edit.js
149 lines
1 import { useBlockProps, useInnerBlocksProps, InspectorControls } from '@wordpress/block-editor';
2 import { useEffect, useMemo } from '@wordpress/element';
3 import { compose } from '@wordpress/compose';
4 import { __ } from '@wordpress/i18n';
5 import { BlockStyles, withUniqueId } from '@edge22/block-styles';
6 import RootElement from '../../components/root-element/index.js';
7 import { BlockSettings } from './components/BlockSettings';
8 import { selectorShortcuts } from '@utils/selectorShortcuts.js';
9 import { withStyles } from '@hoc/withStyles';
10 import { AlignmentToolbar, BlockStylesBuilder, StylesOnboarder } from '@components/index.js';
11 import { withHtmlAttributes } from '@hoc/withHtmlAttributes.js';
12 import { getBlockClasses } from '@utils/getBlockClasses.js';
13 import { BlockAppender } from '@components';
14
15 function EditBlock( props ) {
16 const {
17 attributes,
18 setAttributes,
19 clientId,
20 isSelected,
21 name,
22 getStyleValue,
23 onStyleChange,
24 editorHtmlAttributes,
25 styles,
26 } = props;
27
28 const {
29 tagName,
30 align,
31 } = attributes;
32
33 const classNames = getBlockClasses(
34 'gb-element',
35 {
36 ...attributes,
37 styles,
38 }
39 );
40
41 useEffect( () => {
42 if ( ! tagName ) {
43 setAttributes( { tagName: 'div' } );
44 }
45 }, [ tagName ] );
46
47 const blockProps = useBlockProps(
48 {
49 className: classNames.join( ' ' ).trim(),
50 ...editorHtmlAttributes,
51 }
52 );
53 const innerBlocksProps = useInnerBlocksProps(
54 blockProps,
55 {
56 renderAppender: () => (
57 <BlockAppender
58 clientId={ clientId }
59 isSelected={ isSelected }
60 attributes={ attributes }
61 />
62 ),
63 }
64 );
65 const TagName = tagName || 'div';
66 const shortcuts = useMemo( () => {
67 const visibleSelectors = [];
68 const blockSelectors = { ...selectorShortcuts };
69
70 if ( 'a' !== tagName ) {
71 visibleSelectors.push(
72 {
73 label: __( 'Links', 'generateblocks' ),
74 value: 'a',
75 }
76 );
77 }
78
79 if ( 'a' === tagName || 'button' === tagName ) {
80 if ( blockSelectors?.links ) {
81 delete blockSelectors.links;
82 }
83
84 const defaultItems = blockSelectors?.default?.items || [];
85
86 if ( defaultItems.length > 0 ) {
87 blockSelectors.default.items = defaultItems.filter( ( item ) => {
88 return 'a' !== item.value && ! item.value.startsWith( 'a:' );
89 } );
90 }
91 }
92
93 return {
94 selectorShortcuts: blockSelectors,
95 visibleShortcuts: visibleSelectors,
96 };
97 }, [ tagName ] );
98
99 return (
100 <>
101 <InspectorControls>
102 <StylesOnboarder />
103
104 <AlignmentToolbar
105 withTextAlign
106 withBlockWidth
107 getStyleValue={ getStyleValue }
108 onStyleChange={ onStyleChange }
109 align={ align }
110 setAttributes={ setAttributes }
111 clientId={ clientId }
112 />
113
114 <BlockStyles
115 settingsTab={ (
116 <BlockSettings
117 { ...props }
118 />
119 ) }
120 stylesTab={ (
121 <BlockStylesBuilder
122 attributes={ attributes }
123 setAttributes={ setAttributes }
124 shortcuts={ shortcuts }
125 onStyleChange={ onStyleChange }
126 name={ name }
127 />
128 ) }
129 />
130 </InspectorControls>
131 <RootElement
132 name={ name }
133 clientId={ clientId }
134 align={ align }
135 >
136 <TagName { ...innerBlocksProps } />
137 </RootElement>
138 </>
139 );
140 }
141
142 const Edit = compose(
143 withHtmlAttributes,
144 withStyles,
145 withUniqueId
146 )( EditBlock );
147
148 export { Edit };
149