Skip to content

Commit

Permalink
Revisions
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgefilipecosta committed Mar 28, 2019
1 parent 34f2ae9 commit cb87da3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 81 deletions.
37 changes: 16 additions & 21 deletions packages/block-editor/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ export function toggleSelection( isSelectionEnabled = true ) {
*
* @param {(string|string[])} clientIds Block client ID(s) to replace.
* @param {(Object|Object[])} blocks Replacement block(s).
*
* @yields {Object} Action object.
*/
export function* replaceBlocks( clientIds, blocks ) {
clientIds = castArray( clientIds );
Expand All @@ -229,8 +231,7 @@ export function* replaceBlocks( clientIds, blocks ) {
'getBlockRootClientId',
first( clientIds )
);
// Replace is valid if the new blocks can be inserted in the root block
// or if we had a block of the same type in the position of the block being replaced.
// Replace is valid if the new blocks can be inserted in the root block.
for ( let index = 0; index < blocks.length; index++ ) {
const block = blocks[ index ];
const canInsertBlock = yield select(
Expand All @@ -240,12 +241,7 @@ export function* replaceBlocks( clientIds, blocks ) {
rootClientId
);
if ( ! canInsertBlock ) {
const clientIdToReplace = clientIds[ index ];
const nameOfBlockToReplace = clientIdToReplace &&
( yield select( 'core/block-editor', 'getBlockName', clientIdToReplace ) );
if ( ! nameOfBlockToReplace || ( nameOfBlockToReplace !== block.name ) ) {
return;
}
return;
}
}
yield {
Expand Down Expand Up @@ -300,7 +296,7 @@ export const moveBlocksUp = createOnMove( 'MOVE_BLOCKS_UP' );
* @param {?string} toRootClientId Root client ID destination.
* @param {number} index The index to move the block into.
*
* @return {Object} Action object.
* @yields {Object} Action object.
*/
export function* moveBlockToPosition( clientId, fromRootClientId, toRootClientId, index ) {
const templateLock = yield select(
Expand All @@ -324,7 +320,8 @@ export function* moveBlockToPosition( clientId, fromRootClientId, toRootClientId
};
// If moving inside the same root block the move is always possible.
if ( fromRootClientId === toRootClientId ) {
return action;
yield action;
return;
}

const blockName = yield select(
Expand All @@ -342,7 +339,7 @@ export function* moveBlockToPosition( clientId, fromRootClientId, toRootClientId

// If moving to other parent block, the move is possible if we can insert a block of the same type inside the new parent block.
if ( canInsertBlock ) {
return action;
yield action;
}
}

Expand Down Expand Up @@ -391,16 +388,14 @@ export function* insertBlocks(
blocks = castArray( blocks );
const allowedBlocks = [];
for ( const block of blocks ) {
if ( block ) {
const isValid = yield select(
'core/block-editor',
'canInsertBlockType',
block.name,
rootClientId
);
if ( isValid ) {
allowedBlocks.push( block );
}
const isValid = yield select(
'core/block-editor',
'canInsertBlockType',
block.name,
rootClientId
);
if ( isValid ) {
allowedBlocks.push( block );
}
}
if ( allowedBlocks.length ) {
Expand Down
60 changes: 0 additions & 60 deletions packages/block-editor/src/store/test/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,66 +277,6 @@ describe( 'actions', () => {
done: true,
} );
} );

it( 'should yield the REPLACE_BLOCKS if the block being replaced and the replacement are of the same type', () => {
const blocks = [ {
clientId: 'ribs',
name: 'core/test-block',
} ];

const replaceBlockGenerator = replaceBlocks( [ 'chicken' ], blocks );
expect(
replaceBlockGenerator.next().value,
).toEqual( {
args: [ 'chicken' ],
selectorName: 'getBlockRootClientId',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
replaceBlockGenerator.next().value,
).toEqual( {
args: [ 'core/test-block', undefined ],
selectorName: 'canInsertBlockType',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
replaceBlockGenerator.next( false ).value,
).toEqual( {
args: [ 'chicken' ],
selectorName: 'getBlockName',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
replaceBlockGenerator.next( 'core/test-block' ).value,
).toEqual( {
type: 'REPLACE_BLOCKS',
clientIds: [ 'chicken' ],
blocks,
time: expect.any( Number ),
} );

expect(
replaceBlockGenerator.next().value,
).toEqual( {
args: [],
selectorName: 'getBlockCount',
storeName: 'core/block-editor',
type: 'SELECT',
} );

expect(
replaceBlockGenerator.next( 1 ),
).toEqual( {
value: undefined,
done: true,
} );
} );
} );

describe( 'insertBlock', () => {
Expand Down

0 comments on commit cb87da3

Please sign in to comment.