From 0061ce72a4ef53ac99f75a4f3aa51d5c18a27b5e Mon Sep 17 00:00:00 2001
From: Jan Szejko <janek37@gmail.com>
Date: Fri, 23 Dec 2016 13:54:17 +0100
Subject: [PATCH] pressing enter makes new paragraph after image/video

(cherry picked from commit 8f44492)
---
 .../modules/documentCanvas/canvas/canvas.js   |  4 +++-
 .../modules/documentCanvas/canvas/keyboard.js | 22 +++++++++++++++++++
 src/editor/plugins/core/core.js               |  7 ++++++
 3 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js
index ebede40..fffc61d 100644
--- a/src/editor/modules/documentCanvas/canvas/canvas.js
+++ b/src/editor/modules/documentCanvas/canvas/canvas.js
@@ -174,7 +174,9 @@ $.extend(Canvas.prototype, Backbone.Events, {
 
     triggerKeyEvent: function(keyEvent, selection) {
         selection = selection || this.getSelection();
-        if(selection && (selection.type === 'caret' || selection.type === 'textSelection') && selection.toDocumentFragment().isValid()) {
+        if(selection && (
+            (selection.type === 'caret' || selection.type === 'textSelection') && selection.toDocumentFragment().isValid()
+            || selection.type == 'nodeSelection')) {
             keyboard.handleKeyEvent(keyEvent, selection);
         }
     },
diff --git a/src/editor/modules/documentCanvas/canvas/keyboard.js b/src/editor/modules/documentCanvas/canvas/keyboard.js
index 30754c5..8762d85 100644
--- a/src/editor/modules/documentCanvas/canvas/keyboard.js
+++ b/src/editor/modules/documentCanvas/canvas/keyboard.js
@@ -417,6 +417,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('Splitting node'),
+                    fragment: s.toDocumentFragment()
+                }
+            });
+
+            s.canvas.setCurrentElement(utils.getElementForNode(result), {caretTo: 'start'});
+        }
     }
 ];
 
diff --git a/src/editor/plugins/core/core.js b/src/editor/plugins/core/core.js
index 6dce4ef..8ac4f63 100644
--- a/src/editor/plugins/core/core.js
+++ b/src/editor/plugins/core/core.js
@@ -315,6 +315,13 @@ plugin.documentExtension.documentNode.transformations = {
             }
         });
         return toret;
+    },
+    insertNewNode: function () {
+        var node = this;
+        var newElement = this.document.createDocumentNode({tagName: 'div', attrs: {class: 'p'}});
+        node.after(newElement);
+        newElement.append({text: ''});
+        return newElement;
     }
 };
 
-- 
2.20.1