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);
} 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;
}
});
}
};
+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(),
<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>
},
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) {
transformation = new Transformation(args);
transformation.run();
this.undoStack.push(transformation);
+ this.redoStack = [];
} else {
throw new Error('Transformation ' + transformationName + ' doesn\'t exist!');
}
//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;
};
$.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});
+ }
}
}
});
});
describe('Undo/redo', function() {
+
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();
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);
+
+ });
});
});