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

98.04% Statements 100/102
97.44% Branches 38/39
100% Functions 15/15
98.04% Lines 100/102
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   152× 152× 152× 152× 152× 152×   152× 152× 152× 152× 152× 152×                 70× 70× 70× 70× 70× 70×   70×   24× 17×         34× 34× 34×             171× 171× 171× 171× 171× 171× 171× 131×   171×   36×   135× 135× 135×   135×     21× 20× 20× 20× 20× 20× 20× 20× 20×         27× 27× 27× 22×               355× 355× 220×   355× 59×   355×        
define(["require", "exports", "@syncfusion/ej2-base", "./../../common/constant"], function (require, exports, ej2_base_1, EVENTS) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var UndoRedoCommands = (function () {
        function UndoRedoCommands(parent, options) {
            this.undoRedoStack = [];
            this.parent = parent;
            this.undoRedoSteps = !ej2_base_1.isNullOrUndefined(options) ? options.undoRedoSteps : 30;
            this.undoRedoTimer = !ej2_base_1.isNullOrUndefined(options) ? options.undoRedoTimer : 300;
            this.selection = this.parent.markdownSelection;
            this.addEventListener();
        }
        UndoRedoCommands.prototype.addEventListener = function () {
            var debounceListener = ej2_base_1.debounce(this.keyUp, this.undoRedoTimer);
            this.parent.observer.on(EVENTS.KEY_UP_HANDLER, debounceListener, this);
            this.parent.observer.on(EVENTS.KEY_DOWN_HANDLER, this.keyDown, this);
            this.parent.observer.on(EVENTS.ACTION, this.onAction, this);
            this.parent.observer.on(EVENTS.MODEL_CHANGED_PLUGIN, this.onPropertyChanged, this);
            this.parent.observer.on(EVENTS.INTERNAL_DESTROY, this.destroy, this);
        };
        UndoRedoCommands.prototype.onPropertyChanged = function (props) {
            for (var _i = 0, _a = Object.keys(props.newProp); _i < _a.length; _i++) {
                var prop = _a[_i];
                switch (prop) {
                    case 'undoRedoSteps':
                        this.undoRedoSteps = props.newProp.undoRedoSteps;
                        break;
                    case 'undoRedoTimer':
                        this.undoRedoTimer = props.newProp.undoRedoTimer;
                        break;
                }
            }
        };
        UndoRedoCommands.prototype.removeEventListener = function () {
            var debounceListener = ej2_base_1.debounce(this.keyUp, 300);
            this.parent.observer.off(EVENTS.KEY_UP_HANDLER, debounceListener);
            this.parent.observer.off(EVENTS.KEY_DOWN_HANDLER, this.keyDown);
            this.parent.observer.off(EVENTS.ACTION, this.onAction);
            this.parent.observer.off(EVENTS.MODEL_CHANGED_PLUGIN, this.onPropertyChanged);
            this.parent.observer.off(EVENTS.INTERNAL_DESTROY, this.destroy);
        };
        UndoRedoCommands.prototype.destroy = function () {
            this.removeEventListener();
        };
        UndoRedoCommands.prototype.onAction = function (e) {
            if (e.subCommand === 'Undo') {
                this.undo(e);
            }
            else {
                this.redo(e);
            }
        };
        UndoRedoCommands.prototype.keyDown = function (e) {
            var event = e.event;
            var proxy = this;
            switch (event.action) {
                case 'undo':
                    event.preventDefault();
                    proxy.undo(e);
                    break;
                case 'redo':
                    event.preventDefault();
                    proxy.redo(e);
                    break;
            }
        };
        UndoRedoCommands.prototype.keyUp = function (e) {
            if (e.event.keyCode !== 17 && !e.event.ctrlKey) {
                this.saveData(e);
            }
        };
        UndoRedoCommands.prototype.saveData = function (e) {
            var textArea = this.parent.element;
            this.selection.save(textArea.selectionStart, textArea.selectionEnd);
            var start = textArea.selectionStart;
            var end = textArea.selectionEnd;
            var textValue = this.parent.element.value;
            var changEle = { text: textValue, start: start, end: end };
            if (this.undoRedoStack.length >= this.steps) {
                this.undoRedoStack = this.undoRedoStack.slice(0, this.steps + 1);
            }
            if (this.undoRedoStack.length > 1 && (this.undoRedoStack[this.undoRedoStack.length - 1].start === start) &&
                (this.undoRedoStack[this.undoRedoStack.length - 1].end === end)) {
                return;
            }
            this.undoRedoStack.push(changEle);
            this.steps = this.undoRedoStack.length - 1;
            if (this.steps > this.undoRedoSteps) {
                this.undoRedoStack.shift();
                this.steps--;
            }
            if (e && e.callBack) {
                e.callBack();
            }
        };
        UndoRedoCommands.prototype.undo = function (e) {
            if (this.steps > 0) {
                this.currentAction = 'Undo';
                var start = this.undoRedoStack[this.steps - 1].start;
                var end = this.undoRedoStack[this.steps - 1].end;
                var removedContent = this.undoRedoStack[this.steps - 1].text;
                this.parent.element.value = removedContent;
                this.parent.element.focus();
                this.steps--;
                this.restore(this.parent.element, start, end, e);
            }
        };
        UndoRedoCommands.prototype.redo = function (e) {
            if (this.undoRedoStack[this.steps + 1] != null) {
                this.currentAction = 'Redo';
                var start = this.undoRedoStack[this.steps + 1].start;
                var end = this.undoRedoStack[this.steps + 1].end;
                this.parent.element.value = this.undoRedoStack[this.steps + 1].text;
                this.parent.element.focus();
                this.steps++;
                this.restore(this.parent.element, start, end, e);
            }
        };
        UndoRedoCommands.prototype.restore = function (textArea, start, end, event) {
            this.selection.save(start, end);
            this.selection.restore(textArea);
            if (event && event.callBack) {
                event.callBack({
                    requestType: this.currentAction,
                    selectedText: this.selection.getSelectedText(textArea),
                    editorMode: 'Markdown',
                    event: event.event
                });
            }
        };
        UndoRedoCommands.prototype.getUndoStatus = function () {
            var status = { undo: false, redo: false };
            if (this.steps > 0) {
                status.undo = true;
            }
            if (this.undoRedoStack[this.steps + 1] != null) {
                status.redo = true;
            }
            return status;
        };
        return UndoRedoCommands;
    }());
    exports.UndoRedoCommands = UndoRedoCommands;
});