wip: detach2 ok
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 27 Nov 2013 09:55:22 +0000 (10:55 +0100)
committerAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Mon, 2 Dec 2013 21:56:12 +0000 (22:56 +0100)
src/editor/modules/documentCanvas/canvas/canvas.js
src/editor/modules/documentCanvas/canvas/utils.js
src/editor/modules/documentCanvas/commands.js
src/editor/modules/documentToolbar/template.html
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index cf63c47..05dfbad 100644 (file)
@@ -33,6 +33,8 @@ $.extend(Canvas.prototype, {
 
     reloadRoot: function() {
         var canvasDOM = this.generateCanvasDOM(this.wlxmlDocument.root);
 
     reloadRoot: function() {
         var canvasDOM = this.generateCanvasDOM(this.wlxmlDocument.root);
+        //var canvasDOM = this.wlxmlDocument.root.getData('canvasElement') ? this.wlxmlDocument.root.getData('canvasElement').dom() : this.generateCanvasDOM(this.wlxmlDocument.root);
+
         this.wrapper.empty();
         this.wrapper.append(canvasDOM);
         this.d = this.wrapper.children(0);
         this.wrapper.empty();
         this.wrapper.append(canvasDOM);
         this.d = this.wrapper.children(0);
index caa9af4..e1f0a55 100644 (file)
@@ -45,7 +45,7 @@ var findCanvasElementInParent = function(wlxmlChildNode, wlxmlParentNode) {
     } else {
         parentElement = findCanvasElement(wlxmlParentNode);
         parentElement.children().forEach(function(child) {
     } else {
         parentElement = findCanvasElement(wlxmlParentNode);
         parentElement.children().forEach(function(child) {
-            if(child.data('wlxmlNode').sameNode(wlxmlChildNode)) {
+            if(child.data('wlxmlNode').sameNode(wlxmlChildNode)) { // czemu tu, przy drugim undo child nie mial data?
                 toret = child;
             }
         });
                 toret = child;
             }
         });
index ac56a31..57a5130 100644 (file)
@@ -19,6 +19,28 @@ var commands = {
     }
 };
 
     }
 };
 
+commands.register('undo', function(canvas) {
+    var doc = canvas.wlxmlDocument;
+
+    doc.undo();
+});
+
+commands.register('redo', function(canvas) {
+    var doc = canvas.wlxmlDocument;
+
+    doc.redo();
+});
+
+commands.register('remove-node', function(canvas) {
+    var cursor = canvas.getCursor(),
+        selectionStart = cursor.getSelectionStart(),
+        selectionEnd = cursor.getSelectionEnd(),
+        parent1 = selectionStart.element.parent() || undefined,
+        parent2 = selectionEnd.element.parent() || undefined;
+
+    canvas.wlxmlDocument.transform('detach2', {node:canvas.getCurrentNodeElement().data('wlxmlNode')});
+});
+
 commands.register('unwrap-node', function(canvas) {
     var cursor = canvas.getCursor(),
         selectionStart = cursor.getSelectionStart(),
 commands.register('unwrap-node', function(canvas) {
     var cursor = canvas.getCursor(),
         selectionStart = cursor.getSelectionStart(),
index 7ba2bc5..0572bb5 100644 (file)
         <button data-name="unwrap-node" data-btn-type="cmd" class="btn btn-mini"><i class="icon-arrow-up"></i></button>
         <button data-name="wrap-node" data-btn-type="cmd" class="btn btn-mini"><i class="icon-arrow-down"></i></button>
     </div>
         <button data-name="unwrap-node" data-btn-type="cmd" class="btn btn-mini"><i class="icon-arrow-up"></i></button>
         <button data-name="wrap-node" data-btn-type="cmd" class="btn btn-mini"><i class="icon-arrow-down"></i></button>
     </div>
+
+    <div class="rng-module-documentToolbar-toolbarGroup">
+        <button data-name="remove-node" data-btn-type="cmd" data-meta="bullets" class="btn btn-mini"><i class="icon-ban-circle"></i></button>
+        <button data-name="undo" data-btn-type="cmd" data-meta="bullets" class="btn btn-mini"><i class="icon-arrow-left"></i></button>
+        <button data-name="redo" data-btn-type="cmd" class="btn btn-mini"><i class="icon-share-alt"></i></button>
+    </div>
     <div class="rng-module-documentToolbar-toolbarGroup">
         <button data-name="footnote" data-btn-type="cmd" class="btn btn-mini"><i class="icon-comment"></i></button>
     </div>
     <div class="rng-module-documentToolbar-toolbarGroup">
         <button data-name="footnote" data-btn-type="cmd" class="btn btn-mini"><i class="icon-comment"></i></button>
     </div>
index fa5f551..4e8e575 100644 (file)
@@ -49,7 +49,15 @@ $.extend(DocumentNode.prototype, {
     },
 
     clone: function() {
     },
 
     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) {
     },
 
     getPath: function(ancestor) {
@@ -639,6 +647,7 @@ $.extend(Document.prototype, Backbone.Events, {
             transformation = new Transformation(args);
             transformation.run();
             this.undoStack.push(transformation);
             transformation = new Transformation(args);
             transformation.run();
             this.undoStack.push(transformation);
+            this.redoStack = [];
         } else {
             throw new Error('Transformation ' + transformationName + ' doesn\'t exist!');
         }
         } else {
             throw new Error('Transformation ' + transformationName + ' doesn\'t exist!');
         }
@@ -744,20 +753,20 @@ transformations['detach'] = DetachNodeTransformation;
 //2. detach via wskazanie changeroot
 
 var Detach2NodeTransformation = function(args) {
 //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() {
 };
 $.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() {
     },
     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;
@@ -770,14 +779,33 @@ var Detach3NodeTransformation = function(args) {
 };
 $.extend(Detach3NodeTransformation.prototype, {
     run: function() {
 };
 $.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() {
         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});
+            }
         }
     }
 });
         }
     }
 });
index 64e633f..8e67d74 100644 (file)
@@ -815,11 +815,12 @@ 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('does work', function() {
             var doc = getDocumentFromXML('<section><span>Alice</span></section>'),
                 span = doc.root.contents()[0];
 
-            doc.transform('detach3', {node: span});
+            doc.transform('detach2', {node: span});
 
 
             doc.undo();
 
 
             doc.undo();
@@ -837,6 +838,38 @@ describe('smartxml', function() {
             expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice');
 
         });
             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.transform('detach2', {node: span});
+
+
+            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];
+
+            doc.transform('detach2', {node: a});
+            doc.transform('detach2', {node: b});
+            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);
+
+        });
     });
 
 });
     });
 
 });