Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/block-editor/src/store/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,9 @@ export const blocks = pipe(
if ( fromRootClientId === toRootClientId ) {
const subState = state.get( toRootClientId );
const fromIndex = subState.indexOf( clientIds[ 0 ] );
if ( fromIndex === -1 ) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we be selectively defensive against wrong API usage? I mean, it's fine to add this but there are so many more paths that things can break with wrong usage.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you have in mind? There are many ways similar actions can be misused; I think guarding against content loss is a good start.

IMO, the fromRootClientId argument isn't really required here; we can derive that value from clientIds, but now that would be hard to migrate without breaking something.

We could silently patch the wrong fromRootClientId, but consumers might still use the action incorrectly without even knowing it.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I told you I'm fine with this small change, but I find it weird to be defensive like that and I'll share my thinking.

If I was the one that tried to use that (or any other API) and noticed the issue. I'd see the docs/code and just call the API with the proper argument, that I had missed.. Or I'd suggest in an issue to have a you're doing it wrong message etc..

We could silently patch the wrong fromRootClientId, but consumers might still use the action incorrectly without even knowing it.

That's still the case now.. They don't really know it's wrong, and even though they don't have content loss, they still don't know it's a no-op.

Anyway, that was a harmless change and shared my thoughts candidly. No reason to spent more time on this philosophical discussion I think 😄

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for great feedback, Nik!

return state;
}
const newState = new Map( state );
newState.set(
toRootClientId,
Expand Down
37 changes: 37 additions & 0 deletions packages/block-editor/src/store/test/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -1841,6 +1841,43 @@ describe( 'state', () => {
] );
} );

it( 'should not remove last block when fromRootClientId is incorrect', () => {
const original = blocks( undefined, {
type: 'RESET_BLOCKS',
blocks: [
{
clientId: 'chicken',
name: 'core/test-block',
attributes: {},
innerBlocks: [],
},
{
clientId: 'ribs',
name: 'core/test-block',
attributes: {},
innerBlocks: [
{
clientId: 'nested',
name: 'core/test-block',
attributes: {},
innerBlocks: [],
},
],
},
],
} );
const state = blocks( original, {
type: 'MOVE_BLOCKS_TO_POSITION',
clientIds: [ 'nested' ],
fromRootClientId: '',
toRootClientId: '',
index: 0,
} );

// The state should be unchnaged - no blocks moved or removed.
expect( state.order.get( '' ) ).toEqual( [ 'chicken', 'ribs' ] );
} );

describe( 'blocks', () => {
describe( 'byClientId', () => {
it( 'should ignore updates to non-existent block', () => {
Expand Down
Loading