2 // Source: http://stackoverflow.com/questions/401593/textarea-selection
8 if('selectionStart' in e
)
10 var l
= e
.selectionEnd
- e
.selectionStart
;
11 return { start
: e
.selectionStart
, end
: e
.selectionEnd
, length
: l
, text
: e
.value
.substr(e
.selectionStart
, l
) };
14 else if(document
.selection
)
17 var r
= document
.selection
.createRange();
18 var tr
= e
.createTextRange();
19 var tr2
= tr
.duplicate();
20 tr2
.moveToBookmark(r
.getBookmark());
21 tr
.setEndPoint('EndToStart',tr2
);
22 if (r
== null || tr
== null) return { start
: e
.value
.length
, end
: e
.value
.length
, length
: 0, text
: '' };
23 var text_part
= r
.text
.replace(/[\r\n]/g,'.'); //for some reason IE doesn't always count the \n and \r in the length
24 var text_whole
= e
.value
.replace(/[\r\n]/g,'.');
25 var the_start
= text_whole
.indexOf(text_part
,tr
.text
.length
);
26 return { start
: the_start
, end
: the_start
+ text_part
.length
, length
: text_part
.length
, text
: r
.text
};
28 //Browser not supported
29 else return { start
: e
.value
.length
, end
: e
.value
.length
, length
: 0, text
: '' };
32 replace: function (e
, replace_str
)
34 var selection
= this.get(e
);
35 var start_pos
= selection
.start
;
36 var end_pos
= start_pos
+ replace_str
.length
;
37 e
.value
= e
.value
.substr(0, start_pos
) + replace_str
+ e
.value
.substr(selection
.end
, e
.value
.length
);
38 this.set(e
,start_pos
,end_pos
);
39 return {start
: start_pos
, end
: end_pos
, length
: replace_str
.length
, text
: replace_str
};
42 set: function (e
, start_pos
,end_pos
)
45 if('selectionStart' in e
)
48 e
.selectionStart
= start_pos
;
49 e
.selectionEnd
= end_pos
;
52 else if(document
.selection
)
55 var tr
= e
.createTextRange();
57 //Fix IE from counting the newline characters as two seperate characters
58 var stop_it
= start_pos
;
59 for (i
=0; i
< stop_it
; i
++) if( e
.value
[i
].search(/[\r\n]/) != -1 ) start_pos
= start_pos
- .5;
61 for (i
=0; i
< stop_it
; i
++) if( e
.value
[i
].search(/[\r\n]/) != -1 ) end_pos
= end_pos
- .5;
63 tr
.moveEnd('textedit',-1);
64 tr
.moveStart('character',start_pos
);
65 tr
.moveEnd('character',end_pos
- start_pos
);
71 wrap: function (e
, left_str
, right_str
, sel_offset
, sel_length
)
73 var scroll
= e
.scrollTop
;
74 var the_sel_text
= this.get(e
).text
;
75 var selection
= this.replace(e
, left_str
+ the_sel_text
+ right_str
);
76 if(sel_offset
!== undefined && sel_length
!== undefined) selection
= this.set(e
, selection
.start
+ sel_offset
, selection
.start
+ sel_offset
+ sel_length
);
77 else if(the_sel_text
== '') selection
= this.set(e
, selection
.start
+ left_str
.length
, selection
.start
+ left_str
.length
);
83 function launchToolbar()
85 function addBtn(className
, label
, action
)
87 var btn
= document
.createElement('input');
89 btn
.className
= className
;
92 toolbar
.appendChild(btn
);
95 var txt
= document
.getElementById('f_contenu');
96 var parent
= txt
.parentNode
.parentNode
;
97 var toolbar
= document
.createElement('div');
98 toolbar
.className
= "toolbar";
100 addBtn('title', 'Titre', function () { selection
.wrap(txt
, '{{{', "}}}\n"); } );
101 addBtn('italic', 'Italique', function () { selection
.wrap(txt
, '{', '}'); } );
102 addBtn('bold', 'Gras', function () { selection
.wrap(txt
, '{{', '}}'); } );
103 addBtn('strike', 'Barré', function () { selection
.wrap(txt
, '<del>', '</del>'); } );
104 addBtn('code', 'Chasse fixe', function () { selection
.wrap(txt
, '<pre>', '</pre>'); } );
105 addBtn('link', 'Lien', function () {
106 if (url
= window
.prompt('Adresse du lien ?'))
108 selection
.wrap(txt
, '[', '->' + url
+ ']');
112 parent
.insertBefore(toolbar
, txt
.parentNode
);
115 if (document
.addEventListener
)
117 document
.addEventListener("DOMContentLoaded", launchToolbar
, false);
121 document
.attachEvent("onDOMContentLoaded", launchToolbar
);