If browser returns selection outside of text element return cursor position as undefined
[fnpeditor.git] / modules / documentCanvas / documentCanvas.js
index 6030703..1c3242a 100644 (file)
-// Module that implements main WYSIWIG edit area\r
-\r
-define([\r
-'libs/underscore-min',\r
-'./transformations', \r
-'./wlxmlNode',\r
-'./canvas',\r
-'./canvasManager',\r
-'libs/text!./template.html'], function(_, transformations, wlxmlNode, Canvas, CanvasManager, template) {\r
-\r
-'use strict';\r
-\r
-return function(sandbox) {\r
-\r
-    var canvas = new Canvas.Canvas();\r
-    \r
-    var manager = new CanvasManager(canvas, sandbox);\r
-    \r
-\r
-    var view = {\r
-\r
-        insertNewNode: function(wlxmlTag, wlxmlClass) {\r
-            //TODO: Insert inline\r
-            var anchor = $(window.getSelection().anchorNode);\r
-            var anchorOffset = window.getSelection().anchorOffset;\r
-            var textLen;\r
-            if(anchor[0].nodeType === Node.TEXT_NODE) {\r
-                textLen = anchor.text().length;\r
-                anchor = anchor.parent();\r
-            }\r
-            if(anchor.text() === '') {\r
-                var todel = anchor;\r
-                anchor = anchor.parent();\r
-                todel.remove();\r
-            }\r
-            if(anchorOffset > 0 && anchorOffset < textLen) {\r
-                if(wlxmlTag === null && wlxmlClass === null) {\r
-                    return this.splitWithNewNode(anchor);\r
-                }\r
-                return this.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
-            }\r
-            var newNode = this._createNode(wlxmlTag || anchor.attr('wlxml-tag'), wlxmlClass || anchor.attr('wlxml-class'));\r
-            if(anchorOffset === 0)\r
-                anchor.before(newNode)\r
-            else\r
-                anchor.after(newNode);\r
-            this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
-            //isDirty = true;\r
-            sandbox.publish('contentChanged');\r
-        },\r
-        wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
-            var selection = window.getSelection();\r
-            if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {\r
-                var startOffset = selection.anchorOffset;\r
-                var endOffset = selection.focusOffset;\r
-                if(startOffset > endOffset) {\r
-                    var tmp = startOffset;\r
-                    startOffset = endOffset;\r
-                    endOffset = tmp;\r
-                }\r
-                var node = selection.anchorNode;\r
-                var prefix = node.data.substr(0, startOffset);\r
-                var suffix = node.data.substr(endOffset);\r
-                var core = node.data.substr(startOffset, endOffset - startOffset);\r
-                var newNode = this._createNode(wlxmlTag, wlxmlClass);\r
-                newNode.text(core || 'test');\r
-                $(node).replaceWith(newNode);\r
-                newNode.before(prefix);\r
-                newNode.after(suffix);\r
-                \r
-                this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
-                //isDirty = true;\r
-                sandbox.publish('contentChanged');\r
-            }\r
-        },\r
-        splitWithNewNode: function(node) {\r
-            var selection = window.getSelection();\r
-            if(selection.anchorNode === selection.focusNode && selection.anchorNode.nodeType === Node.TEXT_NODE) {\r
-                var startOffset = selection.anchorOffset;\r
-                var endOffset = selection.focusOffset;\r
-                if(startOffset > endOffset) {\r
-                    var tmp = startOffset;\r
-                    startOffset = endOffset;\r
-                    endOffset = tmp;\r
-                }\r
-                var anchor = selection.anchorNode;\r
-                var prefix = anchor.data.substr(0, startOffset);\r
-                var suffix = anchor.data.substr(endOffset);\r
-                var prefixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
-                var newNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
-                var suffixNode = this._createNode(node.attr('wlxml-tag'), node.attr('wlxml-class'));\r
-                prefixNode.text(prefix);\r
-                suffixNode.text(suffix);\r
-                node.replaceWith(newNode);\r
-                newNode.before(prefixNode);\r
-                newNode.after(suffixNode);\r
-                \r
-                this.selectNode(new wlxmlNode.Node(newNode), {movecaret: true});\r
-                //isDirty = true;\r
-                sandbox.publish('contentChanged');\r
-            }\r
-        }\r
-\r
-    };\r
-    \r
-\r
-    /* public api */\r
-    return {\r
-        start: function() { sandbox.publish('ready'); },\r
-        getView: function() { \r
-            return canvas.dom;\r
-        },\r
-        setDocument: function(xml) {\r
-            canvas.setXML(xml);\r
-            sandbox.publish('documentSet');\r
-        },\r
-        getDocument: function() {\r
-            return canvas.toXML();\r
-        },\r
-        modifyCurrentNode: function(attr, value) {\r
-            if(manager.currentNode) {\r
-                manager.getNodeElement(manager.currentNode).attr('wlxml-'+attr, value);\r
-                sandbox.publish('contentChanged');\r
-            }\r
-        },\r
-        highlightNode: function(wlxmlNode) {\r
-            manager.highlightNode(wlxmlNode);\r
-        },\r
-        dimNode: function(wlxmlNode) {\r
-            manager.dimNode(wlxmlNode);\r
-        },\r
-        selectNode: function(wlxmlNode) {\r
-            if(!wlxmlNode.is(manager.currentNode))\r
-                manager.selectNode(wlxmlNode, {movecaret: true});\r
-        },\r
-        toggleGrid: function(toggle) {\r
-            manager.toggleGrid(toggle);\r
-        },\r
-        insertNewNode: function(wlxmlTag, wlxmlClass) {\r
-            manager.insertNewNode(wlxmlTag, wlxmlClass);\r
-        },\r
-        wrapSelectionWithNewNode: function(wlxmlTag, wlxmlClass) {\r
-            manager.wrapSelectionWithNewNode(wlxmlTag, wlxmlClass);\r
-        }\r
-    }\r
-    \r
-};\r
-\r
+// Module that implements main WYSIWIG edit area
+
+define([
+'libs/underscore-min',
+'./transformations', 
+'./canvas',
+'./canvasManager',
+'./canvas/canvas',
+'libs/text!./template.html'], function(_, transformations, Canvas, CanvasManager, canvas3, template) {
+
+'use strict';
+
+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;
+            canvas.setCurrentElement(canvas.doc().getVerticallyFirstTextElement());
+        } 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 {
+        start: function() { sandbox.publish('ready'); },
+        getView: function() { 
+            return canvasWrapper;
+        },
+        setDocument: function(xml) {
+            canvas.loadWlxml(xml); //canvas.setHTML(transformations.fromXML.getHTMLTree(xml));
+            canvasWrapper.find('#rng-module-documentCanvas-content').empty().append(canvas.view());
+            manager = new CanvasManager(canvas, sandbox);
+            sandbox.publish('documentSet');
+        },
+        getDocument: function() {
+            return transformations.toXML.getXML(canvas.getContent());
+        },
+        modifyCurrentNodeElement: function(attr, value) {
+            if(attr === 'class' || attr === 'tag') {
+                canvas.getCurrentNodeElement()['setWlxml'+(attr[0].toUpperCase() + attr.substring(1))](value);    
+            }
+        },
+        highlightElement: function(element) {
+            canvas.highlightElement(element);
+        },
+        dimElement: function(element) {
+            canvas.dimElement(element);
+        },
+        jumpToElement: function(element) {
+            canvas.setCurrentElement(element);
+        },
+        command: function(command, params) {
+            manager.command(command, params);
+        }
+    };
+    
+};
+
 });
\ No newline at end of file
 });
\ No newline at end of file