+ VisualPerspective.prototype.insertInlineTag = function(tag) {
+ this.caret.detach();
+
+ let selection = window.getSelection();
+ var n = selection.rangeCount;
+ if (n != 1 || selection.isCollapsed) {
+ 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);
+ }
+ });
+ };
+
+ VisualPerspective.prototype.insertAtRange = function(range, elem) {
+ let self = this;
+ let $end = $(range.endContainer);
+ if ($end.attr('id') == 'caret') {
+ self.caret.insert(elem);
+ } else {
+ range.insertNode(elem[0]);
+ }
+ }
+
+ VisualPerspective.prototype.addReference = function() {
+ let self = this;
+ var selection = window.getSelection();
+ var n = selection.rangeCount;
+
+ // TODO: if no selection, take caret position..
+ if (n == 0) {
+ window.alert("Nie zaznaczono żadnego obszaru");
+ return false;
+ }
+
+ var range = selection.getRangeAt(n - 1);
+ if (!verifyTagInsertPoint(range.endContainer)) {
+ window.alert("Nie można wstawić w to miejsce referencji.");
+ return false;
+ }
+
+ var tag = $('<span></span>');
+
+ range.collapse(false);
+ self.insertAtRange(range, tag);
+
+ xml2html({
+ xml: '<ref href=""/>',
+ success: function(text){
+ var t = $(text);
+ tag.replaceWith(t);
+ openForEdit(t);
+ },
+ error: function(){
+ tag.remove();
+ alert('Błąd przy dodawaniu referncji:' + errors);
+ }
+ })
+ }
+