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

fix(scroll-to-selection): use getClientRects when selectionRect.top/height is still 0 in Safari #1446

Merged
merged 8 commits into from Dec 11, 2017
Merged

fix(scroll-to-selection): use getClientRects when selectionRect.top/height is still 0 in Safari #1446

merged 8 commits into from Dec 11, 2017

Conversation

zGrav
Copy link
Collaborator

@zGrav zGrav commented Dec 5, 2017

Tested on Safari 11.0.1 on OS X 10.13.1 with a scrollable container.

Fixes #1445 .

@zGrav
Copy link
Collaborator Author

zGrav commented Dec 5, 2017

Apologies for so many commits about indentation, I finally figured out that it wanted spaces and not tabs (because I sometimes just don't read things...), took me a bit longer than expected 😢

@zGrav zGrav changed the title fix(scroll-to-selection): use getClientRects when startContainer.length is 1 fix(scroll-to-selection): use getClientRects when selectionRect.top/height is still 0 in Safari Dec 5, 2017
Copy link

@maddrag0n maddrag0n left a comment

Choose a reason for hiding this comment

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

sanity checks... 😂

@@ -80,6 +80,10 @@ function scrollToSelection(selection) {
}

selectionRect = range.getBoundingClientRect()

if (selectionRect.top == 0 && selectionRect.height == 0) {
Copy link

@maddrag0n maddrag0n Dec 7, 2017

Choose a reason for hiding this comment

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

if (selectionRect.top === 0 && selectionRect.height === 0) {

Copy link

@maddrag0n maddrag0n Dec 7, 2017

Choose a reason for hiding this comment

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

as slate apparently uses improper == checks throughout the code base... ignore my suggestion maybe?

@@ -80,6 +80,10 @@ function scrollToSelection(selection) {
}

selectionRect = range.getBoundingClientRect()

if (selectionRect.top == 0 && selectionRect.height == 0) {
selectionRect = range.getClientRects()[0]
Copy link

@maddrag0n maddrag0n Dec 7, 2017

Choose a reason for hiding this comment

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

if (range.getClientRects && range.getClientRects().length) {
    selectionRect = range.getClientRects()[0]
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

merci <3

@lxcid
Copy link
Collaborator

lxcid commented Dec 7, 2017

In my own codebase I use this check though, is this alternative suggestion also solve the issue?

    const selection = window.getSelection();
    let range = selection.getRangeAt(0);
    if (IS_SAFARI) {
      // `getBoundingClientRect` is buggy on Safari when there is no selected text active.
      // A workaround is available at https://stackoverflow.com/a/44261188
      range = range.cloneRange();
      range.setStart(range.startContainer, 0);
    }

@lxcid
Copy link
Collaborator

lxcid commented Dec 7, 2017

IS_SAFARI is available in src/contants/environment.js

@zGrav
Copy link
Collaborator Author

zGrav commented Dec 7, 2017

@lxcid , I believe I tried the cloneRange and it wasn’t working as intended, my implementation does work for empty text and keeps the original implementation in place when there is text.

I did not know about IS_SAFARI thanks!

@lxcid
Copy link
Collaborator

lxcid commented Dec 7, 2017

I see! Good to know of alternative solution!

@ianstormtaylor
Copy link
Owner

Thanks @zGrav!

@ianstormtaylor ianstormtaylor merged commit c58e533 into ianstormtaylor:master Dec 11, 2017
@zGrav zGrav deleted the feature/fix-newline-scrolljump-safari branch December 11, 2017 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants