dzialacy remove+text
authorAleksander Łukasz <aleksander.lukasz@nowoczesnapolska.org.pl>
Wed, 27 Nov 2013 11:45:15 +0000 (12:45 +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/documentElement.js
src/smartxml/smartxml.js
src/smartxml/smartxml.test.js

index 05dfbad..b6ddf71 100644 (file)
@@ -10,12 +10,48 @@ define([
     
 '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, {
@@ -56,6 +92,8 @@ $.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)) {
@@ -72,7 +110,11 @@ $.extend(Canvas.prototype, {
                     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);
+                    }
                 }
             });
         });
index 116ebf4..1c0719f 100644 (file)
@@ -396,7 +396,7 @@ $.extend(DocumentTextElement.prototype, {
         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());
index 4e8e575..7b59b18 100644 (file)
@@ -383,6 +383,7 @@ $.extend(TextNode.prototype, {
     },
 
     setText: function(text) {
+        console.log('smartxml: ' + text);
         this.nativeNode.data = text;
         this.triggerTextChangeEvent();
     },
@@ -641,12 +642,14 @@ $.extend(Document.prototype, Backbone.Events, {
     },
 
     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!');
@@ -769,7 +772,60 @@ $.extend(Detach2NodeTransformation.prototype, {
         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
 
index 8e67d74..d2ac4a9 100644 (file)
@@ -848,7 +848,7 @@ describe('smartxml', function() {
             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');
 
         });