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

RichText: only replace range and nodes if different #12547

Merged
merged 8 commits into from
Dec 9, 2018
Merged
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions packages/rich-text/src/to-dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,15 @@ export function applyValue( future, current ) {
}
}

function isRangeEqual( a, b ) {
return (
a.startContainer === b.startContainer &&
a.startOffset === b.startOffset &&
a.endContainer === b.endContainer &&
a.endOffset === b.endOffset
);
}

export function applySelection( selection, current ) {
const { node: startContainer, offset: startOffset } = getNodeByPath( current, selection.startPath );
const { node: endContainer, offset: endOffset } = getNodeByPath( current, selection.endPath );
Expand Down Expand Up @@ -283,6 +292,11 @@ export function applySelection( selection, current ) {
range.setEnd( endContainer, endOffset );
}

// If ranges the live range is the same, there's no need to remove and add.
if ( isRangeEqual( range, windowSelection.getRangeAt( 0 ) ) ) {
return;
}

windowSelection.removeAllRanges();
windowSelection.addRange( range );
}