2
* The DokuWiki editor features
4
* These are the advanced features of the editor. It does NOT contain any
5
* code for the toolbar buttons and it functions. See toolbar.js for that.
11
* initialize the default editor functionality
13
* All other functions can also be called separately for non-default
17
var $editor = jQuery('#wiki__text');
18
if($editor.length === 0) {
22
dw_editor.initSizeCtl('#size__ctl',$editor);
24
if($editor.attr('readOnly')) {
28
// in Firefox, keypress doesn't send the correct keycodes,
29
// in Opera, the default of keydown can't be prevented
30
if (jQuery.browser.opera) {
31
$editor.keypress(dw_editor.keyHandler);
33
$editor.keydown(dw_editor.keyHandler);
39
* Add the edit window size and wrap controls
41
* Initial values are read from cookie if it exists
43
* @param selector ctlarea the div to place the controls
44
* @param selector editor the textarea to control
46
initSizeCtl: function(ctlarea,editor){
47
var $ctl = jQuery(ctlarea),
48
$textarea = jQuery(editor);
50
if($ctl.length === 0 || $textarea.length === 0) {
54
$textarea.css('height', DokuCookie.getValue('sizeCtl') || '300px');
56
var wrp = DokuCookie.getValue('wrapCtl');
58
dw_editor.setWrap($textarea[0], wrp);
59
} // else use default value
62
['larger', function(){dw_editor.sizeCtl(editor,100);}],
63
['smaller', function(){dw_editor.sizeCtl(editor,-100);}],
64
['wrap', function(){dw_editor.toggleWrap(editor);}]
65
], function (_, img) {
66
jQuery(document.createElement('IMG'))
67
.attr('src', DOKU_BASE+'lib/images/' + img[0] + '.gif')
74
* This sets the vertical size of the editbox and adjusts the cookie
76
* @param selector editor the textarea to control
77
* @param int val the relative value to resize in pixel
79
sizeCtl: function(editor,val){
80
var $textarea = jQuery(editor),
81
height = parseInt($textarea.css('height')) + val;
82
$textarea.css('height', height+'px');
83
DokuCookie.setValue('sizeCtl',$textarea.css('height'));
87
* Toggle the wrapping mode of the editor textarea and adjusts the
90
* @param selector editor the textarea to control
92
toggleWrap: function(editor){
93
var $textarea = jQuery(editor),
94
wrap = $textarea.attr('wrap');
95
dw_editor.setWrap($textarea[0],
96
(wrap && wrap.toLowerCase() == 'off') ? 'soft' : 'off');
97
DokuCookie.setValue('wrapCtl',$textarea.attr('wrap'));
101
* Set the wrapping mode of a textarea
103
* @author Fluffy Convict <fluffyconvict@hotmail.com>
104
* @author <shutdown@flashmail.com>
105
* @link http://news.hping.org/comp.lang.javascript.archive/12265.html
106
* @link https://bugzilla.mozilla.org/show_bug.cgi?id=41464
107
* @param DomObject textarea
108
* @param string wrapAttrValue
110
setWrap: function(textarea, wrapAttrValue){
111
textarea.setAttribute('wrap', wrapAttrValue);
113
// Fix display for mozilla
114
var parNod = textarea.parentNode;
115
var nxtSib = textarea.nextSibling;
116
parNod.removeChild(textarea);
117
parNod.insertBefore(textarea, nxtSib);
121
* Make intended formattings easier to handle
123
* Listens to all key inputs and handle indentions
124
* of lists and code blocks
126
* Currently handles space, backspce and enter presses
128
* @author Andreas Gohr <andi@splitbrain.org>
130
* @param event e - the key press event object
132
keyHandler: function(e){
133
if(jQuery.inArray(e.keyCode,[8, 13, 32]) === -1) {
136
var selection = getSelection(this);
137
if(selection.getLength() > 0) {
138
return; //there was text selected, keep standard behavior
140
var search = "\n"+this.value.substr(0,selection.start);
141
var linestart = Math.max(search.lastIndexOf("\n"),
142
search.lastIndexOf("\r")); //IE workaround
143
search = search.substr(linestart);
145
if(e.keyCode == 13){ // Enter
146
// keep current indention for lists and code
147
var match = search.match(/(\n +([\*-] ?)?)/);
149
var scroll = this.scrollHeight;
150
var match2 = search.match(/^\n +[\*-]\s*$/);
151
// Cancel list if the last item is empty (i. e. two times enter)
152
if (match2 && this.value.substr(selection.start).match(/^($|\r?\n)/)) {
153
this.value = this.value.substr(0, linestart) + "\n" +
154
this.value.substr(selection.start);
155
selection.start = linestart + 1;
156
selection.end = linestart + 1;
157
setSelection(selection);
159
insertAtCarret(this.id,match[1]);
161
this.scrollTop += (this.scrollHeight - scroll);
162
e.preventDefault(); // prevent enter key
165
}else if(e.keyCode == 8){ // Backspace
167
var match = search.match(/(\n +)([*-] ?)$/);
169
var spaces = match[1].length-1;
171
if(spaces > 3){ // unindent one level
172
this.value = this.value.substr(0,linestart)+
173
this.value.substr(linestart+2);
174
selection.start = selection.start - 2;
175
selection.end = selection.start;
176
}else{ // delete list point
177
this.value = this.value.substr(0,linestart)+
178
this.value.substr(selection.start);
179
selection.start = linestart;
180
selection.end = linestart;
182
setSelection(selection);
183
e.preventDefault(); // prevent backspace
186
}else if(e.keyCode == 32){ // Space
188
var match = search.match(/(\n +)([*-] )$/);
190
this.value = this.value.substr(0,linestart)+' '+
191
this.value.substr(linestart);
192
selection.start = selection.start + 2;
193
selection.end = selection.start;
194
setSelection(selection);
195
e.preventDefault(); // prevent space
204
jQuery(dw_editor.init);