all files / markdown-parser/plugin/ formats.js

99.35% Statements 153/154
95.65% Branches 88/92
100% Functions 13/13
99.35% Lines 153/154
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   151× 151× 151×   151× 151×   70× 70×   65× 65× 65× 65× 65× 10×           10× 10× 10×   55×     52× 50×     52× 52× 52× 52× 52×         47× 117× 105× 105×   105× 105× 105× 84× 224×   224×       12× 10× 10×     10× 30× 30×         47×   63× 63× 63× 441×     63×   63× 63× 63× 63× 63× 31× 74× 74× 489× 489× 52×   437× 437× 68× 68×   68× 68× 68× 50× 220×   220×     68×     74× 30× 30×         31×                         96× 96× 96× 28×               65× 65× 65× 65× 65× 65× 502× 60× 60×   442× 45× 45× 45× 45×         65×   70×        
define(["require", "exports", "@syncfusion/ej2-base", "./../../common/constant", "./../../markdown-parser/base/constant"], function (require, exports, ej2_base_1, EVENTS, CONSTANT) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MDFormats = (function () {
        function MDFormats(options) {
            ej2_base_1.extend(this, this, options, true);
            this.selection = this.parent.markdownSelection;
            this.addEventListener();
        }
        MDFormats.prototype.addEventListener = function () {
            this.parent.observer.on(EVENTS.FORMAT_TYPE, this.applyFormats, this);
            this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
        };
        MDFormats.prototype.removeEventListener = function () {
            this.parent.observer.off(EVENTS.FORMAT_TYPE, this.applyFormats);
            this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
        };
        MDFormats.prototype.applyFormats = function (e) {
            e.subCommand = e.subCommand.toLowerCase();
            var textArea = this.parent.element;
            this.selection.save(textArea.selectionStart, textArea.selectionEnd);
            var parents = this.selection.getSelectedParentPoints(textArea);
            if (this.isAppliedFormat(parents) === e.subCommand) {
                if (e.subCommand === 'pre') {
                    Eif (parents.length > 1) {
                        this.applyCodeBlock(textArea, e, parents);
                    }
                    else {
                        return;
                    }
                }
                this.cleanFormat(textArea);
                this.restore(textArea, textArea.selectionStart, textArea.selectionEnd, e);
                return;
            }
            if (e.subCommand === 'p') {
                this.cleanFormat(textArea);
                this.restore(textArea, textArea.selectionStart, textArea.selectionEnd, e);
                return;
            }
            else {
                if ((e.subCommand === 'pre' && parents.length !== 1) || e.subCommand !== 'pre') {
                    this.cleanFormat(textArea, e.subCommand);
                }
            }
            var start = textArea.selectionStart;
            var end = textArea.selectionEnd;
            var addedLength = 0;
            parents = this.selection.getSelectedParentPoints(textArea);
            if (e.subCommand === 'pre') {
                if (parents.length > 1) {
                    this.applyCodeBlock(textArea, e, parents);
                }
                else {
                    ej2_base_1.extend(e, e, { subCommand: 'InlineCode' }, true);
                    this.parent.observer.notify(CONSTANT.selectionCommand, e);
                }
                return;
            }
            for (var i = 0; i < parents.length; i++) {
                if (parents[i].text !== '' && !this.selection.isStartWith(parents[i].text, '\\' + this.syntax[e.subCommand])) {
                    parents[i].text = this.syntax[e.subCommand] + parents[i].text;
                    textArea.value = textArea.value.substr(0, parents[i].start) + parents[i].text + '\n' +
                        textArea.value.substr(parents[i].end, textArea.value.length);
                    start = i === 0 ? start + this.syntax[e.subCommand].length : start;
                    addedLength += this.syntax[e.subCommand].length;
                    if (parents.length !== 1) {
                        for (var j = i; j < parents.length; j++) {
                            parents[j].start = j !== 0 ?
                                this.syntax[e.subCommand].length + parents[j].start : parents[j].start;
                            parents[j].end = this.syntax[e.subCommand].length + parents[j].end;
                        }
                    }
                }
                else if (parents[i].text === '' && i === 0) {
                    this.selection.save(start, end);
                    if (this.selection.getSelectedText(textArea).length === 0) {
                        parents[i].text = this.syntax[e.subCommand];
                        textArea.value = textArea.value.substr(0, parents[i].start) + this.syntax[e.subCommand] +
                            textArea.value.substr(parents[i].end, textArea.value.length);
                        start = i === 0 ? start + this.syntax[e.subCommand].length : start;
                        addedLength += this.syntax[e.subCommand].length;
                    }
                    if (parents.length !== 1) {
                        for (var j = i; j < parents.length; j++) {
                            parents[j].start = j !== 0 ? 1 + parents[j].start : parents[j].start;
                            parents[j].end = 1 + parents[j].end;
                        }
                    }
                }
            }
            this.restore(textArea, start, end + addedLength, e);
        };
        MDFormats.prototype.clearRegex = function () {
            var regex = '';
            var configKey = Object.keys(this.syntax);
            for (var j = 0; j < configKey.length && configKey[j] !== 'pre' && configKey[j] !== 'p'; j++) {
                regex += regex === '' ? '^(' + this.selection.replaceSpecialChar(this.syntax[configKey[j]].trim()) + ')' :
                    '|^(' + this.selection.replaceSpecialChar(this.syntax[configKey[j]].trim()) + ')';
            }
            return regex;
        };
        MDFormats.prototype.cleanFormat = function (textArea, command) {
            var parents = this.selection.getSelectedParentPoints(textArea);
            var start = textArea.selectionStart;
            var end = textArea.selectionEnd;
            var removeLength = 0;
            if (this.selection.isClear(parents, this.clearRegex())) {
                for (var i = 0; i < parents.length; i++) {
                    var configKey = Object.keys(this.syntax);
                    for (var j = 0; parents[i].text !== '' && j < configKey.length; j++) {
                        var removeText = this.syntax[configKey[j]];
                        if (configKey[j] === command) {
                            continue;
                        }
                        var regex = new RegExp('^(' + this.selection.replaceSpecialChar(removeText) + ')', 'gim');
                        if (regex.test(parents[i].text)) {
                            parents[i].text = parents[i].text.replace(regex, '');
                            textArea.value = textArea.value.substr(0, parents[i].start) + parents[i].text + '\n' +
                                textArea.value.substr(parents[i].end, textArea.value.length);
                            start = i === 0 ? (start - (removeText.length)) > 0 ? start - (removeText.length) : 0 : start;
                            removeLength += removeText.length;
                            if (parents.length !== 1) {
                                for (var k = 0; k < parents.length; k++) {
                                    parents[k].start = k !== 0 ?
                                        parents[k].start - removeText.length : parents[k].start;
                                    parents[k].end = parents[k].end - removeText.length;
                                }
                            }
                            break;
                        }
                    }
                    if (parents[i].text === '' && i === 0) {
                        this.selection.save(start, end);
                        if (parents.length !== 1) {
                            for (var j = i; j < parents.length; j++) {
                                parents[j].start = j !== 0 ? 1 + parents[j].start : parents[j].start;
                                parents[j].end = 1 + parents[j].end;
                            }
                        }
                    }
                }
                this.restore(textArea, start, end - removeLength);
            }
        };
        MDFormats.prototype.applyCodeBlock = function (textArea, event, parents) {
            var command = event.subCommand;
            var start = parents[0].start;
            var end = parents[parents.length - 1].end;
            var parentLines = this.selection.getAllParents(textArea.value);
            var firstPrevText = parentLines[parents[0].line - 1];
            var lastNextText = parentLines[(parents.length + 1) + 1];
            var addedLength = 0;
            if (!this.selection.isStartWith(firstPrevText, this.syntax.pre.split('\n')[0]) &&
                !this.selection.isStartWith(lastNextText, this.syntax.pre.split('\n')[0])) {
                var lines = textArea.value.substring(start, end).split('\n');
                var lastLine = lines[lines.length - 1] === '' ? '' : '\n';
                textArea.value = textArea.value.substr(0, start) + this.syntax["" + command] + textArea.value.substring(start, end) +
                    lastLine + this.syntax["" + command] +
                    textArea.value.substr(end, textArea.value.length);
                start = this.selection.selectionStart + this.syntax["" + command].length;
                end = this.selection.selectionEnd + this.syntax["" + command].length - 1;
            }
            else {
                var cmd = this.syntax["" + command];
                var selection = this.parent.markdownSelection.getSelectedInlinePoints(textArea);
                var startNo = textArea.value.substr(0, textArea.selectionStart).lastIndexOf(cmd);
                var endNo = textArea.value.substr(textArea.selectionEnd, textArea.selectionEnd).indexOf(cmd);
                endNo = endNo + selection.end;
                var repStartText = this.replaceAt(textArea.value.substr(0, selection.start), cmd, '', startNo, selection.start);
                var repEndText = this.replaceAt(textArea.value.substr(selection.end, textArea.value.length), cmd, '', 0, endNo);
                textArea.value = repStartText + selection.text + repEndText;
                start = this.selection.selectionStart - cmd.length;
                end = this.selection.selectionEnd - cmd.length;
            }
            this.restore(textArea, start, end, event);
        };
        MDFormats.prototype.replaceAt = function (input, search, replace, start, end) {
            return input.slice(0, start)
                + input.slice(start, end).replace(search, replace)
                + input.slice(end);
        };
        MDFormats.prototype.restore = function (textArea, start, end, event) {
            this.selection.save(start, end);
            this.selection.restore(textArea);
            if (event && event.callBack) {
                event.callBack({
                    requestType: event.subCommand,
                    selectedText: this.selection.getSelectedText(textArea),
                    editorMode: 'Markdown',
                    event: event.event
                });
            }
        };
        MDFormats.prototype.isAppliedFormat = function (lines, documentNode) {
            var format = 'p';
            var configKey = Object.keys(this.syntax);
            var keys = Object.keys(this.syntax);
            var direction = this.parent.element.selectionDirection;
            var checkLine = direction === 'backward' ? lines[0].text : lines[lines.length - 1].text;
            for (var i = 0; !documentNode && i < keys.length; i++) {
                if (keys[i] !== 'pre' && this.selection.isStartWith(checkLine, this.syntax[keys[i]])) {
                    format = keys[i];
                    break;
                }
                else if (keys[i] === 'pre') {
                    var parentLines = this.selection.getAllParents(this.parent.element.value);
                    var firstPrevText = parentLines[lines[0].line - 1];
                    var lastNextText = parentLines[lines.length + 1];
                    if (this.selection.isStartWith(firstPrevText, this.syntax[keys[i]].split('\n')[0]) &&
                        this.selection.isStartWith(lastNextText, this.syntax[keys[i]].split('\n')[0])) {
                        format = keys[i];
                        break;
                    }
                }
            }
            return format;
        };
        MDFormats.prototype.destroy = function () {
            this.removeEventListener();
        };
        return MDFormats;
    }());
    exports.MDFormats = MDFormats;
});