From 612d7ff15c5ba8f014755af33b766f9db76f7df5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Wed, 6 Aug 2014 10:58:21 +0200 Subject: [PATCH] editor: canvas - do not break an empty paragraph on ENTER --- .../modules/documentCanvas/canvas/documentElement.js | 3 +++ src/editor/modules/documentCanvas/canvas/keyboard.js | 9 ++++++++- src/editor/plugins/core/core.test.js | 11 +++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index c0dd361..a1965cd 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -241,6 +241,9 @@ $.extend(DocumentNodeElement.prototype, { // }) this.dom.css('display', what); this._container().css('display', what); + }, + children: function() { + return []; } }); diff --git a/src/editor/modules/documentCanvas/canvas/keyboard.js b/src/editor/modules/documentCanvas/canvas/keyboard.js index 7ae6b27..d8052ed 100644 --- a/src/editor/modules/documentCanvas/canvas/keyboard.js +++ b/src/editor/modules/documentCanvas/canvas/keyboard.js @@ -376,9 +376,16 @@ var keyEventHandlers = [ 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(); + + if(children.length === 1 && s.element.isEmpty()) { + return; + } + s.canvas.wlxmlDocument.transaction(function() { result = s.element.wlxmlNode.breakContent({offset: s.offset}); }, { diff --git a/src/editor/plugins/core/core.test.js b/src/editor/plugins/core/core.test.js index f227b67..a33ae61 100644 --- a/src/editor/plugins/core/core.test.js +++ b/src/editor/plugins/core/core.test.js @@ -621,6 +621,17 @@ describe('Keyboard interactions', function() { expect(selection.offset).to.equal(0); }); + it('does nothing on an empty paragraph', function() { + var c = getCanvasFromXML('
a
'), + 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('
this is a paragraph
'), k = new Keyboard(c); -- 2.20.1