-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathedit-wrapper.js
47 lines (41 loc) · 1.46 KB
/
edit-wrapper.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* WordPress dependencies
*/
import { store as blockEditorStore } from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import EditWithInnerBlocks from './edit';
import EditWithoutInnerBlocks from './v1/edit';
/*
* Using a wrapper around the logic to load the edit for v1 of Gallery block
* or the refactored version with InnerBlocks. This is to prevent conditional
* use of hooks lint errors if adding this logic to the top of the edit component.
*/
export default function GalleryEditWrapper( props ) {
const { attributes, clientId } = props;
const innerBlockImages = useSelect(
( select ) => {
return select( blockEditorStore ).getBlock( clientId )?.innerBlocks;
},
[ clientId ]
);
const __unstableGalleryWithImageBlocks = useSelect( ( select ) => {
const settings = select( blockEditorStore ).getSettings();
return settings.__unstableGalleryWithImageBlocks;
}, [] );
// This logic is used to infer version information from content with higher
// precedence than the flag. New galleries (and existing empty galleries) will
// honor the flag.
const hasNewVersionContent = !! innerBlockImages?.length;
const hasOldVersionContent =
0 < attributes?.ids?.length || 0 < attributes?.images?.length;
if (
hasOldVersionContent ||
( ! hasNewVersionContent && ! __unstableGalleryWithImageBlocks )
) {
return <EditWithoutInnerBlocks { ...props } />;
}
return <EditWithInnerBlocks { ...props } />;
}