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

100% Statements 73/73
96.3% Branches 26/27
100% Functions 12/12
100% Lines 73/73
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   151× 151× 151×   151× 151×   70× 70×   24×             24× 24× 24× 24× 24× 24× 24×   21×     18× 18×   24× 24×           11× 11× 11× 16× 32× 32× 32× 176× 176× 160×         16×   11×                     70×        
define(["require", "exports", "./../base/constant", "./../../common/constant"], function (require, exports, CONSTANT, EVENTS) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ClearFormat = (function () {
        function ClearFormat(parent) {
            this.parent = parent;
            this.selection = this.parent.markdownSelection;
            this.addEventListener();
        }
        ClearFormat.prototype.addEventListener = function () {
            this.parent.observer.on(CONSTANT.CLEAR_COMMAND, this.clear, this);
            this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
        };
        ClearFormat.prototype.removeEventListener = function () {
            this.parent.observer.off(CONSTANT.CLEAR_COMMAND, this.clear);
            this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
        };
        ClearFormat.prototype.replaceRegex = function (data) {
            return data.replace(/\*/ig, '\\*').replace(/\&/ig, '\\&')
                .replace(/\-/ig, '\\-').replace(/\^/ig, '\\^')
                .replace(/\$/ig, '\\$').replace(/\./ig, '\\.')
                .replace(/\|/ig, '\\|').replace(/\?/ig, '\\?')
                .replace(/\+/ig, '\\+').replace(/\-/ig, '\\-')
                .replace(/\&/ig, '\\&');
        };
        ClearFormat.prototype.clearSelectionTags = function (text) {
            var data = this.parent.selectionTags;
            var keys = Object.keys(data);
            for (var num = 0; num < keys.length; num++) {
                var key = keys[num];
                Eif (Object.prototype.hasOwnProperty.call(data, key) && data["" + key] !== '') {
                    var expString = this.replaceRegex(data["" + key]);
                    var regExp = void 0;
                    var startExp = data["" + key].length;
                    var endExp = (data["" + key] === '<sup>' || data["" + key] === '<sub>') ? data["" + key].length + 1 : data["" + key].length;
                    if (data["" + key] === '<sup>') {
                        regExp = new RegExp('<sup>(.*?)<\/sup>', 'ig');
                    }
                    else if (data["" + key] === '<sub>') {
                        regExp = new RegExp('<sub>(.*?)<\/sub>', 'ig');
                    }
                    else {
                        var regExpr = RegExp;
                        regExp = new regExpr(expString + '(.*?)' + expString, 'ig');
                    }
                    var val = text.match(regExp);
                    for (var index = 0; val && index < val.length && val[index] !== ''; index++) {
                        text = text.replace(val[index], val[index].substr(startExp, val[index].length - endExp - startExp));
                    }
                }
            }
            return text;
        };
        ClearFormat.prototype.clearFormatTags = function (text) {
            var lines = text.split('\n');
            return this.clearFormatLines(lines);
        };
        ClearFormat.prototype.clearFormatLines = function (lines) {
            var tags = [this.parent.formatTags, this.parent.listTags];
            var str = '';
            for (var len = 0; len < lines.length; len++) {
                for (var num = 0; num < tags.length; num++) {
                    var data = tags[num];
                    var keys = Object.keys(data);
                    for (var index = 0; index < keys.length; index++) {
                        var key = keys[index];
                        if (Object.prototype.hasOwnProperty.call(data, key) && data["" + key] !== '') {
                            if (lines[len].indexOf(data["" + key]) === 0) {
                                lines[len] = lines[len].replace(data["" + key], '');
                                lines[len] = this.clearFormatLines([lines[len]]);
                            }
                        }
                    }
                }
                str = str + lines[len] + ((len !== lines.length - 1) ? '\n' : '');
            }
            return str;
        };
        ClearFormat.prototype.clear = function (e) {
            var textArea = this.parent.element;
            textArea.focus();
            var start = textArea.selectionStart;
            var end = textArea.selectionEnd;
            var text = this.selection.getSelectedText(textArea);
            text = this.clearSelectionTags(text);
            text = this.clearFormatTags(text);
            textArea.value = textArea.value.substr(0, start)
                + text + textArea.value.substr(end, textArea.value.length);
            this.parent.markdownSelection.setSelection(textArea, start, start + text.length);
            this.restore(textArea, start, start + text.length, e);
        };
        ClearFormat.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
                });
            }
        };
        ClearFormat.prototype.destroy = function () {
            this.removeEventListener();
        };
        return ClearFormat;
    }());
    exports.ClearFormat = ClearFormat;
});