refactoring: fromHTMLElement @ canvas, no need to pass canvas instance
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / keyboard.js
index cd12a02..85fbd43 100644 (file)
@@ -131,7 +131,9 @@ handlers.push({keys: [KEYS.ARROW_UP, KEYS.ARROW_DOWN, KEYS.ARROW_LEFT, KEYS.ARRO
                 caretTo = 'start';
             }
             var el = canvas.getDocumentElement(utils.nearestInDocumentOrder('[document-text-element]', direction, element.dom()[0]));
-            canvas.setCurrentElement(el, {caretTo: caretTo});
+            if(el) {
+                canvas.setCurrentElement(el, {caretTo: caretTo});
+            }
         }
     },
     keyup: function(event, canvas) {
@@ -181,10 +183,53 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
         var cursor = canvas.getCursor(),
             position = canvas.getCursor().getPosition(),
             element = position.element,
-            node = element.data('wlxmlNode');
+            node = element.data('wlxmlNode'),
+            direction = 'above',
+            caretTo = 'end',
+            goto;
+
+            
+        if(event.which === KEYS.DELETE) {
+            direction = 'below';
+            caretTo = 'start';
+        }
 
         if(cursor.isSelecting() && !cursor.isSelectingWithinElement()) {
             event.preventDefault();
+            var start = cursor.getSelectionStart(),
+                end = cursor.getSelectionEnd();
+
+            if(direction === 'above') {
+                if(start.offsetAtBeginning) {
+                    goto = start.element.getNearestTextElement('above');
+                    caretTo = 'end';
+                } else {
+                    goto = start.element;
+                    caretTo = start.offset;
+                }
+            } else {
+                if(end.offsetAtEnd) {
+                    goto = start.element.getNearestTextElement('below');
+                    caretTo = 'start';
+                } else {
+                    goto = end.element;
+                    caretTo = 0;
+                }
+            }
+
+            canvas.wlxmlDocument.deleteText({
+                from: {
+                    node: start.element.data('wlxmlNode'),
+                    offset: start.offset
+                },
+                to: {
+                    node: end.element.data('wlxmlNode'),
+                    offset: end.offset
+                }
+            });
+            if(goto) {
+                canvas.setCurrentElement(goto, {caretTo: caretTo});
+            }
             return;
         }
             
@@ -197,47 +242,42 @@ handlers.push({keys: [KEYS.BACKSPACE, KEYS.DELETE],
             return element.getText().length === 1 || selectsWholeTextElement(cursor);
         };
 
-        canvas.wlxmlDocument.startTransaction();
-        
-        if(willDeleteWholeText()) {
-            event.preventDefault();
-            node.setText('');
-        }
-        else if(element.isEmpty()) {
-
-            var direction = 'above',
-                caretTo = 'end';
-                
-            if(event.which === KEYS.DELETE) {
-                direction = 'below';
-                caretTo = 'start';
+        canvas.wlxmlDocument.transaction(function() {
+            if(willDeleteWholeText()) {
+                event.preventDefault();
+                node.setText('');
             }
-
-            event.preventDefault();
-
-            var parent = element.parent(),
-                grandParent = parent ? parent.parent() : null,
-                goto;
-            if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
-                if(grandParent && grandParent.children().length === 1) {
-                    goto = grandParent.data('wlxmlNode').append({text: ''});
+            else if(element.isEmpty()) {
+                event.preventDefault();
+                var parent = element.parent(),
+                    grandParent = parent ? parent.parent() : null;
+                if(!grandParent && parent.children().length === 1) {
+                    return;
+                }
+                if(parent.children().length === 1 && parent.children()[0].sameNode(element)) {
+                    if(grandParent && grandParent.children().length === 1) {
+                        goto = grandParent.data('wlxmlNode').append({text: ''});
+                    } else {
+                        goto = element.getNearestTextElement(direction);
+                    }
+                    parent.data('wlxmlNode').detach();
                 } else {
                     goto = element.getNearestTextElement(direction);
+                    element.data('wlxmlNode').detach();
                 }
-                parent.data('wlxmlNode').detach();
-            } else {
-                goto = element.getNearestTextElement(direction);
-                element.data('wlxmlNode').detach();
+                canvas.setCurrentElement(goto, {caretTo: caretTo});
             }
-            canvas.setCurrentElement(goto, {caretTo: caretTo});
-            canvas.publisher('contentChanged');
-        }
-        else if(cursorAtOperationEdge) {
-            // todo
-            event.preventDefault();
-        }
-        canvas.wlxmlDocument.endTransaction();
-
+            else if(cursorAtOperationEdge) {
+                if(direction === 'below') {
+                    element = element.getNearestTextElement(direction);
+                }
+                if(element) {
+                    goto = element.data('wlxmlNode').mergeContentUp();
+                    canvas.setCurrentElement(goto.node, {caretTo: goto.offset});
+                }
+                event.preventDefault();
+            }
+        });
     }
 });