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 / 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
163 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 {
76 label: __( 'Main', 'generateblocks' ),
77 value: '',
78 },
79 ];
80
81 if ( 'a' === tagName ) {
82 visibleSelectors.push(
83 {
84 label: __( 'Hover', 'generateblocks' ),
85 value: '&:is(:hover, :focus)',
86 }
87 );
88 }
89
90 visibleSelectors.push(
91 {
92 label: __( 'Links', 'generateblocks' ),
93 value: 'a',
94 }
95 );
96
97 return {
98 selectorShortcuts,
99 visibleShortcuts: visibleSelectors,
100 };
101 }, [ tagName ] );
102
103 const contextPostId = context?.postId ?? context?.[ 'generateblocks/loopIndex' ] ?? 0;
104 const previewId = context?.[ 'generateblocks/loopPreviewId' ] ?? {};
105 const hasLoopItems = context?.[ 'generateblocks/hasLoopItems' ] ?? false;
106 const setPreviewId = context?.[ 'generateblocks/setLoopPreviewId' ] ?? null;
107 const queryId = context?.[ 'generateblocks/queryId' ] ?? '';
108 const itemPreviewId = previewId[ queryId ] || 0;
109
110 return (
111 <>
112 <InspectorControls>
113 <BlockStyles
114 settingsTab={ (
115 <BlockSettings
116 { ...props }
117 />
118 ) }
119 stylesTab={ (
120 <BlockStylesBuilder
121 attributes={ attributes }
122 setAttributes={ setAttributes }
123 shortcuts={ shortcuts }
124 onStyleChange={ onStyleChange }
125 name={ name }
126 />
127 ) }
128 />
129 </InspectorControls>
130 <TagName { ...otherInnerBlocksProps }>
131 { innerBlocksChildren }
132 { ( !! hasLoopItems && itemPreviewId !== contextPostId ) && (
133 <button
134 className="gb-block-preview__toggle"
135 data-block-id={ clientId }
136 data-context-post-id={ contextPostId }
137 onClick={ () => {
138 if ( setPreviewId ) {
139 setPreviewId( ( prev ) => {
140 return {
141 ...prev,
142 [ queryId ]: contextPostId,
143 };
144 } );
145 }
146 } }
147 type="button"
148 aria-label={ __( 'Set this block as active', 'generateblocks' ) }
149 />
150 ) }
151 </TagName>
152 </>
153 );
154 }
155
156 const Edit = compose(
157 withHtmlAttributes,
158 withStyles,
159 withUniqueId
160 )( EditBlock );
161
162 export { Edit };
163