X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/0f406b246f9261651b5982be71a49189349c8e3e..daeb3efce48907831ac2fc1b756845c9cb83cd41:/modules/documentCanvas/canvas.js diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index 2d364ac..20c286a 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -9,13 +9,14 @@ define([ 'use strict'; var Canvas = function(xml) { - this.xml = xml; this.dom = $(template); - - this.content = this.dom.find('#rng-module-documentCanvas-content') - - - this.content.html(transformations.fromXML.getHTMLTree(xml)); + this.content = this.dom.find('#rng-module-documentCanvas-content'); + this.setXML(xml); +} + +Canvas.prototype.setXML = function(xml) { + this.xml = xml; + this.content.html(transformations.fromXML.getHTMLTree(xml)); } Canvas.prototype.toXML = function() { @@ -68,22 +69,41 @@ Canvas.prototype.insertNode = function(options) { } Canvas.prototype.splitNode = function(options) { - var element = $(this.content.find('#' + options.node.id).get(0)); + options = _.extend({textNodeIdx: 0}, options); + + var nodeToSplit = $(this.content.find('#' + options.node.id).get(0)); - var elementContents = element.contents(); - if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3) + var nodeContents = nodeToSplit.contents(); + if(nodeContents.length === 0 || + nodeContents.length - 1 < options.textNodeIdx || + nodeContents.get(options.textNodeIdx).nodeType != 3) return false; - var textElement = elementContents.get(0); - var prefix = textElement.data.substr(0, options.offset); - var suffix = textElement.data.substr(options.offset); - var prefixNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class')); - var suffixNode = this._createNode(element.attr('wlxml-tag'), element.attr('wlxml-class')); - prefixNode.text(prefix); - suffixNode.text(suffix); - element.before(prefixNode); - element.after(suffixNode); - element.remove(); + var textNode = $(nodeContents.get(options.textNodeIdx)); + + var succeedingNodes = []; + var passed = false; + nodeContents.each(function() { + var node = this; + if(passed) + succeedingNodes.push(node); + if(node.isSameNode(textNode.get(0))) + passed = true; + }); + + var prefix = textNode.text().substr(0, options.offset); + var suffix = textNode.text().substr(options.offset); + + textNode.before(prefix); + textNode.remove(); + + var newNode = this._createNode(nodeToSplit.attr('wlxml-tag'), nodeToSplit.attr('wlxml-class')); + newNode.append(suffix); + succeedingNodes.forEach(function(node) { + newNode.append(node) + }); + nodeToSplit.after(newNode); + return newNode; } Canvas.prototype.createList = function(options) {