From: Aleksander Ɓukasz Date: Wed, 27 Nov 2013 09:55:22 +0000 (+0100) Subject: wip: detach2 ok X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/e1f340cca43eafb33256f439cac74689d783f292 wip: detach2 ok --- diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index cf63c47..05dfbad 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -33,6 +33,8 @@ $.extend(Canvas.prototype, { 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); diff --git a/src/editor/modules/documentCanvas/canvas/utils.js b/src/editor/modules/documentCanvas/canvas/utils.js index caa9af4..e1f0a55 100644 --- a/src/editor/modules/documentCanvas/canvas/utils.js +++ b/src/editor/modules/documentCanvas/canvas/utils.js @@ -45,7 +45,7 @@ var findCanvasElementInParent = function(wlxmlChildNode, wlxmlParentNode) { } 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; } }); diff --git a/src/editor/modules/documentCanvas/commands.js b/src/editor/modules/documentCanvas/commands.js index ac56a31..57a5130 100644 --- a/src/editor/modules/documentCanvas/commands.js +++ b/src/editor/modules/documentCanvas/commands.js @@ -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(), diff --git a/src/editor/modules/documentToolbar/template.html b/src/editor/modules/documentToolbar/template.html index 7ba2bc5..0572bb5 100644 --- a/src/editor/modules/documentToolbar/template.html +++ b/src/editor/modules/documentToolbar/template.html @@ -26,6 +26,12 @@ + +
+ + + +
diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index fa5f551..4e8e575 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -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) { @@ -639,6 +647,7 @@ $.extend(Document.prototype, Backbone.Events, { transformation = new Transformation(args); transformation.run(); this.undoStack.push(transformation); + this.redoStack = []; } 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) { - 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; @@ -770,14 +779,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}); + } } } }); diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 64e633f..8e67d74 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -815,11 +815,12 @@ describe('smartxml', function() { }); describe('Undo/redo', function() { + it('does work', function() { var doc = getDocumentFromXML('
Alice
'), span = doc.root.contents()[0]; - doc.transform('detach3', {node: span}); + doc.transform('detach2', {node: span}); doc.undo(); @@ -837,6 +838,38 @@ describe('smartxml', function() { expect(doc.root.contents()[0].contents()[0].getText()).to.equal('Alice'); }); + it('does work - merged text nodes case', function() { + var doc = getDocumentFromXML('
Alice has a cat.
'), + 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('
'), + 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); + + }); }); });