list fixes: headers, invalid elements, easier ending
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / keyboard.js
index d8052ed..bc9e276 100644 (file)
@@ -257,6 +257,14 @@ var keyEventHandlers = [
                 return;
             }
 
+
+            var parent = element.wlxmlNode.parent();
+            if(element.wlxmlNode.getIndex() === 0 && parent.isContextRoot() && (!parent.is('item') || parent.getIndex() === 0)) {
+                // Don't even try to do anything at the edge of a context root, except for non-first items
+                // - this is a temporary solution until key events handling get refactored into something more sane.
+                return;
+            }
+
             e.preventDefault();
 
             s.canvas.wlxmlDocument.transaction(function() {
@@ -363,6 +371,9 @@ var keyEventHandlers = [
                 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) {
@@ -405,6 +416,28 @@ var keyEventHandlers = [
 
             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'});
+        }
     }
 ];