X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/0f406b246f9261651b5982be71a49189349c8e3e..b554451bd332b417a9304fd60fafe116220a9ca4:/modules/documentCanvas/canvas.js diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index 2d364ac..eb0fd0b 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() { @@ -35,6 +36,14 @@ Canvas.prototype.getNode = function(desc) { return toret; } +Canvas.prototype.getPreviousNode = function(options) { + var element = $(this.content.find('#' + options.node.id).get(0)); + var prev = element.prev() + if(prev.length === 0) + prev = element.parent(); + return new wlxmlNode.Node(prev); +} + Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) { var toBlock = ['div', 'document', 'section', 'header']; var htmlTag = _.contains(toBlock, wlxmlTag) ? 'div' : 'span'; @@ -48,8 +57,11 @@ Canvas.prototype._createNode = function(wlxmlTag, wlxmlClass) { Canvas.prototype.insertNode = function(options) { var element = $(this.content.find('#' + options.context.id).get(0)); - if(options.place == 'after') - element[options.place](this._createNode(options.tag, options.klass)); + if(options.place == 'after') { + var node = this._createNode(options.tag, options.klass); + element[options.place](node); + return node; + } else if(options.place == 'wrapText') { var elementContents = element.contents(); if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3) @@ -68,22 +80,47 @@ 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 elementContents = element.contents(); - if(elementContents.length !== 1 || elementContents.get(0).nodeType != 3) + var nodeToSplit = $(this.content.find('#' + options.node.id).get(0)); + + 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.removeNode = function(options) { + var toRemove = $(this.content.find('#' + options.node.id).get(0)); + toRemove.remove(); + } Canvas.prototype.createList = function(options) {