Selection.hx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. * Copyright (C)2005-2019 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)
  54. break;
  55. delta++;
  56. p = i + 2;
  57. }
  58. start -= delta;
  59. while (true) {
  60. var i = value.indexOf("\r\n", p);
  61. if (i < 0 || i > end)
  62. break;
  63. delta++;
  64. p = i + 2;
  65. }
  66. end -= delta;
  67. // IE
  68. var r = doc.createTextRange();
  69. r.moveEnd('textedit', -1);
  70. r.moveStart('character', start);
  71. r.moveEnd('character', end - start);
  72. r.select();
  73. }
  74. public function insert(left:String, text:String, right:String) {
  75. doc.focus();
  76. // Mozilla
  77. if (doc.selectionStart != null) {
  78. var top = doc.scrollTop;
  79. var start = doc.selectionStart;
  80. var end = doc.selectionEnd;
  81. doc.value = doc.value.substr(0, start) + left + text + right + doc.value.substr(end);
  82. doc.selectionStart = start + left.length;
  83. doc.selectionEnd = start + left.length + text.length;
  84. doc.scrollTop = top;
  85. return;
  86. }
  87. // IE
  88. var range = untyped js.Lib.document.selection.createRange();
  89. range.text = left + text + right;
  90. range.moveStart('character', -text.length - right.length);
  91. range.moveEnd('character', -right.length);
  92. range.select();
  93. }
  94. }