textselection.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Text selection related functions.
  3. */
  4. /**
  5. * selection prototype
  6. *
  7. * Object that capsulates the selection in a textarea. Returned by DWgetSelection.
  8. *
  9. * @author Andreas Gohr <andi@splitbrain.org>
  10. */
  11. function selection_class(){
  12. this.start = 0;
  13. this.end = 0;
  14. this.obj = null;
  15. this.scroll = 0;
  16. this.fix = 0;
  17. this.getLength = function(){
  18. return this.end - this.start;
  19. };
  20. this.getText = function(){
  21. return (!this.obj) ? '' : this.obj.value.substring(this.start,this.end);
  22. };
  23. }
  24. /**
  25. * Get current selection/cursor position in a given textArea
  26. *
  27. * @link http://groups.drupal.org/node/1210
  28. * @author Andreas Gohr <andi@splitbrain.org>
  29. * @link http://linebyline.blogspot.com/2006/11/textarea-cursor-position-in-internet.html
  30. * @returns object - a selection object
  31. */
  32. function DWgetSelection(textArea) {
  33. var sel = new selection_class();
  34. textArea.focus();
  35. sel.obj = textArea;
  36. sel.start = textArea.selectionStart;
  37. sel.end = textArea.selectionEnd;
  38. sel.scroll = textArea.scrollTop;
  39. return sel;
  40. }
  41. /**
  42. * Set the selection
  43. *
  44. * You need to get a selection object via DWgetSelection() first, then modify the
  45. * start and end properties and pass it back to this function.
  46. *
  47. * @link http://groups.drupal.org/node/1210
  48. * @author Andreas Gohr <andi@splitbrain.org>
  49. * @param {selection_class} selection a selection object as returned by DWgetSelection()
  50. */
  51. function DWsetSelection(selection){
  52. selection.obj.setSelectionRange(selection.start, selection.end);
  53. if(selection.scroll) selection.obj.scrollTop = selection.scroll;
  54. }
  55. /**
  56. * Inserts the given text at the current cursor position or replaces the current
  57. * selection
  58. *
  59. * @author Andreas Gohr <andi@splitbrain.org>
  60. * @param {string} text the new text to be pasted
  61. * @param {selection_class} selection selection object returned by DWgetSelection
  62. * @param {int} opts.startofs number of charcters at the start to skip from new selection
  63. * @param {int} opts.endofs number of characters at the end to skip from new selection
  64. * @param {boolean} opts.nosel set true if new text should not be selected
  65. */
  66. function pasteText(selection,text,opts){
  67. if(!opts) opts = {};
  68. // replace the content
  69. selection.obj.value =
  70. selection.obj.value.substring(0, selection.start) + text +
  71. selection.obj.value.substring(selection.end, selection.obj.value.length);
  72. // set new selection
  73. if (is_opera) {
  74. // Opera replaces \n by \r\n when inserting text.
  75. selection.end = selection.start + text.replace(/\r?\n/g, '\r\n').length;
  76. } else {
  77. selection.end = selection.start + text.length;
  78. }
  79. // modify the new selection if wanted
  80. if(opts.startofs) selection.start += opts.startofs;
  81. if(opts.endofs) selection.end -= opts.endofs;
  82. // no selection wanted? set cursor to end position
  83. if(opts.nosel) selection.start = selection.end;
  84. DWsetSelection(selection);
  85. }
  86. /**
  87. * Format selection
  88. *
  89. * Apply tagOpen/tagClose to selection in textarea, use sampleText instead
  90. * of selection if there is none.
  91. *
  92. * @author Andreas Gohr <andi@splitbrain.org>
  93. */
  94. function insertTags(textAreaID, tagOpen, tagClose, sampleText){
  95. var txtarea = jQuery('#' + textAreaID)[0];
  96. var selection = DWgetSelection(txtarea);
  97. var text = selection.getText();
  98. var opts;
  99. // don't include trailing space in selection
  100. if(text.charAt(text.length - 1) == ' '){
  101. selection.end--;
  102. text = selection.getText();
  103. }
  104. if(!text){
  105. // nothing selected, use the sample text and select it
  106. text = sampleText;
  107. opts = {
  108. startofs: tagOpen.length,
  109. endofs: tagClose.length
  110. };
  111. }else{
  112. // place cursor at the end
  113. opts = {
  114. nosel: true
  115. };
  116. }
  117. // surround with tags
  118. text = tagOpen + text + tagClose;
  119. // do it
  120. pasteText(selection,text,opts);
  121. }
  122. /**
  123. * Wraps around pasteText() for backward compatibility
  124. *
  125. * @author Andreas Gohr <andi@splitbrain.org>
  126. */
  127. function insertAtCarret(textAreaID, text){
  128. var txtarea = jQuery('#' + textAreaID)[0];
  129. var selection = DWgetSelection(txtarea);
  130. pasteText(selection,text,{nosel: true});
  131. }