smartxml: Automatically rollback transaction on error thrown
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.js
index 60782c2..6bc5c41 100644 (file)
@@ -2,15 +2,17 @@ define([
 'libs/jquery',
 'libs/underscore',
 'libs/backbone',
 'libs/jquery',
 'libs/underscore',
 'libs/backbone',
+'fnpjs/logging/logging',
 'modules/documentCanvas/canvas/documentElement',
 'modules/documentCanvas/canvas/keyboard',
 'modules/documentCanvas/canvas/utils',
 'modules/documentCanvas/canvas/wlxmlListener'
 'modules/documentCanvas/canvas/documentElement',
 'modules/documentCanvas/canvas/keyboard',
 'modules/documentCanvas/canvas/utils',
 'modules/documentCanvas/canvas/wlxmlListener'
-], function($, _, Backbone, documentElement, keyboard, utils, wlxmlListener) {
+], function($, _, Backbone, logging, documentElement, keyboard, utils, wlxmlListener) {
     
 'use strict';
 /* global document:false, window:false, Node:false */
 
     
 'use strict';
 /* global document:false, window:false, Node:false */
 
+var logger = logging.getLogger('canvas');
 
 var TextHandler = function(canvas) {this.canvas = canvas; this.buffer = null;};
 $.extend(TextHandler.prototype, {
 
 var TextHandler = function(canvas) {this.canvas = canvas; this.buffer = null;};
 $.extend(TextHandler.prototype, {
@@ -89,9 +91,21 @@ $.extend(Canvas.prototype, {
             keyboard.handleKey(e, this);
         }.bind(this));
 
             keyboard.handleKey(e, this);
         }.bind(this));
 
+        var mouseDown;
+        this.wrapper.on('mousedown', '[document-node-element], [document-text-element]', function(e) {
+            mouseDown = e.target;
+        });
+
         this.wrapper.on('click', '[document-node-element], [document-text-element]', function(e) {
             e.stopPropagation();
         this.wrapper.on('click', '[document-node-element], [document-text-element]', function(e) {
             e.stopPropagation();
-            canvas.setCurrentElement(canvas.getDocumentElement(e.currentTarget), {caretTo: false});
+            if(e.originalEvent.detail === 3) {
+                e.preventDefault();
+                canvas._moveCaretToTextElement(canvas.getDocumentElement(e.currentTarget), 'whole');
+            } else {
+                if(mouseDown === e.target) {
+                    canvas.setCurrentElement(canvas.getDocumentElement(e.currentTarget), {caretTo: false});
+                }
+            }
         });
 
         this.wrapper.on('paste', function(e) {
         });
 
         this.wrapper.on('paste', function(e) {
@@ -102,7 +116,7 @@ $.extend(Canvas.prototype, {
                 return; // TODO: alert
             }
 
                 return; // TODO: alert
             }
 
-            var text = clipboardData.getData('text/plain'),
+            var text = clipboardData.getData('text/plain').replace(/\r?\n|\r/g, ' '),
                 cursor = canvas.getCursor(),
                 element = cursor.getPosition().element,
                 lhs, rhs;
                 cursor = canvas.getCursor(),
                 element = cursor.getPosition().element,
                 lhs, rhs;
@@ -131,7 +145,6 @@ $.extend(Canvas.prototype, {
                         canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end');
                     }
                     observer.observe(canvas.wrapper[0], config);
                         canvas._moveCaretToTextElement(canvas.getDocumentElement(mutation.target), 'end');
                     }
                     observer.observe(canvas.wrapper[0], config);
-                    canvas.publisher('contentChanged');
 
                     var textElement = canvas.getDocumentElement(mutation.target),
                         toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
 
                     var textElement = canvas.getDocumentElement(mutation.target),
                         toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
@@ -217,13 +230,20 @@ $.extend(Canvas.prototype, {
         return this.getDocumentElement(this.wrapper.find('.current-text-element')[0]);
     },
 
         return this.getDocumentElement(this.wrapper.find('.current-text-element')[0]);
     },
 
-
+    contains: function(element) {
+        return element.dom().parents().index(this.wrapper) !== -1;
+    },
 
     setCurrentElement: function(element, params) {
         if(!(element instanceof documentElement.DocumentElement)) {
             element = utils.findCanvasElement(element);
         }
 
 
     setCurrentElement: function(element, params) {
         if(!(element instanceof documentElement.DocumentElement)) {
             element = utils.findCanvasElement(element);
         }
 
+        if(!element || !this.contains(element)) {
+            logger.warning('Cannot set current element: element doesn\'t exist on canvas');
+            return;
+        }
+
         params = _.extend({caretTo: 'end'}, params);
         var findFirstDirectTextChild = function(e, nodeToLand) {
             var byBrowser = this.getCursor().getPosition().element;
         params = _.extend({caretTo: 'end'}, params);
         var findFirstDirectTextChild = function(e, nodeToLand) {
             var byBrowser = this.getCursor().getPosition().element;
@@ -245,7 +265,6 @@ $.extend(Canvas.prototype, {
             } else {
                 this.wrapper.find('.current-node-element').removeClass('current-node-element');
                 element._container().addClass('current-node-element');
             } else {
                 this.wrapper.find('.current-node-element').removeClass('current-node-element');
                 element._container().addClass('current-node-element');
-                this.publisher('currentElementChanged', element);
             }
         }.bind(this);
 
             }
         }.bind(this);
 
@@ -289,12 +308,13 @@ $.extend(Canvas.prototype, {
             range.setStart(node, where);
         }
         
             range.setStart(node, where);
         }
         
-        var collapseArg = true;
-        if(where === 'end') {
-            collapseArg = false;
+        if(where !== 'whole') {
+            var collapseArg = true;
+            if(where === 'end') {
+                collapseArg = false;
+            }
+            range.collapse(collapseArg);
         }
         }
-        range.collapse(collapseArg);
-        
         var selection = document.getSelection();
 
         selection.removeAllRanges();
         var selection = document.getSelection();
 
         selection.removeAllRanges();
@@ -357,16 +377,16 @@ $.extend(Cursor.prototype, {
             return {
                 element: anchorElement,
                 offset: selection.anchorOffset,
             return {
                 element: anchorElement,
                 offset: selection.anchorOffset,
-                offsetAtBeginning: selection.anchorOffset === 0,
-                offsetAtEnd: selection.anchorNode.data.length === selection.anchorOffset
+                offsetAtBeginning: selection.anchorOffset === 0 || anchorElement.getText() === '',
+                offsetAtEnd: selection.anchorNode.data.length === selection.anchorOffset || anchorElement.getText() === ''
             };
         }
         if(which === 'focus') {
             return {
                 element: focusElement,
                 offset: selection.focusOffset,
             };
         }
         if(which === 'focus') {
             return {
                 element: focusElement,
                 offset: selection.focusOffset,
-                offsetAtBeginning: selection.focusOffset === 0,
-                offsetAtEnd: selection.focusNode.data.length === selection.focusOffset
+                offsetAtBeginning: selection.focusOffset === 0 || focusElement.getText() === '',
+                offsetAtEnd: selection.focusNode.data.length === selection.focusOffset || focusElement.getText() === '',
             };
         }
         
             };
         }
         
@@ -414,8 +434,8 @@ $.extend(Cursor.prototype, {
         return {
             element: placeData.element,
             offset: placeData.offset,
         return {
             element: placeData.element,
             offset: placeData.offset,
-            offsetAtBeginning: placeData.offset === 0,
-            offsetAtEnd: nodeLen === placeData.offset
+            offsetAtBeginning: placeData.offset === 0 || focusElement.getText() === '',
+            offsetAtEnd: nodeLen === placeData.offset || focusElement.getText() === ''
         };
     }
 });
         };
     }
 });