editor: fix minor bug
[fnpeditor.git] / src / smartxml / core.js
index b5f8c44..4d3c2c1 100644 (file)
@@ -1,8 +1,10 @@
-define([
-
-], function() {
+define(function(require) {
     
 'use strict';
+/* globals Node */
+
+var _ = require('libs/underscore'),
+    TEXT_NODE = Node.TEXT_NODE;
 
 
 var INSERTION = function(implementation) {
@@ -81,7 +83,7 @@ var elementNodeTransformations = {
             this.prev().appendText(next.getText());
             next.detach();
         }
-        return DocumentNode.prototype.detach.call(this);
+        return this.__super__.detach();
     },
 
     setTag: function(tagName) {
@@ -95,8 +97,10 @@ var elementNodeTransformations = {
         node.setData(this.getData());
 
         if(this.sameNode(this.document.root)) {
-            defineDocumentProperties(this.document, node._$);
+            this.document._defineDocumentProperties(node._$);
         }
+
+        /* TODO: This invalidates old references to this node. Caching instances on nodes would fix this. */
         this._$.replaceWith(node._$);
         this._setNativeNode(node._$[0]);
         this._$.append(myContents);
@@ -184,10 +188,15 @@ var elementNodeTransformations = {
 };
 
 var textNodeTransformations = {
-    setText: function(text) {
-        //console.log('smartxml: ' + text);
-        this.nativeNode.data = text;
-        this.triggerTextChangeEvent();
+    setText: {
+        impl: function(t, text) {
+            t.oldText = this.getText();
+            this.nativeNode.data = text;
+            this.triggerTextChangeEvent();
+        },
+        undo: function(t) {
+            this.setText(t.oldText);
+        }
     },
 
     appendText: function(text) {
@@ -210,7 +219,7 @@ var textNodeTransformations = {
                 _with: {tagName: desc.tagName, attrs: desc.attrs}
             });
         } else {
-            return DocumentNode.prototype.wrapWith.call(this, desc);
+            return this.__super__.wrapWith.call(this, desc);
         }
     },
 
@@ -250,6 +259,27 @@ var textNodeTransformations = {
         });
 
         return {first: parentElement, second: newElement};
+    },
+
+    divideWithElementNode: function(node, params) {
+        var insertion = this.getNodeInsertion(node),
+            myText = this.getText();
+
+        if(params.offset === myText.length) {
+            return this.after(node);
+        }
+        if(params.offset === 0) {
+            return this.before(node);
+        }
+
+        var lhsText = myText.substr(0, params.offset),
+            rhsText = myText.substr(params.offset),
+            rhsTextNode = this.document.createDocumentNode({text: rhsText});
+
+        this.setText(lhsText);
+        this.after(insertion.ofNode);
+        insertion.ofNode.after(rhsTextNode);
+        return insertion.ofNode;
     }
 };
 
@@ -342,11 +372,11 @@ var documentTransformations = {
     replaceRoot: function(node) {
         var insertion = this.getNodeInsertion(node);
         this.root.detach();
-        defineDocumentProperties(this, insertion.ofNode._$);
+        this._defineDocumentProperties(insertion.ofNode._$);
         insertion.ofNode.triggerChangeEvent('nodeAdded');
         return insertion.ofNode;
     }
-}
+};
 
 return {
     document: {