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

96.62% Statements 200/207
96.05% Branches 170/177
100% Functions 20/20
96.59% Lines 198/205
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 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336   151× 151× 151×   151× 151× 151×   70× 70× 70×   30×                   4347×   878×   4648× 4648×   1522× 1522×   1463× 1463×   1663× 1663×   4648×   892× 892×   4733× 4733×   894× 894× 894× 894× 894× 894× 894× 894× 894× 894× 19×   875× 149× 149× 149×       874×   424×         424×         424×       424×         874×     22×   874× 874× 874×       25×   874×       15×   874×         874×     874×     874×   78× 78× 78× 78× 78× 78× 78× 78×     70× 60×   60× 60× 60×                           60×     53×       47×   60×   60×     10×                       70×   16×       78× 78× 78× 73×                                     67× 67× 67×         31× 31×       17× 17× 17×     14×   17×         70×        
define(["require", "exports", "@syncfusion/ej2-base", "./../base/constant", "@syncfusion/ej2-base", "./../../common/constant"], function (require, exports, ej2_base_1, CONSTANT, ej2_base_2, EVENTS) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MDSelectionFormats = (function () {
        function MDSelectionFormats(parent) {
            ej2_base_2.extend(this, this, parent, true);
            this.selection = this.parent.markdownSelection;
            this.addEventListener();
        }
        MDSelectionFormats.prototype.addEventListener = function () {
            this.parent.observer.on(CONSTANT.selectionCommand, this.applyCommands, this);
            this.parent.observer.on(EVENTS.KEY_DOWN_HANDLER, this.keyDownHandler, this);
            this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
        };
        MDSelectionFormats.prototype.removeEventListener = function () {
            this.parent.observer.off(CONSTANT.selectionCommand, this.applyCommands);
            this.parent.observer.off(EVENTS.KEY_DOWN_HANDLER, this.keyDownHandler);
            this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
        };
        MDSelectionFormats.prototype.keyDownHandler = function (e) {
            switch (e.event.action) {
                case 'bold':
                    this.applyCommands({ subCommand: 'Bold', callBack: e.callBack });
                    e.event.preventDefault();
                    break;
                case 'italic':
                    this.applyCommands({ subCommand: 'Italic', callBack: e.callBack });
                    e.event.preventDefault();
                    break;
                case 'strikethrough':
                    this.applyCommands({ subCommand: 'StrikeThrough', callBack: e.callBack });
                    e.event.preventDefault();
                    break;
                case 'uppercase':
                    this.applyCommands({ subCommand: 'UpperCase', callBack: e.callBack });
                    e.event.preventDefault();
                    break;
                case 'lowercase':
                    this.applyCommands({ subCommand: 'LowerCase', callBack: e.callBack });
                    e.event.preventDefault();
                    break;
                case 'superscript':
                    this.applyCommands({ subCommand: 'SuperScript', callBack: e.callBack });
                    e.event.preventDefault();
                    break;
                case 'subscript':
                    this.applyCommands({ subCommand: 'SubScript', callBack: e.callBack });
                    e.event.preventDefault();
                    break;
            }
        };
        MDSelectionFormats.prototype.isBold = function (text, cmd) {
            return text.search('\\' + cmd + '\\' + cmd + '') !== -1;
        };
        MDSelectionFormats.prototype.isItalic = function (text, cmd) {
            return text.search('\\' + cmd) !== -1;
        };
        MDSelectionFormats.prototype.isMatch = function (text, cmd) {
            var matchText = [''];
            switch (cmd) {
                case this.syntax.Italic:
                    matchText = text.match(this.singleCharRegx(cmd));
                    break;
                case this.syntax.InlineCode:
                    matchText = text.match(this.singleCharRegx(cmd));
                    break;
                case this.syntax.StrikeThrough:
                    matchText = text.match(this.singleCharRegx(cmd));
                    break;
            }
            return matchText;
        };
        MDSelectionFormats.prototype.multiCharRegx = function (cmd) {
            var regExp = RegExp;
            return new regExp('(\\' + cmd + '\\' + cmd + ')', 'g');
        };
        MDSelectionFormats.prototype.singleCharRegx = function (cmd) {
            var regExp = RegExp;
            return new regExp('(\\' + cmd + ')', 'g');
        };
        MDSelectionFormats.prototype.isAppliedCommand = function (cmd) {
            var selectCmd = '';
            var isFormat = false;
            var textArea = this.parent.element;
            var start = textArea.selectionStart;
            var splitAt = function (index) { return function (x) { return [x.slice(0, index), x.slice(index)]; }; };
            var splitText = splitAt(start)(textArea.value);
            var cmdB = this.syntax.Bold.substr(0, 1);
            var cmdI = this.syntax.Italic;
            var selectedText = this.parent.markdownSelection.getSelectedText(textArea);
            if (selectedText !== '' && selectedText === selectedText.toLocaleUpperCase() && cmd === 'UpperCase') {
                return true;
            }
            else if (selectedText === '') {
                var beforeText = textArea.value.substr(splitText[0].length - 1, 1);
                var afterText = splitText[1].substr(0, 1);
                if ((beforeText !== '' && afterText !== '' && beforeText.match(/[a-z]/i)) &&
                    beforeText === beforeText.toLocaleUpperCase() && afterText === afterText.toLocaleUpperCase() && cmd === 'UpperCase') {
                    return true;
                }
            }
            if (!(this.isBold(splitText[0], cmdB)) && !(this.isItalic(splitText[0], cmdI)) && !(this.isBold(splitText[1], cmdB)) &&
                !(this.isItalic(splitText[1], cmdI))) {
                if ((!ej2_base_1.isNullOrUndefined(this.isMatch(splitText[0], this.syntax.StrikeThrough)) &&
                    !ej2_base_1.isNullOrUndefined(this.isMatch(splitText[1], this.syntax.StrikeThrough))) &&
                    (this.isMatch(splitText[0], this.syntax.StrikeThrough).length % 2 === 1 &&
                        this.isMatch(splitText[1], this.syntax.StrikeThrough).length % 2 === 1) && cmd === 'StrikeThrough') {
                    isFormat = true;
                }
                if ((!ej2_base_1.isNullOrUndefined(this.isMatch(splitText[0], this.syntax.InlineCode)) &&
                    !ej2_base_1.isNullOrUndefined(this.isMatch(splitText[1], this.syntax.InlineCode))) &&
                    (this.isMatch(splitText[0], this.syntax.InlineCode).length % 2 === 1 &&
                        this.isMatch(splitText[1], this.syntax.InlineCode).length % 2 === 1) && cmd === 'InlineCode') {
                    isFormat = true;
                }
                if ((!ej2_base_1.isNullOrUndefined(splitText[0].match(/\<sub>/g)) && !ej2_base_1.isNullOrUndefined(splitText[1].match(/\<\/sub>/g))) &&
                    (splitText[0].match(/\<sub>/g).length % 2 === 1 &&
                        splitText[1].match(/\<\/sub>/g).length % 2 === 1) && cmd === 'SubScript') {
                    isFormat = true;
                }
                if ((!ej2_base_1.isNullOrUndefined(splitText[0].match(/\<sup>/g)) && !ej2_base_1.isNullOrUndefined(splitText[1].match(/\<\/sup>/g))) &&
                    (splitText[0].match(/\<sup>/g).length % 2 === 1 && splitText[1].match(/\<\/sup>/g).length % 2 === 1) &&
                    cmd === 'SuperScript') {
                    isFormat = true;
                }
            }
            if ((this.isBold(splitText[0], cmdB) && this.isBold(splitText[1], cmdB)) &&
                (splitText[0].match(this.multiCharRegx(cmdB)).length % 2 === 1 &&
                    splitText[1].match(this.multiCharRegx(cmdB)).length % 2 === 1) && cmd === 'Bold') {
                isFormat = true;
            }
            splitText[0] = this.isBold(splitText[0], cmdB) ? splitText[0].replace(this.multiCharRegx(cmdB), '$%@') : splitText[0];
            splitText[1] = this.isBold(splitText[1], cmdB) ? splitText[1].replace(this.multiCharRegx(cmdB), '$%@') : splitText[1];
            if ((!ej2_base_1.isNullOrUndefined(this.isMatch(splitText[0], this.syntax.Italic)) &&
                !ej2_base_1.isNullOrUndefined(this.isMatch(splitText[1], this.syntax.Italic))) &&
                (this.isMatch(splitText[0], this.syntax.Italic).length % 2 === 1 &&
                    this.isMatch(splitText[1], this.syntax.Italic).length % 2 === 1) && cmd === 'Italic') {
                isFormat = true;
            }
            if ((!ej2_base_1.isNullOrUndefined(this.isMatch(splitText[0], this.syntax.StrikeThrough)) &&
                !ej2_base_1.isNullOrUndefined(this.isMatch(splitText[1], this.syntax.StrikeThrough))) &&
                (this.isMatch(splitText[0], this.syntax.StrikeThrough).length % 2 === 1 &&
                    this.isMatch(splitText[1], this.syntax.StrikeThrough).length % 2 === 1) && cmd === 'StrikeThrough') {
                isFormat = true;
            }
            if ((!ej2_base_1.isNullOrUndefined(this.isMatch(splitText[0], this.syntax.InlineCode)) &&
                !ej2_base_1.isNullOrUndefined(this.isMatch(splitText[1], this.syntax.InlineCode))) &&
                (this.isMatch(splitText[0], this.syntax.InlineCode).length % 2 === 1 &&
                    this.isMatch(splitText[1], this.syntax.InlineCode).length % 2 === 1) && cmd === 'InlineCode') {
                isFormat = true;
            }
            if ((!ej2_base_1.isNullOrUndefined(splitText[0].match(/\<sub>/g)) && !ej2_base_1.isNullOrUndefined(splitText[1].match(/\<\/sub>/g))) &&
                (splitText[0].match(/\<sub>/g).length % 2 === 1 && splitText[1].match(/\<\/sub>/g).length % 2 === 1) && cmd === 'SubScript') {
                isFormat = true;
            }
            if ((!ej2_base_1.isNullOrUndefined(splitText[0].match(/\<sup>/g)) && !ej2_base_1.isNullOrUndefined(splitText[1].match(/\<\/sup>/g))) &&
                (splitText[0].match(/\<sup>/g).length % 2 === 1 && splitText[1].match(/\<\/sup>/g).length % 2 === 1) && cmd === 'SuperScript') {
                isFormat = true;
            }
            return isFormat;
        };
        MDSelectionFormats.prototype.applyCommands = function (e) {
            this.currentAction = e.subCommand;
            var textArea = this.parent.element;
            this.selection.save(textArea.selectionStart, textArea.selectionEnd);
            var start = textArea.selectionStart;
            var end = textArea.selectionEnd;
            var addedLength = 0;
            var selection = this.parent.markdownSelection.getSelectedInlinePoints(textArea);
            if (this.isAppliedCommand(e.subCommand) && selection.text !== '') {
                var startCmd = this.syntax[e.subCommand];
                var endCmd = e.subCommand === 'SubScript' ? '</sub>' :
                    e.subCommand === 'SuperScript' ? '</sup>' : this.syntax[e.subCommand];
                var startLength = (e.subCommand === 'UpperCase' || e.subCommand === 'LowerCase') ? 0 : startCmd.length;
                var startNo = textArea.value.substr(0, selection.start).lastIndexOf(startCmd);
                var endNo = textArea.value.substr(selection.end, textArea.value.length).indexOf(endCmd);
                endNo = endNo + selection.end;
                var repStartText = this.replaceAt(textArea.value.substr(0, selection.start), startCmd, '', startNo, selection.start);
                var repEndText = this.replaceAt(textArea.value.substr(selection.end, textArea.value.length), endCmd, '', 0, endNo);
                textArea.value = repStartText + selection.text + repEndText;
                this.restore(textArea, start - startLength, end - startLength, e);
                return;
            }
            if (selection.text !== '' && !this.isApplied(selection, e.subCommand)) {
                addedLength = (e.subCommand === 'UpperCase' || e.subCommand === 'LowerCase') ? 0 :
                    this.syntax[e.subCommand].length;
                var repStart = textArea.value.substr(selection.start - this.syntax[e.subCommand].length, this.syntax[e.subCommand].length);
                var repEnd = void 0;
                Iif ((repStart === e.subCommand) || ((selection.start - this.syntax[e.subCommand].length ===
                    textArea.value.indexOf(this.syntax[e.subCommand])) && (selection.end === textArea.value.lastIndexOf(this.syntax[e.subCommand]) || selection.end === textArea.value.lastIndexOf('</' + this.syntax[e.subCommand].substring(1, 5))))) {
                    if (e.subCommand === 'SubScript' || e.subCommand === 'SuperScript') {
                        repEnd = textArea.value.substr(selection.end, this.syntax[e.subCommand].length + 1);
                    }
                    else {
                        repEnd = textArea.value.substr(selection.end, this.syntax[e.subCommand].length);
                    }
                    var repStartText = this.replaceAt(textArea.value.substr(0, selection.start), repStart, '', selection.start - this.syntax[e.subCommand].length, selection.start);
                    var repEndText = this.replaceAt(textArea.value.substr(selection.end, textArea.value.length), repEnd, '', 0, repEnd.length);
                    textArea.value = repStartText + selection.text + repEndText;
                    this.restore(textArea, start - addedLength, end - addedLength, e);
                }
                else {
                    if (e.subCommand === 'SubScript' || e.subCommand === 'SuperScript') {
                        selection.text = this.syntax[e.subCommand] + selection.text
                            + '</' + this.syntax[e.subCommand].substring(1, 5);
                    }
                    else if (e.subCommand === 'UpperCase' || e.subCommand === 'LowerCase') {
                        selection.text = (e.subCommand === 'UpperCase') ? selection.text.toUpperCase()
                            : selection.text.toLowerCase();
                    }
                    else {
                        selection.text = this.syntax[e.subCommand] + selection.text + this.syntax[e.subCommand];
                    }
                    textArea.value = textArea.value.substr(0, selection.start) + selection.text +
                        textArea.value.substr(selection.end, textArea.value.length);
                    this.restore(textArea, start + addedLength, end + addedLength, e);
                }
            }
            else if (e.subCommand !== 'UpperCase' && e.subCommand !== 'LowerCase') {
                if (e.subCommand === 'SubScript' || e.subCommand === 'SuperScript') {
                    selection.text = this.textReplace(selection.text, e.subCommand);
                    selection.text = this.syntax[e.subCommand] + selection.text
                        + '</' + this.syntax[e.subCommand].substring(1, 5);
                }
                else {
                    selection.text = this.textReplace(selection.text, e.subCommand);
                    selection.text = this.syntax[e.subCommand] + selection.text + this.syntax[e.subCommand];
                }
                textArea.value = textArea.value.substr(0, selection.start)
                    + selection.text + textArea.value.substr(selection.end, textArea.value.length);
                addedLength = this.syntax[e.subCommand].length;
                if (selection.start === selection.end) {
                    this.restore(textArea, start + addedLength, end + addedLength, e);
                }
                else {
                    this.restore(textArea, start + addedLength, end - addedLength, e);
                }
            }
            else {
                this.restore(textArea, start, end, e);
            }
            this.parent.undoRedoManager.saveData();
        };
        MDSelectionFormats.prototype.replaceAt = function (input, search, replace, start, end) {
            return input.slice(0, start)
                + input.slice(start, end).replace(search, replace)
                + input.slice(end);
        };
        MDSelectionFormats.prototype.restore = function (textArea, start, end, event) {
            this.selection.save(start, end);
            this.selection.restore(textArea);
            if (event && event.callBack) {
                event.callBack({
                    requestType: this.currentAction,
                    selectedText: this.selection.getSelectedText(textArea),
                    editorMode: 'Markdown',
                    event: event.event
                });
            }
        };
        MDSelectionFormats.prototype.textReplace = function (text, command) {
            var regx = this.singleCharRegx(this.syntax["" + command]);
            switch (command) {
                case 'Bold':
                    regx = this.multiCharRegx(this.syntax["" + command].substr(0, 1));
                    text = text.replace(regx, '');
                    break;
                case 'Italic':
                    if (!this.isBold(text, this.syntax["" + command].substr(0, 1))) {
                        text = text.replace(regx, '');
                    }
                    else {
                        var regxB = this.multiCharRegx(this.syntax["" + command].substr(0, 1));
                        var repText = text;
                        repText = repText.replace(regxB, '$%@').replace(regx, '');
                        var regxTemp = new RegExp('\\$%@', 'g');
                        text = repText.replace(regxTemp, this.syntax["" + command].substr(0, 1) + this.syntax["" + command].substr(0, 1));
                    }
                    break;
                case 'StrikeThrough':
                    text = text.replace(regx, '');
                    break;
                case 'InlineCode':
                    text = text.replace(regx, '');
                    break;
                case 'SubScript':
                    text = text.replace(/<sub>/g, '').replace(/<\/sub>/g, '');
                    break;
                case 'SuperScript':
                    text = text.replace(/<sup>/g, '').replace(/<\/sup>/g, '');
                    break;
            }
            return text;
        };
        MDSelectionFormats.prototype.isApplied = function (line, command) {
            var regx = this.singleCharRegx(this.syntax["" + command]);
            var regExp;
            switch (command) {
                case 'SubScript':
                case 'SuperScript':
                    regx = this.singleCharRegx(this.syntax["" + command]);
                    return regx.test(line.text);
                case 'Bold':
                case 'StrikeThrough':
                    regx = this.multiCharRegx(this.syntax["" + command].substr(0, 1));
                    return regx.test(line.text);
                case 'UpperCase':
                case 'LowerCase':
                    regExp = RegExp;
                    regx = new regExp('^[' + this.syntax["" + command] + ']*$', 'g');
                    return regx.test(line.text);
                case 'Italic': {
                    var regTest = void 0;
                    var regxB = this.multiCharRegx(this.syntax["" + command].substr(0, 1));
                    if (regxB.test(line.text)) {
                        var repText = line.text;
                        repText = repText.replace(regxB, '$%#');
                        regTest = regx.test(repText);
                    }
                    else {
                        regTest = regx.test(line.text);
                    }
                    return regTest;
                }
                case 'InlineCode':
                    return regx.test(line.text);
            }
        };
        MDSelectionFormats.prototype.destroy = function () {
            this.removeEventListener();
        };
        return MDSelectionFormats;
    }());
    exports.MDSelectionFormats = MDSelectionFormats;
});