all files / rich-text-editor/actions/ markdown-toolbar-status.js

98.77% Statements 80/81
94.92% Branches 56/59
100% Functions 14/14
98.73% Lines 78/79
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   70× 70× 70× 70× 70×   70× 70×   70× 70×   108× 108×                         108×   108× 108× 108× 105× 105×     200× 200× 100× 107× 107× 96× 96×         100× 113× 94× 94×       200×   108× 108× 108× 108× 108× 806× 75× 75×   731× 82× 19× 19×       108×   82× 82× 82× 82× 82× 82× 82× 82× 17×   65×         65×       65×   82×   67×          
define(["require", "exports", "../../markdown-parser/plugin/markdown-selection", "../base/constant", "../../common/util"], function (require, exports, markdown_selection_1, events, util_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MarkdownToolbarStatus = (function () {
        function MarkdownToolbarStatus(parent) {
            this.toolbarStatus = this.prevToolbarStatus = util_1.getDefaultMDTbStatus();
            this.selection = new markdown_selection_1.MarkdownSelection();
            this.parent = parent;
            this.element = this.parent.contentModule.getEditPanel();
            this.addEventListener();
        }
        MarkdownToolbarStatus.prototype.addEventListener = function () {
            this.parent.on(events.toolbarRefresh, this.onRefreshHandler, this);
            this.parent.on(events.destroy, this.removeEventListener, this);
        };
        MarkdownToolbarStatus.prototype.removeEventListener = function () {
            this.parent.off(events.toolbarRefresh, this.onRefreshHandler);
            this.parent.off(events.destroy, this.removeEventListener);
        };
        MarkdownToolbarStatus.prototype.onRefreshHandler = function (args) {
            var parentsLines = this.selection.getSelectedParentPoints(this.element);
            this.toolbarStatus = {
                orderedlist: args.documentNode ? false : this.isListsApplied(parentsLines, 'OL'),
                unorderedlist: args.documentNode ? false : this.isListsApplied(parentsLines, 'UL'),
                formats: this.currentFormat(parentsLines, args.documentNode),
                bold: args.documentNode ? false : this.parent.formatter.editorManager.mdSelectionFormats.isAppliedCommand('Bold'),
                italic: args.documentNode ? false : this.parent.formatter.editorManager.mdSelectionFormats.isAppliedCommand('Italic'),
                inlinecode: args.documentNode ? false : this.parent.formatter.editorManager.mdSelectionFormats.isAppliedCommand('InlineCode'),
                strikethrough: args.documentNode ? false :
                    this.parent.formatter.editorManager.mdSelectionFormats.isAppliedCommand('StrikeThrough'),
                subscript: args.documentNode ? false : this.parent.formatter.editorManager.mdSelectionFormats.isAppliedCommand('SubScript'),
                superscript: args.documentNode ? false : this.parent.formatter.editorManager.mdSelectionFormats.isAppliedCommand('SuperScript'),
                uppercase: args.documentNode ? false : this.parent.formatter.editorManager.mdSelectionFormats.isAppliedCommand('UpperCase')
            };
            if (this.parent.formatter.editorManager.mdSelectionFormats.isAppliedCommand('InlineCode')) {
                this.toolbarStatus.formats = 'pre';
            }
            var tbStatusString = JSON.stringify(this.toolbarStatus);
            this.parent.notify(events.toolbarUpdated, this.toolbarStatus);
            if (JSON.stringify(this.prevToolbarStatus) !== tbStatusString) {
                this.parent.notify(events.updateTbItemsStatus, { html: null, markdown: JSON.parse(tbStatusString) });
                this.prevToolbarStatus = JSON.parse(tbStatusString);
            }
        };
        MarkdownToolbarStatus.prototype.isListsApplied = function (lines, type) {
            var isApply = true;
            if (type === 'OL') {
                for (var i = 0; i < lines.length; i++) {
                    var lineSplit = lines[i].text.trim().split(' ', 2)[0] + ' ';
                    if (!/^[\d.]+[ ]+$/.test(lineSplit)) {
                        isApply = false;
                        break;
                    }
                }
            }
            else {
                for (var i = 0; i < lines.length; i++) {
                    if (!this.selection.isStartWith(lines[i].text, this.parent.formatter.listTags["" + type])) {
                        isApply = false;
                        break;
                    }
                }
            }
            return isApply;
        };
        MarkdownToolbarStatus.prototype.currentFormat = function (lines, documentNode) {
            var format = 'p';
            var keys = Object.keys(this.parent.formatter.formatTags);
            var direction = this.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.parent.formatter.formatTags[keys[i]])) {
                    format = keys[i];
                    break;
                }
                else if (keys[i] === 'pre') {
                    if (this.codeFormat()) {
                        format = keys[i];
                        break;
                    }
                }
            }
            return format;
        };
        MarkdownToolbarStatus.prototype.codeFormat = function () {
            var isFormat = false;
            var textArea = this.parent.inputElement;
            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 cmdPre = this.parent.formatter.formatTags.pre;
            var selectedText = this.getSelectedText(textArea);
            if (selectedText !== '' && selectedText === selectedText.toLocaleUpperCase()) {
                return true;
            }
            else if (selectedText === '') {
                var beforeText = textArea.value.substr(splitText[0].length - 1, 1);
                var afterText = splitText[1].substr(0, 1);
                Iif ((beforeText !== '' && afterText !== '' && beforeText.match(/[a-z]/i)) &&
                    beforeText === beforeText.toLocaleUpperCase() && afterText === afterText.toLocaleUpperCase()) {
                    return true;
                }
            }
            if ((this.isCode(splitText[0], cmdPre) && this.isCode(splitText[1], cmdPre)) &&
                (splitText[0].match(this.multiCharRegx(cmdPre)).length % 2 === 1 &&
                    splitText[1].match(this.multiCharRegx(cmdPre)).length % 2 === 1)) {
                isFormat = true;
            }
            return isFormat;
        };
        MarkdownToolbarStatus.prototype.getSelectedText = function (textarea) {
            return textarea.value.substring(textarea.selectionStart, textarea.selectionEnd);
        };
        MarkdownToolbarStatus.prototype.isCode = function (text, cmd) {
            return text.search('\\' + cmd + '') !== -1;
        };
        MarkdownToolbarStatus.prototype.multiCharRegx = function (cmd) {
            var regExp = RegExp;
            return new regExp('(\\' + cmd + ')', 'g');
        };
        return MarkdownToolbarStatus;
    }());
    exports.MarkdownToolbarStatus = MarkdownToolbarStatus;
});