list fixes: headers, invalid elements, easier ending
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / keyboard.js
index e43ea89..e7a3ba3 100644 (file)
@@ -349,6 +349,10 @@ var keyEventHandlers = [
             }, {
                 success: function() {
                     if(goto) {
             }, {
                 success: function() {
                     if(goto) {
+                        if(!doc.containsNode(goto.wlxmlNode)) {
+                            goto = s.startElement;
+                            caretTo = s.startOffset;
+                        }
                         s.canvas.setCurrentElement(goto, {caretTo: caretTo});
                     }
                 }
                         s.canvas.setCurrentElement(goto, {caretTo: caretTo});
                     }
                 }
@@ -371,6 +375,9 @@ var keyEventHandlers = [
                 var p = list.after({tagName: 'div', attrs: {'class': 'p'}});
                 p.append({text: ''});
                 item.detach();
                 var p = list.after({tagName: 'div', attrs: {'class': 'p'}});
                 p.append({text: ''});
                 item.detach();
+                if(list.contents().length === 0) {
+                    list.detach();
+                }
                 return p;
             }, {
                 success: function(p) {
                 return p;
             }, {
                 success: function(p) {
@@ -413,6 +420,28 @@ var keyEventHandlers = [
 
             s.canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions);
         }
 
             s.canvas.setCurrentElement(utils.getElementForNode(goto), gotoOptions);
         }
+    },
+    {
+        applies: function (e, s) {
+            return s.type === 'nodeSelection' && e.key === KEYS.ENTER && !s.element.isRootElement();
+        },
+        run: function (e, s) {
+            var parent = s.element.parent(),
+                children = parent.children(),
+                result, goto, gotoOptions;
+            e.preventDefault();
+
+            s.canvas.wlxmlDocument.transaction(function() {
+                result = s.element.wlxmlNode.insertNewNode();
+            }, {
+                metadata: {
+                    description: gettext('Inserting node'),
+                    fragment: s.toDocumentFragment()
+                }
+            });
+
+            s.canvas.setCurrentElement(utils.getElementForNode(result), {caretTo: 'start'});
+        }
     }
 ];
 
     }
 ];