Selection.hx 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package js;
  26. import js.Dom.Textarea;
  27. class Selection {
  28. var doc : Dynamic;
  29. public function new( doc : Textarea ) {
  30. this.doc = doc;
  31. }
  32. public function get() {
  33. // Mozilla
  34. if( doc.selectionStart != null )
  35. return doc.value.substring(doc.selectionStart,doc.selectionEnd);
  36. // IE
  37. var range = untyped js.Lib.document.selection.createRange();
  38. if( range.parentElement() != doc )
  39. return "";
  40. return range.text;
  41. }
  42. public function select( start : Int, end : Int ) {
  43. doc.focus();
  44. // Mozilla
  45. if( doc.selectionStart != null ) {
  46. doc.selectionStart = start;
  47. doc.selectionEnd = end;
  48. return;
  49. }
  50. // FIX : IE count \r\n as one single char for selection
  51. // operations, we must then deal with it
  52. var value : String = doc.value;
  53. var p = 0, delta = 0;
  54. while( true ) {
  55. var i = value.indexOf("\r\n", p);
  56. if( i < 0 || i > start ) break;
  57. delta++;
  58. p = i + 2;
  59. }
  60. start -= delta;
  61. while( true ) {
  62. var i = value.indexOf("\r\n", p);
  63. if( i < 0 || i > end ) break;
  64. delta++;
  65. p = i + 2;
  66. }
  67. end -= delta;
  68. // IE
  69. var r = doc.createTextRange();
  70. r.moveEnd('textedit',-1);
  71. r.moveStart('character',start);
  72. r.moveEnd('character',end - start);
  73. r.select();
  74. }
  75. public function insert( left : String, text : String, right : String ) {
  76. doc.focus();
  77. // Mozilla
  78. if( doc.selectionStart != null ) {
  79. var top = doc.scrollTop;
  80. var start = doc.selectionStart;
  81. var end = doc.selectionEnd;
  82. doc.value = doc.value.substr(0,start) + left + text + right + doc.value.substr(end);
  83. doc.selectionStart = start + left.length;
  84. doc.selectionEnd = start + left.length + text.length;
  85. doc.scrollTop = top;
  86. return;
  87. }
  88. // IE
  89. var range = untyped js.Lib.document.selection.createRange();
  90. range.text = left + text + right;
  91. range.moveStart('character',-text.length-right.length);
  92. range.moveEnd('character',-right.length);
  93. range.select();
  94. }
  95. }