editor: canvas - do not break an empty paragraph on ENTER
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 6 Aug 2014 08:58:21 +0000 (10:58 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Thu, 14 Aug 2014 14:26:13 +0000 (16:26 +0200)
src/editor/modules/documentCanvas/canvas/documentElement.js
src/editor/modules/documentCanvas/canvas/keyboard.js
src/editor/plugins/core/core.test.js

index c0dd361..a1965cd 100644 (file)
@@ -241,6 +241,9 @@ $.extend(DocumentNodeElement.prototype, {
         // })
         this.dom.css('display', what);
         this._container().css('display', what);
         // })
         this.dom.css('display', what);
         this._container().css('display', what);
+    },
+    children: function() {
+        return [];
     }
 });
 
     }
 });
 
index 7ae6b27..d8052ed 100644 (file)
@@ -376,9 +376,16 @@ var keyEventHandlers = [
             return s.type === 'caret' && e.key === KEYS.ENTER && !s.element.parent().isRootElement();
         },
         run: function(e, s) {
             return s.type === 'caret' && e.key === KEYS.ENTER && !s.element.parent().isRootElement();
         },
         run: function(e, s) {
-            var result, goto, gotoOptions;
+            var parent = s.element.parent(),
+                children = parent.children(),
+                result, goto, gotoOptions;
             void(e);
             e.preventDefault();
             void(e);
             e.preventDefault();
+
+            if(children.length === 1 && s.element.isEmpty()) {
+                return;
+            }
+
             s.canvas.wlxmlDocument.transaction(function() {
                 result = s.element.wlxmlNode.breakContent({offset: s.offset});
             }, {
             s.canvas.wlxmlDocument.transaction(function() {
                 result = s.element.wlxmlNode.breakContent({offset: s.offset});
             }, {
index f227b67..a33ae61 100644 (file)
@@ -621,6 +621,17 @@ describe('Keyboard interactions', function() {
             expect(selection.offset).to.equal(0);
         });
 
             expect(selection.offset).to.equal(0);
         });
 
+        it('does nothing on an empty paragraph', function() {
+            var c = getCanvasFromXML('<section><div class="p">a</div></section>'),
+                k = new Keyboard(c),
+                spy = sinon.spy();
+
+            k.withCaret('a|').press(K.BACKSPACE);
+            c.wlxmlDocument.on('change', spy);
+            k.press(K.ENTER);
+            expect(spy.callCount).to.equal(0);
+        });
+
         it('splits its parent box if inside a span', function() {
             var c = getCanvasFromXML('<section><div class="p">this <span>is</span> a paragraph</div></section>'),
                 k = new Keyboard(c);
         it('splits its parent box if inside a span', function() {
             var c = getCanvasFromXML('<section><div class="p">this <span>is</span> a paragraph</div></section>'),
                 k = new Keyboard(c);