diff options
Diffstat (limited to 'lib/scripts/edit.js')
-rw-r--r-- | lib/scripts/edit.js | 69 |
1 files changed, 68 insertions, 1 deletions
diff --git a/lib/scripts/edit.js b/lib/scripts/edit.js index 267f51194..51a8e3f8e 100644 --- a/lib/scripts/edit.js +++ b/lib/scripts/edit.js @@ -186,8 +186,75 @@ function addBtnActionAutohead(btn, props, edid, id) return true; } +/** + * Make intended formattings easier to handle + * + * Listens to all key inputs and handle indentions + * of lists and code blocks + * + * Currently handles space, backspce and enter presses + * + * @author Andreas Gohr <andi@splitbrain.org> + * @fixme handle tabs + * @fixme IE compatibility not tested yet + */ +function keyHandler(e){ + if(e.keyCode != 13 && + e.keyCode != 8 && + e.keyCode != 32) return; //FIXME IE + var field = e.target; + var selection = getSelection(field); + var search = "\n"+field.value.substr(0,selection.start); + var linestart = search.lastIndexOf("\n"); + search = search.substr(linestart); + + if(e.keyCode == 13){ // Enter //FIXME IE + // keep current indention for lists and code + var match = search.match(/(\n +([*-] ?)?)/); + if(match){ + insertAtCarret(field.id,match[1]); + e.preventDefault(); // prevent enter key + } + }else if(e.keyCode == 8){ // Backspace + // unindent lists + var match = search.match(/(\n +)([*-] ?)$/); + if(match){ + var spaces = match[1].length-1; + + if(spaces > 3){ // unindent one level + field.value = field.value.substr(0,linestart)+ + field.value.substr(linestart+2); + selection.start = selection.start - 2; + selection.end = selection.start; + }else{ // delete list point + field.value = field.value.substr(0,linestart)+ + field.value.substr(selection.start); + selection.start = linestart; + selection.end = linestart; + } + setSelection(selection); + e.preventDefault(); // prevent backspace + } + }else if(e.keyCode == 32){ // Space + // intend list item + var match = search.match(/(\n +)([*-] )$/); + if(match){ + field.value = field.value.substr(0,linestart)+' '+ + field.value.substr(linestart); + selection.start = selection.start + 2; + selection.end = selection.start; + setSelection(selection); + e.preventDefault(); // prevent space + } + } +} - +//FIXME consolidate somewhere else +addInitEvent(function(){ + var field = $('wiki__text'); + if(!field) return; + addEvent(field,'keydown',keyHandler); +}); /** * Determine the current section level while editing |