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

100% Statements 147/147
97.96% Branches 48/49
100% Functions 26/26
100% Lines 147/147
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   155× 155× 155×   155× 155× 155×   74× 74× 74×   74×   33×     26× 26× 26× 26× 26× 26× 26× 26×   26× 26× 26× 26× 26×   26× 26× 52× 52× 104× 52×     52×     52×   26×   26× 26× 52× 52× 104×   52×   26× 26×   176× 176× 176×   26× 26× 26× 26× 26×     23× 18×         26×   21× 21× 21×   49× 49× 49× 49× 441×   49× 98×   49×   23× 23× 23× 124× 18×       18× 18× 16×     14×   14× 12×     13×     13×     18×   13×               13×     104× 104× 104× 104× 104× 104×   104×   26× 26× 26× 23× 23×     26×   26×   13×   52× 52× 52× 26×   26×                    
define(["require", "exports", "./../base/constant", "@syncfusion/ej2-base", "./../../common/constant"], function (require, exports, CONSTANT, ej2_base_1, EVENTS) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MDTable = (function () {
        function MDTable(options) {
            ej2_base_1.extend(this, this, options, true);
            this.selection = this.parent.markdownSelection;
            this.addEventListener();
        }
        MDTable.prototype.addEventListener = function () {
            this.parent.observer.on(CONSTANT.MD_TABLE, this.createTable, this);
            this.parent.observer.on(EVENTS.KEY_DOWN_HANDLER, this.onKeyDown, this);
            this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
        };
        MDTable.prototype.removeEventListener = function () {
            this.parent.observer.off(CONSTANT.MD_TABLE, this.createTable);
            this.parent.observer.off(EVENTS.KEY_DOWN_HANDLER, this.onKeyDown);
            this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
        };
        MDTable.prototype.destroy = function () {
            this.removeEventListener();
        };
        MDTable.prototype.onKeyDown = function (e) {
            if (e.event.action === 'insert-table') {
                e.item = e.value;
                this.createTable(e);
            }
        };
        MDTable.prototype.createTable = function (e) {
            this.element = this.parent.element;
            var start = this.element.selectionStart;
            var end = this.element.selectionEnd;
            var textAreaInitial = this.element.value;
            this.locale = e;
            this.selection.save(start, end);
            this.restore(this.element.selectionStart, this.element.selectionEnd, null);
            this.insertTable(start, end, textAreaInitial, e);
        };
        MDTable.prototype.getTable = function () {
            var table = '';
            table += this.textNonEmpty();
            table += this.tableHeader(this.locale);
            table += this.tableCell(this.locale);
            return table;
        };
        MDTable.prototype.tableHeader = function (e) {
            var text = '';
            for (var i = 1; i <= 2; i++) {
                text += '|';
                for (var j = 1; j <= 2; j++) {
                    if (i === 1) {
                        text += e.item.headingText + ' ' + j + '|';
                    }
                    else {
                        text += '---------|';
                    }
                }
                text += this.insertLine();
            }
            return text;
        };
        MDTable.prototype.tableCell = function (e) {
            var text = '';
            for (var i = 1; i <= 2; i++) {
                text += '|';
                for (var j = 1; j <= 2; j++) {
                    text += e.item.colText + ' ' + this.convertToLetters(i) + j + '|';
                }
                text += this.insertLine();
            }
            text += this.insertLine();
            return text;
        };
        MDTable.prototype.insertLine = function () {
            var dummyElement = document.createElement('div');
            dummyElement.innerHTML = '\n';
            return dummyElement.textContent;
        };
        MDTable.prototype.insertTable = function (start, end, textAreaInitial, e) {
            var parentText = this.selection.getSelectedParentPoints(this.element);
            var lastLineSplit = parentText[parentText.length - 1].text.split(' ', 2);
            var syntaxArr = this.getFormatTag();
            var syntaxCount = 0;
            if (lastLineSplit.length < 2) {
                this.element.value = this.updateValue(this.getTable());
                this.makeSelection(textAreaInitial, start, end);
            }
            else {
                if (this.ensureFormatApply(parentText[parentText.length - 1].text)) {
                    this.checkValid(start, end, this.getTable(), textAreaInitial, e, lastLineSplit, parentText, syntaxArr);
                }
                else {
                    this.element.value = this.updateValue(this.getTable());
                    this.makeSelection(textAreaInitial, start, end);
                }
            }
            this.restore(this.element.selectionStart, this.element.selectionEnd, e);
        };
        MDTable.prototype.makeSelection = function (textAreaInitial, start, end) {
            end = start + (textAreaInitial.length > 0 ? 12 : 10);
            start += textAreaInitial.length > 0 ? 3 : 1;
            this.selection.setSelection(this.element, start, end);
        };
        MDTable.prototype.getFormatTag = function () {
            var syntaxFormatKey = Object.keys(this.syntaxTag.Formats);
            var syntaxListKey = Object.keys(this.syntaxTag.List);
            var syntaxArr = [];
            for (var i = 0; i < syntaxFormatKey.length; i++) {
                syntaxArr.push(this.syntaxTag.Formats[syntaxFormatKey[i]]);
            }
            for (var j = 0; j < syntaxListKey.length; j++) {
                syntaxArr.push(this.syntaxTag.List[syntaxListKey[j]]);
            }
            return syntaxArr;
        };
        MDTable.prototype.ensureFormatApply = function (line) {
            var formatTags = this.getFormatTag();
            var formatSplitZero = line.trim().split(' ', 2)[0] + ' ';
            for (var i = 0; i < formatTags.length; i++) {
                if (formatSplitZero === formatTags[i] || /^[\d.]+[ ]+$/.test(formatSplitZero)) {
                    return true;
                }
            }
            return false;
        };
        MDTable.prototype.ensureStartValid = function (firstLine, parentText) {
            var firstLineSplit = parentText[0].text.split(' ', 2);
            for (var i = firstLine + 1; i <= firstLine + firstLineSplit[0].length - 1; i++) {
                if (this.element.selectionStart === i || this.element.selectionEnd === i) {
                    return false;
                }
            }
            return true;
        };
        MDTable.prototype.ensureEndValid = function (lastLine, formatSplitLength) {
            for (var i = lastLine + 1; i <= lastLine + formatSplitLength - 1; i++) {
                if (this.element.selectionEnd === i) {
                    return false;
                }
            }
            return true;
        };
        MDTable.prototype.updateValueWithFormat = function (formatSplit, text) {
            var textApplyFormat = this.element.value.substring(this.element.selectionEnd, this.element.value.length);
            text += textApplyFormat.replace(textApplyFormat, (formatSplit[0] + ' ' + textApplyFormat));
            return this.element.value.substr(0, this.element.selectionStart) + text;
        };
        MDTable.prototype.updateValue = function (text) {
            return this.element.value.substr(0, this.element.selectionStart) + text +
                this.element.value.substr(this.element.selectionEnd, this.element.value.length);
        };
        MDTable.prototype.checkValid = function (start, end, text, textAreaInitial, e, formatSplit, parentText, syntaxArr) {
            if (this.ensureStartValid(parentText[0].start, parentText) &&
                this.ensureEndValid(parentText[parentText.length - 1].start, formatSplit[0].length)) {
                if (start === parentText[0].start) {
                    if (start !== end && end !== (parentText[parentText.length - 1].end - 1)) {
                        this.element.value = this.updateValueWithFormat(formatSplit, text);
                    }
                    else {
                        this.element.value = this.updateValue(text);
                    }
                }
                else if (end === parentText[parentText.length - 1].end - 1) {
                    this.element.value = this.updateValue(text);
                }
                else {
                    this.element.value = this.updateValueWithFormat(formatSplit, text);
                }
                this.makeSelection(textAreaInitial, start, end);
            }
        };
        MDTable.prototype.convertToLetters = function (rowNumber) {
            var baseChar = ('A').charCodeAt(0);
            var letters = '';
            do {
                rowNumber -= 1;
                letters = String.fromCharCode(baseChar + (rowNumber % 26)) + letters;
                rowNumber = (rowNumber / 26) >> 0;
            } while (rowNumber > 0);
            return letters;
        };
        MDTable.prototype.textNonEmpty = function () {
            var emptyText = '';
            Eif (this.isCursorBased() || this.isSelectionBased()) {
                if (this.element.value.length > 0) {
                    emptyText += this.insertLine();
                    emptyText += this.insertLine();
                }
            }
            return emptyText;
        };
        MDTable.prototype.isCursorBased = function () {
            return this.element.selectionStart === this.element.selectionEnd;
        };
        MDTable.prototype.isSelectionBased = function () {
            return this.element.selectionStart !== this.element.selectionEnd;
        };
        MDTable.prototype.restore = function (start, end, event) {
            this.selection.save(start, end);
            this.selection.restore(this.element);
            if (event && event.callBack) {
                if (ej2_base_1.isNullOrUndefined(event.subCommand) && 'action' in event.event && event.event.action === 'insert-table') {
                    event.subCommand = 'CreateTable';
                }
                event.callBack({
                    requestType: event.subCommand,
                    selectedText: this.selection.getSelectedText(this.element),
                    editorMode: 'Markdown',
                    event: event.event
                });
            }
        };
        return MDTable;
    }());
    exports.MDTable = MDTable;
});