dzialacy remove+text
[fnpeditor.git] / src / smartxml / smartxml.js
index fa5f551..7b59b18 100644 (file)
@@ -49,7 +49,15 @@ $.extend(DocumentNode.prototype, {
     },
 
     clone: function() {
-        return this.document.createDocumentNode(this._$.clone(true, true)[0]);
+        var clone = this._$.clone(true, true);
+        // clone.find('*').addBack().each(function() {
+        //     var n = $(this);
+        //     if(n.data('canvasElement')) {
+        //         n.data('canvasElement', $.extend(true, {}, n.data('canvasElement')));
+        //         n.data('canvasElement').$element = n.data('canvasElement').$element.clone(true, true);
+        //     }
+        // });
+        return this.document.createDocumentNode(clone[0]);
     },
 
     getPath: function(ancestor) {
@@ -375,6 +383,7 @@ $.extend(TextNode.prototype, {
     },
 
     setText: function(text) {
+        console.log('smartxml: ' + text);
         this.nativeNode.data = text;
         this.triggerTextChangeEvent();
     },
@@ -633,12 +642,15 @@ $.extend(Document.prototype, Backbone.Events, {
     },
 
     transform: function(transformationName, args) {
+        console.log('transform');
         var Transformation = transformations[transformationName],
             transformation;
         if(Transformation) {
             transformation = new Transformation(args);
             transformation.run();
             this.undoStack.push(transformation);
+            console.log('clearing redo stack');
+            this.redoStack = [];
         } else {
             throw new Error('Transformation ' + transformationName + ' doesn\'t exist!');
         }
@@ -744,23 +756,76 @@ transformations['detach'] = DetachNodeTransformation;
 //2. detach via wskazanie changeroot
 
 var Detach2NodeTransformation = function(args) {
-    this.node = args.node;
-    this.document = this.node.document;
+    this.nodePath = args.node.getPath();
+    this.document = args.node.document;
 };
 $.extend(Detach2NodeTransformation.prototype, {
     run: function() {
-        this.root = this.node.parent() ? this.node.parent() : this.node.document.root;
-        this.oldRoot = (this.root).clone();
-        this.path = this.node.getPath();
-        this.node.detach();
+        var node = this.document.getNodeByPath(this.nodePath),
+            root = node.parent() ? node.parent() : this.document.root;
         
+        this.rootPath = root.getPath();
+        this.oldRoot = (root).clone();
+        node.detach();
     },
     undo: function() {
-        this.root.replaceWith(this.oldRoot); // this.getDocument?
-        this.node = this.document.getNodeByPath(this.path);
+        this.document.getNodeByPath(this.rootPath).replaceWith(this.oldRoot);
     }
 });
-transformations['detach2'] = Detach2NodeTransformation;
+//transformations['detach2'] = Detach2NodeTransformation;
+
+//2a. generyczna transformacja
+
+var createTransformation = function(desc) {
+
+    var NodeTransformation = function(args) {
+        this.nodePath = args.node.getPath();
+        this.document = args.node.document;
+        this.args = args;
+    };
+    $.extend(NodeTransformation.prototype, {
+        run: function() {
+            var node = this.document.getNodeByPath(this.nodePath),
+                root;
+
+            if(desc.getRoot) {
+                root = desc.getRoot(node);
+            } else {
+                root = this.document.root;
+            }
+            
+            this.rootPath = root.getPath();
+            this.oldRoot = (root).clone();
+            desc.impl.call(node, this.args);
+        },
+        undo: function() {
+            this.document.getNodeByPath(this.rootPath).replaceWith(this.oldRoot);
+        }
+    });
+
+    return NodeTransformation;
+}
+
+transformations['detach2'] = createTransformation({
+    // impl: function() {
+    //     //this.setAttr('class', 'cite'); //  
+    // },
+    impl: ElementNode.prototype.detach,
+    getRoot: function(node) {
+        return node.parent();
+    }
+
+});
+
+transformations['setText'] = createTransformation({
+    impl: function(args) {
+        this.setText(args.text)
+    },
+    getRoot: function(node) {
+        return node;
+    }
+
+});
 
 //3. detach z pełnym własnym redo
 
@@ -770,14 +835,33 @@ var Detach3NodeTransformation = function(args) {
 };
 $.extend(Detach3NodeTransformation.prototype, {
     run: function() {
-        this.index = this.node.getIndex();
-        this.parent = this.node.parent();
+        //this.index = this.node.getIndex();
+        //this.parent = this.node.parent();
+        
+        this.path = this.node.getPath();
+        if(this.node.isSurroundedByTextElements()) {
+            this.prevText = this.node.prev().getText();
+            this.nextText = this.node.next().getText();
+            this.merge = true;
+        } else {
+            this.prevText = this.nextText = null;
+            this.merge = false;
+        }
+
         this.node.detach();
     },
     undo: function() {
-        var contents = this.parent.contents();
-        if(contents.length === 0) {
-            this.parent.append(this.node)
+        var parent = this.document.getNodeByPath(this.path.slice(0,-1)),
+            idx = _.last(this.path);
+        var inserted = parent.insertAtIndex(this.node, idx);
+        if(this.merge) {
+            if(inserted.next()) {
+                inserted.before({text: this.prevText});
+                inserted.next().setText(this.nextText);
+            } else {
+                inserted.prev().setText(this.prevText);
+                inserted.after({text: this.nextText});
+            }
         }
     }
 });