1
1
import { useCallback } from "@wordpress/element" ;
2
2
import { useDispatch , useSelect } from "@wordpress/data" ;
3
3
4
- import { getChildAttributes } from "../utils" ;
5
4
import { getAlignFromMatrix } from "@novablocks/utils" ;
5
+ import { useInnerBlocks } from "@novablocks/block-editor" ;
6
+
7
+ import { getChildAttributes } from "../utils" ;
6
8
7
9
const withSetChildrenAttributes = OriginalComponent => {
8
10
@@ -15,6 +17,7 @@ const withSetChildrenAttributes = OriginalComponent => {
15
17
const { clientId } = props ;
16
18
const { updateBlockAttributes } = useDispatch ( 'core/block-editor' ) ;
17
19
const { getBlock } = useSelect ( select => select ( 'core/block-editor' ) ) ;
20
+ const innerBlocks = useInnerBlocks ( clientId ) ;
18
21
19
22
const normalizeAttributes = useCallback ( attributes => {
20
23
const { cardLayout } = attributes ;
@@ -32,37 +35,38 @@ const withSetChildrenAttributes = OriginalComponent => {
32
35
} , [ ] ) ;
33
36
34
37
const setChildrenAttributes = useCallback ( attributes => {
35
- const { innerBlocks } = getBlock ( clientId ) ;
36
38
const newAttributes = getChildAttributes ( attributes ) ;
37
39
38
- if ( attributes . contentPosition && Array . isArray ( innerBlocks ) ) {
40
+ if ( Array . isArray ( innerBlocks ) ) {
39
41
innerBlocks . filter ( block => block . name === 'novablocks/supernova-item' ) . forEach ( block => {
40
42
updateBlockAttributes ( block . clientId , newAttributes ) ;
41
43
42
- const contentBlocks = getBlock ( block . clientId ) . innerBlocks ;
43
- const contentAlign = getAlignFromMatrix ( attributes ?. contentPosition ) ;
44
+ if ( attributes . contentPosition ) {
45
+ const contentBlocks = getBlock ( block . clientId ) . innerBlocks ;
46
+ const contentAlign = getAlignFromMatrix ( attributes ?. contentPosition ) ;
44
47
45
- if ( Array . isArray ( contentBlocks ) ) {
48
+ if ( Array . isArray ( contentBlocks ) ) {
46
49
47
- contentBlocks . filter ( block => block . name === 'novablocks/headline' ) . forEach ( block => {
48
- updateBlockAttributes ( block . clientId , { textAlign : contentAlign [ 1 ] } ) ;
49
- } )
50
+ contentBlocks . filter ( block => block . name === 'novablocks/headline' ) . forEach ( block => {
51
+ updateBlockAttributes ( block . clientId , { textAlign : contentAlign [ 1 ] } ) ;
52
+ } )
50
53
51
- contentBlocks . filter ( block => block . name === 'core/heading' ) . forEach ( block => {
52
- updateBlockAttributes ( block . clientId , { textAlign : contentAlign [ 1 ] } ) ;
53
- } )
54
+ contentBlocks . filter ( block => block . name === 'core/heading' ) . forEach ( block => {
55
+ updateBlockAttributes ( block . clientId , { textAlign : contentAlign [ 1 ] } ) ;
56
+ } )
54
57
55
- contentBlocks . filter ( block => block . name === 'core/paragraph' ) . forEach ( block => {
56
- updateBlockAttributes ( block . clientId , { align : contentAlign [ 1 ] } ) ;
57
- } )
58
+ contentBlocks . filter ( block => block . name === 'core/paragraph' ) . forEach ( block => {
59
+ updateBlockAttributes ( block . clientId , { align : contentAlign [ 1 ] } ) ;
60
+ } )
58
61
59
- contentBlocks . filter ( block => block . name === 'core/buttons' ) . forEach ( block => {
60
- updateBlockAttributes ( block . clientId , { layout : { ...block . attributes . layout , type : 'flex' , justifyContent : contentAlign [ 1 ] } } ) ;
61
- } )
62
+ contentBlocks . filter ( block => block . name === 'core/buttons' ) . forEach ( block => {
63
+ updateBlockAttributes ( block . clientId , { layout : { ...block . attributes . layout , type : 'flex' , justifyContent : contentAlign [ 1 ] } } ) ;
64
+ } )
65
+ }
62
66
}
63
67
} ) ;
64
68
}
65
- } , [ clientId ] ) ;
69
+ } , [ innerBlocks ] ) ;
66
70
67
71
return (
68
72
< OriginalComponent { ...props } setAttributes = { attributes => {
0 commit comments