From 439024603d74ab79c0ba86d718b810b00f389210 Mon Sep 17 00:00:00 2001
From: =?utf8?q?Aleksander=20=C5=81ukasz?=
 <aleksander.lukasz@nowoczesnapolska.org.pl>
Date: Mon, 9 Dec 2013 17:38:40 +0100
Subject: [PATCH] editor: canvas keys handling - wrapping document
 transformations in transactions

This gets as one step undo
---
 .../modules/documentCanvas/canvas/keyboard.js      | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/src/editor/modules/documentCanvas/canvas/keyboard.js b/src/editor/modules/documentCanvas/canvas/keyboard.js
index 0b58443..d2a8e44 100644
--- a/src/editor/modules/documentCanvas/canvas/keyboard.js
+++ b/src/editor/modules/documentCanvas/canvas/keyboard.js
@@ -50,11 +50,13 @@ handlers.push({key: KEYS.ENTER,
         if(Object.keys(cursor.getPosition()).length === 0) {
             var currentElement = canvas.getCurrentNodeElement();
             if(currentElement) {
+                canvas.wlxmlDocument.startTransaction();
                 added = currentElement.data('wlxmlNode').after({
                     tag: currentElement.getWlxmlTag() || 'div',
                     attrs: {'class': currentElement.getWlxmlClass() || 'p'}
                 });
                 added.append({text:''});
+                canvas.wlxmlDocument.endTransaction();
                 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
             }
             return;
@@ -65,10 +67,13 @@ handlers.push({key: KEYS.ENTER,
                 if(element instanceof documentElement.DocumentTextElement) {
                     element = element.parent();
                 }
+
+                canvas.wlxmlDocument.startTransaction();
                 added = element.data('wlxmlNode').after(
                     {tagName: element.getWlxmlTag() || 'div', attrs: {'class': element.getWlxmlClass() || 'p'}}
                 );
                 added.append({text: ''});
+                canvas.wlxmlDocument.endTransaction();
                 canvas.setCurrentElement(utils.findCanvasElement(added), {caretTo: 'start'});
 
             } else {
@@ -169,7 +174,8 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
     keydown: function(event, canvas) {
         var cursor = canvas.getCursor(),
             position = canvas.getCursor().getPosition(),
-            element = position.element;
+            element = position.element,
+            node = element.data('wlxmlNode');
 
         if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
             event.preventDefault();
@@ -185,9 +191,11 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
             return element.getText().length === 1 || selectsWholeTextElement(cursor);
         };
 
+        canvas.wlxmlDocument.startTransaction();
+        
         if(willDeleteWholeText()) {
             event.preventDefault();
-            element.setText('');
+            node.setText('');
         }
         else if(element.isEmpty()) {
 
@@ -222,6 +230,8 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
             // todo
             event.preventDefault();
         }
+        canvas.wlxmlDocument.endTransaction();
+
     }
 });
 
-- 
2.20.1