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

Fixed Rect results for a sequenced range #7840

Merged
merged 6 commits into from
Aug 17, 2020
Merged

Fixed Rect results for a sequenced range #7840

merged 6 commits into from
Aug 17, 2020

Conversation

mlewand
Copy link
Contributor

@mlewand mlewand commented Aug 13, 2020

Suggested merge commit message (convention)

Fix (utils): Rect utility returns wrong sizes in case of a sequenced range. Closes #7838

Feature (utils): Introduced the Rect#getBoundingRect() method that returns a Rect instance containing all other rectangles. Closes #7858.


Additional information

Initially I was even tempted to simplify it and use Range.getBoundingClientRect() to do the computation for us, but then I saw a plethora of issues that this method not always give reliable results:

@niegowski niegowski self-requested a review August 17, 2020 08:44
Comment on lines 415 to 430
function copyRangeRectProperties( rect, source ) {
const rangeRects = Rect.getDomRangeRects( source );
const combinedRect = rangeRects[ 0 ];

for ( let i = 1; i < rangeRects.length; i++ ) {
combinedRect.right = Math.max( combinedRect.right, rangeRects[ i ].right );
combinedRect.left = Math.min( combinedRect.left, rangeRects[ i ].left );
combinedRect.bottom = Math.max( combinedRect.bottom, rangeRects[ i ].bottom );
combinedRect.top = Math.min( combinedRect.top, rangeRects[ i ].top );
}

combinedRect.width = combinedRect.right - combinedRect.left;
combinedRect.height = combinedRect.bottom - combinedRect.top;

copyRectProperties( rect, combinedRect );
}
Copy link
Contributor

Choose a reason for hiding this comment

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

In the table package we have a helper function that is very similar (the same purpose but different approach). Maybe we could unify those to one helper function? 

https://github.com/ckeditor/ckeditor5/blob/master/packages/ckeditor5-table/src/utils/ui/contextualballoon.js#L123-L144

Copy link
Contributor Author

@mlewand mlewand Aug 17, 2020

Choose a reason for hiding this comment

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

Good idea, added #7858 that will also be closed by this PR.

I had a small problem with the naming, first wanted to make it a Rect#expand() member, but the name didn't feel right. Ended up with static Rect.getBoundingRect() which feels just right 👌

Also I simplified mentioned contextualballoon logic a little bit as the way how createBoundingRect internal logic was implemented in the getBalloonCellPositionData function was a little confusing.

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess you mean Rect not Range 😉

@niegowski niegowski merged commit ccfaf5e into master Aug 17, 2020
@niegowski niegowski deleted the i/7838 branch August 17, 2020 15:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rect utility returns wrong sizes in case of sequenced range
2 participants