all files / markdown-parser/plugin/ insert-text.js

100% Statements 33/33
100% Branches 4/4
100% Functions 8/8
100% Lines 33/33
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   151× 151× 151×   151× 151×   70× 70×                     70×        
define(["require", "exports", "./../base/constant", "./../../common/constant"], function (require, exports, CONSTANT, EVENTS) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MDInsertText = (function () {
        function MDInsertText(parent) {
            this.parent = parent;
            this.selection = this.parent.markdownSelection;
            this.addEventListener();
        }
        MDInsertText.prototype.addEventListener = function () {
            this.parent.observer.on(CONSTANT.INSERT_TEXT_COMMAND, this.InsertTextExec, this);
            this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
        };
        MDInsertText.prototype.removeEventListener = function () {
            this.parent.observer.off(CONSTANT.INSERT_TEXT_COMMAND, this.InsertTextExec);
            this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
        };
        MDInsertText.prototype.InsertTextExec = function (e) {
            var textArea = this.parent.element;
            textArea.focus();
            var start = textArea.selectionStart;
            var end = textArea.selectionEnd;
            var text = e.value.text;
            var startOffset = start + text.length;
            var endOffset = end + text.length;
            textArea.value = textArea.value.substr(0, start)
                + text + textArea.value.substr(end, textArea.value.length);
            this.parent.markdownSelection.setSelection(textArea, startOffset, endOffset);
            this.restore(textArea, startOffset, endOffset, e);
        };
        MDInsertText.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
                });
            }
        };
        MDInsertText.prototype.destroy = function () {
            this.removeEventListener();
        };
        return MDInsertText;
    }());
    exports.MDInsertText = MDInsertText;
});