Skip to content

Commit 3f8f0dd

Browse files
committed
Add an, as of yet undocumented, fourth argument to addWidget
Can be one of 'below' (default), 'over', or 'fit', and controls the way the widget is positioned in relation to the given character position. 'fit' means CodeMirror does its best to keep the widget inside the current dimensions of the editor.
1 parent 7db788e commit 3f8f0dd

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

lib/codemirror.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,16 +154,22 @@ var CodeMirror = (function() {
154154
clearMarker: removeGutterMarker,
155155
setLineClass: operation(setLineClass),
156156
lineInfo: lineInfo,
157-
addWidget: function(pos, node, scroll) {
158-
pos = clipPos(pos);
159-
var wcoords = localCoords(pos, true);
160-
node.style.top = (showingFrom * lineHeight() + wcoords.yBot + paddingTop()) + "px";
161-
node.style.left = (wcoords.x + paddingLeft()) + "px";
157+
addWidget: function(pos, node, scroll, where) {
158+
pos = localCoords(clipPos(pos));
159+
var top = pos.yBot, left = pos.x;
160+
node.style.position = "absolute";
162161
code.appendChild(node);
163-
if (scroll) {
164-
var vcoords = localCoords(pos);
165-
scrollIntoView(vcoords.x, vcoords.yBot, vcoords.x + node.offsetWidth, vcoords.yBot + node.offsetHeight);
162+
node.style.left = left + "px";
163+
if (where == "over") top = pos.y;
164+
else if (where == "fit") {
165+
var vspace = lines.length * lineHeight(), hspace = code.clientWidth - paddingLeft();
166+
top = pos.y + node.offsetHeight > vspace ? vspace - node.offsetHeight : pos.y;
167+
if (left + node.offsetWidth > hspace) left = hspace - node.offsetWidth;
166168
}
169+
node.style.top = (top + paddingTop()) + "px";
170+
node.style.left = (left + paddingLeft()) + "px";
171+
if (scroll)
172+
scrollIntoView(left, top, left + node.offsetWidth, top + node.offsetHeight);
167173
},
168174

169175
lineCount: function() {return lines.length;},
@@ -757,6 +763,8 @@ var CodeMirror = (function() {
757763

758764
var textWidth = stringWidth(maxLine);
759765
lineSpace.style.width = textWidth > scroller.clientWidth ? textWidth + "px" : "";
766+
// Needed to prevent odd wrapping/hiding of widgets placed in here.
767+
code.style.width = (lineSpace.offsetWidth + lineSpace.offsetLeft) + "px";
760768

761769
// Since this is all rather error prone, it is honoured with the
762770
// only assertion in the whole file.

0 commit comments

Comments
 (0)