Skip to content

Commit 78ba450

Browse files
committed
Support editors in fixed-position elements
This might very well break something else, but passes my minimal tests.
1 parent f3839a6 commit 78ba450

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

lib/codemirror.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,13 +1106,13 @@ var CodeMirror = (function() {
11061106
function paddingLeft() {return lineSpace.offsetLeft;}
11071107

11081108
function posFromMouse(e, liberal) {
1109-
var offW = eltOffset(wrapper), x = e.pageX(), y = e.pageY();
1109+
var offW = eltOffset(wrapper, true), x = e.e.clientX, y = e.e.clientY;
11101110
// This is a mess of a heuristic to try and determine whether a
11111111
// scroll-bar was clicked or not, and to return null if one was
11121112
// (and !liberal).
11131113
if (!liberal && (x - offW.left > wrapper.clientWidth || y - offW.top > wrapper.clientHeight))
11141114
return null;
1115-
var offL = eltOffset(lineSpace);
1115+
var offL = eltOffset(lineSpace, true);
11161116
var line = showingFrom + Math.floor((y - offL.top) / lineHeight());
11171117
return clipPos({line: line, ch: charFromX(clipLine(line), x - offL.left)});
11181118
}
@@ -1879,10 +1879,18 @@ var CodeMirror = (function() {
18791879
}
18801880

18811881
// Find the position of an element by following the offsetParent chain.
1882-
function eltOffset(node) {
1883-
var x = 0, y = 0;
1884-
for (var n = node; n; n = n.offsetParent) {x += n.offsetLeft; y += n.offsetTop;}
1885-
for (var n = node.parentNode; n != node.ownerDocument.body; n = n.parentNode) {x -= n.scrollLeft; y -= n.scrollTop;}
1882+
// If screen==true, it returns screen (rather than page) coordinates.
1883+
function eltOffset(node, screen) {
1884+
var doc = node.ownerDocument.body;
1885+
var x = 0, y = 0, hitDoc = false;
1886+
for (var n = node; n; n = n.offsetParent) {
1887+
x += n.offsetLeft; y += n.offsetTop;
1888+
// Fixed-position elements don't have the document in their offset chain
1889+
if (n == doc) hitDoc = true;
1890+
}
1891+
var e = screen && hitDoc ? null : doc;
1892+
for (var n = node.parentNode; n != e; n = n.parentNode)
1893+
if (n.scrollLeft != null) { x -= n.scrollLeft; y -= n.scrollTop;}
18861894
return {left: x, top: y};
18871895
}
18881896
// Get a node's text content.

0 commit comments

Comments
 (0)