all files / rich-text-editor/formatter/ formatter.js

97.5% Statements 156/160
95.15% Branches 255/268
100% Functions 21/21
97.5% Lines 156/160
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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302     2571× 2571× 2571× 2571× 2571× 2571× 349×   2571×   2571× 2461× 1746× 1714× 265×     1449×         2571×                               34×   2537×         2534× 653× 653× 653×         653× 653× 346×         346× 346× 191× 191×             653× 650×   650× 646× 341×       646×                       1881×         1560× 1560× 1559× 1555× 1555× 1555×   513×   1555× 1555× 1555× 1484×   1555× 1401× 1380×     1555× 1555× 59×   1555×     1547× 1547×           2534×   2514×     1168× 1168×   258× 258×   16× 16×         2467× 2467× 2467× 2467× 2462× 389× 389×   2073×                   2069× 2069×     2467× 2467× 2467× 2467× 2433× 2433×       34×     2467× 2467× 109× 109×       105×         2463×     2463× 2463×   4336×   5075×   5820×   5075× 5075× 83×     4992× 4972×       24×   2630×     289× 289× 288×           288× 288× 288×     288×                                
define(["require", "exports", "@syncfusion/ej2-base", "../base/constant", "../base/util", "../../common/util", "./../../common/constant"], function (require, exports, ej2_base_1, CONSTANT, util_1, util_2, constant_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var Formatter = (function () {
        function Formatter() {
        }
        Formatter.prototype.process = function (self, args, event, value) {
            var _this = this;
            var selection = self.contentModule.getDocument().getSelection();
            var range = (selection.rangeCount > 0) ? selection.getRangeAt(selection.rangeCount - 1) : null;
            var saveSelection;
            var newRange;
            if (!ej2_base_1.isNullOrUndefined(value) && !ej2_base_1.isNullOrUndefined(value.selection)) {
                newRange = value.selection.range;
            }
            var isKeyboardVideoInsert = (!ej2_base_1.isNullOrUndefined(value) && !ej2_base_1.isNullOrUndefined(value.cssClass) &&
                value.cssClass !== 'e-video-inline');
            if (self.editorMode === 'HTML') {
                if (!ej2_base_1.isNullOrUndefined(args) && !isKeyboardVideoInsert) {
                    if (ej2_base_1.isNullOrUndefined(args.name) || (!ej2_base_1.isNullOrUndefined(args.name) && args.name !== 'showDialog') && !ej2_base_1.isNullOrUndefined(args.item.command)) {
                        if (newRange) {
                            saveSelection = this.editorManager.nodeSelection.save(newRange, self.contentModule.getDocument());
                        }
                        else {
                            saveSelection = this.editorManager.nodeSelection.save(range, self.contentModule.getDocument());
                        }
                    }
                }
            }
            if (!ej2_base_1.isNullOrUndefined(args)
                && args.item.command
                && args.item.command !== 'Table'
                && args.item.command !== 'Actions'
                && args.item.command !== 'Links'
                && args.item.command !== 'Images'
                && args.item.command !== 'Files'
                && args.item.command !== 'Audios'
                && args.item.command !== 'Videos'
                && args.item.command !== 'EmojiPicker'
                && args.item.command !== 'CodeBlock'
                && args.item.command !== 'AIAssistant'
                && range
                && !(self.contentModule.getEditPanel().contains(this.getAncestorNode(range.commonAncestorContainer))
                    || self.contentModule.getEditPanel() === range.commonAncestorContainer
                    || self.contentModule.getPanel() === range.commonAncestorContainer)) {
                return;
            }
            if (!ej2_base_1.isNullOrUndefined(args) && self.maxLength !== -1 && !ej2_base_1.isNullOrUndefined(args.item.command)) {
                var currentInsertContentLength = 0;
                if (args.item.command === 'Links') {
                    currentInsertContentLength = value.text.length === 0 ? value.url.length : value.text.length;
                }
                if (args.item.command === 'Images' || args.item.command === 'Videos' || args.item.command === 'Table' || args.item.command === 'Files') {
                    currentInsertContentLength = 1;
                }
                var currentLength = self.getText().trim().replace(/(\r\n|\n|\r|\t)/gm, '').replace(/\u200B/g, '').length;
                var selectionLength = self.getSelection().length;
                var totalLength = (currentLength - selectionLength) + currentInsertContentLength;
                var hasMaxLength = self.maxLength !== -1;
                var exceedsTotalLength = totalLength > self.maxLength;
                var exceedsCurrentLength = currentLength > self.maxLength;
                var exceedsMaxLength = hasMaxLength && (exceedsTotalLength || exceedsCurrentLength);
                var isNotUndoRedo = args.item.subCommand !== 'Undo' && args.item.subCommand !== 'Redo';
                if (exceedsMaxLength && isNotUndoRedo) {
                    return;
                }
            }
            if (ej2_base_1.isNullOrUndefined(args)) {
                var action_1 = event.action;
                args = {};
                var items = {
                    originalEvent: event, cancel: false,
                    requestType: action_1 || (event.key + 'Key'),
                    itemCollection: value
                };
                ej2_base_1.extend(args, args, items, true);
                if (action_1 !== 'tab' && action_1 !== 'enter' && action_1 !== 'space' && action_1 !== 'escape') {
                    if (self.editorMode === 'Markdown' && action_1 === 'insert-table') {
                        value = {
                            'headingText': self.localeObj.getConstant('TableHeadingText'),
                            'colText': self.localeObj.getConstant('TableColText')
                        };
                    }
                    var rangeContainer = range ? range.commonAncestorContainer : null;
                    if (action_1 !== 'backspace' && action_1 !== 'delete' || (rangeContainer && rangeContainer.nodeType === Node.ELEMENT_NODE && rangeContainer.querySelectorAll('img, audio, video').length > 0)) {
                        self.trigger(CONSTANT.actionBegin, args, function (actionBeginArgs) {
                            if (actionBeginArgs.cancel) {
                                Iif (action_1 === 'paste' || action_1 === 'cut' || action_1 === 'copy') {
                                    event.preventDefault();
                                }
                            }
                        });
                    }
                }
                if (!args.cancel) {
                    var isTableModule = ej2_base_1.isNullOrUndefined(self.tableModule) ? true : self.tableModule ?
                        self.tableModule.tableObj && self.tableModule.tableObj.ensureInsideTableList : false;
                    if ((event.which === 9 && isTableModule) || event.which !== 9) {
                        if (event.which === 13 && self.editorMode === 'HTML') {
                            value = {
                                'enterAction': self.enterKey
                            };
                        }
                        this.editorManager.observer.notify((event.type === 'keydown' ? constant_1.KEY_DOWN : constant_1.KEY_UP), {
                            event: event,
                            callBack: event.which === 13 && self.editorMode === 'HTML' && event.type === 'keydown' ? this.enterKeyActionBeginHandler.bind(this, self) : this.onSuccess.bind(this, self),
                            value: value,
                            enterAction: self.enterKey,
                            shiftEnterKey: self.shiftEnterKey,
                            enableTabKey: self.enableTabKey,
                            maxLength: self.maxLength
                        });
                    }
                }
            }
            else if (!ej2_base_1.isNullOrUndefined(args) && args.item.command && args.item.subCommand && ((args.item.command !== args.item.subCommand
                && args.item.command !== 'Font' && args.item.command !== 'Export' && args.item.subCommand !== 'TableBackgroundColor' && args.item.subCommand !== 'BorderColor')
                || ((args.item.subCommand === 'FontName' || args.item.subCommand === 'FontSize') && args.name === 'dropDownSelect')
                || ((args.item.subCommand === 'BackgroundColor' || args.item.subCommand === 'FontColor')
                    && (args.name === 'colorPickerChanged' || args.name === 'tableColorPickerChanged')) || args.item.subCommand === 'FormatPainter' || args.item.subCommand === 'EmojiPicker' || args.item.subCommand === 'CodeBlock' || args.item.subCommand === 'Checklist')) {
                ej2_base_1.extend(args, args, { requestType: (args.item.subCommand === 'LineHeights') ? 'LineHeight' : args.item.subCommand, cancel: false, itemCollection: value, selectType: args.name }, true);
                self.trigger(CONSTANT.actionBegin, args, function (actionBeginArgs) {
                    if (!actionBeginArgs.cancel) {
                        var formatPainterCopy = !ej2_base_1.isNullOrUndefined(actionBeginArgs.requestType) && actionBeginArgs.requestType === 'FormatPainter' && actionBeginArgs.name === 'format-copy';
                        var formatPainterPaste = !ej2_base_1.isNullOrUndefined(actionBeginArgs.requestType) && actionBeginArgs.requestType === 'FormatPainter' && actionBeginArgs.name === 'format-paste';
                        if ((_this.getUndoRedoStack().length === 0 && actionBeginArgs.item.command !== 'Links' && actionBeginArgs.item.command !== 'Images' && !formatPainterCopy)
                            || formatPainterPaste) {
                            _this.saveData();
                        }
                        self.isBlur = false;
                        var quickToolbarAction = !ej2_base_1.isNullOrUndefined(event) && !ej2_base_1.isNullOrUndefined(event.target) && (!ej2_base_1.isNullOrUndefined(ej2_base_1.closest(event.target, '.e-rte-elements.e-dropdown-popup.e-rte-dropdown-popup.e-quick-dropdown.e-popup-open')) || !ej2_base_1.isNullOrUndefined(ej2_base_1.closest(event.target, '.e-rte-elements.e-rte-quick-popup.e-popup-open')));
                        if (ej2_base_1.isNullOrUndefined(saveSelection) || (!quickToolbarAction && (ej2_base_1.isNullOrUndefined(ej2_base_1.closest(saveSelection.range.startContainer.parentElement, '.e-img-caption-container')) ? true : !(ej2_base_1.closest(saveSelection.range.startContainer.parentElement, '.e-img-caption-container').getAttribute('contenteditable') === 'false'))) && !(ej2_base_1.Browser.userAgent.indexOf('Firefox') !== -1)) {
                            self.contentModule.getEditPanel().focus({ preventScroll: true });
                        }
                        if (self.editorMode === 'HTML' && !isKeyboardVideoInsert) {
                            if (ej2_base_1.isNullOrUndefined(args.selectType) || (!ej2_base_1.isNullOrUndefined(args.selectType) && args.selectType !== 'showDialog')) {
                                saveSelection.restore();
                            }
                        }
                        var command = actionBeginArgs.item.subCommand.toLocaleLowerCase();
                        if (command === 'image' || command === 'file' || command === 'replace') {
                            value = actionBeginArgs.itemCollection;
                        }
                        if (command === 'paste' || command === 'cut' || command === 'copy') {
                            self.clipboardAction(command, event);
                        }
                        else {
                            _this.editorManager.observer.notify(CONSTANT.checkUndo, { subCommand: actionBeginArgs.item.subCommand });
                            _this.editorManager.execCommand(actionBeginArgs.item.command, actionBeginArgs.item.subCommand, event, _this.onSuccess.bind(_this, self), actionBeginArgs.item.value, actionBeginArgs.item.subCommand === 'Pre' && actionBeginArgs.selectType === 'dropDownSelect' ?
                                { name: actionBeginArgs.selectType } : value, ('#' + self.getID() + ' iframe'), self.enterKey);
                        }
                    }
                });
            }
            if ((ej2_base_1.isNullOrUndefined(event) || (event && event.action !== 'copy' && event.action !== 'html-source')) &&
                !(event && event.shiftKey && event.key === 'Tab')) {
                this.enableUndo(self);
            }
        };
        Formatter.prototype.getAncestorNode = function (node) {
            node = node.nodeType === 3 ? node.parentNode : node;
            return node;
        };
        Formatter.prototype.onKeyHandler = function (self, e) {
            var _this = this;
            this.editorManager.observer.notify(constant_1.KEY_UP, {
                event: e, callBack: function () {
                    self.notify(CONSTANT.contentChanged, {});
                    _this.enableUndo(self);
                },
                enterAction: self.enterKey
            });
        };
        Formatter.prototype.onSuccess = function (self, events) {
            var _this = this;
            var _a;
            self.notify(CONSTANT.contentChanged, {});
            if (events && (ej2_base_1.isNullOrUndefined(events.event) || (events.event.action !== 'copy' && events.event.action !== 'html-source'))) {
                if (events.requestType === 'Paste') {
                    self.notify(CONSTANT.execCommandCallBack, events);
                    this.enableUndo(self);
                }
                else if (events.requestType === 'Images' && self.imageModule.isMultiImagePaste) {
                    var currentElements = events.elements;
                    Eif (currentElements) {
                        Eif (Array.isArray(currentElements)) {
                            (_a = self.imageModule.collectedImageElements).push.apply(_a, currentElements);
                        }
                        else {
                            self.imageModule.collectedImageElements.push(currentElements);
                        }
                    }
                    var remainingPastedImages = self.imageModule.remainingPastedImages;
                    Eif (remainingPastedImages > 0) {
                        remainingPastedImages--;
                    }
                    if (remainingPastedImages === 0) {
                        this.enableUndo(self);
                        events.elements = self.imageModule.collectedImageElements;
                        self.notify(CONSTANT.execCommandCallBack, events);
                        self.imageModule.collectedImageElements = [];
                    }
                }
                else {
                    this.enableUndo(self);
                    self.notify(CONSTANT.execCommandCallBack, events);
                }
            }
            var selection = self.contentModule.getDocument().getSelection();
            var range = (selection.rangeCount > 0) ? selection.getRangeAt(selection.rangeCount - 1) : null;
            var rangeContainer = range ? range.commonAncestorContainer : null;
            if (events.requestType !== 'delete' || (range && rangeContainer.nodeType === Node.ELEMENT_NODE && rangeContainer.querySelectorAll('img, audio, video').length > 0)) {
                self.trigger(CONSTANT.actionComplete, events, function (callbackArgs) {
                    _this.actionCompleteCallBack(self, callbackArgs);
                });
            }
            else {
                this.actionCompleteCallBack(self, events);
            }
        };
        Formatter.prototype.actionCompleteCallBack = function (self, callbackArgs) {
            self.setPlaceHolder();
            if ((callbackArgs.requestType === 'Images' || callbackArgs.requestType === 'Replace' || callbackArgs.requestType === 'Links' || callbackArgs.requestType === 'Audios' || callbackArgs.requestType === 'Videos') && self.editorMode === 'HTML') {
                var args = callbackArgs;
                if (callbackArgs.requestType === 'Links' && callbackArgs.event &&
                    callbackArgs.event.type === 'keydown' &&
                    callbackArgs.event.keyCode === 32) {
                    return;
                }
                self.notify(CONSTANT.insertCompleted, {
                    args: args.event, type: callbackArgs.requestType, isNotify: true,
                    elements: args.elements
                });
            }
            Iif (callbackArgs.requestType === 'VideosPlayPause') {
                self.notify('editAreaClick', { args: event });
            }
            self.isSelectAll = false;
            self.autoResize();
        };
        Formatter.prototype.saveData = function (e) {
            this.editorManager.undoRedoManager.saveData(e);
        };
        Formatter.prototype.getUndoStatus = function () {
            return this.editorManager.undoRedoManager.getUndoStatus();
        };
        Formatter.prototype.getUndoRedoStack = function () {
            return this.editorManager.undoRedoManager.undoRedoStack;
        };
        Formatter.prototype.enableUndo = function (self) {
            var status = this.getUndoStatus();
            if (self.inlineMode.enable && (!ej2_base_1.Browser.isDevice || util_2.isIDevice())) {
                util_1.updateUndoRedoStatus(self.quickToolbarModule.inlineQTBar.quickTBarObj, status);
            }
            else {
                if (self.toolbarModule && self.toolbarModule.baseToolbar) {
                    util_1.updateUndoRedoStatus(self.toolbarModule.baseToolbar, status);
                }
            }
        };
        Formatter.prototype.beforeSlashMenuApply = function () {
            this.editorManager.beforeSlashMenuApplyFormat();
        };
        Formatter.prototype.getCurrentStackIndex = function () {
            return this.editorManager.undoRedoManager.getCurrentStackIndex();
        };
        Formatter.prototype.clearUndoRedoStack = function () {
            this.editorManager.undoRedoManager.clear();
        };
        Formatter.prototype.enterKeyActionBeginHandler = function (self, args) {
            var _this = this;
            if (args.isEnterAction) {
                var actionBeginArgs = {
                    cancel: false,
                    name: CONSTANT.actionBegin,
                    requestType: args.isShiftEnterAction ? 'ShiftEnterAction' : 'EnterAction',
                    originalEvent: args.event
                };
                self.trigger(CONSTANT.actionBegin, actionBeginArgs, function (successArgs) {
                    Eif (!successArgs.cancel) {
                        Iif (_this.getUndoRedoStack().length === 0) {
                            _this.saveData();
                        }
                        _this.editorManager.observer.notify(constant_1.ENTER_KEYDOWN_HANLDER, {
                            requestType: 'EnterKey',
                            enterAction: self.enterKey,
                            shiftEnterAction: self.shiftEnterKey,
                            isEnterAction: true,
                            cancel: false,
                            event: args.event,
                            isSelectAll: self.isSelectAll,
                            callBack: _this.onSuccess.bind(_this, self)
                        });
                    }
                });
            }
        };
        return Formatter;
    }());
    exports.Formatter = Formatter;
});