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

98.99% Statements 98/99
85% Branches 17/20
100% Functions 16/16
98.99% Lines 98/99
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   70× 70× 70× 70× 70×   136× 66×   70× 70×   70×     70× 70× 70× 70× 70× 70× 70× 70× 70× 70× 70×   74×     69× 69×     27× 27×   26× 26× 26×   84× 84× 84× 70×   84× 84× 84× 84×                     75× 75×     70×   70× 70× 70× 70× 70× 70× 70× 70× 70× 70×   70× 70× 70× 70× 61×             70× 70×   70×   48× 48× 48×   48×     32470×            
define(["require", "exports", "../base/constant", "@syncfusion/ej2-base", "../formatter/markdown-formatter", "../base/enum", "../base/classes", "./markdown-toolbar-status", "../renderer/markdown-renderer", "./../../markdown-parser/plugin/markdown-selection"], function (require, exports, events, ej2_base_1, markdown_formatter_1, enum_1, classes, markdown_toolbar_status_1, markdown_renderer_1, markdown_selection_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var MarkdownEditor = (function () {
        function MarkdownEditor(parent, serviceLocator) {
            this.parent = parent;
            this.locator = serviceLocator;
            this.renderFactory = this.locator.getService('rendererFactory');
            this.addEventListener();
            this.isDestroyed = false;
        }
        MarkdownEditor.prototype.destroy = function () {
            if (this.isDestroyed) {
                return;
            }
            this.removeEventListener();
            this.isDestroyed = true;
        };
        MarkdownEditor.prototype.addEventListener = function () {
            Iif (this.parent.isDestroyed) {
                return;
            }
            this.saveSelection = new markdown_selection_1.MarkdownSelection();
            this.parent.on(events.initialLoad, this.instantiateRenderer, this);
            this.parent.on(events.initialEnd, this.render, this);
            this.parent.on(events.modelChanged, this.onPropertyChanged, this);
            this.parent.on(events.markdownToolbarClick, this.onToolbarClick, this);
            this.parent.on(events.destroy, this.destroy, this);
            this.parent.on(events.selectAll, this.selectAll, this);
            this.parent.on(events.getSelectedHtml, this.getSelectedHtml, this);
            this.parent.on(events.selectionSave, this.onSelectionSave, this);
            this.parent.on(events.selectionRestore, this.onSelectionRestore, this);
            this.parent.on(events.readOnlyMode, this.updateReadOnly, this);
        };
        MarkdownEditor.prototype.updateReadOnly = function () {
            if (this.parent.readonly) {
                this.parent.contentModule.getEditPanel().setAttribute('readonly', 'readonly');
                ej2_base_1.addClass([this.parent.element], classes.CLS_RTE_READONLY);
            }
            else {
                this.parent.contentModule.getEditPanel().removeAttribute('readonly');
                ej2_base_1.removeClass([this.parent.element], classes.CLS_RTE_READONLY);
            }
        };
        MarkdownEditor.prototype.onSelectionSave = function () {
            var textArea = this.parent.contentModule.getEditPanel();
            this.saveSelection.save(textArea.selectionStart, textArea.selectionEnd);
        };
        MarkdownEditor.prototype.onSelectionRestore = function (e) {
            this.contentRenderer.getEditPanel().focus();
            var textArea = this.parent.contentModule.getEditPanel();
            this.saveSelection.restore(textArea);
        };
        MarkdownEditor.prototype.onToolbarClick = function (args) {
            var item = args.item;
            var textArea = this.parent.contentModule.getEditPanel();
            if (item.command !== 'Formats') {
                textArea.focus();
            }
            var startOffset = textArea.selectionStart;
            var endOffset = textArea.selectionEnd;
            var text = textArea.value.substring(startOffset, endOffset);
            switch (item.subCommand) {
                case 'Maximize':
                    this.parent.notify(events.enableFullScreen, { args: args });
                    break;
                case 'Minimize':
                    this.parent.notify(events.disableFullScreen, { args: args });
                    break;
                case 'CreateLink':
                    this.parent.notify(events.insertLink, { member: 'link', args: args, text: text, module: 'Markdown' });
                    break;
                case 'Image':
                    this.parent.notify(events.insertImage, { member: 'image', args: args, text: text, module: 'Markdown' });
                    break;
                case 'CreateTable': {
                    var tableConstant = {
                        'headingText': this.parent.localeObj.getConstant('TableHeadingText'),
                        'colText': this.parent.localeObj.getConstant('TableColText')
                    };
                    this.parent.formatter.process(this.parent, args, args.originalEvent, tableConstant);
                    break;
                }
                default:
                    this.parent.formatter.process(this.parent, args, args.originalEvent, null);
                    break;
            }
        };
        MarkdownEditor.prototype.instantiateRenderer = function () {
            this.renderFactory.addRenderer(enum_1.RenderType.Content, new markdown_renderer_1.MarkdownRender(this.parent));
        };
        MarkdownEditor.prototype.removeEventListener = function () {
            this.parent.off(events.initialEnd, this.render);
            this.parent.off(events.modelChanged, this.onPropertyChanged);
            this.parent.off(events.destroy, this.destroy);
            this.parent.off(events.markdownToolbarClick, this.onToolbarClick);
            this.parent.off(events.initialLoad, this.instantiateRenderer);
            this.parent.off(events.selectAll, this.selectAll);
            this.parent.off(events.getSelectedHtml, this.getSelectedHtml);
            this.parent.off(events.selectionSave, this.onSelectionSave);
            this.parent.off(events.selectionRestore, this.onSelectionRestore);
            this.parent.off(events.readOnlyMode, this.updateReadOnly);
        };
        MarkdownEditor.prototype.render = function () {
            this.contentRenderer = this.renderFactory.getRenderer(enum_1.RenderType.Content);
            var editElement = this.contentRenderer.getEditPanel();
            var option = { undoRedoSteps: this.parent.undoRedoSteps, undoRedoTimer: this.parent.undoRedoTimer };
            if (ej2_base_1.isNullOrUndefined(this.parent.formatter)) {
                this.parent.formatter = new markdown_formatter_1.MarkdownFormatter({
                    element: editElement,
                    options: option
                });
            }
            else {
                this.parent.formatter.updateFormatter(editElement, this.contentRenderer.getDocument(), option);
            }
            Eif (this.parent.toolbarSettings.enable) {
                this.toolbarUpdate = new markdown_toolbar_status_1.MarkdownToolbarStatus(this.parent);
            }
            this.parent.notify(events.bindOnEnd, {});
        };
        MarkdownEditor.prototype.onPropertyChanged = function (e) {
            Eif (!ej2_base_1.isNullOrUndefined(e.newProp.formatter)) {
                var editElement = this.contentRenderer.getEditPanel();
                var option = { undoRedoSteps: this.parent.undoRedoSteps,
                    undoRedoTimer: this.parent.undoRedoTimer };
                this.parent.formatter.updateFormatter(editElement, this.contentRenderer.getDocument(), option);
            }
        };
        MarkdownEditor.prototype.getModuleName = function () {
            return 'markdownEditor';
        };
        MarkdownEditor.prototype.selectAll = function () {
            this.parent.formatter.editorManager.markdownSelection.setSelection(this.parent.contentModule.getEditPanel(), 0, this.parent.contentModule.getEditPanel().value.length);
        };
        MarkdownEditor.prototype.getSelectedHtml = function (e) {
            e.callBack(this.parent.formatter.editorManager.markdownSelection.getSelectedText(this.parent.contentModule.getEditPanel()));
        };
        return MarkdownEditor;
    }());
    exports.MarkdownEditor = MarkdownEditor;
});