'use strict';
+var TextHandler = function(canvas) {this.canvas = canvas; this.buffer = null};
+$.extend(TextHandler.prototype, {
+ handle: function(node, text) {
+ console.log('canvas text handler: ' + text);
+ this.setText(text, node);
+ return;
+ if(!this.node) {
+ this.node = node;
+ }
+ if(this.node.sameNode(node)) {
+ this._ping(text);
+ } else {
+ this.flush();
+ this.node = node;
+ this._ping(text);
+ }
+ },
+ _ping: _.throttle(function(text) {
+ this.buffer = text;
+ this.flush();
+ }, 1000),
+ flush: function() {
+ if(this.buffer != null) {
+ this.setText(this.buffer, this.node);
+ this.buffer = null;
+ }
+ },
+ setText: function(text, node) {
+ this.canvas.wlxmlDocument.transform('setText', {node:node, text: text});
+
+ }
+
+});
+
+
var Canvas = function(wlxmlDocument, publisher) {
this.eventBus = _.extend({}, Backbone.Events);
this.wrapper = $('<div>').addClass('canvas-wrapper').attr('contenteditable', true);
this.wlxmlListener = wlxmlListener.create(this);
this.loadWlxmlDocument(wlxmlDocument);
this.publisher = publisher ? publisher : function() {};
+ this.textHandler = new TextHandler(this);
};
$.extend(Canvas.prototype, {
canvas.setCurrentElement(canvas.getDocumentElement(e.currentTarget), {caretTo: false});
});
+
+
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if(documentElement.DocumentTextElement.isContentContainer(mutation.target)) {
var textElement = canvas.getDocumentElement(mutation.target),
toSet = mutation.target.data !== utils.unicode.ZWS ? mutation.target.data : '';
- textElement.data('wlxmlNode').setText(toSet);
+ //textElement.data('wlxmlNode').setText(toSet);
+ //textElement.data('wlxmlNode').document.transform('setText', {node: textElement.data('wlxmlNode'), text: toSet});
+ if(textElement.data('wlxmlNode').getText() != toSet) {
+ canvas.textHandler.handle(textElement.data('wlxmlNode'), toSet);
+ }
}
});
});
if(params instanceof DocumentNodeElement) {
element = params;
} else {
- element = DocumentNodeElement.create(params, this.canvas);
+ element = DocumentElement.create(params, this.canvas);
}
this.dom().wrap('<div>');
this.dom().parent().after(element.dom());
},
setText: function(text) {
+ console.log('smartxml: ' + text);
this.nativeNode.data = text;
this.triggerTextChangeEvent();
},
},
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!');
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
doc.undo();
expect(doc.root.contents().length).to.equal(3);
- console.log(doc.toXML());
+ //console.log(doc.toXML());
expect(doc.root.contents()[1].contents()[0].getText()).to.equal('has');
});