editor: cleanup
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.js
index 8ca71ac..29edbe5 100644 (file)
@@ -138,8 +138,10 @@ $.extend(Canvas.prototype, Backbone.Events, {
     },
 
     reloadRoot: function() {
+        if(this.rootElement) {
+            this.rootElement.detach();
+        }
         this.rootElement = this.createElement(this.wlxmlDocument.root);
-        this.rootWrapper.empty();
         this.rootWrapper.append(this.rootElement.dom);
     },
 
@@ -198,7 +200,7 @@ $.extend(Canvas.prototype, Backbone.Events, {
         /* globals MutationObserver */
         var observer = new MutationObserver(function(mutations) {
             mutations.forEach(function(mutation) {
-                if(documentElement.DocumentTextElement.isContentContainer(mutation.target)) {
+                if(canvas.dom[0].contains(mutation.target) && documentElement.DocumentTextElement.isContentContainer(mutation.target)) {
                     observer.disconnect();
                     if(mutation.target.data === '') {
                         mutation.target.data = utils.unicode.ZWS;
@@ -347,7 +349,7 @@ $.extend(Canvas.prototype, Backbone.Events, {
             if(byBrowser && byBrowser.parent().sameNode(nodeToLand)) {
                 return byBrowser;
             }
-            return e.getVerticallyFirstTextElement();
+            return _.isFunction(e.getVerticallyFirstTextElement) ? e.getVerticallyFirstTextElement({considerChildren: false}) : null;
         }.bind(this);
         var _markAsCurrent = function(element) {
             if(element instanceof documentElement.DocumentTextElement) {
@@ -416,14 +418,6 @@ $.extend(Canvas.prototype, Backbone.Events, {
         if(position.element) {
             this._moveCaretToTextElement(position.element, position.offset);
         }
-    },
-
-    toggleGrid: function() {
-        this.rootWrapper.toggleClass('grid-on');
-        this.trigger('changed');
-    },
-    isGridToggled: function() {
-        return this.rootWrapper.hasClass('grid-on');
     }
 });
 
@@ -463,6 +457,9 @@ $.extend(Selection.prototype, {
             return doc.createFragment(doc.CaretFragment, {node: anchorNode, offset: this.nativeSelection.anchorOffset});
         }
         if(this.type === 'textSelection') {
+            if(!anchorNode || !focusNode) {
+                return;
+            }
             if(anchorNode.isSiblingOf(focusNode)) {
                 return doc.createFragment(doc.TextRangeFragment, {
                     node1: anchorNode,