Skip to content
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

Rich text: avoid updating partial selection unnecessarily #44330

Merged
merged 2 commits into from
Oct 4, 2022
Merged
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
22 changes: 22 additions & 0 deletions packages/block-editor/src/components/rich-text/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ function RichTextWrapper(
// retreived from the store on merge.
// To do: fix this somehow.
const { selectionStart, selectionEnd, isSelected } = useSelect( selector );
const { getSelectionStart, getSelectionEnd, getBlockRootClientId } =
useSelect( blockEditorStore );
const { selectionChange } = useDispatch( blockEditorStore );
const multilineTag = getMultilineTag( multiline );
const adjustedAllowedFormats = getAllowedFormats( {
Expand Down Expand Up @@ -195,6 +197,18 @@ function RichTextWrapper(
const unset = start === undefined && end === undefined;

if ( typeof start === 'number' || unset ) {
// If we are only setting the start (or the end below), which
// means a partial selection, and we're not updating a selection
// with the same client ID, abort. This means the selected block
// is a parent block.
if (
end === undefined &&
getBlockRootClientId( clientId ) !==
getBlockRootClientId( getSelectionEnd().clientId )
) {
return;
}

selection.start = {
clientId,
attributeKey: identifier,
Expand All @@ -203,6 +217,14 @@ function RichTextWrapper(
}

if ( typeof end === 'number' || unset ) {
if (
start === undefined &&
getBlockRootClientId( clientId ) !==
getBlockRootClientId( getSelectionStart().clientId )
) {
return;
}

selection.end = {
clientId,
attributeKey: identifier,
Expand Down