2 * Text selection related functions.
8 * Object that capsulates the selection in a textarea. Returned by DWgetSelection.
10 * @author Andreas Gohr <andi@splitbrain.org>
12 function selection_class(){
19 this.getLength = function(){
20 return this.end - this.start;
23 this.getText = function(){
24 return (!this.obj) ? '' : this.obj.value.substring(this.start,this.end);
29 * Get current selection/cursor position in a given textArea
31 * @link http://groups.drupal.org/node/1210
32 * @author Andreas Gohr <andi@splitbrain.org>
33 * @link http://linebyline.blogspot.com/2006/11/textarea-cursor-position-in-internet.html
34 * @returns object - a selection object
36 function DWgetSelection(textArea) {
37 var sel = new selection_class();
41 sel.start = textArea.selectionStart;
42 sel.end = textArea.selectionEnd;
43 sel.scroll = textArea.scrollTop;
50 * You need to get a selection object via DWgetSelection() first, then modify the
51 * start and end properties and pass it back to this function.
53 * @link http://groups.drupal.org/node/1210
54 * @author Andreas Gohr <andi@splitbrain.org>
55 * @param {selection_class} selection a selection object as returned by DWgetSelection()
57 function DWsetSelection(selection){
58 selection.obj.setSelectionRange(selection.start, selection.end);
59 if(selection.scroll) selection.obj.scrollTop = selection.scroll;
63 * Inserts the given text at the current cursor position or replaces the current
66 * @author Andreas Gohr <andi@splitbrain.org>
67 * @param {string} text the new text to be pasted
68 * @param {selection_class} selection selection object returned by DWgetSelection
69 * @param {int} opts.startofs number of charcters at the start to skip from new selection
70 * @param {int} opts.endofs number of characters at the end to skip from new selection
71 * @param {boolean} opts.nosel set true if new text should not be selected
73 function pasteText(selection,text,opts){
75 // replace the content
78 selection.obj.value.substring(0, selection.start) + text +
79 selection.obj.value.substring(selection.end, selection.obj.value.length);
83 // Opera replaces \n by \r\n when inserting text.
84 selection.end = selection.start + text.replace(/\r?\n/g, '\r\n').length;
86 selection.end = selection.start + text.length;
90 // modify the new selection if wanted
91 if(opts.startofs) selection.start += opts.startofs;
92 if(opts.endofs) selection.end -= opts.endofs;
94 // no selection wanted? set cursor to end position
95 if(opts.nosel) selection.start = selection.end;
97 DWsetSelection(selection);
104 * Apply tagOpen/tagClose to selection in textarea, use sampleText instead
105 * of selection if there is none.
107 * @author Andreas Gohr <andi@splitbrain.org>
109 function insertTags(textAreaID, tagOpen, tagClose, sampleText){
110 var txtarea = jQuery('#' + textAreaID)[0];
112 var selection = DWgetSelection(txtarea);
113 var text = selection.getText();
116 // don't include trailing space in selection
117 if(text.charAt(text.length - 1) == ' '){
119 text = selection.getText();
123 // nothing selected, use the sample text and select it
126 startofs: tagOpen.length,
127 endofs: tagClose.length
130 // place cursor at the end
136 // surround with tags
137 text = tagOpen + text + tagClose;
140 pasteText(selection,text,opts);
144 * Wraps around pasteText() for backward compatibility
146 * @author Andreas Gohr <andi@splitbrain.org>
148 function insertAtCarret(textAreaID, text){
149 var txtarea = jQuery('#' + textAreaID)[0];
150 var selection = DWgetSelection(txtarea);
151 pasteText(selection,text,{nosel: true});