Skip to content

Commit 8e75e0e

Browse files
authored
fix (performance): Performance Cleanup (#3320)
* update timelineIsLast attribute * remove useBlockContext * remove useBlockContext * remove useGeneratedCss * do not use stackable/block-context * remove hook
1 parent 86d143f commit 8e75e0e

File tree

32 files changed

+237
-312
lines changed

32 files changed

+237
-312
lines changed

src/block-components/column/use-column.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ export const useColumn = () => {
1010
const { clientId } = useBlockEditContext()
1111

1212
const onChangeDesktop = widths => {
13-
const { parentBlock } = select( 'stackable/block-context' ).getBlockContext( clientId )
13+
const parentBlockId = select( 'core/block-editor' ).getBlockRootClientId( clientId )
14+
const parentBlock = select( 'core/block-editor' ).getBlock( parentBlockId )
1415
const adjacentBlocks = parentBlock?.innerBlocks || []
1516

1617
if ( adjacentBlocks.length ) {
@@ -36,7 +37,8 @@ export const useColumn = () => {
3637
[ clientId ]: { columnWidth: width },
3738
}
3839

39-
const { parentBlock } = select( 'stackable/block-context' ).getBlockContext( clientId )
40+
const parentBlockId = select( 'core/block-editor' ).getBlockRootClientId( clientId )
41+
const parentBlock = select( 'core/block-editor' ).getBlock( parentBlockId )
4042
const adjacentBlocks = parentBlock?.innerBlocks || []
4143

4244
if ( adjacentBlocks.length ) {
@@ -63,7 +65,8 @@ export const useColumn = () => {
6365
[ clientId ]: { columnWidthTablet: width },
6466
}
6567

66-
const { parentBlock } = select( 'stackable/block-context' ).getBlockContext( clientId )
68+
const parentBlockId = select( 'core/block-editor' ).getBlockRootClientId( clientId )
69+
const parentBlock = select( 'core/block-editor' ).getBlock( parentBlockId )
6770
const adjacentBlocks = parentBlock?.innerBlocks || []
6871

6972
if ( adjacentBlocks.length ) {
@@ -89,7 +92,8 @@ export const useColumn = () => {
8992
[ clientId ]: { columnWidthMobile: width },
9093
}
9194

92-
const { parentBlock } = select( 'stackable/block-context' ).getBlockContext( clientId )
95+
const parentBlockId = select( 'core/block-editor' ).getBlockRootClientId( clientId )
96+
const parentBlock = select( 'core/block-editor' ).getBlock( parentBlockId )
9397
const adjacentBlocks = parentBlock?.innerBlocks || []
9498

9599
if ( adjacentBlocks.length ) {
@@ -110,7 +114,8 @@ export const useColumn = () => {
110114
}
111115

112116
const onResetDesktop = () => {
113-
const { parentBlock } = select( 'stackable/block-context' ).getBlockContext( clientId )
117+
const parentBlockId = select( 'core/block-editor' ).getBlockRootClientId( clientId )
118+
const parentBlock = select( 'core/block-editor' ).getBlock( parentBlockId )
114119
const adjacentBlocks = parentBlock?.innerBlocks || []
115120

116121
if ( adjacentBlocks.length ) {
@@ -120,7 +125,8 @@ export const useColumn = () => {
120125
}
121126

122127
const onResetTabletMobile = () => {
123-
const { parentBlock } = select( 'stackable/block-context' ).getBlockContext( clientId )
128+
const parentBlockId = select( 'core/block-editor' ).getBlockRootClientId( clientId )
129+
const parentBlock = select( 'core/block-editor' ).getBlock( parentBlockId )
124130
const adjacentBlocks = parentBlock?.innerBlocks || []
125131

126132
if ( adjacentBlocks.length ) {

src/block-components/columns/column-settings-button.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ export const ColumnsControl = props => {
5454
}
5555

5656
multiClientIds.forEach( clientId => {
57-
const { numInnerBlocks, innerBlocks } = select( 'stackable/block-context' ).getBlockContext( clientId )
57+
const innerBlocks = select( 'core/block-editor' ).getBlock( clientId )?.innerBlocks
58+
const numInnerBlocks = innerBlocks.length
5859
multiInnerBlocks[ clientId ] = innerBlocks
5960
multiNumInnerBlocks[ clientId ] = numInnerBlocks
6061
} )

src/block-components/columns/edit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export const Controls = props => {
6161
const multiInnerBlocks = {}
6262

6363
multiClientIds.forEach( clientId => {
64-
const { innerBlocks } = select( 'stackable/block-context' ).getBlockContext( clientId )
64+
const innerBlocks = select( 'core/block-editor' ).getBlock( clientId )?.innerBlocks
6565
multiInnerBlocks[ clientId ] = innerBlocks
6666
} )
6767

src/block-components/image/index.js

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ import Image_ from './image'
1010
/**
1111
* External dependencies
1212
*/
13-
import { useBlockAttributesContext, useBlockContext } from '~stackable/hooks'
13+
import { useBlockAttributesContext } from '~stackable/hooks'
1414
import { pickBy } from 'lodash'
1515

1616
/**
1717
* WordPress dependencies
1818
*/
1919
import { useBlockEditContext } from '@wordpress/block-editor'
2020
import { applyFilters } from '@wordpress/hooks'
21+
import { useSelect } from '@wordpress/data'
2122

2223
export { deprecationImageOverlayOpacity } from './deprecated'
2324

@@ -28,7 +29,7 @@ export const Image = props => {
2829
...propsToPass
2930
} = props
3031

31-
const { isSelected } = useBlockEditContext()
32+
const { isSelected, clientId } = useBlockEditContext()
3233
const attributes = useBlockAttributesContext( attributes => {
3334
return {
3435
imageOverlayColorType: attributes.imageOverlayColorType,
@@ -64,7 +65,13 @@ export const Image = props => {
6465
figcaptionShow: attributes.figcaptionShow,
6566
}
6667
} )
67-
const { parentBlock } = useBlockContext()
68+
const { parentBlock } = useSelect( select => {
69+
const { getBlockRootClientId, getBlock } = select( 'core/block-editor' )
70+
const parentClientId = getBlockRootClientId( clientId )
71+
return {
72+
parentBlock: getBlock( parentClientId ),
73+
}
74+
}, [ clientId ] )
6875

6976
const { setImage } = useImage()
7077

src/block-components/linking/index.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import classnames from 'classnames'
22
import { i18n } from 'stackable'
3-
import { useBlockContext, useLinking } from '~stackable/hooks'
3+
import { useLinking } from '~stackable/hooks'
44
import { useClosestLinkableBlock } from '~stackable/plugins/block-linking'
55
import { Tooltip } from '~stackable/components'
66

77
import { Dashicon } from '@wordpress/components'
88
import { useBlockEditContext } from '@wordpress/block-editor'
99
import { __ } from '@wordpress/i18n'
1010
import { getPlugin } from '@wordpress/plugins'
11+
import { useSelect } from '@wordpress/data'
1112

1213
// We split this off because we use hooks that won't allow conditional rendering
1314
// for the Linking component.
@@ -19,8 +20,14 @@ export const Linking = props => {
1920
export const _Linking = () => {
2021
const [ isLinked, setIsLinked ] = useLinking()
2122

22-
const { isOnlyBlock } = useBlockContext()
2323
const { clientId } = useBlockEditContext()
24+
const { isOnlyBlock } = useSelect( select => {
25+
const { getBlockRootClientId, getBlock } = select( 'core/block-editor' )
26+
const parentClientId = getBlockRootClientId( clientId )
27+
return {
28+
isOnlyBlock: getBlock( parentClientId ).innerBlocks.length === 1,
29+
}
30+
}, [ clientId ] )
2431

2532
const closestLinkableBlock = useClosestLinkableBlock( clientId )
2633

src/block-components/style/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,4 +389,3 @@ export const mergeStyles = ( styles, important = true ) => {
389389

390390
Style.addAttributes = addAttributes
391391

392-
export * from './use-generated-css'

src/block-components/style/use-generated-css.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/block/accordion/edit.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import {
3232
Transform,
3333
} from '~stackable/block-components'
3434
import {
35-
useBlockAttributesContext, useBlockContext, useBlockSetAttributesContext,
35+
useBlockAttributesContext, useBlockSetAttributesContext,
3636
} from '~stackable/hooks'
3737
import {
3838
withBlockAttributeContext, withBlockWrapperIsHovered, withQueryLoopContext,
@@ -46,7 +46,7 @@ import variations, { defaultIcon } from './variations'
4646
/**
4747
* WordPress dependencies
4848
*/
49-
import { InnerBlocks } from '@wordpress/block-editor'
49+
import { InnerBlocks, useBlockEditContext } from '@wordpress/block-editor'
5050
import { __ } from '@wordpress/i18n'
5151
import { compose } from '@wordpress/compose'
5252
import { useSelect } from '@wordpress/data'
@@ -64,7 +64,12 @@ const Edit = props => {
6464
} = props
6565

6666
const [ isOpen, setIsOpen ] = useState( props.attributes.startOpen )
67-
const { hasInnerBlocks } = useBlockContext()
67+
const { hasInnerBlocks } = useSelect( select => {
68+
const { getBlockOrder } = select( 'core/block-editor' )
69+
return {
70+
hasInnerBlocks: getBlockOrder( clientId ).length > 0,
71+
}
72+
}, [ clientId ] )
6873
const [ hasInitClickHandler, setHasInitClickHandler ] = useState( false )
6974
const { getEditorDom } = useSelect( 'stackable/editor-dom' )
7075

@@ -215,8 +220,15 @@ addFilter( 'stackable.block-component.icon.after', 'stackable/blockquote', outpu
215220
const icon2 = useBlockAttributesContext( attributes => attributes.icon2 )
216221
const setAttributes = useBlockSetAttributesContext()
217222

218-
const { parentTree } = useBlockContext()
219-
const { getBlock } = useSelect( 'core/block-editor' )
223+
const { clientId } = useBlockEditContext()
224+
const { parentTree, getBlock } = useSelect( select => {
225+
const { getBlock, getBlockParents } = select( 'core/block-editor' )
226+
const parentTree = getBlockParents( clientId ).map( parentClientId => ( { clientId: parentClientId, name: getBlock( parentClientId ).name } ) )
227+
return {
228+
getBlock,
229+
parentTree,
230+
}
231+
}, [ clientId ] )
220232
const { getActiveBlockVariation } = useSelect( 'core/blocks' )
221233

222234
const accordionBlock = findLast( parentTree, pt => pt.name === 'stackable/accordion' )
@@ -259,8 +271,15 @@ addFilter( 'stackable.block-default-styles.use-saved-style', 'stackable/icon-lab
259271

260272
// Return default icon for accordion
261273
addFilter( 'stackable.block-component.icon.default', 'stackable/accordion', starIcon => {
262-
const { parentTree } = useBlockContext()
263-
const { getBlock } = useSelect( 'core/block-editor' )
274+
const { clientId } = useBlockEditContext()
275+
const { parentTree, getBlock } = useSelect( select => {
276+
const { getBlock, getBlockParents } = select( 'core/block-editor' )
277+
const parentTree = getBlockParents( clientId ).map( parentClientId => ( { clientId: parentClientId, name: getBlock( parentClientId ).name } ) )
278+
return {
279+
getBlock,
280+
parentTree,
281+
}
282+
}, [ clientId ] )
264283
const { getActiveBlockVariation } = useSelect( 'core/blocks' )
265284

266285
const accordionBlock = findLast( parentTree, pt => pt.name === 'stackable/accordion' )

src/block/blockquote/edit.js

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import {
3232
MarginBottom,
3333
Transform,
3434
} from '~stackable/block-components'
35-
import { useBlockContext } from '~stackable/hooks'
3635
import {
3736
withBlockAttributeContext,
3837
withBlockWrapperIsHovered,
@@ -42,11 +41,12 @@ import {
4241
/**
4342
* WordPress dependencies
4443
*/
45-
import { InnerBlocks } from '@wordpress/block-editor'
44+
import { InnerBlocks, useBlockEditContext } from '@wordpress/block-editor'
4645
import { compose } from '@wordpress/compose'
4746
import { renderToString, memo } from '@wordpress/element'
4847
import { __ } from '@wordpress/i18n'
4948
import { addFilter } from '@wordpress/hooks'
49+
import { useSelect } from '@wordpress/data'
5050

5151
export const defaultIcon = renderToString( <SVGDefaultQuote /> )
5252

@@ -58,7 +58,12 @@ const Edit = props => {
5858
} = props
5959

6060
const blockAlignmentClass = getAlignmentClasses( props.attributes )
61-
const { hasInnerBlocks } = useBlockContext()
61+
const { hasInnerBlocks } = useSelect( select => {
62+
const { getBlockOrder } = select( 'core/block-editor' )
63+
return {
64+
hasInnerBlocks: getBlockOrder( props.clientId ).length > 0,
65+
}
66+
}, [ props.clientId ] )
6267

6368
const blockClassNames = classnames( [
6469
className,
@@ -146,7 +151,14 @@ addFilter( 'stackable.edit.margin-bottom.enable-handlers', 'stackable/blockquote
146151

147152
// Add more quotation mark icons in the Icon Block if it's inside the blockquote block.
148153
addFilter( 'stackable.block-component.icon.after', 'stackable/blockquote', output => {
149-
const { parentBlock } = useBlockContext()
154+
const { clientId } = useBlockEditContext()
155+
const { parentBlock } = useSelect( select => {
156+
const { getBlockRootClientId, getBlock } = select( 'core/block-editor' )
157+
const parentClientId = getBlockRootClientId( clientId )
158+
return {
159+
parentBlock: getBlock( parentClientId ),
160+
}
161+
}, [ clientId ] )
150162
if ( parentBlock?.name === 'stackable/blockquote' ) {
151163
return (
152164
<>
@@ -165,7 +177,14 @@ addFilter( 'stackable.block-component.icon.after', 'stackable/blockquote', outpu
165177

166178
// Change the icon picker label for the Icon Block if it's inside the blockquote block.
167179
addFilter( 'stackable.block-component.icon.label', 'stackable/blockquote', label => {
168-
const { parentBlock } = useBlockContext()
180+
const { clientId } = useBlockEditContext()
181+
const { parentBlock } = useSelect( select => {
182+
const { getBlockRootClientId, getBlock } = select( 'core/block-editor' )
183+
const parentClientId = getBlockRootClientId( clientId )
184+
return {
185+
parentBlock: getBlock( parentClientId ),
186+
}
187+
}, [ clientId ] )
169188
if ( parentBlock?.name === 'stackable/blockquote' ) {
170189
return __( 'Pick another icon', i18n )
171190
}

src/block/call-to-action/edit.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import {
3535
ContentAlign,
3636
getContentAlignmentClasses,
3737
} from '~stackable/block-components'
38-
import { useBlockContext } from '~stackable/hooks'
3938
import {
4039
withBlockAttributeContext, withBlockWrapperIsHovered, withQueryLoopContext,
4140
} from '~stackable/higher-order'
@@ -47,6 +46,7 @@ import { InnerBlocks } from '@wordpress/block-editor'
4746
import { compose } from '@wordpress/compose'
4847
import { __ } from '@wordpress/i18n'
4948
import { memo } from '@wordpress/element'
49+
import { useSelect } from '@wordpress/data'
5050

5151
const TEMPLATE = variations[ 0 ].innerBlocks
5252

@@ -55,7 +55,14 @@ const Edit = props => {
5555
className,
5656
} = props
5757

58-
const { hasInnerBlocks, innerBlocks } = useBlockContext()
58+
const { hasInnerBlocks, innerBlocks } = useSelect( select => {
59+
const { getBlock } = select( 'core/block-editor' )
60+
const innerBlocks = getBlock( props.clientId ).innerBlocks
61+
return {
62+
hasInnerBlocks: innerBlocks.length > 0,
63+
innerBlocks,
64+
}
65+
}, [ props.clientId ] )
5966
const blockAlignmentClass = getAlignmentClasses( props.attributes )
6067
const separatorClass = getSeparatorClasses( props.attributes )
6168

0 commit comments

Comments
 (0)