Skip to content

Commit

Permalink
fix (performance): Performance Cleanup (#3320)
Browse files Browse the repository at this point in the history
* update timelineIsLast attribute

* remove useBlockContext

* remove useBlockContext

* remove useGeneratedCss

* do not use stackable/block-context

* remove hook
  • Loading branch information
mxkae authored Nov 5, 2024
1 parent 86d143f commit 8e75e0e
Show file tree
Hide file tree
Showing 32 changed files with 237 additions and 312 deletions.
18 changes: 12 additions & 6 deletions src/block-components/column/use-column.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ export const useColumn = () => {
const { clientId } = useBlockEditContext()

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

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

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

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

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

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

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

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

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

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

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

if ( adjacentBlocks.length ) {
Expand Down
3 changes: 2 additions & 1 deletion src/block-components/columns/column-settings-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ export const ColumnsControl = props => {
}

multiClientIds.forEach( clientId => {
const { numInnerBlocks, innerBlocks } = select( 'stackable/block-context' ).getBlockContext( clientId )
const innerBlocks = select( 'core/block-editor' ).getBlock( clientId )?.innerBlocks
const numInnerBlocks = innerBlocks.length
multiInnerBlocks[ clientId ] = innerBlocks
multiNumInnerBlocks[ clientId ] = numInnerBlocks
} )
Expand Down
2 changes: 1 addition & 1 deletion src/block-components/columns/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const Controls = props => {
const multiInnerBlocks = {}

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

Expand Down
13 changes: 10 additions & 3 deletions src/block-components/image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ import Image_ from './image'
/**
* External dependencies
*/
import { useBlockAttributesContext, useBlockContext } from '~stackable/hooks'
import { useBlockAttributesContext } from '~stackable/hooks'
import { pickBy } from 'lodash'

/**
* WordPress dependencies
*/
import { useBlockEditContext } from '@wordpress/block-editor'
import { applyFilters } from '@wordpress/hooks'
import { useSelect } from '@wordpress/data'

export { deprecationImageOverlayOpacity } from './deprecated'

Expand All @@ -28,7 +29,7 @@ export const Image = props => {
...propsToPass
} = props

const { isSelected } = useBlockEditContext()
const { isSelected, clientId } = useBlockEditContext()
const attributes = useBlockAttributesContext( attributes => {
return {
imageOverlayColorType: attributes.imageOverlayColorType,
Expand Down Expand Up @@ -64,7 +65,13 @@ export const Image = props => {
figcaptionShow: attributes.figcaptionShow,
}
} )
const { parentBlock } = useBlockContext()
const { parentBlock } = useSelect( select => {
const { getBlockRootClientId, getBlock } = select( 'core/block-editor' )
const parentClientId = getBlockRootClientId( clientId )
return {
parentBlock: getBlock( parentClientId ),
}
}, [ clientId ] )

const { setImage } = useImage()

Expand Down
11 changes: 9 additions & 2 deletions src/block-components/linking/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import classnames from 'classnames'
import { i18n } from 'stackable'
import { useBlockContext, useLinking } from '~stackable/hooks'
import { useLinking } from '~stackable/hooks'
import { useClosestLinkableBlock } from '~stackable/plugins/block-linking'
import { Tooltip } from '~stackable/components'

import { Dashicon } from '@wordpress/components'
import { useBlockEditContext } from '@wordpress/block-editor'
import { __ } from '@wordpress/i18n'
import { getPlugin } from '@wordpress/plugins'
import { useSelect } from '@wordpress/data'

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

const { isOnlyBlock } = useBlockContext()
const { clientId } = useBlockEditContext()
const { isOnlyBlock } = useSelect( select => {
const { getBlockRootClientId, getBlock } = select( 'core/block-editor' )
const parentClientId = getBlockRootClientId( clientId )
return {
isOnlyBlock: getBlock( parentClientId ).innerBlocks.length === 1,
}
}, [ clientId ] )

const closestLinkableBlock = useClosestLinkableBlock( clientId )

Expand Down
1 change: 0 additions & 1 deletion src/block-components/style/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,3 @@ export const mergeStyles = ( styles, important = true ) => {

Style.addAttributes = addAttributes

export * from './use-generated-css'
10 changes: 0 additions & 10 deletions src/block-components/style/use-generated-css.js

This file was deleted.

33 changes: 26 additions & 7 deletions src/block/accordion/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
Transform,
} from '~stackable/block-components'
import {
useBlockAttributesContext, useBlockContext, useBlockSetAttributesContext,
useBlockAttributesContext, useBlockSetAttributesContext,
} from '~stackable/hooks'
import {
withBlockAttributeContext, withBlockWrapperIsHovered, withQueryLoopContext,
Expand All @@ -46,7 +46,7 @@ import variations, { defaultIcon } from './variations'
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor'
import { InnerBlocks, useBlockEditContext } from '@wordpress/block-editor'
import { __ } from '@wordpress/i18n'
import { compose } from '@wordpress/compose'
import { useSelect } from '@wordpress/data'
Expand All @@ -64,7 +64,12 @@ const Edit = props => {
} = props

const [ isOpen, setIsOpen ] = useState( props.attributes.startOpen )
const { hasInnerBlocks } = useBlockContext()
const { hasInnerBlocks } = useSelect( select => {
const { getBlockOrder } = select( 'core/block-editor' )
return {
hasInnerBlocks: getBlockOrder( clientId ).length > 0,
}
}, [ clientId ] )
const [ hasInitClickHandler, setHasInitClickHandler ] = useState( false )
const { getEditorDom } = useSelect( 'stackable/editor-dom' )

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

const { parentTree } = useBlockContext()
const { getBlock } = useSelect( 'core/block-editor' )
const { clientId } = useBlockEditContext()
const { parentTree, getBlock } = useSelect( select => {
const { getBlock, getBlockParents } = select( 'core/block-editor' )
const parentTree = getBlockParents( clientId ).map( parentClientId => ( { clientId: parentClientId, name: getBlock( parentClientId ).name } ) )
return {
getBlock,
parentTree,
}
}, [ clientId ] )
const { getActiveBlockVariation } = useSelect( 'core/blocks' )

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

// Return default icon for accordion
addFilter( 'stackable.block-component.icon.default', 'stackable/accordion', starIcon => {
const { parentTree } = useBlockContext()
const { getBlock } = useSelect( 'core/block-editor' )
const { clientId } = useBlockEditContext()
const { parentTree, getBlock } = useSelect( select => {
const { getBlock, getBlockParents } = select( 'core/block-editor' )
const parentTree = getBlockParents( clientId ).map( parentClientId => ( { clientId: parentClientId, name: getBlock( parentClientId ).name } ) )
return {
getBlock,
parentTree,
}
}, [ clientId ] )
const { getActiveBlockVariation } = useSelect( 'core/blocks' )

const accordionBlock = findLast( parentTree, pt => pt.name === 'stackable/accordion' )
Expand Down
29 changes: 24 additions & 5 deletions src/block/blockquote/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
MarginBottom,
Transform,
} from '~stackable/block-components'
import { useBlockContext } from '~stackable/hooks'
import {
withBlockAttributeContext,
withBlockWrapperIsHovered,
Expand All @@ -42,11 +41,12 @@ import {
/**
* WordPress dependencies
*/
import { InnerBlocks } from '@wordpress/block-editor'
import { InnerBlocks, useBlockEditContext } from '@wordpress/block-editor'
import { compose } from '@wordpress/compose'
import { renderToString, memo } from '@wordpress/element'
import { __ } from '@wordpress/i18n'
import { addFilter } from '@wordpress/hooks'
import { useSelect } from '@wordpress/data'

export const defaultIcon = renderToString( <SVGDefaultQuote /> )

Expand All @@ -58,7 +58,12 @@ const Edit = props => {
} = props

const blockAlignmentClass = getAlignmentClasses( props.attributes )
const { hasInnerBlocks } = useBlockContext()
const { hasInnerBlocks } = useSelect( select => {
const { getBlockOrder } = select( 'core/block-editor' )
return {
hasInnerBlocks: getBlockOrder( props.clientId ).length > 0,
}
}, [ props.clientId ] )

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

// Add more quotation mark icons in the Icon Block if it's inside the blockquote block.
addFilter( 'stackable.block-component.icon.after', 'stackable/blockquote', output => {
const { parentBlock } = useBlockContext()
const { clientId } = useBlockEditContext()
const { parentBlock } = useSelect( select => {
const { getBlockRootClientId, getBlock } = select( 'core/block-editor' )
const parentClientId = getBlockRootClientId( clientId )
return {
parentBlock: getBlock( parentClientId ),
}
}, [ clientId ] )
if ( parentBlock?.name === 'stackable/blockquote' ) {
return (
<>
Expand All @@ -165,7 +177,14 @@ addFilter( 'stackable.block-component.icon.after', 'stackable/blockquote', outpu

// Change the icon picker label for the Icon Block if it's inside the blockquote block.
addFilter( 'stackable.block-component.icon.label', 'stackable/blockquote', label => {
const { parentBlock } = useBlockContext()
const { clientId } = useBlockEditContext()
const { parentBlock } = useSelect( select => {
const { getBlockRootClientId, getBlock } = select( 'core/block-editor' )
const parentClientId = getBlockRootClientId( clientId )
return {
parentBlock: getBlock( parentClientId ),
}
}, [ clientId ] )
if ( parentBlock?.name === 'stackable/blockquote' ) {
return __( 'Pick another icon', i18n )
}
Expand Down
11 changes: 9 additions & 2 deletions src/block/call-to-action/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ import {
ContentAlign,
getContentAlignmentClasses,
} from '~stackable/block-components'
import { useBlockContext } from '~stackable/hooks'
import {
withBlockAttributeContext, withBlockWrapperIsHovered, withQueryLoopContext,
} from '~stackable/higher-order'
Expand All @@ -47,6 +46,7 @@ import { InnerBlocks } from '@wordpress/block-editor'
import { compose } from '@wordpress/compose'
import { __ } from '@wordpress/i18n'
import { memo } from '@wordpress/element'
import { useSelect } from '@wordpress/data'

const TEMPLATE = variations[ 0 ].innerBlocks

Expand All @@ -55,7 +55,14 @@ const Edit = props => {
className,
} = props

const { hasInnerBlocks, innerBlocks } = useBlockContext()
const { hasInnerBlocks, innerBlocks } = useSelect( select => {
const { getBlock } = select( 'core/block-editor' )
const innerBlocks = getBlock( props.clientId ).innerBlocks
return {
hasInnerBlocks: innerBlocks.length > 0,
innerBlocks,
}
}, [ props.clientId ] )
const blockAlignmentClass = getAlignmentClasses( props.attributes )
const separatorClass = getSeparatorClasses( props.attributes )

Expand Down
18 changes: 11 additions & 7 deletions src/block/card/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@ import {
InspectorTabs,
useBlockCssGenerator,
} from '~stackable/components'
import {
useBlockContext, useBlockStyle, useDeviceType,
} from '~stackable/hooks'
import { useBlockStyle, useDeviceType } from '~stackable/hooks'
import {
withBlockAttributeContext,
withBlockWrapperIsHovered,
Expand Down Expand Up @@ -49,17 +47,14 @@ import { InnerBlocks } from '@wordpress/block-editor'
import { compose } from '@wordpress/compose'
import { __ } from '@wordpress/i18n'
import { memo } from '@wordpress/element'
import { useSelect } from '@wordpress/data'

const TEMPLATE = variations[ 0 ].innerBlocks

const widthUnit = [ 'px', 'vw' ]
const heightUnit = [ 'px', 'vh' ]

const Edit = props => {
const {
hasInnerBlocks, innerBlocks,
} = useBlockContext()

const {
hasContainer,
} = props.attributes
Expand All @@ -68,6 +63,15 @@ const Edit = props => {
className, //isHovered,
} = props

const { hasInnerBlocks, innerBlocks } = useSelect( select => {
const { getBlock } = select( 'core/block-editor' )
const innerBlocks = getBlock( props.clientId ).innerBlocks
return {
hasInnerBlocks: innerBlocks.length > 0,
innerBlocks,
}
}, [ props.clientId ] )

const blockOrientation = getBlockOrientation( props.attributes )
const blockAlignmentClass = getAlignmentClasses( props.attributes )
const blockStyle = useBlockStyle( variations )
Expand Down
Loading

0 comments on commit 8e75e0e

Please sign in to comment.