all files / editor-manager/plugin/ link.js

98.98% Statements 194/196
91.85% Branches 124/135
100% Functions 17/17
98.98% Lines 194/196
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316   1907× 1907×   1907× 1907×   1762× 1762×   56×     45× 45×         47×   47×   47× 11× 11× 11× 11×   11× 11×   11× 10× 10×         11×       11×           36× 36× 36×     36×   36×   22× 22× 22× 22×   22× 22× 22× 22×       16× 16×       14×     47× 40×                 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 14× 31× 31×   14× 31× 31× 31× 35×   35×   31× 31×           14× 31× 14×   31× 17×         17× 17×       14× 14× 14× 31× 14× 14×   31× 17×           14× 14× 45× 14× 14× 10×   14× 14×   45× 17×           14× 31× 31×       45×             45× 42×   45× 32×   45×   14× 14× 56× 27× 25× 25×       14×   35×   12×                                   13× 11×                 1762×        
define(["require", "exports", "./../base/constant", "../../selection/selection", "./nodecutter", "./inserthtml", "@syncfusion/ej2-base", "./../../common/constant"], function (require, exports, CONSTANT, selection_1, nodecutter_1, inserthtml_1, ej2_base_1, EVENTS) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var LinkCommand = (function () {
        function LinkCommand(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        LinkCommand.prototype.addEventListener = function () {
            this.parent.observer.on(CONSTANT.LINK, this.linkCommand, this);
            this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
        };
        LinkCommand.prototype.removeEventListener = function () {
            this.parent.observer.off(CONSTANT.LINK, this.linkCommand);
            this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
        };
        LinkCommand.prototype.linkCommand = function (e) {
            switch (e.value.toString().toLocaleLowerCase()) {
                case 'createlink':
                case 'editlink':
                    this.createLink(e);
                    break;
                case 'openlink':
                    this.openLink(e);
                    break;
                case 'removelink':
                    this.removeLink(e);
                    break;
            }
        };
        LinkCommand.prototype.createLink = function (e) {
            var closestAnchor = (!ej2_base_1.isNullOrUndefined(e.item.selectParent) && e.item.selectParent.length > 0) &&
                ej2_base_1.closest(e.item.selectParent[0], 'a');
            closestAnchor = !ej2_base_1.isNullOrUndefined(closestAnchor) ? closestAnchor :
                (!ej2_base_1.isNullOrUndefined(e.item.selectParent) && e.item.selectParent.length > 0) ? (e.item.selectParent[0]) : null;
            if (!ej2_base_1.isNullOrUndefined(closestAnchor) && closestAnchor.tagName === 'A') {
                var anchorEle = closestAnchor;
                var linkText = '';
                Eif (!ej2_base_1.isNullOrUndefined(e.item.url)) {
                    anchorEle.setAttribute('href', e.item.url);
                }
                Eif (!ej2_base_1.isNullOrUndefined(e.item.title)) {
                    anchorEle.setAttribute('title', e.item.title);
                }
                if (!ej2_base_1.isNullOrUndefined(e.item.text) && e.item.text !== '') {
                    linkText = anchorEle.innerText;
                    if (anchorEle.firstChild.nodeName === '#text') {
                        anchorEle.innerText = e.item.text;
                    }
                    else {
                        anchorEle.firstChild.innerText = e.item.text;
                    }
                }
                if (!ej2_base_1.isNullOrUndefined(e.item.target)) {
                    anchorEle.setAttribute('target', e.item.target);
                    anchorEle.setAttribute('aria-label', e.item.ariaLabel);
                }
                else {
                    anchorEle.removeAttribute('target');
                    anchorEle.removeAttribute('aria-label');
                }
                if (linkText === e.item.text) {
                    e.item.selection.setSelectionText(this.parent.currentDocument, anchorEle, anchorEle, 1, 1);
                    e.item.selection.restore();
                }
                else {
                    var startIndex = e.item.action === 'Paste' ? anchorEle.childNodes[0].textContent.length : 0;
                    var endIndex = anchorEle.firstChild.nodeName === '#text' ? anchorEle.childNodes[0].textContent.length : anchorEle.childNodes.length;
                    e.item.selection.setSelectionText(this.parent.currentDocument, anchorEle.childNodes[0], anchorEle.childNodes[0], startIndex, endIndex);
                }
            }
            else {
                var domSelection = new selection_1.NodeSelection(this.parent.editableElement);
                var range = domSelection.getRange(this.parent.currentDocument);
                if (range.endContainer.nodeName === '#text' && range.startContainer.textContent.length === (range.endOffset + 1) &&
                    range.endContainer.textContent.charAt(range.endOffset) === ' ' && (!ej2_base_1.isNullOrUndefined(range.endContainer.nextSibling) && range.endContainer.nextSibling.nodeName === 'A')) {
                    domSelection.setSelectionText(this.parent.currentDocument, range.startContainer, range.endContainer, range.startOffset, range.endOffset + 1);
                    range = domSelection.getRange(this.parent.currentDocument);
                }
                var text = ej2_base_1.isNullOrUndefined(e.item.text) ? true : e.item.text.replace(/ /g, '').localeCompare(range.toString()
                    .replace(/\n/g, ' ').replace(/ /g, '')) < 0;
                if (e.event && e.event.type === 'keydown' && (e.event.keyCode === 32
                    || e.event.keyCode === 13) || e.item.action === 'Paste' || range.collapsed || text) {
                    var anchor = this.createAchorNode(e);
                    anchor.innerText = e.item.text === '' ? e.item.url : e.item.text;
                    var text_1 = anchor.innerText;
                    var modifiedText = text_1.replace(/  +/g, function (match) {
                        return '\u00A0'.repeat(match.length);
                    });
                    anchor.innerText = modifiedText;
                    e.item.selection.restore();
                    inserthtml_1.InsertHtml.Insert(this.parent.currentDocument, anchor, this.parent.editableElement);
                    if (e.event && e.event.type === 'keydown' && (e.event.keyCode === 32
                        || e.event.keyCode === 13)) {
                        var startContainer = e.item.selection.range.startContainer;
                        startContainer.textContent = this.removeText(startContainer.textContent, e.item.text);
                    }
                    else {
                        var startIndex = e.item.action === 'Paste' ? anchor.childNodes[0].textContent.length : 0;
                        e.item.selection.setSelectionText(this.parent.currentDocument, anchor.childNodes[0], anchor.childNodes[0], startIndex, anchor.childNodes[0].textContent.length);
                    }
                }
                else {
                    this.createLinkNode(e);
                }
            }
            if (e.callBack) {
                e.callBack({
                    requestType: 'Links',
                    editorMode: 'HTML',
                    event: e.event,
                    range: this.parent.nodeSelection.getRange(this.parent.currentDocument),
                    elements: this.parent.nodeSelection.getSelectedNodes(this.parent.currentDocument)
                });
            }
        };
        LinkCommand.prototype.createLinkNode = function (e) {
            var domSelection = new selection_1.NodeSelection(this.parent.editableElement);
            var nodeCutter = new nodecutter_1.NodeCutter();
            var range = domSelection.getRange(this.parent.currentDocument);
            var nodes = this.getSelectionNodes(domSelection.getNodeCollection(range));
            var save = domSelection.save(range, this.parent.currentDocument);
            var txtArray = [];
            var inlineNodes = [];
            var currentNode;
            var removeNodes = [];
            var anchorNodes = [];
            var finalinlineNodes = [];
            var cloneNode;
            for (var index = 0; index < nodes.length; index++) {
                nodes[index] = nodeCutter.GetSpliceNode(range, nodes[index]);
                txtArray[index] = nodes[index];
            }
            for (var i = 0; i < txtArray.length; i++) {
                var check = true;
                currentNode = txtArray[i];
                while (check === true) {
                    if (currentNode.parentNode.nodeName === 'A') {
                        var anchorEle = currentNode.parentNode;
                        currentNode.parentNode.parentNode.insertBefore(anchorEle.firstChild, anchorEle);
                        currentNode.parentNode.removeChild(anchorEle);
                    }
                    if (this.isBlockNode(currentNode.parentNode) || txtArray.length === 0 || i === 0 || i === txtArray.length - 1
                        || range.startContainer.nodeType === 3) {
                        inlineNodes[i] = currentNode;
                        check = false;
                    }
                    else {
                        currentNode = currentNode.parentNode;
                    }
                }
            }
            for (var i = 0, j_1 = 0; i < inlineNodes.length; i++) {
                if (i === 0) {
                    finalinlineNodes[j_1] = inlineNodes[i];
                }
                if (inlineNodes.length > 1 && i < inlineNodes.length - 1) {
                    Iif ((inlineNodes[i].parentElement === inlineNodes[i + 1].parentElement) &&
                        (inlineNodes[i] === inlineNodes[i + 1])) {
                        continue;
                    }
                    else {
                        finalinlineNodes[j_1 + 1] = inlineNodes[i + 1];
                        j_1++;
                    }
                }
            }
            var j = 0;
            anchorNodes[j] = this.createAchorNode(e);
            for (var i = 0; i < finalinlineNodes.length; i++) {
                if (i === 0) {
                    cloneNode = finalinlineNodes[i].cloneNode(true);
                    anchorNodes[i].appendChild(cloneNode);
                }
                if (i < finalinlineNodes.length - 1) {
                    if (finalinlineNodes[i].parentNode === finalinlineNodes[i + 1].parentNode) {
                        var cln = finalinlineNodes[i + 1].cloneNode(true);
                        anchorNodes[j].appendChild(cln);
                    }
                    else {
                        j = j + 1;
                        anchorNodes[j] = this.createAchorNode(e);
                        cloneNode = finalinlineNodes[i + 1].cloneNode(true);
                        anchorNodes[j].appendChild(cloneNode);
                    }
                }
            }
            this.parent.nodeSelection.setRange(document, save.range);
            for (var i = 0, j_2 = 0, k = 0; i <= finalinlineNodes.length; i++) {
                if (i === 0) {
                    finalinlineNodes[i].parentNode.insertBefore(anchorNodes[j_2], finalinlineNodes[i].nextSibling);
                    if (this.parent.domNode.blockNodes().length === 1 && anchorNodes.length === 1) {
                        this.parent.nodeSelection.setSelectionNode(this.parent.currentDocument, anchorNodes[j_2]);
                    }
                    removeNodes[k] = finalinlineNodes[i];
                    k++;
                }
                if (i < finalinlineNodes.length - 1) {
                    if (finalinlineNodes[i].parentNode === finalinlineNodes[i + 1].parentNode) {
                        removeNodes[k] = finalinlineNodes[i + 1];
                        k++;
                    }
                    else {
                        j_2 = j_2 + 1;
                        finalinlineNodes[i + 1].parentNode.insertBefore(anchorNodes[j_2], finalinlineNodes[i + 1]);
                        removeNodes[k] = finalinlineNodes[i + 1];
                        k++;
                    }
                }
            }
            for (var i = 0; i < removeNodes.length; i++) {
                Eif (removeNodes[i].parentNode) {
                    removeNodes[i].parentNode.removeChild(removeNodes[i]);
                }
            }
        };
        LinkCommand.prototype.createAchorNode = function (e) {
            var anchorEle = ej2_base_1.createElement('a', {
                className: 'e-rte-anchor',
                attrs: {
                    href: e.item.url,
                    title: ej2_base_1.isNullOrUndefined(e.item.title) || e.item.title === '' ? e.item.url : e.item.title
                }
            });
            if (!ej2_base_1.isNullOrUndefined(e.item.target)) {
                anchorEle.setAttribute('target', e.item.target);
            }
            if (!ej2_base_1.isNullOrUndefined(e.item.ariaLabel)) {
                anchorEle.setAttribute('aria-label', e.item.ariaLabel);
            }
            return anchorEle;
        };
        LinkCommand.prototype.getSelectionNodes = function (nodeCollection) {
            nodeCollection = nodeCollection.reverse();
            for (var index = 0; index < nodeCollection.length; index++) {
                if (nodeCollection[index].nodeType !== 3 || nodeCollection[index].textContent.trim() === '') {
                    if (nodeCollection[index].nodeName !== 'IMG') {
                        nodeCollection.splice(index, 1);
                        index--;
                    }
                }
            }
            return nodeCollection.reverse();
        };
        LinkCommand.prototype.isBlockNode = function (element) {
            return (!!element && (element.nodeType === Node.ELEMENT_NODE && CONSTANT.BLOCK_TAGS.indexOf(element.tagName.toLowerCase()) >= 0));
        };
        LinkCommand.prototype.removeText = function (text, val) {
            var arr = text.split(' ');
            for (var i = 0; i < arr.length; i++) {
                if (arr[i] === val) {
                    arr.splice(i, 1);
                    i--;
                }
            }
            return arr.join(' ') + ' ';
        };
        LinkCommand.prototype.openLink = function (e) {
            document.defaultView.open(e.item.url, e.item.target);
            this.callBack(e);
        };
        LinkCommand.prototype.removeLink = function (e) {
            var blockNodes = this.parent.domNode.blockNodes();
            if (blockNodes.length < 2) {
                this.parent.domNode.setMarker(e.item.selection);
                var closestAnchor = ej2_base_1.closest(e.item.selectParent[0], 'a');
                var selectParent = closestAnchor ? closestAnchor : e.item.selectParent[0];
                var parent_1 = selectParent.parentNode;
                var child = [];
                for (; selectParent.firstChild; null) {
                    Eif (parent_1) {
                        child.push(parent_1.insertBefore(selectParent.firstChild, selectParent));
                    }
                    else {
                        break;
                    }
                }
                parent_1.removeChild(selectParent);
                Eif (child && child.length === 1) {
                    e.item.selection.startContainer = e.item.selection.getNodeArray(child[child.length - 1], true);
                    e.item.selection.endContainer = e.item.selection.startContainer;
                }
                e.item.selection = this.parent.domNode.saveMarker(e.item.selection);
            }
            else {
                for (var i = 0; i < blockNodes.length; i++) {
                    var linkNode = blockNodes[i].querySelectorAll('a');
                    for (var j = 0; j < linkNode.length; j++) {
                        Eif (this.parent.currentDocument.getSelection().containsNode(linkNode[j], true)) {
                            linkNode[j].outerHTML = linkNode[j].innerHTML;
                        }
                    }
                }
            }
            e.item.selection.restore();
            this.callBack(e);
        };
        LinkCommand.prototype.callBack = function (e) {
            if (e.callBack) {
                e.callBack({
                    requestType: e.item.subCommand,
                    editorMode: 'HTML',
                    event: e.event,
                    range: this.parent.nodeSelection.getRange(this.parent.currentDocument),
                    elements: this.parent.nodeSelection.getSelectedNodes(this.parent.currentDocument)
                });
            }
        };
        LinkCommand.prototype.destroy = function () {
            this.removeEventListener();
        };
        return LinkCommand;
    }());
    exports.LinkCommand = LinkCommand;
});