+ VisualPerspective.prototype.insertInlineTag = function(tag) {
+ this.caret.detach();
+
+ let selection = window.getSelection();
+ var n = selection.rangeCount;
+ if (n != 1) {
+ window.alert("Nie zaznaczono obszaru");
+ return false
+ }
+ let range = selection.getRangeAt(0);
+
+ // Make sure that:
+ // Both ends are in the same x-node container.
+ // TODO: That the container is a inline-text container.
+ let node = range.startContainer;
+ if (node.nodeType == node.TEXT_NODE) {
+ node = node.parentNode;
+ }
+ let endNode = range.endContainer;
+ if (endNode.nodeType == endNode.TEXT_NODE) {
+ endNode = endNode.parentNode;
+ }
+ if (node != endNode) {
+ window.alert("Zły obszar.");
+ return false;
+ }
+
+ // We will construct a HTML element with the range selected.
+ let div = $("<span x-pass-thru='true'>");
+
+ contents = $(node).contents();
+ let startChildIndex = node == range.startContainer ? 0 : contents.index(range.startContainer);
+ let endChildIndex = contents.index(range.endContainer);
+
+ current = range.startContainer;
+ if (current.nodeType == current.TEXT_NODE) {
+ current = current.splitText(range.startOffset);
+ }
+ while (current != range.endContainer) {
+ n = current.nextSibling;
+ $(current).appendTo(div);
+ current = n;
+ }
+ if (current.nodeType == current.TEXT_NODE) {
+ end = current.splitText(range.endOffset);
+ }
+ $(current).appendTo(div);
+
+ html2text({
+ element: div[0],
+ success: function(d) {
+ xml2html({
+ xml: d = '<' + tag + '>' + d + '</' + tag + '>',
+ success: function(html) {
+ // What if no end?
+ node.insertBefore($(html)[0], end);
+ }
+ });
+ },
+ error: function(a, b) {
+ console.log(a, b);
+ }
+ });
+ };
+