editor: plugins/core - restore selection after editing/deleting link via a link tool box
[fnpeditor.git] / src / editor / modules / documentCanvas / canvas / canvas.js
index a467bee..a057e05 100644 (file)
@@ -58,7 +58,7 @@ $.extend(TextHandler.prototype, {
 
 
 var Canvas = function(wlxmlDocument, elements) {
 
 
 var Canvas = function(wlxmlDocument, elements) {
-    this.elementsRegister = new ElementsRegister(documentElement.DocumentNodeElement, genericElement);
+    this.elementsRegister = new ElementsRegister(documentElement.DocumentNodeElement);
 
     elements = [
         {tag: 'section', klass: null, prototype: genericElement},
 
     elements = [
         {tag: 'section', klass: null, prototype: genericElement},
@@ -290,12 +290,38 @@ $.extend(Canvas.prototype, Backbone.Events, {
 
     triggerSelectionChanged: function() {
         this.trigger('selectionChanged', this.getSelection());
 
     triggerSelectionChanged: function() {
         this.trigger('selectionChanged', this.getSelection());
+        var s = this.getSelection(),
+            f = s.toDocumentFragment();
+        if(f && f instanceof f.RangeFragment) {
+                var $current = this.wrapper.find('.current-node-element');
+                var current = $current && this.getDocumentElement($current.parent()[0]);
+                
+                if($current) {
+                    $current.removeClass('current-node-element');
+                }
+                if(current) {
+                    current.markAsCurrent(false);
+                }
+        }
     },
 
     getSelection: function() {
         return new Selection(this);
     },
 
     },
 
     getSelection: function() {
         return new Selection(this);
     },
 
+    select: function(fragment) {
+        if(fragment instanceof this.wlxmlDocument.RangeFragment) {
+            this.setCurrentElement(fragment.endNode, {caretTo: fragment.endOffset});
+        } else if(fragment instanceof this.wlxmlDocument.NodeFragment) {
+            var params = {
+                caretTo: fragment instanceof this.wlxmlDocument.CaretFragment ? fragment.offset : 'start'
+            };
+            this.setCurrentElement(fragment.node, params);
+        } else {
+            logger.debug('Fragment not supported');
+        }
+    },
+
     setCurrentElement: function(element, params) {
         if(!element) {
             logger.debug('Invalid element passed to setCurrentElement: ' + element);
     setCurrentElement: function(element, params) {
         if(!element) {
             logger.debug('Invalid element passed to setCurrentElement: ' + element);
@@ -324,8 +350,15 @@ $.extend(Canvas.prototype, Backbone.Events, {
                 this.wrapper.find('.current-text-element').removeClass('current-text-element');
                 element.dom.addClass('current-text-element');
             } else {
                 this.wrapper.find('.current-text-element').removeClass('current-text-element');
                 element.dom.addClass('current-text-element');
             } else {
-                this.wrapper.find('.current-node-element').removeClass('current-node-element');
+                var $current = this.wrapper.find('.current-node-element');
+                var current = this.getDocumentElement($current.parent()[0]);
+                $current.removeClass('current-node-element');
+
+                if(current) {
+                    current.markAsCurrent(false);
+                }
                 element._container().addClass('current-node-element');
                 element._container().addClass('current-node-element');
+                element.markAsCurrent(true);
             }
         }.bind(this);
 
             }
         }.bind(this);