Permalink
Browse files

Add some docs, license file, and extract selection_range.js

  • Loading branch information...
1 parent a6f60e4 commit bc701aa40eff6ebc38d9ccec98f794545253cac7 @kir committed Apr 27, 2012
Showing with 77 additions and 35 deletions.
  1. +20 −0 MIT-LICENSE.txt
  2. +2 −1 README.md
  3. +2 −34 src/cursor_position.js
  4. +52 −0 src/selection_range.js
  5. +1 −0 test/cursor_position_test.html
View
@@ -0,0 +1,20 @@
+Copyright (c) 2012 Kirill Maximov, http://gplus.to/maxkir
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -14,7 +14,7 @@ Why
How
---
The library tries to model text wrapping in the textarea and to find out relative cursor position.
- The results are not 100% accurate, but really close to it.
+ The results are not 100% accurate, but really close to it. Tested against IE8+, Chrome, Opera, FF.
Usage:
@@ -28,5 +28,6 @@ Dependencies
------------
None, plain javascript code
+
--
Copyright (c) 2010-2012 Kirill Maximov, released under the MIT license
View
@@ -16,42 +16,10 @@ if (typeof document.defaultView == 'undefined') {
maxkir.CursorPosition = function(element, padding) {
this.element = element;
this.padding = padding;
+ this.selection_range = new maxkir.SelectionRange(element);
var that = this;
- var get_sel_range = function() {
- // thanks to http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/
- if( (typeof element.selectionStart == 'undefined') && document.selection ){
- // The current selection
- var range = document.selection.createRange();
- // We'll use this as a 'dummy'
- var stored_range = range.duplicate();
- // Select all text
- if (element.type == 'text') {
- stored_range.moveStart('character', -element.value.length);
- stored_range.moveEnd('character', element.value.length);
- } else { // textarea
- stored_range.moveToElementText( element );
- }
- // Now move 'dummy' end point to end point of original range
- stored_range.setEndPoint( 'EndToEnd', range );
- // Now we can calculate start and end points
- var selectionStart = stored_range.text.length - range.text.length;
- var selectionEnd = selectionStart + range.text.length;
- return [selectionStart, selectionEnd];
- }
- return [element.selectionStart, element.selectionEnd];
- };
-
- this.get_selection_range = function() {
- try {
- return get_sel_range();
- }
- catch(e) {
- return [0,0]
- }
- };
-
this.get_string_metrics = function(s) {
return maxkir.CursorPosition.getTextMetrics(element, s, padding);
};
@@ -68,7 +36,7 @@ maxkir.CursorPosition = function(element, padding) {
innerAreaWidth -= 4;
}
- var pos = that.get_selection_range()[0];
+ var pos = that.selection_range.get_selection_range()[0];
return splitter.splitString(element.value.substr(0, pos), innerAreaWidth);
};
View
@@ -0,0 +1,52 @@
+if (!window.maxkir) maxkir = {};
+
+/**
+ * Get current selection range for the TEXTAREA or INPUT[text] element.
+ *
+ * Usage:
+ *
+ * var selection = new maxkir.SelectionRange(textarea).get_selection_range()
+ * var selectionStart = selection[0]
+ * var selectionEnd = selection[1]
+ *
+ * On a error, returns [0,0]
+ *
+ * */
+maxkir.SelectionRange = function(element) {
+ this.element = element;
+};
+
+
+maxkir.SelectionRange.prototype.get_selection_range = function() {
+
+ var get_sel_range = function(element) {
+ // thanks to http://the-stickman.com/web-development/javascript/finding-selection-cursor-position-in-a-textarea-in-internet-explorer/
+ if( (typeof element.selectionStart == 'undefined') && document.selection ){
+ // The current selection
+ var range = document.selection.createRange();
+ // We'll use this as a 'dummy'
+ var stored_range = range.duplicate();
+ // Select all text
+ if (element.type == 'text') {
+ stored_range.moveStart('character', -element.value.length);
+ stored_range.moveEnd('character', element.value.length);
+ } else { // textarea
+ stored_range.moveToElementText( element );
+ }
+ // Now move 'dummy' end point to end point of original range
+ stored_range.setEndPoint( 'EndToEnd', range );
+ // Now we can calculate start and end points
+ var selectionStart = stored_range.text.length - range.text.length;
+ var selectionEnd = selectionStart + range.text.length;
+ return [selectionStart, selectionEnd];
+ }
+ return [element.selectionStart, element.selectionEnd];
+ };
+
+ try {
+ return get_sel_range(this.element);
+ }
+ catch(e) {
+ return [0,0]
+ }
+};
@@ -4,6 +4,7 @@
<head>
<title></title>
<script type="text/javascript" src="prototype.js"></script>
+ <script type="text/javascript" src="../src/selection_range.js"></script>
<script type="text/javascript" src="../src/string_splitter.js"></script>
<script type="text/javascript" src="../src/cursor_position.js"></script>

0 comments on commit bc701aa

Please sign in to comment.