integration wip: restoring caret position and editor scroll after returning to editor tab
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 15 Jul 2013 12:40:17 +0000 (14:40 +0200)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 15 Jul 2013 12:40:17 +0000 (14:40 +0200)
modules/documentCanvas/canvas/canvas.js
modules/documentCanvas/documentCanvas.js

index 0d92d55..7a1d930 100644 (file)
@@ -197,18 +197,7 @@ $.extend(Canvas.prototype, {
                 this.publisher('currentElementChanged', element);
             }
         }.bind(this);
-        var _moveCaretToTextElement = function(element, where) {
-            var range = document.createRange();
-            range.selectNodeContents(element.dom().contents()[0]);
-            
-            var collapseArg = true;
-            if(where === 'end')
-                collapseArg = false;
-            range.collapse(collapseArg);
-            var selection = document.getSelection();
-            selection.removeAllRanges();
-            selection.addRange(range);
-        };
+
 
         var isTextElement = element instanceof documentElement.DocumentTextElement,
             textElementToLand = isTextElement ? element : findFirstDirectTextChild(element),
@@ -222,7 +211,7 @@ $.extend(Canvas.prototype, {
         if(textElementToLand && !(textElementToLand.sameNode(currentTextElement))) {
             _markAsCurrent(textElementToLand);
             if(params.caretTo)
-                _moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
+                this._moveCaretToTextElement(textElementToLand, params.caretTo); // as method on element?
             this.publisher('currentTextElementSet', element);
         }
 
@@ -232,8 +221,34 @@ $.extend(Canvas.prototype, {
                 document.getSelection().removeAllRanges();
             this.publisher('currentNodeElementSet', nodeElementToLand);
         }
-    }
+    },
+
+    _moveCaretToTextElement: function(element, where) {
+        var range = document.createRange(),
+            node = element.dom().contents()[0];
+
+        if(typeof where !== 'number') {
+            range.selectNodeContents(node);
+        } else {
+            range.setStart(node, where);
+        }
+        
+        var collapseArg = true;
+        if(where === 'end')
+            collapseArg = false;
+        range.collapse(collapseArg);
+        
+        var selection = document.getSelection();
 
+        selection.removeAllRanges();
+        selection.addRange(range);
+        this.wrapper.focus(); // FF requires this for caret to be put where range colllapses, Chrome doesn't.
+    },
+
+    setCursorPosition: function(position) {
+        if(position.element)
+            this._moveCaretToTextElement(position.element, position.offset);
+    }
 });
 
 $.extend(Canvas.prototype.list, {
index 9488aed..bde08d3 100644 (file)
@@ -15,6 +15,23 @@ return function(sandbox) {
     var canvas = canvas3.fromXML('', sandbox.publish); //canvasCanvas.create();
     var manager;
     var canvasWrapper = $(template);
+    var shownAlready = false;
+    var scrollbarPosition = 0,
+        cursorPosition;
+    
+    canvasWrapper.onShow = function() {
+        if(!shownAlready) {
+            shownAlready = true;
+        } else {
+            canvas.setCursorPosition(cursorPosition);
+            this.find('#rng-module-documentCanvas-contentWrapper').scrollTop(scrollbarPosition);
+        }
+    };
+    
+    canvasWrapper.onHide = function() {
+       scrollbarPosition = this.find('#rng-module-documentCanvas-contentWrapper').scrollTop();
+       cursorPosition = canvas.getCursor().getPosition();
+    };
 
     /* public api */
     return {