Skip to content

Commit

Permalink
Fix pasting content into inner blocks (#16717)
Browse files Browse the repository at this point in the history
  • Loading branch information
talldan authored and youknowriad committed Jul 26, 2019
1 parent e54043a commit 297396b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 15 additions & 3 deletions packages/block-editor/src/store/reducer.js
Expand Up @@ -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;
Expand Down Expand Up @@ -264,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( keys( flattenBlocks( action.blocks ) ) ),
),
...omit( parentClientIds, action.replacedClientIds ),
...fillKeysWithEmptyObject( keys( flattenBlocks( action.blocks ) ) ),
};
break;
case 'REMOVE_BLOCKS_AUGMENTED_WITH_CHILDREN':
Expand Down
8 changes: 8 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Expand Up @@ -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 ]: {},
Expand Down

0 comments on commit 297396b

Please sign in to comment.