Skip to content

Commit cf1facb

Browse files
committed
add coordsChar method
1 parent f053e46 commit cf1facb

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

lib/codemirror.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ var CodeMirror = (function() {
125125
return pageCoords(start ? sel.from : sel.to);
126126
},
127127
charCoords: function(pos){return pageCoords(clipPos(pos));},
128+
coordsChar: function(coords) {
129+
var off = eltOffset(lineSpace);
130+
var line = Math.min(showingTo - 1, showingFrom + Math.floor(coords.y / lineHeight()));
131+
return clipPos({line: line, ch: charFromX(clipLine(line), coords.x)});
132+
},
128133
getSearchCursor: function(query, pos, caseFold) {return new SearchCursor(query, pos, caseFold);},
129134
markText: operation(function(a, b, c){return operation(markText(a, b, c));}),
130135
setMarker: addGutterMarker,

manual.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,10 @@ <h2 id="api">Programming API</h2>
368368
<dd>Like <code>cursorCoords</code>, but returns the position of
369369
an arbitrary characters. <code>pos</code> should be
370370
a <code>{line, ch}</code> object.</dd>
371+
<dt id="coordsChar"><code>coordsChar(object) → pos</code></dt>
372+
<dd>Given an <code>{x, y}</code> object (in page coordinates),
373+
returns the <code>{line, ch}</code> position that corresponds to
374+
it.</dd>
371375

372376
<dt id="undo"><code>undo()</code></dt>
373377
<dd>Undo one edit (if any undo events are stored).</dd>

0 commit comments

Comments
 (0)