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

100% Statements 79/79
100% Branches 12/12
100% Functions 16/16
100% Lines 79/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     866×   1604× 1604× 1604×   718×   72× 72× 72×   156× 156×   393× 393× 393× 393× 393× 393× 393× 393× 393× 393× 393×   393× 393× 393× 144× 302× 302× 302× 302× 302× 302×   144× 144× 144× 144× 144× 144×   393×   667× 667×   1131× 1131×   576×   1617× 1617× 1617× 1482×     1482×   1617×   2477×   63× 63× 63× 86× 31×     32×   83× 83× 83× 83×        
define(["require", "exports"], function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MarkdownSelection = (function () {
        function MarkdownSelection() {
        }
        MarkdownSelection.prototype.getLineNumber = function (textarea, point) {
            return textarea.value.substr(0, point).split('\n').length;
        };
        MarkdownSelection.prototype.getSelectedText = function (textarea) {
            var start = textarea.selectionStart;
            var end = textarea.selectionEnd;
            return textarea.value.substring(start, end);
        };
        MarkdownSelection.prototype.getAllParents = function (value) {
            return value.split('\n');
        };
        MarkdownSelection.prototype.getSelectedLine = function (textarea) {
            var lines = this.getAllParents(textarea.value);
            var index = this.getLineNumber(textarea, textarea.selectionStart);
            return lines[index - 1];
        };
        MarkdownSelection.prototype.getLine = function (textarea, index) {
            var lines = this.getAllParents(textarea.value);
            return lines[index];
        };
        MarkdownSelection.prototype.getSelectedParentPoints = function (textarea) {
            var lines = this.getAllParents(textarea.value);
            var start = this.getLineNumber(textarea, textarea.selectionStart);
            var end = this.getLineNumber(textarea, textarea.selectionEnd);
            var parents = this.getSelectedText(textarea).split('\n');
            var selectedPoints = [];
            var selectedLine = lines[start - 1];
            var startLength = lines.slice(0, start - 1).join('').length;
            var firstPoint = {};
            firstPoint.line = start - 1;
            firstPoint.start = startLength + firstPoint.line;
            firstPoint.end = selectedLine !== '' ? firstPoint.start +
                selectedLine.length + 1 : firstPoint.start + selectedLine.length;
            firstPoint.text = selectedLine;
            selectedPoints.push(firstPoint);
            if (parents.length > 1) {
                for (var i = 1; i < parents.length - 1; i++) {
                    var points = {};
                    points.line = selectedPoints[i - 1].line + 1;
                    points.start = parents[i] !== '' ? selectedPoints[i - 1].end : selectedPoints[i - 1].end;
                    points.end = points.start + parents[i].length + 1;
                    points.text = parents[i];
                    selectedPoints.push(points);
                }
                var lastPoint = {};
                lastPoint.line = selectedPoints[selectedPoints.length - 1].line + 1;
                lastPoint.start = selectedPoints[selectedPoints.length - 1].end;
                lastPoint.end = lastPoint.start + lines[end - 1].length + 1;
                lastPoint.text = lines[end - 1];
                selectedPoints.push(lastPoint);
            }
            return selectedPoints;
        };
        MarkdownSelection.prototype.setSelection = function (textarea, start, end) {
            textarea.setSelectionRange(start, end);
            textarea.focus();
        };
        MarkdownSelection.prototype.save = function (start, end) {
            this.selectionStart = start;
            this.selectionEnd = end;
        };
        MarkdownSelection.prototype.restore = function (textArea) {
            this.setSelection(textArea, this.selectionStart, this.selectionEnd);
        };
        MarkdownSelection.prototype.isStartWith = function (line, command) {
            var isStart = false;
            var regExp = RegExp;
            if (line) {
                var reg = line.trim() === command.trim() ?
                    new regExp('^(' + this.replaceSpecialChar(command.trim()) + ')', 'gim') :
                    new regExp('^(' + this.replaceSpecialChar(command) + ')', 'gim');
                isStart = reg.test(line.trim());
            }
            return isStart;
        };
        MarkdownSelection.prototype.replaceSpecialChar = function (value) {
            return value.replace(/[`~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/g, '\\$&');
        };
        MarkdownSelection.prototype.isClear = function (parents, regex) {
            var isClear = false;
            var regExp = RegExp;
            for (var i = 0; i < parents.length; i++) {
                if (new regExp(regex, 'gim').test(parents[i].text)) {
                    return true;
                }
            }
            return isClear;
        };
        MarkdownSelection.prototype.getSelectedInlinePoints = function (textarea) {
            var start = textarea.selectionStart;
            var end = textarea.selectionEnd;
            var selection = this.getSelectedText(textarea);
            return { start: start, end: end, text: selection };
        };
        return MarkdownSelection;
    }());
    exports.MarkdownSelection = MarkdownSelection;
});