xref: /dokuwiki/lib/scripts/editor.js (revision a76231726379ab65af1ce994b8d74e2ac814f8db)
1/**
2 * The DokuWiki editor features
3 *
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.
6 */
7
8var dw_editor = {
9
10    /**
11     * initialize the default editor functionality
12     *
13     * All other functions can also be called separately for non-default
14     * textareas
15     */
16    init: function(){
17        var $editor = jQuery('#wiki__text');
18        if($editor.length === 0) {
19            return;
20        }
21
22        dw_editor.initSizeCtl('#size__ctl',$editor);
23
24        if($editor.attr('readOnly')) {
25            return;
26        }
27
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);
32        } else {
33            $editor.keydown(dw_editor.keyHandler);
34        }
35
36    },
37
38    /**
39     * Add the edit window size and wrap controls
40     *
41     * Initial values are read from cookie if it exists
42     *
43     * @param selector ctlarea the div to place the controls
44     * @param selector editor  the textarea to control
45     */
46    initSizeCtl: function(ctlarea,editor){
47        var $ctl      = jQuery(ctlarea),
48            $textarea = jQuery(editor);
49
50        if($ctl.length === 0 || $textarea.length === 0) {
51            return;
52        }
53
54        $textarea.css('height', DokuCookie.getValue('sizeCtl') || '300px');
55
56        var wrp = DokuCookie.getValue('wrapCtl');
57        if(wrp){
58            dw_editor.setWrap($textarea[0], wrp);
59        } // else use default value
60
61        jQuery.each([
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')
68                .click(img[1])
69                .appendTo($ctl);
70        });
71    },
72
73    /**
74     * This sets the vertical size of the editbox and adjusts the cookie
75     *
76     * @param selector editor  the textarea to control
77     * @param int val          the relative value to resize in pixel
78     */
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'));
84    },
85
86    /**
87     * Toggle the wrapping mode of the editor textarea and adjusts the
88     * cookie
89     *
90     * @param selector editor  the textarea to control
91     */
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'));
98    },
99
100    /**
101     * Set the wrapping mode of a textarea
102     *
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
109     */
110    setWrap: function(textarea, wrapAttrValue){
111        textarea.setAttribute('wrap', wrapAttrValue);
112
113        // Fix display for mozilla
114        var parNod = textarea.parentNode;
115        var nxtSib = textarea.nextSibling;
116        parNod.removeChild(textarea);
117        parNod.insertBefore(textarea, nxtSib);
118    },
119
120    /**
121     * Make intended formattings easier to handle
122     *
123     * Listens to all key inputs and handle indentions
124     * of lists and code blocks
125     *
126     * Currently handles space, backspce and enter presses
127     *
128     * @author Andreas Gohr <andi@splitbrain.org>
129     * @fixme handle tabs
130     * @param event e - the key press event object
131     */
132    keyHandler: function(e){
133        if([8, 13, 32].indexOf(e.keyCode) === -1) {
134            return;
135        }
136        var selection = getSelection(this);
137        if(selection.getLength() > 0) {
138            return; //there was text selected, keep standard behavior
139        }
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);
144
145        if(e.keyCode == 13){ // Enter
146            // keep current indention for lists and code
147            var match = search.match(/(\n  +([\*-] ?)?)/);
148            if(match){
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);
158                } else {
159                    insertAtCarret(this.id,match[1]);
160                }
161                this.scrollTop += (this.scrollHeight - scroll);
162                e.preventDefault(); // prevent enter key
163                return false;
164            }
165        }else if(e.keyCode == 8){ // Backspace
166            // unindent lists
167            var match = search.match(/(\n  +)([*-] ?)$/);
168            if(match){
169                var spaces = match[1].length-1;
170
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;
181                }
182                setSelection(selection);
183                e.preventDefault(); // prevent backspace
184                return false;
185            }
186        }else if(e.keyCode == 32){ // Space
187            // intend list item
188            var match = search.match(/(\n  +)([*-] )$/);
189            if(match){
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
196                return false;
197            }
198        }
199    }
200
201
202};
203
204jQuery(dw_editor.init);
205