1
EditArea.prototype.focus = function() {
3
this.textareaFocused=true;
7
EditArea.prototype.check_line_selection= function(timer_checkup){
8
//if(do_highlight==false){
10
alert("ONCE a"+ this.isResizing);
13
if(!editAreas[this.id])
17
t1=t2=t3= time.getTime();
19
if(!this.smooth_selection && !this.do_highlight){
21
}else if(this.textareaFocused && editAreas[this.id]["displayed"]==true && this.isResizing==false){
22
infos= this.get_selection_infos();
26
//if(this.last_selection["line_start"] != infos["line_start"] || this.last_selection["line_nb"] != infos["line_nb"] || infos["full_text"] != this.last_selection["full_text"]){
27
if(this.last_selection["line_start"] != infos["line_start"] || this.last_selection["line_nb"] != infos["line_nb"] || infos["full_text"] != this.last_selection["full_text"] || this.reload_highlight){
28
// if selection change
30
new_top=this.lineHeight * (infos["line_start"]-1);
31
new_height=Math.max(0, this.lineHeight * infos["line_nb"]);
32
new_width=Math.max(this.textarea.scrollWidth, this.container.clientWidth -50);
34
this.selection_field.style.top=new_top+"px";
35
this.selection_field.style.width=new_width+"px";
36
this.selection_field.style.height=new_height+"px";
37
document.getElementById("cursor_pos").style.top=new_top+"px";
39
if(this.do_highlight==true){
40
var curr_text=infos["full_text"].split("\n");
42
//alert("length: "+curr_text.length+ " i: "+ Math.max(0,infos["line_start"]-1)+ " end: "+Math.min(curr_text.length, infos["line_start"]+infos["line_nb"]-1)+ " line: "+infos["line_start"]+" [0]: "+curr_text[0]+" [1]: "+curr_text[1]);
43
var start=Math.max(0,infos["line_start"]-1);
44
var end=Math.min(curr_text.length, infos["line_start"]+infos["line_nb"]-1);
46
//curr_text[start]= curr_text[start].substr(0,infos["curr_pos"]-1) +"¤_overline_¤"+ curr_text[start].substr(infos["curr_pos"]-1);
47
for(i=start; i< end; i++){
48
content+= curr_text[i]+"\n";
51
content= content.replace(/&/g,"&");
52
content= content.replace(/</g,"<");
53
content= content.replace(/>/g,">");
55
if(this.nav['isIE'] || this.nav['isOpera'])
56
this.selection_field.innerHTML= "<pre>" + content.replace("\n", "<br/>") + "</pre>";
58
this.selection_field.innerHTML=content;
60
if(this.reload_highlight || (infos["full_text"] != this.last_text_to_highlight && (this.last_selection["line_start"]!=infos["line_start"] || this.last_selection["line_nb"]!=infos["line_nb"] || this.last_selection["nb_line"]!=infos["nb_line"]) ) )
61
this.maj_highlight(infos);
67
// manage bracket finding
68
if(infos["line_start"] != this.last_selection["line_start"] || infos["curr_pos"] != this.last_selection["curr_pos"] || infos["full_text"].length!=this.last_selection["full_text"].length || this.reload_highlight){
70
var selec_char= infos["curr_line"].charAt(infos["curr_pos"]-1);
71
var no_real_move=true;
72
if(infos["line_nb"]==1 && (this.assocBracket[selec_char] || this.revertAssocBracket[selec_char]) ){
75
//findEndBracket(infos["line_start"], infos["curr_pos"], selec_char);
76
if(this.findEndBracket(infos, selec_char) === true){
77
document.getElementById("end_bracket").style.visibility="visible";
78
document.getElementById("cursor_pos").style.visibility="visible";
79
document.getElementById("cursor_pos").innerHTML= selec_char;
80
document.getElementById("end_bracket").innerHTML= (this.assocBracket[selec_char] || this.revertAssocBracket[selec_char]);
82
document.getElementById("end_bracket").style.visibility="hidden";
83
document.getElementById("cursor_pos").style.visibility="hidden";
86
document.getElementById("cursor_pos").style.visibility="hidden";
87
document.getElementById("end_bracket").style.visibility="hidden";
89
//alert("move cursor");
90
this.displayToCursorPosition("cursor_pos", infos["line_start"], infos["curr_pos"]-1, infos["curr_line"], no_real_move);
91
if(infos["line_nb"]==1 && infos["line_start"]!=this.last_selection["line_start"])
92
this.scroll_to_view();
94
this.last_selection=infos;
98
//this.debug.value="tps total: "+ (tend-t1) + " tps get_infos: "+ (t2-t1)+ " tps jaune: "+ (t3-t2) +" tps cursor: "+ (tend-t3)+" "+typeof(infos);
101
if(this.do_highlight==true) //can slow down check speed when highlight mode is on
102
setTimeout("editArea.check_line_selection(true)", 50);
104
setTimeout("editArea.check_line_selection(true)", 50);
109
EditArea.prototype.get_selection_infos= function(){
111
this.getIESelection();
112
start=this.textarea.selectionStart;
113
end=this.textarea.selectionEnd;
115
if(this.last_selection["selectionStart"]==start && this.last_selection["selectionEnd"]==end && this.last_selection["full_text"]==this.textarea.value)
116
return this.last_selection;
118
if(this.tabulation!="\t" && this.textarea.value.indexOf("\t")!=-1)
119
{ // can append only after copy/paste
120
var len= this.textarea.value.length;
121
this.textarea.value=this.replace_tab(this.textarea.value);
122
start=end= start+(this.textarea.value.length-len);
123
this.area_select(start, 0);
125
var selections=new Object();
126
selections["selectionStart"]= start;
127
selections["selectionEnd"]= end;
128
selections["full_text"]= this.textarea.value;
129
selections["line_start"]=1;
130
selections["line_nb"]=1;
131
selections["curr_pos"]=0;
132
selections["curr_line"]="";
133
selections["indexOfCursor"]=0;
134
selections["selec_direction"]= this.last_selection["selec_direction"];
138
var splitTab=selections["full_text"].split("\n");
139
var nbLine=Math.max(0, splitTab.length);
140
var nbChar=Math.max(0, selections["full_text"].length - (nbLine - 1)); // (remove \n caracters from the count)
141
if(selections["full_text"].indexOf("\r")!=-1)
142
nbChar= nbChar - (nbLine -1); // (remove \r caracters from the count)
143
selections["nb_line"]=nbLine;
144
selections["nb_char"]=nbChar;
146
var str=selections["full_text"].substr(0,start);
147
selections["curr_pos"]= start - str.lastIndexOf("\n");
148
selections["line_start"]=Math.max(1, str.split("\n").length);
150
selections["curr_pos"]=1;
153
selections["line_nb"]=selections["full_text"].substring(start,end).split("\n").length;
155
selections["indexOfCursor"]=this.textarea.selectionStart;
156
selections["curr_line"]=splitTab[Math.max(0,selections["line_start"]-1)];
159
// determine in with direction the direction grow
160
if(selections["selectionStart"]==this.last_selection["selectionStart"]){
161
if(selections["selectionEnd"]>this.last_selection["selectionEnd"])
162
selections["selec_direction"]= "down";
163
else if(selections["selectionEnd"] == this.last_selection["selectionStart"])
164
selections["selec_direction"]= this.last_selection["selec_direction"];
165
}else if(selections["selectionStart"] == this.last_selection["selectionEnd"] && selections["selectionEnd"]>this.last_selection["selectionEnd"]){
166
selections["selec_direction"]= "down";
168
selections["selec_direction"]= "up";
171
document.getElementById("nbLine").innerHTML= nbLine;
172
document.getElementById("nbChar").innerHTML= nbChar;
173
document.getElementById("linePos").innerHTML=selections["line_start"];
174
document.getElementById("currPos").innerHTML=selections["curr_pos"];
179
// set IE position in Firefox mode (textarea.selectionStart and textarea.selectionEnd)
180
EditArea.prototype.getIESelection= function(){
181
var range = document.selection.createRange();
182
var stored_range = range.duplicate();
183
stored_range.moveToElementText( this.textarea );
184
stored_range.setEndPoint( 'EndToEnd', range );
185
if(stored_range.parentElement() !=this.textarea)
188
// the range don't take care of empty lines in the end of the selection
189
var scrollTop= this.result.scrollTop + document.body.scrollTop;
191
var relative_top= range.offsetTop - parent.calculeOffsetTop(this.textarea) + scrollTop;
193
var line_start = Math.round((relative_top / this.lineHeight) +1);
195
var line_nb=Math.round(range.boundingHeight / this.lineHeight);
197
var range_start=stored_range.text.length - range.text.length;
198
var tab=this.textarea.value.substr(0, range_start).split("\n");
199
range_start+= (line_start - tab.length)*2; // add missing empty lines to the selection
200
this.textarea.selectionStart = range_start;
202
var range_end=this.textarea.selectionStart + range.text.length;
203
tab=this.textarea.value.substr(0, range_start + range.text.length).split("\n");
204
range_end+= (line_start + line_nb - 1 - tab.length)*2;
206
this.textarea.selectionEnd = range_end;
207
/*this.textarea.selectionStart = 10;
208
this.textarea.selectionEnd = 50;*/
211
// select the text for IE (and take care of \r caracters)
212
EditArea.prototype.setIESelection= function(){
213
var nbLineStart=this.textarea.value.substr(0, this.textarea.selectionStart).split("\n").length - 1;
214
var nbLineEnd=this.textarea.value.substr(0, this.textarea.selectionEnd).split("\n").length - 1;
215
var range = document.selection.createRange();
216
range.moveToElementText( this.textarea );
217
range.setEndPoint( 'EndToStart', range );
219
range.moveStart('character', this.textarea.selectionStart - nbLineStart);
220
range.moveEnd('character', this.textarea.selectionEnd - nbLineEnd - (this.textarea.selectionStart - nbLineStart) );
224
EditArea.prototype.tab_selection= function(){
227
this.is_tabbing=true;
228
//infos=getSelectionInfos();
229
//if( document.selection ){
230
if( this.nav['isIE'] )
231
this.getIESelection();
232
/* Insertion du code de formatage */
233
var start = this.textarea.selectionStart;
234
var end = this.textarea.selectionEnd;
235
var insText = this.textarea.value.substring(start, end);
237
/* Insert tabulation and ajust cursor position */
240
if (insText.length == 0) {
241
// if only one line selected
242
this.textarea.value = this.textarea.value.substr(0, start) + this.tabulation + this.textarea.value.substr(end);
243
pos_start = start + this.tabulation.length;
246
start= Math.max(0, this.textarea.value.substr(0, start).lastIndexOf("\n")+1);
247
endText=this.textarea.value.substr(end);
248
startText=this.textarea.value.substr(0, start);
249
tmp= this.textarea.value.substring(start, end).split("\n");
250
insText= this.tabulation+tmp.join("\n"+this.tabulation);
251
this.textarea.value = startText + insText + endText;
253
pos_end= this.textarea.value.indexOf("\n", startText.length + insText.length);
255
pos_end=this.textarea.value.length;
256
//pos = start + repdeb.length + insText.length + ;
258
this.textarea.selectionStart = pos_start;
259
this.textarea.selectionEnd = pos_end;
261
//if( document.selection ){
262
if(this.nav['isIE']){
263
this.setIESelection();
264
setTimeout("editArea.is_tabbing=false;", 100); // IE can't accept to make 2 tabulation without a little break between both
266
this.is_tabbing=false;
270
EditArea.prototype.invert_tab_selection= function(){
273
this.is_tabbing=true;
274
//infos=getSelectionInfos();
275
//if( document.selection ){
277
this.getIESelection();
279
var start = this.textarea.selectionStart;
280
var end = this.textarea.selectionEnd;
281
var insText = this.textarea.value.substring(start, end);
283
/* Tab remove and cursor seleciton adjust */
286
if (insText.length == 0) {
287
if(this.textarea.value.substring(start-this.tabulation.length, start)==this.tabulation)
289
this.textarea.value = this.textarea.value.substr(0, start-this.tabulation.length) + this.textarea.value.substr(end);
290
pos_start= Math.max(0, start-this.tabulation.length);
294
this.textarea.value = this.textarea.value.substr(0, start) + this.tabulation + insText + this.textarea.value.substr(end);
295
pos_start = start + this.tabulation.length;
298
start= this.textarea.value.substr(0, start).lastIndexOf("\n")+1;
299
endText=this.textarea.value.substr(end);
300
startText=this.textarea.value.substr(0, start);
301
tmp= this.textarea.value.substring(start, end).split("\n");
303
for(i=0; i<tmp.length; i++){
304
for(j=0; j<this.tab_nb_char; j++){
305
if(tmp[i].charAt(0)=="\t"){
306
tmp[i]=tmp[i].substr(1);
308
}else if(tmp[i].charAt(0)==" ")
309
tmp[i]=tmp[i].substr(1);
316
this.textarea.value = startText + insText + endText;
318
pos_end= this.textarea.value.indexOf("\n", startText.length + insText.length);
320
pos_end=this.textarea.value.length;
321
//pos = start + repdeb.length + insText.length + ;
323
this.textarea.selectionStart = pos_start;
324
this.textarea.selectionEnd = pos_end;
326
//if( document.selection ){
327
if(this.nav['isIE']){
328
// select the text for IE
329
this.setIESelection();
330
setTimeout("editArea.is_tabbing=false;", 100); // IE can accept to make 2 tabulation without a little break between both
332
this.is_tabbing=false;
335
EditArea.prototype.press_enter= function(){
336
if(!this.smooth_selection)
339
this.getIESelection();
340
var scrollTop= this.result.scrollTop;
341
var scrollLeft= this.result.scrollLeft;
342
var start=this.textarea.selectionStart;
343
var end= this.textarea.selectionEnd;
344
var start_last_line= Math.max(0 , this.textarea.value.substring(0, start).lastIndexOf("\n") + 1 );
345
var begin_line= this.textarea.value.substring(start_last_line, start).replace(/^([ \t]*).*/gm, "$1");
346
if(begin_line=="\n" || begin_line=="\r" || begin_line.length==0)
349
if(this.nav['isIE'] || this.nav['isOpera']){
350
begin_line="\r\n"+ begin_line;
352
begin_line="\n"+ begin_line;
354
//alert(start_last_line+" strat: "+start +"\n"+this.textarea.value.substring(start_last_line, start)+"\n_"+begin_line+"_")
355
this.textarea.value= this.textarea.value.substring(0, start) + begin_line + this.textarea.value.substring(end);
357
this.area_select(start+ begin_line.length ,0);
358
// during this process IE scroll back to the top of the textarea
359
if(this.nav['isIE']){
360
this.result.scrollTop= scrollTop;
361
this.result.scrollLeft= scrollLeft;
368
EditArea.prototype.findEndBracket= function(infos, bracket){
370
var start=infos["indexOfCursor"];
371
var normal_order=true;
372
//curr_text=infos["full_text"].split("\n");
373
if(this.assocBracket[bracket])
374
endBracket=this.assocBracket[bracket];
375
else if(this.revertAssocBracket[bracket]){
376
endBracket=this.revertAssocBracket[bracket];
382
for(var i=start; i<infos["full_text"].length && i>=0; ){
383
if(infos["full_text"].charAt(i)==endBracket){
385
if(nbBracketOpen<=0){
386
//i=infos["full_text"].length;
390
}else if(infos["full_text"].charAt(i)==bracket)
398
//end=infos["full_text"].indexOf("}", start);
401
var endLastLine=infos["full_text"].substr(0, end).lastIndexOf("\n");
405
line= infos["full_text"].substr(0, endLastLine).split("\n").length + 1;
407
var curPos= end - endLastLine;
409
this.displayToCursorPosition("end_bracket", line, curPos, infos["full_text"].substring(endLastLine +1, end));
413
EditArea.prototype.displayToCursorPosition= function(id, start_line, cur_pos, lineContent, no_real_move){
414
var elem= document.getElementById("test_font_size");
415
var dest= document.getElementById(id);
417
elem.innerHTML="<pre><span id='test_font_size_inner'>"+lineContent.substr(0, cur_pos).replace(/&/g,"&").replace(/</g,"<")+"</span></pre>";
418
posLeft= 45 + document.getElementById('test_font_size_inner').offsetWidth;
420
var posTop=this.lineHeight * (start_line-1);
422
if(no_real_move!=true){ // when the cursor is hidden no need to move him
423
dest.style.top=posTop+"px";
424
dest.style.left=posLeft+"px";
426
// usefull for smarter scroll
427
dest.cursor_top=posTop;
428
dest.cursor_left=posLeft;
430
// document.getElementById(id).style.marginLeft=posLeft+"px";
435
EditArea.prototype.area_select= function(start, length){
436
this.textarea.focus();
438
start= Math.max(0, Math.min(this.textarea.value.length, start));
439
end= Math.max(start, Math.min(this.textarea.value.length, start+length));
441
if(this.nav['isIE']){
442
this.textarea.selectionStart = start;
443
this.textarea.selectionEnd = end;
444
this.setIESelection();
446
if(this.nav['isOpera']){ // Opera bug when moving selection start and selection end
447
/*this.textarea.selectionEnd = 1;
448
this.textarea.selectionStart = 0;
449
this.textarea.selectionEnd = 1;
450
this.textarea.selectionStart = 0;
451
this.textarea.selectionEnd = 0;
452
this.textarea.selectionStart = 0;
453
this.textarea.selectionEnd = 0;
454
this.textarea.selectionStart = 0;*/
455
this.textarea.setSelectionRange(0, 0);
457
this.textarea.setSelectionRange(start, end);
459
this.check_line_selection();
463
EditArea.prototype.area_get_selection= function(){
465
if( document.selection ){
466
var range = document.selection.createRange();
469
text= this.textarea.value.substring(this.textarea.selectionStart, this.textarea.selectionEnd);