From 0c345c7ea1dfb3a48f9db45a3c62e502873e6ab9 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 23 Jul 2019 16:59:33 +0800 Subject: [PATCH 1/3] Update REPLACE_BLOCKS_AUGMENTED_WITH_CHILDREN so that it invalidates the correct parent blocks --- packages/block-editor/src/store/reducer.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index 97a366e8884d2..5bdf23e867aff 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -267,7 +267,7 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => { newState.cache = { ...omit( newState.cache, action.replacedClientIds ), ...fillKeysWithEmptyObject( - getBlocksWithParentsClientIds( keys( flattenBlocks( action.blocks ) ) ), + getBlocksWithParentsClientIds( action.replacedClientIds ) ), }; break; From 2c416fd68b159cb067aab2286724ca10b24798db Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 23 Jul 2019 17:27:18 +0800 Subject: [PATCH 2/3] Add jsdoc to clarify usage of getBlocksWithParentsClientIds --- packages/block-editor/src/store/reducer.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index 5bdf23e867aff..d8709bcee90f4 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -225,6 +225,17 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => { } newState.cache = state.cache ? state.cache : {}; + /** + * For each clientId provided, traverses up parents, adding the provided clientIds + * and each parent's clientId to the returned array. + * + * When calling this function consider that it uses the old state, so any state + * modifications made by the `reducer` will not be present. + * + * @param {Array} clientIds an Array of block clientIds. + * + * @return {Array} The provided clientIds and all of their parent clientIds. + */ const getBlocksWithParentsClientIds = ( clientIds ) => { return clientIds.reduce( ( result, clientId ) => { let current = clientId; From 8655de34733d297ef4a33a36dffbfd0819e5a9c1 Mon Sep 17 00:00:00 2001 From: Daniel Richards Date: Tue, 23 Jul 2019 17:51:30 +0800 Subject: [PATCH 3/3] Update reducer to omit the replaced clientId, except for when it is the same as the new client id. Update tests to ensure wrapper generates new key --- packages/block-editor/src/store/reducer.js | 7 ++++--- packages/block-editor/src/store/test/reducer.js | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/block-editor/src/store/reducer.js b/packages/block-editor/src/store/reducer.js index d8709bcee90f4..df81bd5df22f0 100644 --- a/packages/block-editor/src/store/reducer.js +++ b/packages/block-editor/src/store/reducer.js @@ -275,11 +275,12 @@ const withBlockCache = ( reducer ) => ( state = {}, action ) => { }; break; case 'REPLACE_BLOCKS_AUGMENTED_WITH_CHILDREN': + const parentClientIds = fillKeysWithEmptyObject( getBlocksWithParentsClientIds( action.replacedClientIds ) ); + newState.cache = { ...omit( newState.cache, action.replacedClientIds ), - ...fillKeysWithEmptyObject( - getBlocksWithParentsClientIds( action.replacedClientIds ) - ), + ...omit( parentClientIds, action.replacedClientIds ), + ...fillKeysWithEmptyObject( keys( flattenBlocks( action.blocks ) ) ), }; break; case 'REMOVE_BLOCKS_AUGMENTED_WITH_CHILDREN': diff --git a/packages/block-editor/src/store/test/reducer.js b/packages/block-editor/src/store/test/reducer.js index 0626f57fb4129..7eb698be77865 100644 --- a/packages/block-editor/src/store/test/reducer.js +++ b/packages/block-editor/src/store/test/reducer.js @@ -759,21 +759,29 @@ describe( 'state', () => { blocks: [ wrapperBlock ], } ); + const originalWrapperBlockCacheKey = original.cache[ wrapperBlock.clientId ]; + const state = blocks( original, { type: 'REPLACE_BLOCKS', clientIds: [ nestedBlock.clientId ], blocks: [ replacementBlock ], } ); + const newWrapperBlockCacheKey = state.cache[ wrapperBlock.clientId ]; + + expect( newWrapperBlockCacheKey ).not.toBe( originalWrapperBlockCacheKey ); + expect( state.order ).toEqual( { '': [ wrapperBlock.clientId ], [ wrapperBlock.clientId ]: [ replacementBlock.clientId ], [ replacementBlock.clientId ]: [], } ); + expect( state.parents ).toEqual( { [ wrapperBlock.clientId ]: '', [ replacementBlock.clientId ]: wrapperBlock.clientId, } ); + expect( state.cache ).toEqual( { [ wrapperBlock.clientId ]: {}, [ replacementBlock.clientId ]: {},