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 / loop-item / edit.js
generateblocks / src / blocks / loop-item Last commit date
components 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
164 lines
1 import { useBlockProps, InspectorControls, useInnerBlocksProps } from '@wordpress/block-editor';
2 import { useMemo, useEffect } from '@wordpress/element';
3 import { compose } from '@wordpress/compose';
4 import { __ } from '@wordpress/i18n';
5
6 import { BlockStyles, withUniqueId } from '@edge22/block-styles';
7
8 import { BlockSettings } from './components/BlockSettings';
9 import { selectorShortcuts } from '@utils/selectorShortcuts';
10 import { withStyles } from '@hoc/withStyles';
11 import { BlockStylesBuilder, BlockAppender } from '@components/index';
12 import { withHtmlAttributes } from '@hoc/withHtmlAttributes.js';
13 import { getBlockClasses } from '@utils/getBlockClasses';
14
15 import './editor.scss';
16
17 function EditBlock( props ) {
18 const {
19 attributes,
20 setAttributes,
21 clientId,
22 isSelected,
23 onStyleChange,
24 editorHtmlAttributes,
25 styles,
26 context,
27 name,
28 } = props;
29
30 const {
31 tagName,
32 } = attributes;
33
34 useEffect( () => {
35 if ( ! tagName ) {
36 setAttributes( { tagName: 'div' } );
37 }
38 }, [ tagName ] );
39
40 const classNames = getBlockClasses(
41 'gb-loop-item',
42 {
43 ...attributes,
44 styles,
45 },
46 true
47 );
48
49 const blockProps = useBlockProps( {
50 className: classNames.join( ' ' ).trim(),
51 ...editorHtmlAttributes,
52 } );
53
54 const innerBlocksProps = useInnerBlocksProps(
55 blockProps,
56 {
57 renderAppender: () => (
58 <BlockAppender
59 clientId={ clientId }
60 isSelected={ isSelected }
61 attributes={ attributes }
62 />
63 ),
64 }
65 );
66
67 const {
68 children: innerBlocksChildren,
69 ...otherInnerBlocksProps
70 } = innerBlocksProps;
71
72 const TagName = tagName || 'div';
73 const shortcuts = useMemo( () => {
74 const visibleSelectors = [];
75 const blockSelectors = { ...selectorShortcuts };
76
77 visibleSelectors.push(
78 {
79 label: __( 'Links', 'generateblocks' ),
80 value: 'a',
81 }
82 );
83
84 if ( 'a' === tagName || 'button' === tagName ) {
85 if ( blockSelectors?.links ) {
86 delete blockSelectors.links;
87 }
88
89 const defaultItems = blockSelectors?.default?.items || [];
90
91 if ( defaultItems.length > 0 ) {
92 blockSelectors.default.items = defaultItems.filter( ( item ) => {
93 return 'a' !== item.value && ! item.value.startsWith( 'a:' );
94 } );
95 }
96 }
97
98 return {
99 selectorShortcuts: blockSelectors,
100 visibleShortcuts: visibleSelectors,
101 };
102 }, [ tagName ] );
103
104 const contextPostId = context?.postId ?? context?.[ 'generateblocks/loopIndex' ] ?? 0;
105 const previewId = context?.[ 'generateblocks/loopPreviewId' ] ?? {};
106 const hasLoopItems = context?.[ 'generateblocks/hasLoopItems' ] ?? false;
107 const setPreviewId = context?.[ 'generateblocks/setLoopPreviewId' ] ?? null;
108 const queryId = context?.[ 'generateblocks/queryId' ] ?? '';
109 const itemPreviewId = previewId[ queryId ] || 0;
110
111 return (
112 <>
113 <InspectorControls>
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 <TagName { ...otherInnerBlocksProps }>
132 { innerBlocksChildren }
133 { ( !! hasLoopItems && itemPreviewId !== contextPostId ) && (
134 <button
135 className="gb-block-preview__toggle"
136 data-block-id={ clientId }
137 data-context-post-id={ contextPostId }
138 onClick={ () => {
139 if ( setPreviewId ) {
140 setPreviewId( ( prev ) => {
141 return {
142 ...prev,
143 [ queryId ]: contextPostId,
144 };
145 } );
146 }
147 } }
148 type="button"
149 aria-label={ __( 'Set this block as active', 'generateblocks' ) }
150 />
151 ) }
152 </TagName>
153 </>
154 );
155 }
156
157 const Edit = compose(
158 withHtmlAttributes,
159 withStyles,
160 withUniqueId
161 )( EditBlock );
162
163 export { Edit };
164