From 9529670b7c66f954200a85ee13386f678c526f6f Mon Sep 17 00:00:00 2001 From: Jorge Costa Date: Thu, 31 Oct 2019 17:16:15 +0000 Subject: [PATCH] Fix: consecutive updates may trigger a blocks reset (#18219) --- .../block-editor/src/components/provider/index.js | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/packages/block-editor/src/components/provider/index.js b/packages/block-editor/src/components/provider/index.js index c2b29f13fa74ac..2f49a5d7e77d57 100644 --- a/packages/block-editor/src/components/provider/index.js +++ b/packages/block-editor/src/components/provider/index.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { noop } from 'lodash'; +import { last, noop } from 'lodash'; /** * WordPress dependencies @@ -20,6 +20,7 @@ class BlockEditorProvider extends Component { this.props.updateSettings( this.props.settings ); this.props.resetBlocks( this.props.value ); this.attachChangeObserver( this.props.registry ); + this.isSyncingOutcomingValue = []; } componentDidUpdate( prevProps ) { @@ -39,20 +40,22 @@ class BlockEditorProvider extends Component { this.attachChangeObserver( registry ); } - if ( this.isSyncingOutcomingValue !== null && this.isSyncingOutcomingValue === value ) { + if ( this.isSyncingOutcomingValue.includes( value ) ) { // Skip block reset if the value matches expected outbound sync // triggered by this component by a preceding change detection. // Only skip if the value matches expectation, since a reset should // still occur if the value is modified (not equal by reference), // to allow that the consumer may apply modifications to reflect // back on the editor. - this.isSyncingOutcomingValue = null; + if ( last( this.isSyncingOutcomingValue ) === value ) { + this.isSyncingOutcomingValue = []; + } } else if ( value !== prevProps.value ) { // Reset changing value in all other cases than the sync described // above. Since this can be reached in an update following an out- // bound sync, unset the outbound value to avoid considering it in // subsequent renders. - this.isSyncingOutcomingValue = null; + this.isSyncingOutcomingValue = []; this.isSyncingIncomingValue = value; resetBlocks( value ); } @@ -119,7 +122,7 @@ class BlockEditorProvider extends Component { // When knowing the blocks value is changing, assign instance // value to skip reset in subsequent `componentDidUpdate`. if ( newBlocks !== blocks ) { - this.isSyncingOutcomingValue = newBlocks; + this.isSyncingOutcomingValue.push( newBlocks ); } blocks = newBlocks;