X-Git-Url: https://git.mdrn.pl/fnpeditor.git/blobdiff_plain/a398f2a96740ebe6dd60559d90e74c648e7009af..428f22c06a5a72b9f036429f84eec9e1deea1c57:/modules/documentCanvas/canvas.js diff --git a/modules/documentCanvas/canvas.js b/modules/documentCanvas/canvas.js index 9c926e9..ab8a578 100644 --- a/modules/documentCanvas/canvas.js +++ b/modules/documentCanvas/canvas.js @@ -97,6 +97,61 @@ Canvas.prototype.nodeWrap = function(options) { options._with.dom.after(suffixOutside); }; +Canvas.prototype.nodeUnwrap = function(options) { + + var removeWithJoin = function(node) { + var contents = node.parent().contents(), + idx = contents.index(node), + prev = idx > 0 ? contents[idx-1] : null, + next = idx + 1 < contents.length ? contents[idx+1] : null; + + if(prev && prev.nodeType === 3 && next && next.nodeType === 3) { + prev.data = prev.data + next.data; + $(next).remove(); + } + node.remove(); + }; + + var toUnwrap = $(this.content.find('#' + options.node.getId()).get(0)); + + + var parent = toUnwrap.parent(); + var parentContents = parent.contents(); + + if(toUnwrap.contents().length !== 1 || toUnwrap.contents()[0].nodeType !== 3) + return false; + + var idx = parentContents.index(toUnwrap); + + var combineWith, + action; + + if(idx > 0 && parentContents[idx-1].nodeType === 3) { + combineWith = parentContents[idx-1]; + action = 'append'; + } else if(idx + 1 < parentContents.length && parentContents[idx+1].nodeType === 3) { + combineWith = parentContents[idx+1]; + action = 'prepend'; + } + + if(combineWith) { + var text = + (action === 'prepend' ? toUnwrap.text() : '') + + combineWith.data + + (action === 'append' ? toUnwrap.text() : '') + ; + combineWith.data = text; + removeWithJoin(toUnwrap); + } else { + if(parentContents.length === 1 || idx === 0) { + parent.append(toUnwrap.text()); + } else { + toUnwrap.prev().after(toUnwrap.text()); + } + toUnwrap.remove(); + } +}; + Canvas.prototype.nodeSplit = function(options) { options = _.extend({textNodeIdx: 0}, options); @@ -201,23 +256,36 @@ Canvas.prototype.listCreate = function(options) { Canvas.prototype.listRemove = function(options) { var pointerElement = $(this.content.find('#' + options.pointer.getId())); - var listElement = options.pointer.getClass() === 'list-items' ? pointerElement : + var listElement = options.pointer.isOfClass('list-items') ? pointerElement : pointerElement.parents('[wlxml-class|="list-items"][wlxml-tag]'); - var nested = false; + var nested = false, + nestedLists; if(listElement.length > 1) { listElement = $(listElement[0]); nested = true; } if(nested) { + // We are only moving one level up listElement.unwrap(); } else { + // We are removing the whole list + nestedLists = listElement.find('[wlxml-class=item] > [wlxml-class|=list-items]'); + nestedLists.unwrap(); listElement.find('[wlxml-class=item]').each(function() { $(this).removeAttr('wlxml-class'); }); } + listElement.children().unwrap(); + + var c = this; + if(nestedLists) { + nestedLists.each(function() { + c.listRemove({pointer: canvasNode.create($(this))}); + }); + } }; Canvas.prototype.getPrecedingNode = function(options) {