From: Aleksander Ɓukasz Date: Wed, 24 Jul 2013 16:30:44 +0000 (+0200) Subject: Refactoring: nodeElement renders itself with its contents X-Git-Url: https://git.mdrn.pl/fnpeditor.git/commitdiff_plain/cf7c3b4fb3a419476a0d7f6118d3ba9a3fd0c809?hp=a1c752a1fd15569e947246924254c2a072462fb8 Refactoring: nodeElement renders itself with its contents --- diff --git a/modules/documentCanvas/canvas/canvas.js b/modules/documentCanvas/canvas/canvas.js index 96206c3..8882c1c 100644 --- a/modules/documentCanvas/canvas/canvas.js +++ b/modules/documentCanvas/canvas/canvas.js @@ -320,35 +320,8 @@ $.extend(Canvas.prototype, { }, toXML: function() { - var d = function(element, parent, level) { - console.log(element.getText ? 'text: ' + element.getText() : 'node: ' + element.getWlxmlTag()); - var isElementNode = element instanceof documentElement.DocumentNodeElement; - var parentChildren = parent.contents(); - var dom; - - var elXML = element.toXML(level); - if(parentChildren.length > 2 && parentChildren[0].nodeType === Node.TEXT_NODE && parentChildren[1].nodeType == Node.TEXT_NODE) { - $(parentChildren[0]).after(elXML); - } else { - parent.prepend(elXML); - } - - - if(isElementNode) { - elXML.each(function() { - if(this.nodeType === Node.ELEMENT_NODE) { - dom = $(this); - return; - } - }); - var elementChildren = element.children(); - for(var i = elementChildren.length - 1; i >= 0; i--) { - d(elementChildren[i], dom, level + 1); - } - } - } var parent = $('
'); - d(this.doc(), parent, 0); + this.doc().toXML(parent, 0) return parent.html(); } }); diff --git a/modules/documentCanvas/canvas/documentElement.js b/modules/documentCanvas/canvas/documentElement.js index 969d85b..9178a5a 100644 --- a/modules/documentCanvas/canvas/documentElement.js +++ b/modules/documentCanvas/canvas/documentElement.js @@ -148,6 +148,15 @@ var manipulate = function(e, params, action) { DocumentNodeElement.prototype = new DocumentElement(); +var addParts = function(parts, parent) { + var parentChildren = parent.contents(); + if(parentChildren.length > 2 && parentChildren[0].nodeType === Node.TEXT_NODE && parentChildren[1].nodeType == Node.TEXT_NODE) { + $(parentChildren[0]).after(parts); + } else { + parent.prepend(parts); + } +} + $.extend(DocumentNodeElement.prototype, { data: function() { var dom = this.dom(), @@ -156,9 +165,9 @@ $.extend(DocumentNodeElement.prototype, { return dom.removeData(args[1]); return dom.data.apply(dom, arguments); }, - toXML: function(level) { - var node = $('<' + this.getWlxmlTag() + '>'), - toret = $('
'); + toXML: function(parent, level) { + var node = $('<' + this.getWlxmlTag() + '>'); + if(this.getWlxmlClass()) node.attr('class', this.getWlxmlClass()); var meta = this.getWlxmlMetaAttrs(); @@ -170,26 +179,37 @@ $.extend(DocumentNodeElement.prototype, { node.attr(key, this.data('other-attrs')[key]); }, this); - if(this.data('orig-before') && this.data('orig-before').length) { - this.data('orig-before').forEach(function(toAdd) { - if(toAdd) - toret.prepend(document.createTextNode(toAdd)); - }); - } else if(level && this.getWlxmlTag() !== 'span') { - toret.append('\n' + (new Array(level * 2 + 1)).join(' ')); - } - if(this.data('orig-append') && this.data('orig-append').length) { - this.data('orig-append').forEach(function(toAdd) { - if(toAdd) - node.prepend(toAdd); - }); - //toret = toret.prepend(document.createTextNode(this.data('orig-prepend'))); - } else if(this.getWlxmlTag() !== 'span'){ - node.append('\n' + (new Array(level * 2 + 1)).join(' ')); + var addFormatting = function() { + var toret = $('
'); + + if(this.data('orig-before') && this.data('orig-before').length) { + this.data('orig-before').forEach(function(toAdd) { + if(toAdd) + toret.prepend(document.createTextNode(toAdd)); + }); + } else if(level && this.getWlxmlTag() !== 'span') { + toret.append('\n' + (new Array(level * 2 + 1)).join(' ')); + } + if(this.data('orig-append') && this.data('orig-append').length) { + this.data('orig-append').forEach(function(toAdd) { + if(toAdd) + node.prepend(toAdd); + }); + } else if(this.getWlxmlTag() !== 'span'){ + node.append('\n' + (new Array(level * 2 + 1)).join(' ')); + } + toret.append(node); + return toret.contents(); + }.bind(this); + + var parts = addFormatting(node); + + var children = this.children(); + for(var i = children.length - 1; i >= 0; i--) { + children[i].toXML(node, level + 1); } - toret.append(node); - return toret.contents(); + addParts(parts, parent); }, append: function(params) { if(params.tag !== 'span') @@ -305,8 +325,8 @@ $.extend(DocumentTextElement, { DocumentTextElement.prototype = new DocumentElement(); $.extend(DocumentTextElement.prototype, { - toXML: function() { - return this.getText(); + toXML: function(parent) { + addParts(this.getText(), parent); }, _setupDOMHandler: function(htmlElement) { var $element = $(htmlElement);