-
-
Notifications
You must be signed in to change notification settings - Fork 5k
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
[hint addon] Offset the hint to position it correctly when hintOptions.container is given #5857
Conversation
addon/hint/show-hint.js
Outdated
var pos = cm.cursorCoords(completion.options.alignWithWord ? data.from : null); | ||
var left = pos.left, top = pos.bottom, below = true; | ||
// We offset the cursor position because left and top are relative to the offsetParent's top left corner. | ||
var left = pos.left - positionOffset.x, top = pos.bottom - positionOffset.y, below = true; |
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.
.x
and .y
don't exist on IE and Edge, so you'll want to use .left
and .top
instead.
Did you also test on a scrolled document? It seems that subtracting the top of |
e7dcd58
to
9a252ab
Compare
You are right, the previous solution did not work with IE/Edge, nor on a scrolled document (whether Here is a new attempt where all of this should be fixed:
|
addon/hint/show-hint.js
Outdated
var pos = cm.cursorCoords(completion.options.alignWithWord ? data.from : null); | ||
var left = pos.left, top = pos.bottom, below = true; | ||
|
||
if(container !== ownerDocument.body) { | ||
var offsetParentPosition = container.offsetParent.getBoundingClientRect(); |
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.
Hi, I tried this out locally and feel like this line should be container.getBoundingClientRect();
to get the hint to show up at the exact same position as before. I maybe wrong, but it didn't work for me as is.
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.
I guess that depends on whether container
is positioned or not—I guess we'd want the offsetParent
of a child of the container.
9a252ab
to
f105cc5
Compare
As you pointed out, the previous did not work well if the Here is a new attempt, that should work in this case too. |
That should work better, but now you're introducing another layout cycle (changing the DOM first, then measuring it). I think using |
58fbe7c
to
6c0f0cf
Compare
Here's a new possible option. It works well too as far as I tested and does not change the points at which the script interacts with the DOM. Also, I realized that other style updates (aiming at repositioning the hint if it overflows) reassigned |
addon/hint/show-hint.js
Outdated
var offsetLeft = 0, offsetTop = 0; | ||
if(container !== ownerDocument.body) { | ||
// We offset the cursor position because left and top are relative to the offsetParent's top left corner. | ||
var offsetParent = ['absolute', 'relative', 'fixed'].includes(parentWindow.getComputedStyle(container).position) ? container : container.offsetParent; |
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.
Another nitpick: includes
doesn't exist on all browsers that we support. Use indexOf
instead, or a regexp.
Also, a space after if
would make the code look more like the rest of the codebase.
…s.container is given
6c0f0cf
to
94de699
Compare
Sorry about all these roundtrips, and thanks for your patience @marijnh 🙏 Here's a new one, including your last remarks. |
Thanks for bearing with me! |
See #5854.
I tested it on the
complete
demo but did not commit changes to the demo file since I believe that having acontainer
is not the most common usage. Let met know if you would prefer that this demo uses a container.