From 9aea9d34d423242846f5fcd066a3156564314994 Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Thu, 19 Jan 2023 17:41:02 +0000 Subject: [PATCH 1/4] Subscribe only to changes on core/block-editor --- assets/js/blocks/cart-checkout-shared/use-forced-layout.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts b/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts index 72183567f0c..554eeb76032 100644 --- a/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts +++ b/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts @@ -178,6 +178,6 @@ export const useForcedLayout = ( { .dispatch( 'core/block-editor' ) .insertBlocks( blockConfig, insertAtPosition, clientId ); } ); - } ); + }, 'core/block-editor' ); }, [ clientId, registry ] ); }; From fdb5afd0529acde6e9bff963f4df4abd3e562176 Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Thu, 19 Jan 2023 17:43:39 +0000 Subject: [PATCH 2/4] Improve performance of useForcedLayou This is because by the time we reach this line, innerBlocks will be an empty array (or we wouldn't make it this far) and if nextBlocks contains ANY items it will, by definition be unequal, so a length check is simpler and more performant. Also we can remove the dependence on yet another lodash function by doing it this way. --- assets/js/blocks/cart-checkout-shared/use-forced-layout.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts b/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts index 554eeb76032..1a90969396f 100644 --- a/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts +++ b/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts @@ -10,7 +10,6 @@ import { BlockInstance, } from '@wordpress/blocks'; import type { Block, TemplateArray } from '@wordpress/blocks'; -import { isEqual } from 'lodash'; import { MutableRefObject } from 'react'; interface LockableBlock extends Block { @@ -130,7 +129,7 @@ export const useForcedLayout = ( { const nextBlocks = createBlocksFromInnerBlocksTemplate( currentDefaultTemplate.current ); - if ( ! isEqual( nextBlocks, innerBlocks ) ) { + if ( nextBlocks.length !== 0 ) { replaceInnerBlocks( clientId, nextBlocks ); return; } From 3be3fc65b0e1cf13797d67cf79735223b974dc37 Mon Sep 17 00:00:00 2001 From: Thomas Roberts Date: Thu, 19 Jan 2023 17:44:41 +0000 Subject: [PATCH 3/4] Check if templates synced before doing it again in useForcedLayout --- assets/js/blocks/cart-checkout-shared/use-forced-layout.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts b/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts index 1a90969396f..72068736f5f 100644 --- a/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts +++ b/assets/js/blocks/cart-checkout-shared/use-forced-layout.ts @@ -115,6 +115,7 @@ export const useForcedLayout = ( { const registry = useRegistry(); useEffect( () => { + let templateSynced = false; const { replaceInnerBlocks } = dispatch( 'core/block-editor' ); return registry.subscribe( () => { const innerBlocks = registry @@ -124,12 +125,14 @@ export const useForcedLayout = ( { // If there are NO inner blocks, sync with the given template. if ( innerBlocks.length === 0 && - currentDefaultTemplate.current.length > 0 + currentDefaultTemplate.current.length > 0 && + ! templateSynced ) { const nextBlocks = createBlocksFromInnerBlocksTemplate( currentDefaultTemplate.current ); if ( nextBlocks.length !== 0 ) { + templateSynced = true; replaceInnerBlocks( clientId, nextBlocks ); return; } From 09e942b32d83b46e18c9a15851eb23fdec265d2c Mon Sep 17 00:00:00 2001 From: Luigi Date: Wed, 25 Jan 2023 18:02:35 +0100 Subject: [PATCH 4/4] fix filter blocks preview on stylebook --- assets/js/blocks/filter-wrapper/upgrade.tsx | 2 +- assets/js/blocks/price-filter/block.json | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/assets/js/blocks/filter-wrapper/upgrade.tsx b/assets/js/blocks/filter-wrapper/upgrade.tsx index db58d66244e..74423ede737 100644 --- a/assets/js/blocks/filter-wrapper/upgrade.tsx +++ b/assets/js/blocks/filter-wrapper/upgrade.tsx @@ -73,7 +73,7 @@ export const UpgradeNotice = ( { } ); }; - if ( isInsideFilterWrapper || ! filterType ) { + if ( isInsideFilterWrapper || ! filterType || attributes?.isPreview ) { return null; } diff --git a/assets/js/blocks/price-filter/block.json b/assets/js/blocks/price-filter/block.json index e97b1323273..bbd37bed047 100644 --- a/assets/js/blocks/price-filter/block.json +++ b/assets/js/blocks/price-filter/block.json @@ -40,6 +40,10 @@ "headingLevel": { "type": "number", "default": 3 + }, + "isPreview": { + "type": "boolean", + "default": false } }, "textdomain": "woo-gutenberg-products-block",