undo wip: fixes + smoke test
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Fri, 6 Dec 2013 16:30:30 +0000 (17:30 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Sun, 15 Dec 2013 21:32:49 +0000 (22:32 +0100)
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index 47c527f..d722c20 100644 (file)
@@ -431,7 +431,7 @@ $.extend(Document.prototype, Backbone.Events, {
         if(transformation) {
             this._transformationLevel++;
             toret = transformation.run();
         if(transformation) {
             this._transformationLevel++;
             toret = transformation.run();
-            if(this._transformationLevel === 1) {
+            if(this._transformationLevel === 1 && !this._undoInProgress) {
                 this.undoStack.push(transformation);
             }
             this._transformationLevel--;
                 this.undoStack.push(transformation);
             }
             this._transformationLevel--;
@@ -445,14 +445,18 @@ $.extend(Document.prototype, Backbone.Events, {
     undo: function() {
         var transformation = this.undoStack.pop();
         if(transformation) {
     undo: function() {
         var transformation = this.undoStack.pop();
         if(transformation) {
+            this._undoInProgress = true;
             transformation.undo();
             transformation.undo();
+            this._undoInProgress = false;
             this.redoStack.push(transformation);
         }
     },
     redo: function() {
         var transformation = this.redoStack.pop();
         if(transformation) {
             this.redoStack.push(transformation);
         }
     },
     redo: function() {
         var transformation = this.redoStack.pop();
         if(transformation) {
+            this._transformationLevel++;
             transformation.run();
             transformation.run();
+            this._transformationLevel--;
             this.undoStack.push(transformation);
         }
     },
             this.undoStack.push(transformation);
         }
     },
index 08598aa..3ee2793 100644 (file)
@@ -994,63 +994,93 @@ describe('smartxml', function() {
         });
     });
 
         });
     });
 
-    // describe('Undo/redo', function() {
+    describe('Undo/redo', function() {
 
 
-    //     it('does work', function() {
-    //         var doc = getDocumentFromXML('<section><span>Alice</span></section>'),
-    //             span = doc.root.contents()[0];
+        it('smoke tests', function() {
+            var doc = getDocumentFromXML('<div>Alice</div>'),
+                textNode = doc.root.contents()[0],
+                result;
+
+            expect(doc.undoStack).to.have.length(0);
+            
+            textNode.wrapWith({tagName: 'span', start:1, end:2});
+            expect(doc.undoStack).to.have.length(1, '1');
+            expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
 
 
-    //         span.transform('smartxml.detach');
+            doc.undo();
+            expect(doc.undoStack).to.have.length(0, '2');
+            expect(doc.toXML()).to.equal('<div>Alice</div>');
 
 
+            debugger;
+            doc.redo();
+            expect(doc.undoStack).to.have.length(1, '3');
+            expect(doc.toXML()).to.equal('<div>A<span>l</span>ice</div>');
 
 
-    //         doc.undo();
+            doc.undo();
+            expect(doc.undoStack).to.have.length(0, '4');
+            expect(doc.toXML()).to.equal('<div>Alice</div>');
+
+            doc.undo();
+            expect(doc.undoStack).to.have.length(0, '5');
+            expect(doc.toXML()).to.equal('<div>Alice</div>');
+
+        });
 
 
-    //         expect(doc.root.contents()).to.have.length(1);
-    //         expect(doc.root.contents()[0].getTagName()).to.equal('span');
-    //         expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice');
+        // it('does work', function() {
+        //     var doc = getDocumentFromXML('<section><span>Alice</span></section>'),
+        //         span = doc.root.contents()[0];
 
 
-    //         doc.redo();
-    //         expect(doc.root.contents()).to.have.length(0);
+        //     span.transform('smartxml.detach');
 
 
-    //         doc.undo();
-    //         expect(doc.root.contents()).to.have.length(1);
-    //         expect(doc.root.contents()[0].getTagName()).to.equal('span');
-    //         expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice');
 
 
-    //     });
-    //     it('does work - merged text nodes case', function() {
-    //         var doc = getDocumentFromXML('<section>Alice <span>has</span> a cat.</section>'),
-    //             span = doc.root.contents()[1];
+        //     doc.undo();
 
 
-    //         span.transform('smartxml.detach');
+        //     expect(doc.root.contents()).to.have.length(1);
+        //     expect(doc.root.contents()[0].getTagName()).to.equal('span');
+        //     expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice');
 
 
+        //     doc.redo();
+        //     expect(doc.root.contents()).to.have.length(0);
 
 
-    //         doc.undo();
+        //     doc.undo();
+        //     expect(doc.root.contents()).to.have.length(1);
+        //     expect(doc.root.contents()[0].getTagName()).to.equal('span');
+        //     expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice');
 
 
-    //         expect(doc.root.contents().length).to.equal(3);
-    //         //console.log(doc.toXML());
-    //         expect(doc.root.contents()[1].contents()[0].getText()).to.equal('has');
+        // });
+        // it('does work - merged text nodes case', function() {
+        //     var doc = getDocumentFromXML('<section>Alice <span>has</span> a cat.</section>'),
+        //         span = doc.root.contents()[1];
 
 
-    //     });
-    //     it('dbg - don not store nodes in tranformation state!', function() {
-    //         var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
-    //             a = doc.root.contents()[0],
-    //             b = doc.root.contents()[1];
+        //     span.transform('smartxml.detach');
 
 
-    //         a.transform('smartxml.detach');
-    //         b.transform('smartxml.detach');
-    //         doc.undo();
-    //         doc.undo();
-    //         expect(doc.root.contents().length).to.equal(2);
-    //         expect(doc.root.contents()[0].getTagName()).to.equal('a');
-    //         expect(doc.root.contents()[1].getTagName()).to.equal('b');
 
 
-    //         doc.redo();
-    //         doc.redo();
-    //         expect(doc.root.contents().length).to.equal(0);
+        //     doc.undo();
 
 
-    //     });
-    // });
+        //     expect(doc.root.contents().length).to.equal(3);
+        //     //console.log(doc.toXML());
+        //     expect(doc.root.contents()[1].contents()[0].getText()).to.equal('has');
+
+        // });
+        // it('dbg - don not store nodes in tranformation state!', function() {
+        //     var doc = getDocumentFromXML('<section><a></a><b></b></section>'),
+        //         a = doc.root.contents()[0],
+        //         b = doc.root.contents()[1];
+
+        //     a.transform('smartxml.detach');
+        //     b.transform('smartxml.detach');
+        //     doc.undo();
+        //     doc.undo();
+        //     expect(doc.root.contents().length).to.equal(2);
+        //     expect(doc.root.contents()[0].getTagName()).to.equal('a');
+        //     expect(doc.root.contents()[1].getTagName()).to.equal('b');
+
+        //     doc.redo();
+        //     doc.redo();
+        //     expect(doc.root.contents().length).to.equal(0);
+
+        // });
+    });
 
 });
 
 
 });