-
Notifications
You must be signed in to change notification settings - Fork 4.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[RNMobile] When inserting block from title replace block if appropriate #16574
[RNMobile] When inserting block from title replace block if appropriate #16574
Conversation
// so replaceBlock does not select the new block and we need to manually select the new block | ||
const { clientId: firstBlockId } = this.props.firstBlock; | ||
this.props.replaceBlock( firstBlockId, newBlock ); | ||
this.props.selectBlock( newBlock.clientId ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some additional context for this line:
If replacing the first block of the post when the title selected, it is also necessary to select the new block at the beginning of the post. This is because the block selection reducer checks and only updates the block selection if the action's clientId matches the clientId from that state's start
property. When the post's title is selected, the state's start
property is an empty object, so the undefined clientId
does not match the action's clientId
and the block selection is not updated. When a block is selected (such as if you're replacing an empty paragraph block) then the clientId
's do match and the selection state is updated, resulting in the new block being selected. I feel that just sending of the SELECT_BLOCK
action works well for this rather-edge case, and it avoids the complexity/risk inherent in updating the blockSelection
reducer we share with web. I'm certainly open to suggestions for better ways to handle this though.
|
||
return { | ||
blockClientIds: getBlockOrder( rootClientId ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey @mchowning nice work here :)
Just curious about this line of the code. Why we don't need to set blockClientIds
anymore with getBlockOrder( rootClientId )
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We definitly still need this! My intention was just to extract the assignment of getBlockOrder( rootClientId )
to a blockClientIds
variable on line 294, and then use that variable to return both the blockClientIds
(line 297) and the firstBlock
(line 302):
const blockClientIds = getBlockOrder( rootClientId ); // line 294
return {
blockClientIds, // line 297
[...]
firstBlock: getBlock( blockClientIds[ 0 ] ), // line 302
[...]
};
I believe my changes should not alter the value that is being returned as the blockClientIds
, so if I am altering that in some way, definitely let me know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, @mchowning, I definitely missed 294.
It seems good to me.
blockCount: getBlockCount( rootClientId ), | ||
getBlockName, | ||
isBlockSelected, | ||
selectedBlock: getSelectedBlock(), | ||
firstBlock: getBlock( blockClientIds[ 0 ] ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we defend ourselves here if blockClientIds()
at some scenario can be null?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, we're protected from this because blockClientIds
is assigned from getBlockOrder( rootClientId )
, which will always return at least an empty array:
export function getBlockOrder( state, rootClientId ) {
return state.blocks.order[ rootClientId || '' ] || EMPTY_ARRAY;
}
I also confirmed having an undefined firstBlock
prop (as a result of assigning [][0]
) did not cause any problems.
With that said, I wouldn't be opposed to still adding in the protection since it doesn't hurt and it would protect us against a change to the implementation of getBlockOrder
. What do you think @marecar3 ?
As an aside, it's cases like this that make me miss working with typescript (or flow). Felt good knowing that the compiler would catch something like this if it was a problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the details explanation. I think we are fine with the current solution :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @mchowning !
LGTM!
Adding a link to some discussion that we might not want this change: #16677 (comment) |
Related gutenberg-mobile PR
Description
Fixes an issue where inserting a block from the post title would not replace an empty default block if that block was the first block in the post. This would allow the user to, for example, repeatedly insert consecutive empty paragraph blocks at the beginning of the post without the blocks ever being merged/replaced.
Test scenarios
Test the fix
Regression Checks
Checklist: