Selection.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js;
  23. import js.html.TextAreaElement;
  24. class Selection {
  25. var doc : Dynamic;
  26. public function new( doc : TextAreaElement ) {
  27. this.doc = doc;
  28. }
  29. public function get() {
  30. // Mozilla
  31. if( doc.selectionStart != null )
  32. return doc.value.substring(doc.selectionStart,doc.selectionEnd);
  33. // IE
  34. var range = untyped js.Lib.document.selection.createRange();
  35. if( range.parentElement() != doc )
  36. return "";
  37. return range.text;
  38. }
  39. public function select( start : Int, end : Int ) {
  40. doc.focus();
  41. // Mozilla
  42. if( doc.selectionStart != null ) {
  43. doc.selectionStart = start;
  44. doc.selectionEnd = end;
  45. return;
  46. }
  47. // FIX : IE count \r\n as one single char for selection
  48. // operations, we must then deal with it
  49. var value : String = doc.value;
  50. var p = 0, delta = 0;
  51. while( true ) {
  52. var i = value.indexOf("\r\n", p);
  53. if( i < 0 || i > start ) break;
  54. delta++;
  55. p = i + 2;
  56. }
  57. start -= delta;
  58. while( true ) {
  59. var i = value.indexOf("\r\n", p);
  60. if( i < 0 || i > end ) break;
  61. delta++;
  62. p = i + 2;
  63. }
  64. end -= delta;
  65. // IE
  66. var r = doc.createTextRange();
  67. r.moveEnd('textedit',-1);
  68. r.moveStart('character',start);
  69. r.moveEnd('character',end - start);
  70. r.select();
  71. }
  72. public function insert( left : String, text : String, right : String ) {
  73. doc.focus();
  74. // Mozilla
  75. if( doc.selectionStart != null ) {
  76. var top = doc.scrollTop;
  77. var start = doc.selectionStart;
  78. var end = doc.selectionEnd;
  79. doc.value = doc.value.substr(0,start) + left + text + right + doc.value.substr(end);
  80. doc.selectionStart = start + left.length;
  81. doc.selectionEnd = start + left.length + text.length;
  82. doc.scrollTop = top;
  83. return;
  84. }
  85. // IE
  86. var range = untyped js.Lib.document.selection.createRange();
  87. range.text = left + text + right;
  88. range.moveStart('character',-text.length-right.length);
  89. range.moveEnd('character',-right.length);
  90. range.select();
  91. }
  92. }