diff --git a/src/js/textmode.js b/src/js/textmode.js index ae1f722..26f6b58 100644 --- a/src/js/textmode.js +++ b/src/js/textmode.js @@ -389,10 +389,10 @@ textmode._updateCursorInfoDisplay = function () { if (this.textarea) { setTimeout(function() { //this to verify we get the most updated textarea cursor selection var selectionRange = util.getInputSelection(me.textarea); - line = selectionRange.row; - col = selectionRange.col; - if (selectionRange.start !== selectionRange.end) { - count = selectionRange.end - selectionRange.start; + line = selectionRange.end.row; + col = selectionRange.end.column; + if (selectionRange.startIndex !== selectionRange.endIndex) { + count = selectionRange.endIndex - selectionRange.startIndex; } updateDisplay(); },0); @@ -644,6 +644,37 @@ textmode.validate = function () { } }; +/** + * Get the selection details + * @returns {{start:{row:Number, column:Number},end:{row:Number, column:Number},text:String}} + */ +textmode.getTextSelection = function () { + if (this.textarea) { + var selectionRange = util.getInputSelection(this.textarea); + return { + start: selectionRange.start, + end: selectionRange.end, + text: this.textarea.value.substring(selectionRange.startIndex, selectionRange.endIndex) + } + } + + if (this.aceEditor) { + var curserPos = this.aceEditor.getSelectionRange(); + var selectedText = this.aceEditor.getSelectedText(); + return { + start: { + row: curserPos.start.row + 1, + column: curserPos.start.column + 1 + }, + end: { + row: curserPos.end.row + 1, + column: curserPos.end.column + 1 + }, + text: selectedText + }; + } +}; + // define modes module.exports = [ { diff --git a/src/js/util.js b/src/js/util.js index 6ddde16..ecf971f 100644 --- a/src/js/util.js +++ b/src/js/util.js @@ -825,11 +825,11 @@ exports.textDiff = function textDiff(oldText, newText) { * @return {Object} reference Object with 2 properties (start and end) with the identifier of the location of the cursor and selected text. **/ exports.getInputSelection = function(el) { - var start = 0, end = 0, normalizedValue, range, textInputRange, len, endRange; + var startIndex = 0, endIndex = 0, normalizedValue, range, textInputRange, len, endRange; if (typeof el.selectionStart == "number" && typeof el.selectionEnd == "number") { - start = el.selectionStart; - end = el.selectionEnd; + startIndex = el.selectionStart; + endIndex = el.selectionEnd; } else { range = document.selection.createRange(); @@ -841,38 +841,50 @@ exports.getInputSelection = function(el) { textInputRange = el.createTextRange(); textInputRange.moveToBookmark(range.getBookmark()); - // Check if the start and end of the selection are at the very end + // Check if the startIndex and endIndex of the selection are at the very end // of the input, since moveStart/moveEnd doesn't return what we want // in those cases endRange = el.createTextRange(); endRange.collapse(false); if (textInputRange.compareEndPoints("StartToEnd", endRange) > -1) { - start = end = len; + startIndex = endIndex = len; } else { - start = -textInputRange.moveStart("character", -len); - start += normalizedValue.slice(0, start).split("\n").length - 1; + startIndex = -textInputRange.moveStart("character", -len); + startIndex += normalizedValue.slice(0, startIndex).split("\n").length - 1; if (textInputRange.compareEndPoints("EndToEnd", endRange) > -1) { - end = len; + endIndex = len; } else { - end = -textInputRange.moveEnd("character", -len); - end += normalizedValue.slice(0, end).split("\n").length - 1; + endIndex = -textInputRange.moveEnd("character", -len); + endIndex += normalizedValue.slice(0, endIndex).split("\n").length - 1; } } } } - var textTillCaret = el.value.substring(0,end); - var row = (textTillCaret.match(/\n/g) || []).length + 1; - var col = textTillCaret.length - textTillCaret.lastIndexOf("\n"); - return { - start: start, - end: end, - col: col, - row: row + startIndex: startIndex, + endIndex: endIndex, + start: _positionForIndex(startIndex), + end: _positionForIndex(endIndex) }; + + /** + * Returns textarea row and column position for certain index + * @param {Number} index text index + * @returns {{row: Number, col: Number}} + */ + function _positionForIndex(index) { + var textTillIndex = el.value.substring(0,index); + var row = (textTillIndex.match(/\n/g) || []).length + 1; + var col = textTillIndex.length - textTillIndex.lastIndexOf("\n"); + + return { + row: row, + column: col + } + } }