Skip to content

Commit

Permalink
Add mode argument to cursorCoords and charCoords
Browse files Browse the repository at this point in the history
Passing local will return coordinates inside the edited document.
  • Loading branch information
marijnh committed Mar 14, 2012
1 parent b0826e8 commit d94b8b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
9 changes: 6 additions & 3 deletions doc/manual.html
Original file line number Diff line number Diff line change
Expand Up @@ -475,13 +475,16 @@ <h2 id="api">Programming API</h2>
<dd>Retrieves the current value of the given option for this
editor instance.</dd>

<dt id="cursorCoords"><code>cursorCoords(start) → object</code></dt>
<dt id="cursorCoords"><code>cursorCoords(start, mode) → object</code></dt>
<dd>Returns an <code>{x, y, yBot}</code> object containing the
coordinates of the cursor relative to the top-left corner of the
coordinates of the cursor. If <code>mode</code>
is <code>"local"</code>, they will be relative to the top-left
corner of the editable document. If it is <code>"page"</code> or
not given, they are relative to the top-left corner of the
page. <code>yBot</code> is the coordinate of the bottom of the
cursor. <code>start</code> is a boolean indicating whether you
want the start or the end of the selection.</dd>
<dt id="charCoords"><code>charCoords(pos) → object</code></dt>
<dt id="charCoords"><code>charCoords(pos, mode) → object</code></dt>
<dd>Like <code>cursorCoords</code>, but returns the position of
an arbitrary characters. <code>pos</code> should be
a <code>{line, ch}</code> object.</dd>
Expand Down
11 changes: 8 additions & 3 deletions lib/codemirror.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,16 @@ var CodeMirror = (function() {
line = clipLine(line == null ? doc.size - 1: line);
return getStateBefore(line + 1);
},
cursorCoords: function(start){
cursorCoords: function(start, mode) {
if (start == null) start = sel.inverted;
return pageCoords(start ? sel.from : sel.to);
return this.charCoords(start ? sel.from : sel.to, mode);
},
charCoords: function(pos, mode) {
pos = clipPos(pos);
if (mode == "local") return localCoords(pos, false);
if (mode == "div") return localCoords(pos, true);
return pageCoords(pos);
},
charCoords: function(pos){return pageCoords(clipPos(pos));},
coordsChar: function(coords) {
var off = eltOffset(lineSpace);
return coordsChar(coords.x - off.left, coords.y - off.top);
Expand Down

0 comments on commit d94b8b8

Please sign in to comment.