summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--lib/scripts/edit.js88
-rw-r--r--lib/scripts/editor.js91
2 files changed, 91 insertions, 88 deletions
diff --git a/lib/scripts/edit.js b/lib/scripts/edit.js
index a9623e14d..816568e92 100644
--- a/lib/scripts/edit.js
+++ b/lib/scripts/edit.js
@@ -156,86 +156,6 @@ function addBtnActionSignature(btn, props, edid) {
}
/**
- * 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
- */
-function keyHandler(e){
- if(e.keyCode != 13 &&
- e.keyCode != 8 &&
- e.keyCode != 32) return;
- var field = e.target;
- var selection = getSelection(field);
- if(selection.getLength()) return; //there was text selected, keep standard behavior
- var search = "\n"+field.value.substr(0,selection.start);
- var linestart = Math.max(search.lastIndexOf("\n"),
- search.lastIndexOf("\r")); //IE workaround
- search = search.substr(linestart);
-
-
- if(e.keyCode == 13){ // Enter
- // keep current indention for lists and code
- var match = search.match(/(\n +([\*-] ?)?)/);
- if(match){
- var scroll = field.scrollHeight;
- var match2 = search.match(/^\n +[\*-]\s*$/);
- // Cancel list if the last item is empty (i. e. two times enter)
- if (match2 && field.value.substr(selection.start).match(/^($|\r?\n)/)) {
- field.value = field.value.substr(0, linestart) + "\n" +
- field.value.substr(selection.start);
- selection.start = linestart + 1;
- selection.end = linestart + 1;
- setSelection(selection);
- } else {
- insertAtCarret(field.id,match[1]);
- }
- field.scrollTop += (field.scrollHeight - scroll);
- e.preventDefault(); // prevent enter key
- return false;
- }
- }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
- return false;
- }
- }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
- return false;
- }
- }
-}
-
-/**
* Determine the current section level while editing
*
* @author Andreas Gohr <gohr@cosmocode.de>
@@ -303,14 +223,6 @@ addInitEvent(function () {
if (edit_text.length > 0) {
if(edit_text.attr('readOnly')) return;
- // in Firefox, keypress doesn't send the correct keycodes,
- // in Opera, the default of keydown can't be prevented
- if (is_opera) {
- edit_text.keypress(keyHandler);
- } else {
- edit_text.keydown(keyHandler);
- }
-
// set focus and place cursor at the start
var sel = getSelection(edit_text.get(0));
sel.start = 0;
diff --git a/lib/scripts/editor.js b/lib/scripts/editor.js
index d1bfe0232..6d7f9f4a8 100644
--- a/lib/scripts/editor.js
+++ b/lib/scripts/editor.js
@@ -18,6 +18,17 @@ var dw_editor = {
if(!$editor.length) return;
dw_editor.initSizeCtl('#size__ctl',$editor);
+
+ if($editor.attr('readOnly')) return;
+
+ // in Firefox, keypress doesn't send the correct keycodes,
+ // in Opera, the default of keydown can't be prevented
+ if (jQuery.browser.opera) {
+ $editor.keypress(dw_editor.keyHandler);
+ } else {
+ $editor.keydown(dw_editor.keyHandler);
+ }
+
},
/**
@@ -108,6 +119,86 @@ var dw_editor = {
var 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, backspce and enter presses
+ *
+ * @author Andreas Gohr <andi@splitbrain.org>
+ * @fixme handle tabs
+ * @param event e - the key press event object
+ */
+ keyHandler: function(e){
+ if(e.keyCode != 13 &&
+ e.keyCode != 8 &&
+ e.keyCode != 32) return;
+ var field = e.target;
+ var selection = getSelection(field);
+ if(selection.getLength()) return; //there was text selected, keep standard behavior
+ var search = "\n"+field.value.substr(0,selection.start);
+ var linestart = Math.max(search.lastIndexOf("\n"),
+ search.lastIndexOf("\r")); //IE workaround
+ search = search.substr(linestart);
+
+ if(e.keyCode == 13){ // Enter
+ // keep current indention for lists and code
+ var match = search.match(/(\n +([\*-] ?)?)/);
+ if(match){
+ var scroll = field.scrollHeight;
+ var match2 = search.match(/^\n +[\*-]\s*$/);
+ // Cancel list if the last item is empty (i. e. two times enter)
+ if (match2 && field.value.substr(selection.start).match(/^($|\r?\n)/)) {
+ field.value = field.value.substr(0, linestart) + "\n" +
+ field.value.substr(selection.start);
+ selection.start = linestart + 1;
+ selection.end = linestart + 1;
+ setSelection(selection);
+ } else {
+ insertAtCarret(field.id,match[1]);
+ }
+ field.scrollTop += (field.scrollHeight - scroll);
+ e.preventDefault(); // prevent enter key
+ return false;
+ }
+ }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
+ return false;
+ }
+ }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
+ return false;
+ }
+ }
}