Skip to content

Commit b52a06c

Browse files
authored
fix (timeline block): set attribute timelineIsLast (#3292)
* update timelineIsLast attribute * remove useBlockContext
1 parent 3a71d1c commit b52a06c

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/block/timeline/edit.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ import {
3535
getContentAlignmentClasses,
3636
Typography,
3737
} from '~stackable/block-components'
38-
import { useBlockContext, useDeviceType } from '~stackable/hooks'
38+
import { useDeviceType } from '~stackable/hooks'
3939
import {
4040
withBlockAttributeContext,
4141
withBlockWrapperIsHovered,
@@ -52,7 +52,7 @@ import {
5252
} from '@wordpress/i18n'
5353
import { InnerBlocks } from '@wordpress/block-editor'
5454
import { addFilter } from '@wordpress/hooks'
55-
import { dispatch } from '@wordpress/data'
55+
import { dispatch, useSelect } from '@wordpress/data'
5656
import {
5757
useEffect, useRef, useState, memo,
5858
} from '@wordpress/element'
@@ -96,9 +96,30 @@ const Edit = props => {
9696
const separatorClass = getSeparatorClasses( props.attributes )
9797
const blockAlignmentClass = getAlignmentClasses( props.attributes )
9898
const typographyClass = getTypographyClasses( props.attributes )
99+
99100
const {
100101
hasInnerBlocks, nextBlock, previousBlock, adjacentBlocks, blockIndex,
101-
} = useBlockContext()
102+
} = useSelect( select => {
103+
const {
104+
getBlockRootClientId, getBlocks, getBlockIndex,
105+
} = select( 'core/block-editor' )
106+
107+
const parentClientId = getBlockRootClientId( clientId )
108+
const adjacentBlocks = getBlocks( parentClientId )
109+
const blockIndex = getBlockIndex( clientId )
110+
const hasInnerBlocks = adjacentBlocks[ blockIndex ]?.innerBlocks?.length > 0
111+
const nextBlock = blockIndex < adjacentBlocks.length - 1 ? adjacentBlocks[ blockIndex + 1 ] : undefined
112+
const previousBlock = blockIndex > 0 ? adjacentBlocks[ blockIndex - 1 ] : undefined
113+
114+
return {
115+
hasInnerBlocks,
116+
nextBlock,
117+
previousBlock,
118+
adjacentBlocks,
119+
blockIndex,
120+
}
121+
}, [ clientId ] )
122+
102123
const deviceType = useDeviceType()
103124

104125
const middleRef = useRef()
@@ -302,7 +323,7 @@ const Edit = props => {
302323
dispatch( 'core/block-editor' ).__unstableMarkNextChangeAsNotPersistent()
303324
setAttributes( { timelineIsLast: false } )
304325
isUpdating = true
305-
} else if ( ( ! nextBlock || nextBlock.name !== 'stackable/timeline' ) && props.attributes.timelineIsLast ) {
326+
} else if ( ( ! nextBlock || nextBlock.name !== 'stackable/timeline' ) && ! props.attributes.timelineIsLast ) {
306327
dispatch( 'core/block-editor' ).__unstableMarkNextChangeAsNotPersistent()
307328
setAttributes( { timelineIsLast: true } )
308329
isUpdating = true

0 commit comments

Comments
 (0)