1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
/**
* The DokuWiki editor features
*
* These are the advanced features of the editor. It does NOT contain any
* code for the toolbar buttons and its functions. See toolbar.js for that.
*/
let dw_editor = {
/**
* initialize the default editor functionality
*
* All other functions can also be called separately for non-default
* textareas
*/
init: function () {
const $editor = jQuery('#wiki__text');
if ($editor.length === 0) {
return;
}
dw_editor.initSizeCtl('#size__ctl', $editor);
if ($editor.attr('readOnly')) {
return;
}
$editor.keydown(dw_editor.keyHandler);
},
/**
* Add the edit window size and wrap controls
*
* Initial values are read from cookie if it exists
*
* @param {string|Element|jQuery} ctlarea the div to place the controls
* @param {string|Element|jQuery} editor the textarea to control
*/
initSizeCtl: function (ctlarea, editor) {
const $ctl = jQuery(ctlarea),
$textarea = jQuery(editor);
if ($ctl.length === 0 || $textarea.length === 0) {
return;
}
$textarea.css('height', DokuCookie.getValue('sizeCtl') || '300px');
const wrp = DokuCookie.getValue('wrapCtl');
if (wrp) {
dw_editor.setWrap($textarea[0], wrp);
} // else use default value
jQuery.each([
['larger', function () {
dw_editor.sizeCtl(editor, 100);
}],
['smaller', function () {
dw_editor.sizeCtl(editor, -100);
}],
['wrap', function () {
dw_editor.toggleWrap(editor);
}]
], function (_, img) {
jQuery(document.createElement('img'))
.attr('src', DOKU_BASE + 'lib/images/' + img[0] + '.gif')
.attr('alt', '')
.on('click', img[1])
.appendTo($ctl);
});
},
/**
* This sets the vertical size of the editbox and adjusts the cookie
*
* @param {string|Element|jQuery} editor the textarea to control
* @param {int} val the relative value to resize in pixel
*/
sizeCtl: function (editor, val) {
const $textarea = jQuery(editor),
height = parseInt($textarea.css('height')) + val;
$textarea.css('height', height + 'px');
DokuCookie.setValue('sizeCtl', $textarea.css('height'));
},
/**
* Toggle the wrapping mode of the editor textarea and adjusts the
* cookie
*
* @param {string|Element|jQuery} editor the textarea to control
*/
toggleWrap: function (editor) {
const $textarea = jQuery(editor),
wrap = $textarea.attr('wrap');
dw_editor.setWrap($textarea[0],
(wrap && wrap.toLowerCase() === 'off') ? 'soft' : 'off');
DokuCookie.setValue('wrapCtl', $textarea.attr('wrap'));
},
/**
* Set the wrapping mode of a textarea
*
* @author Fluffy Convict <fluffyconvict@hotmail.com>
* @author <shutdown@flashmail.com>
* @link http://news.hping.org/comp.lang.javascript.archive/12265.html
* @link https://bugzilla.mozilla.org/show_bug.cgi?id=41464
* @param {Element} textarea
* @param {string} wrapAttrValue
*/
setWrap: function (textarea, wrapAttrValue) {
textarea.setAttribute('wrap', wrapAttrValue);
// Fix display for mozilla
const parNod = textarea.parentNode;
const nxtSib = textarea.nextSibling;
parNod.removeChild(textarea);
parNod.insertBefore(textarea, nxtSib);
},
/**
* Make intended formattings easier to handle
*
* Listens to all key inputs and handle indentions
* of lists and code blocks
*
* Currently handles space, backspace, enter and
* ctrl-enter presses
*
* @author Andreas Gohr <andi@splitbrain.org>
* @fixme handle tabs
* @param {KeyboardEvent} e - the key press event object
*/
keyHandler: function (e) {
const selection = DWgetSelection(this);
if (selection.getLength() > 0) {
return; //there was text selected, keep standard behavior
}
let search = "\n" + this.value.substring(0, selection.start);
const linestart = Math.max(
search.lastIndexOf("\n"),
search.lastIndexOf("\r") //IE workaround
);
search = search.substring(linestart);
if (e.key === 'Enter' && e.ctrlKey) {
// Submit current edit
jQuery('#edbtn__save').trigger('click');
e.preventDefault(); // prevent enter key
return false;
} else if (e.key === 'Enter') {
// keep current indention for lists and code
const match = search.match(/(\n +([*-] ?)?)/);
if (match) {
const scroll = this.scrollHeight;
const match2 = search.match(/^\n +[*-]\s*$/);
// Cancel list if the last item is empty (i.e. two times enter)
if (match2 && this.value.substring(selection.start).match(/^($|\r?\n)/)) {
this.value = this.value.substring(0, linestart) + "\n" +
this.value.substring(selection.start);
selection.start = linestart + 1;
selection.end = linestart + 1;
DWsetSelection(selection);
} else {
insertAtCarret(this.id, match[1]);
}
this.scrollTop += (this.scrollHeight - scroll);
e.preventDefault(); // prevent enter key
return false;
}
} else if (e.key === 'Backspace') {
// unindent lists
const match = search.match(/(\n +)([*-] ?)$/);
if (match) {
const spaces = match[1].length - 1;
if (spaces > 3) { // unindent one level
this.value = this.value.substring(0, linestart) +
this.value.substring(linestart + 2);
selection.start = selection.start - 2;
selection.end = selection.start;
} else { // delete list point
this.value = this.value.substring(0, linestart) +
this.value.substring(selection.start);
selection.start = linestart;
selection.end = linestart;
}
DWsetSelection(selection);
e.preventDefault(); // prevent backspace
return false;
}
} else if (e.key === ' ') { // Space
// intend list item
const match = search.match(/(\n +)([*-] )$/);
if (match) {
this.value = this.value.substring(0, linestart) + ' ' +
this.value.substring(linestart);
selection.start = selection.start + 2;
selection.end = selection.start;
DWsetSelection(selection);
e.preventDefault(); // prevent space
return false;
}
}
}
};
jQuery(dw_editor.init);
|