From 0c978bca67983e93096a8e744b53e50861a9333d Mon Sep 17 00:00:00 2001 From: =?utf8?q?Aleksander=20=C5=81ukasz?= Date: Wed, 27 Nov 2013 12:45:15 +0100 Subject: [PATCH] dzialacy remove+text --- .../modules/documentCanvas/canvas/canvas.js | 44 +++++++++++++- .../documentCanvas/canvas/documentElement.js | 2 +- src/smartxml/smartxml.js | 58 ++++++++++++++++++- src/smartxml/smartxml.test.js | 2 +- 4 files changed, 102 insertions(+), 4 deletions(-) diff --git a/src/editor/modules/documentCanvas/canvas/canvas.js b/src/editor/modules/documentCanvas/canvas/canvas.js index 05dfbad..b6ddf71 100644 --- a/src/editor/modules/documentCanvas/canvas/canvas.js +++ b/src/editor/modules/documentCanvas/canvas/canvas.js @@ -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 = $('
').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); + } } }); }); diff --git a/src/editor/modules/documentCanvas/canvas/documentElement.js b/src/editor/modules/documentCanvas/canvas/documentElement.js index 116ebf4..1c0719f 100644 --- a/src/editor/modules/documentCanvas/canvas/documentElement.js +++ b/src/editor/modules/documentCanvas/canvas/documentElement.js @@ -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('
'); this.dom().parent().after(element.dom()); diff --git a/src/smartxml/smartxml.js b/src/smartxml/smartxml.js index 4e8e575..7b59b18 100644 --- a/src/smartxml/smartxml.js +++ b/src/smartxml/smartxml.js @@ -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 diff --git a/src/smartxml/smartxml.test.js b/src/smartxml/smartxml.test.js index 8e67d74..d2ac4a9 100644 --- a/src/smartxml/smartxml.test.js +++ b/src/smartxml/smartxml.test.js @@ -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'); }); -- 2.20.1