all files / rich-text-editor/actions/ enter-key.js

94.59% Statements 385/407
91.78% Branches 480/523
100% Functions 21/21
94.59% Lines 385/407
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 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680   1818× 1818×   1818× 1818×   1817×     1817×   1817× 1817×   199× 199×   199×     184× 184× 184× 184× 184× 184× 12×     178× 15×     173×     173× 173× 141×       133× 133×           133× 133× 133× 60×   133× 24×   20×   24×           20×   19× 13× 13×     11× 11×                     11×     131×               131×     90×     89× 89× 58×       58×       31×   89× 89× 89×     29× 29× 29×     60× 50×   89× 89×     89×         89×     47× 47× 47×                 39×     17×     16× 16×   17×     22× 22×     47× 47× 46×       47× 51×   47× 47× 25× 25× 25×         24×   25× 25×     47×   42×                           42×                                           38× 38×   15×   38× 31× 31×     30× 29×     31×     31×   31×   38×   38× 38× 37× 37× 42×   37× 37× 37×           90×   131× 41× 41× 15× 15× 15× 18× 18×   15×       26×   41× 41× 41× 41× 41×     41×   41×   41×         11× 11×     10×                       11×   30× 14×             10× 10× 10×                     16×   41×   131× 131×           130×           20× 20× 20×   20× 20× 20× 18×   20×     20×     18×     18×     17×     20× 18× 18×                         92× 92× 22×   70× 70×   92× 92× 92×   92×   92×   142×                                                                         184× 184× 184× 184×     184×     180×     184× 184× 184× 184×     184×     175×     184× 184× 184×        
define(["require", "exports", "@syncfusion/ej2-base", "../base/constant"], function (require, exports, ej2_base_1, events) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var EnterKeyAction = (function () {
        function EnterKeyAction(parent) {
            this.parent = parent;
            this.addEventListener();
        }
        EnterKeyAction.prototype.addEventListener = function () {
            this.parent.on(events.enterHandler, this.enterHandler, this);
            this.parent.on(events.destroy, this.destroy, this);
        };
        EnterKeyAction.prototype.destroy = function () {
            Iif (ej2_base_1.isNullOrUndefined(this.parent)) {
                return;
            }
            this.removeEventListener();
        };
        EnterKeyAction.prototype.removeEventListener = function () {
            this.parent.off(events.enterHandler, this.enterHandler);
            this.parent.off(events.destroy, this.destroy);
        };
        EnterKeyAction.prototype.getRangeNode = function () {
            this.range = this.parent.getRange();
            this.startNode = this.range.startContainer.nodeName === '#text' ? this.range.startContainer.parentElement :
                this.range.startContainer;
            this.endNode = this.range.endContainer.nodeName === '#text' ? this.range.endContainer.parentElement :
                this.range.endContainer;
        };
        EnterKeyAction.prototype.enterHandler = function (e) {
            var _this = this;
            this.getRangeNode();
            var isTableEnter = true;
            this.formatTags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'blockquote'];
            var tableImagCursor = this.processedTableImageCursor();
            if (tableImagCursor.start || tableImagCursor.end) {
                if (tableImagCursor.startName === 'TABLE' || tableImagCursor.endName === 'TABLE') {
                    this.handleCursorAtTableSide(e, tableImagCursor.start, tableImagCursor.end);
                    return;
                }
            }
            if (tableImagCursor.start || tableImagCursor.end || this.range.startContainer.nodeName === 'IMG') {
                if (this.parent.enterKey === 'BR' && (tableImagCursor.startName === 'IMG' || tableImagCursor.endName === 'IMG' || this.range.startContainer.nodeName === 'IMG')) {
                    this.handleEnterKeyAtImageSide(e, tableImagCursor.start, tableImagCursor.end);
                    return;
                }
            }
            if (!ej2_base_1.isNullOrUndefined(this.startNode.closest('TABLE')) && !ej2_base_1.isNullOrUndefined(this.endNode.closest('TABLE'))) {
                isTableEnter = false;
                var curElement = this.startNode;
                var blockElement = curElement;
                while (!this.parent.formatter.editorManager.domNode.isBlockNode(curElement)) {
                    curElement = curElement.parentElement;
                    blockElement = curElement;
                }
                isTableEnter = blockElement.tagName === 'TH' || blockElement.tagName === 'TD' || blockElement.tagName === 'TBODY' ? false : true;
            }
            var eventArgs = e.args;
            if (eventArgs.which === 13 && !eventArgs.ctrlKey && (!ej2_base_1.Browser.isDevice ? (eventArgs.code === 'Enter' || eventArgs.code === 'NumpadEnter') : eventArgs.key === 'Enter')) {
                if (ej2_base_1.isNullOrUndefined(this.startNode.closest('LI, UL, OL')) && ej2_base_1.isNullOrUndefined(this.endNode.closest('LI, UL, OL')) &&
                    ej2_base_1.isNullOrUndefined(this.startNode.closest('.e-img-inner')) && isTableEnter &&
                    ej2_base_1.isNullOrUndefined(this.startNode.closest('PRE')) && ej2_base_1.isNullOrUndefined(this.endNode.closest('PRE')) &&
                    ej2_base_1.isNullOrUndefined(this.startNode.closest('BLOCKQUOTE')) && ej2_base_1.isNullOrUndefined(this.endNode.closest('BLOCKQUOTE'))) {
                    var shiftKey_1 = e.args.shiftKey;
                    var actionBeginArgs = {
                        cancel: false,
                        name: events.actionBegin,
                        requestType: shiftKey_1 ? 'ShiftEnterAction' : 'EnterAction',
                        originalEvent: e.args
                    };
                    this.parent.trigger(events.actionBegin, actionBeginArgs, function (actionBeginArgs) {
                        Eif (!actionBeginArgs.cancel) {
                            if (_this.parent.formatter.getUndoRedoStack().length === 0) {
                                _this.parent.formatter.saveData();
                            }
                            if (!(_this.range.startOffset === _this.range.endOffset && _this.range.startContainer === _this.range.endContainer)) {
                                if (!(_this.range.startContainer.nodeName === 'SPAN' && (_this.range.startContainer.classList.contains('e-video-wrap') ||
                                    _this.range.startContainer.classList.contains('e-audio-wrap')))) {
                                    _this.range.deleteContents();
                                }
                                if (_this.range.startContainer.nodeName === '#text' && _this.range.startContainer.textContent.length === 0 &&
                                    _this.range.startContainer.parentElement !== _this.parent.inputElement) {
                                    if (_this.parent.enterKey === 'BR') {
                                        _this.range.startContainer.parentElement.innerHTML = '​';
                                    }
                                    else {
                                        _this.range.startContainer.parentElement.innerHTML = '<br>';
                                    }
                                }
                                else if (_this.range.startContainer === _this.parent.inputElement && _this.range.startContainer.innerHTML === '') {
                                    _this.range.startContainer.innerHTML = '<br>';
                                    var focusElem = _this.range.startContainer.childNodes[_this.range.startOffset];
                                    _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), focusElem, 0);
                                }
                                else if (_this.parent.inputElement === _this.range.startContainer) {
                                    var focusElem = _this.range.startContainer.childNodes[_this.range.startOffset];
                                    if (focusElem.nodeName === '#text' && focusElem.textContent.length === 0) {
                                        _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), focusElem, focusElem.textContent.length === 0 ? 0 : focusElem.previousSibling.textContent.length);
                                    }
                                    else {
                                        _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), focusElem, focusElem.textContent.length >= 0 ? 0 : 1);
                                        if (focusElem.previousSibling.textContent.length === 0) {
                                            ej2_base_1.detach(focusElem.previousSibling);
                                            Eif (!shiftKey_1) {
                                                var currentFocusElem = !ej2_base_1.isNullOrUndefined(focusElem.lastChild) ? focusElem.lastChild : focusElem;
                                                while (!ej2_base_1.isNullOrUndefined(currentFocusElem) && currentFocusElem.nodeName !== '#text' && currentFocusElem.nodeName !== 'BR') {
                                                    currentFocusElem = currentFocusElem.lastChild;
                                                }
                                                if (currentFocusElem.nodeName !== 'BR' && currentFocusElem.parentElement.textContent.length === 0 && currentFocusElem.parentElement.innerHTML.length === 0 &&
                                                    currentFocusElem.parentElement.nodeName !== 'BR') {
                                                    currentFocusElem.parentElement.appendChild(_this.parent.createElement('BR'));
                                                }
                                                _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), currentFocusElem.nodeName === 'BR' ? currentFocusElem : currentFocusElem.parentElement, currentFocusElem.parentElement.textContent.length >= 0 || currentFocusElem.nodeName === 'BR' ? 0 : 1);
                                            }
                                        }
                                        else if (focusElem.textContent.length === 0) {
                                            var currentFocusElem = focusElem.previousSibling.nodeName === '#text' ? focusElem.previousSibling : focusElem.previousSibling.lastChild;
                                            while (!ej2_base_1.isNullOrUndefined(currentFocusElem) && currentFocusElem.nodeName !== '#text') {
                                                currentFocusElem = currentFocusElem.lastChild;
                                            }
                                            _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), currentFocusElem, currentFocusElem.textContent.length);
                                            ej2_base_1.detach(focusElem);
                                        }
                                        else if (_this.parent.enterKey !== 'BR' &&
                                            focusElem.previousSibling.textContent.length !== 0 && focusElem.textContent.length !== 0) {
                                            e.args.preventDefault();
                                            return;
                                        }
                                    }
                                    _this.getRangeNode();
                                }
                            }
                            if (_this.range.startContainer === _this.range.endContainer &&
                                _this.range.startOffset === _this.range.endOffset && _this.range.startContainer === _this.parent.inputElement) {
                                Eif (!(_this.parent.inputElement.childNodes.length === 1 && _this.parent.inputElement.childNodes[0].nodeName === 'TABLE')) {
                                    if (ej2_base_1.isNullOrUndefined(_this.range.startContainer.childNodes[_this.range.startOffset])) {
                                        var currentLastElem = _this.range.startContainer.childNodes[_this.range.startOffset - 1];
                                        while (currentLastElem.lastChild !== null && currentLastElem.nodeName !== '#text') {
                                            currentLastElem = currentLastElem.lastChild;
                                        }
                                        _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), currentLastElem, (currentLastElem.nodeName === 'BR' ? 0 : currentLastElem.textContent.length));
                                    }
                                    else {
                                        _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), _this.range.startContainer.childNodes[_this.range.startOffset], 0);
                                    }
                                }
                                _this.getRangeNode();
                            }
                            if ((_this.parent.enterKey === 'P' && !shiftKey_1) || (_this.parent.enterKey === 'DIV' && !shiftKey_1) ||
                                (_this.parent.shiftEnterKey === 'P' && shiftKey_1) ||
                                (_this.parent.shiftEnterKey === 'DIV' && shiftKey_1)) {
                                if ((_this.range.startOffset === 1 && _this.parent.inputElement.childNodes.length === 1 && _this.parent.inputElement.childNodes[0].nodeName === 'TABLE') || (_this.parent.enterKey === 'BR' && shiftKey_1)) {
                                    var newElem = _this.createInsertElement(shiftKey_1);
                                    newElem.appendChild(_this.parent.createElement('BR'));
                                    _this.parent.inputElement.appendChild(newElem);
                                    _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), newElem, 0);
                                }
                                else {
                                    var nearBlockNode = void 0;
                                    if (isTableEnter && _this.parent.formatter.editorManager.domNode.isBlockNode(_this.startNode)) {
                                        Iif (_this.range.startContainer.nodeName === '#text' && !ej2_base_1.isNullOrUndefined(_this.range.startContainer.previousSibling) && _this.range.startContainer.previousSibling.nodeName === 'HR') {
                                            nearBlockNode = _this.range.startContainer.nextSibling;
                                        }
                                        else {
                                            nearBlockNode = _this.startNode;
                                        }
                                    }
                                    else {
                                        nearBlockNode = _this.parent.formatter.editorManager.domNode.blockParentNode(_this.startNode);
                                    }
                                    var isMediaNode = false;
                                    var isFocusedFirst = false;
                                    if (_this.range.startOffset !== 0 && _this.range.endOffset !== 0 &&
                                        _this.range.startContainer === _this.range.endContainer && !(!ej2_base_1.isNullOrUndefined(nearBlockNode.childNodes[0])
                                        && (nearBlockNode.childNodes[0].nodeName === 'IMG' || nearBlockNode.querySelectorAll('img, audio, video').length > 0))) {
                                        var startNodeText = _this.range.startContainer.textContent;
                                        var splitFirstText = startNodeText.substring(0, _this.range.startOffset);
                                        if (splitFirstText.charCodeAt(_this.range.startOffset - 1) !== 160 && splitFirstText.trim().length === 0) {
                                            isFocusedFirst = true;
                                        }
                                    }
                                    else if (_this.range.startOffset === 0 && _this.range.endOffset === 0) {
                                        isFocusedFirst = true;
                                    }
                                    _this.removeBRElement(nearBlockNode);
                                    var fireFoxEnterAtMiddle = ej2_base_1.Browser.userAgent.indexOf('Firefox') !== -1 && _this.range.startOffset === 0 && _this.range.startContainer === _this.range.endContainer &&
                                        _this.range.startContainer.nodeName === '#text' && !_this.parent.formatter.editorManager.domNode.isBlockNode(_this.range.startContainer.previousSibling) &&
                                        _this.range.startContainer.parentElement === _this.range.startContainer.previousSibling.parentElement;
                                    var preventZeroWithSpace = ((_this.range.startContainer.nodeName === '#text' && _this.range.startContainer.textContent.includes('\u200B') &&
                                        _this.range.startContainer.textContent.trim() === '\u200B') ||
                                        (_this.range.startContainer.nodeName === '#text' && !ej2_base_1.isNullOrUndefined(_this.range.startContainer.textContent[_this.range.startOffset]) &&
                                            _this.range.startContainer.textContent[_this.range.startOffset].includes('\u200B') && _this.range.startContainer.textContent[_this.range.startOffset] === '\u200B' &&
                                            _this.parent.inputElement.textContent[0] !== '\u200B'));
                                    if (!preventZeroWithSpace && !fireFoxEnterAtMiddle && ((_this.range.startOffset === 0 && _this.range.endOffset === 0) || isFocusedFirst) &&
                                        !(!ej2_base_1.isNullOrUndefined(_this.range.startContainer.previousSibling) &&
                                            (_this.range.startContainer.previousSibling.nodeName === 'IMG' || _this.range.startContainer.previousSibling.nodeName === 'BR'))) {
                                        var isNearBlockLengthZero = void 0;
                                        var newElem = void 0;
                                        if (!ej2_base_1.isNullOrUndefined(_this.range.startContainer.childNodes) &&
                                            (_this.range.startContainer.textContent.length === 0 ||
                                                (_this.range.startContainer.nodeName !== '#text' && !ej2_base_1.isNullOrUndefined(_this.range.startContainer.querySelector('.e-video-clickelem')) &&
                                                    _this.range.startContainer.querySelector('.e-video-clickelem').textContent.length === 0)) &&
                                            (_this.range.startContainer.querySelectorAll('img, audio, video').length > 0 ||
                                                !ej2_base_1.isNullOrUndefined(_this.range.startContainer.querySelector('.e-video-clickelem')) ||
                                                _this.range.startContainer.nodeName === 'IMG' || _this.range.startContainer.nodeName === 'TABLE')) {
                                            newElem = _this.createInsertElement(shiftKey_1);
                                            isMediaNode = true;
                                            isNearBlockLengthZero = false;
                                        }
                                        else {
                                            if ((nearBlockNode.textContent.trim().length !== 0 ||
                                                (!ej2_base_1.isNullOrUndefined(nearBlockNode.childNodes[0]) && nearBlockNode.childNodes[0].nodeName === 'IMG') ||
                                                (nearBlockNode.textContent.trim() === '' && nearBlockNode.querySelectorAll('img, audio, video').length > 0))) {
                                                if ((_this.range.startOffset === _this.range.endOffset && _this.range.startOffset !== 0)) {
                                                    newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, nearBlockNode, false).cloneNode(true);
                                                }
                                                else {
                                                    newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, nearBlockNode, true).cloneNode(true);
                                                    isMediaNode = true;
                                                }
                                                isNearBlockLengthZero = false;
                                            }
                                            else {
                                                newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, nearBlockNode, true).cloneNode(true);
                                                isNearBlockLengthZero = true;
                                            }
                                        }
                                        var insertElem = void 0;
                                        if (_this.formatTags.indexOf(newElem.nodeName.toLocaleLowerCase()) < 0) {
                                            insertElem = _this.createInsertElement(shiftKey_1);
                                        }
                                        else {
                                            insertElem = _this.parent.createElement(newElem.nodeName);
                                        }
                                        while (newElem.firstChild) {
                                            insertElem.appendChild(newElem.firstChild);
                                        }
                                        nearBlockNode.parentElement.insertBefore(insertElem, nearBlockNode);
                                        if (!isNearBlockLengthZero) {
                                            var currentFocusElem = insertElem;
                                            var finalFocusElem = void 0;
                                            if (_this.range.startOffset === _this.range.endOffset && _this.range.startOffset !== 0) {
                                                while (!ej2_base_1.isNullOrUndefined(currentFocusElem) && currentFocusElem.nodeName !== '#text' &&
                                                    currentFocusElem.nodeName !== 'BR') {
                                                    finalFocusElem = currentFocusElem;
                                                    currentFocusElem = currentFocusElem.lastChild;
                                                }
                                            }
                                            else {
                                                finalFocusElem = currentFocusElem;
                                            }
                                            finalFocusElem.innerHTML = '<br>';
                                            if (!isMediaNode) {
                                                ej2_base_1.detach(nearBlockNode);
                                            }
                                        }
                                        _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), insertElem.nextElementSibling, 0);
                                    }
                                    else Iif (nearBlockNode.textContent.length === 0 && !(!ej2_base_1.isNullOrUndefined(nearBlockNode.childNodes[0]) && nearBlockNode.childNodes[0].nodeName === 'IMG' ||
                                        (nearBlockNode.querySelectorAll('video').length > 0) || (nearBlockNode.querySelectorAll('audio').length > 0) || (nearBlockNode.querySelectorAll('img').length > 0))) {
                                        if (!ej2_base_1.isNullOrUndefined(nearBlockNode.children[0]) && nearBlockNode.children[0].tagName !== 'BR') {
                                            var newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, nearBlockNode, false).cloneNode(true);
                                            _this.parent.formatter.editorManager.domNode.insertAfter(newElem, nearBlockNode);
                                            _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), newElem, newElem.textContent.length >= 0 ? 0 : 1);
                                        }
                                        else {
                                            var insertElem = _this.createInsertElement(shiftKey_1);
                                            insertElem.innerHTML = '<br>';
                                            _this.parent.formatter.editorManager.domNode.insertAfter(insertElem, nearBlockNode);
                                            _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), insertElem, 0);
                                        }
                                    }
                                    else if (_this.range.startContainer === _this.range.endContainer && _this.range.startContainer.nodeName === 'SPAN' &&
                                        (_this.range.startContainer.classList.contains('e-video-wrap') ||
                                            _this.range.startContainer.classList.contains('e-audio-wrap'))) {
                                        if (nearBlockNode.textContent.trim().length > 0) {
                                            var newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, nearBlockNode, true);
                                            var audioVideoElem = !ej2_base_1.isNullOrUndefined(newElem.previousSibling.querySelector('.e-video-wrap')) ?
                                                newElem.previousSibling.querySelector('.e-video-wrap') : newElem.previousSibling.querySelector('.e-audio-wrap');
                                            var isBRInserted = false;
                                            var lastNode = audioVideoElem.previousSibling;
                                            while (!ej2_base_1.isNullOrUndefined(lastNode) && lastNode.nodeName !== '#text') {
                                                lastNode = lastNode.lastChild;
                                            }
                                            Iif (ej2_base_1.isNullOrUndefined(lastNode)) {
                                                var brElm = _this.parent.createElement('br');
                                                audioVideoElem.parentElement.appendChild(brElm);
                                                isBRInserted = true;
                                            }
                                            Iif (isBRInserted) {
                                                _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), audioVideoElem.parentElement, 0);
                                            }
                                            else {
                                                _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), lastNode, lastNode.textContent.length);
                                            }
                                            ej2_base_1.detach(audioVideoElem);
                                        }
                                        else {
                                            var newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, nearBlockNode, true);
                                            var focusElem = newElem.previousSibling;
                                            while (!ej2_base_1.isNullOrUndefined(focusElem.firstChild)) {
                                                ej2_base_1.detach(focusElem.firstChild);
                                            }
                                            var brElm = _this.parent.createElement('br');
                                            focusElem.appendChild(brElm);
                                            _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), focusElem, 0);
                                        }
                                        Eif (!ej2_base_1.isNullOrUndefined(_this.parent.audioModule)) {
                                            _this.parent.audioModule.hideAudioQuickToolbar();
                                        }
                                        Eif (!ej2_base_1.isNullOrUndefined(_this.parent.videoModule)) {
                                            _this.parent.videoModule.hideVideoQuickToolbar();
                                        }
                                    }
                                    else {
                                        var newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, nearBlockNode, true);
                                        if (!ej2_base_1.isNullOrUndefined(newElem.childNodes[0]) && newElem.childNodes[0].nodeName === '#text' &&
                                            newElem.childNodes[0].textContent.length === 0) {
                                            ej2_base_1.detach(newElem.childNodes[0]);
                                        }
                                        if (newElem.textContent.trim().length === 0 || (newElem.childNodes[0].textContent.trim().includes('\u200B') && newElem.childNodes[0].textContent.trim() === '\u200B')) {
                                            var brElm = _this.parent.createElement('br');
                                            if (_this.startNode.nodeName === 'A') {
                                                var startParentElem = _this.startNode.parentElement;
                                                _this.startNode.parentElement.insertBefore(brElm, _this.startNode);
                                                ej2_base_1.detach(_this.startNode);
                                                _this.startNode = startParentElem;
                                            }
                                            else {
                                                if (_this.startNode.nodeName !== 'BR') {
                                                    _this.startNode.appendChild(brElm);
                                                }
                                            }
                                            Iif (newElem.childNodes[0].textContent === '\n') {
                                                ej2_base_1.detach(newElem.childNodes[0]);
                                            }
                                            if (newElem.childNodes[0].textContent.trim().includes('\u200B') && newElem.childNodes[0].textContent.trim() === '\u200B') {
                                                ej2_base_1.detach(newElem.childNodes[0]);
                                            }
                                            _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), _this.startNode, 0);
                                        }
                                        Eif (((_this.parent.enterKey === 'P' || _this.parent.enterKey === 'DIV') && !shiftKey_1) || ((_this.parent.shiftEnterKey === 'DIV' ||
                                            _this.parent.shiftEnterKey === 'P') && shiftKey_1)) {
                                            var isHeadingTag = _this.formatTags.indexOf(newElem.nodeName.toLocaleLowerCase());
                                            if ((isHeadingTag < 0) || (isHeadingTag >= 0 && newElem.textContent.trim().length === 0)) {
                                                var insertElm = _this.createInsertElement(shiftKey_1);
                                                while (newElem.firstChild) {
                                                    insertElm.appendChild(newElem.firstChild);
                                                }
                                                _this.parent.formatter.editorManager.domNode.insertAfter(insertElm, newElem);
                                                ej2_base_1.detach(newElem);
                                                _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), _this.parent.formatter.editorManager.domNode.isBlockNode(_this.startNode) ?
                                                    insertElm : _this.startNode, 0);
                                            }
                                        }
                                    }
                                }
                                e.args.preventDefault();
                            }
                            if ((_this.parent.enterKey === 'BR' && !shiftKey_1) || (_this.parent.shiftEnterKey === 'BR' && shiftKey_1)) {
                                var currentParent = void 0;
                                if (!_this.parent.formatter.editorManager.domNode.isBlockNode(_this.startNode)) {
                                    var currentNode = _this.startNode;
                                    var previousNode = currentNode;
                                    while (!_this.parent.formatter.editorManager.domNode.isBlockNode(currentNode)) {
                                        previousNode = currentNode;
                                        currentNode = currentNode.parentElement;
                                    }
                                    currentParent = currentNode === _this.parent.inputElement ?
                                        previousNode : currentNode;
                                }
                                else {
                                    currentParent = _this.startNode;
                                }
                                var currentParentStyle = window.getComputedStyle(currentParent);
                                _this.removeBRElement(currentParent);
                                var isEmptyBrInserted = false;
                                var currentParentLastChild = currentParent.lastChild;
                                while (!ej2_base_1.isNullOrUndefined(currentParentLastChild) && !(currentParentLastChild.nodeName === '#text' || currentParentLastChild.nodeName === 'BR'
                                    || currentParentLastChild.nodeName === 'IMG')) {
                                    currentParentLastChild = currentParentLastChild.lastChild;
                                }
                                var isLastNodeLength = _this.range.startContainer === currentParentLastChild ?
                                    _this.range.startContainer.textContent.length : currentParent.textContent.length;
                                var isImageElement = (_this.range.startContainer.nodeName === 'IMG' || (_this.range.startContainer.childNodes.length > 0
                                    && _this.range.startContainer.childNodes[_this.range.startOffset].nodeName === 'IMG'));
                                if (currentParent !== _this.parent.inputElement &&
                                    _this.parent.formatter.editorManager.domNode.isBlockNode(currentParent) &&
                                    _this.range.startOffset === _this.range.endOffset &&
                                    (_this.range.startOffset === isLastNodeLength ||
                                        (currentParent.textContent.trim().length === 0 && isImageElement))) {
                                    var focusBRElem = _this.parent.createElement('br');
                                    if (_this.range.startOffset === 0 && _this.range.startContainer.nodeName === 'TABLE') {
                                        _this.range.startContainer.parentElement.insertBefore(focusBRElem, _this.range.startContainer);
                                    }
                                    else {
                                        if (currentParentLastChild.nodeName === 'BR' && currentParent.textContent.length === 0) {
                                            _this.parent.formatter.editorManager.domNode.insertAfter(focusBRElem, currentParentLastChild);
                                        }
                                        else if (_this.range.startOffset === 0 && _this.range.endOffset === 0 && isImageElement) {
                                            var imageElement = _this.range.startContainer.nodeName === 'IMG' ? _this.range.startContainer :
                                                _this.range.startContainer.childNodes[_this.range.startOffset];
                                            currentParent.insertBefore(focusBRElem, imageElement);
                                            focusBRElem = imageElement;
                                        }
                                        else {
                                            var lineBreakBRElem = _this.parent.createElement('br');
                                            var anchorElement = (!ej2_base_1.isNullOrUndefined(_this.range.startContainer.parentElement) && _this.range.startContainer.parentElement.nodeName === 'A'
                                                && _this.range.startContainer.parentElement.textContent.length === _this.range.startOffset)
                                                ? _this.range.startContainer.parentElement : _this.range.startContainer;
                                            _this.parent.formatter.editorManager.domNode.insertAfter(focusBRElem, anchorElement);
                                            _this.parent.formatter.editorManager.domNode.insertAfter(lineBreakBRElem, anchorElement);
                                            var brSibling = anchorElement.nextElementSibling;
                                            var brNextSibling = !ej2_base_1.isNullOrUndefined(brSibling) ? brSibling.nextElementSibling : null;
                                            Iif (!ej2_base_1.isNullOrUndefined(brSibling) && !ej2_base_1.isNullOrUndefined(brNextSibling) && !ej2_base_1.isNullOrUndefined(brNextSibling.nextElementSibling) &&
                                                brSibling.nodeName === 'BR' && brNextSibling.nodeName === 'BR' && brNextSibling.nextElementSibling.nodeName === 'BR') {
                                                brNextSibling.nextElementSibling.remove();
                                            }
                                        }
                                    }
                                    _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), focusBRElem, 0);
                                }
                                else if (!ej2_base_1.isNullOrUndefined(currentParent) && currentParent !== _this.parent.inputElement && currentParent.nodeName !== 'BR') {
                                    if (currentParent.textContent.trim().length === 0 || (currentParent.textContent.trim().length === 1 &&
                                        currentParent.textContent.charCodeAt(0) === 8203)) {
                                        if (currentParent.childElementCount > 1 && currentParent.lastElementChild.nodeName === 'IMG') {
                                            _this.insertBRElement();
                                        }
                                        else {
                                            var newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, currentParent, true).cloneNode(true);
                                            _this.parent.formatter.editorManager.domNode.insertAfter(newElem, currentParent);
                                            var outerBRElem = _this.parent.createElement('br');
                                            newElem.parentElement.insertBefore(outerBRElem, newElem);
                                            _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), newElem, 0);
                                        }
                                    }
                                    else {
                                        var newElem = void 0;
                                        var outerBRElem = _this.parent.createElement('br');
                                        if (_this.range.startOffset === 0 && _this.range.endOffset === 0 &&
                                            !ej2_base_1.isNullOrUndefined(currentParent.previousSibling) && currentParent.previousSibling.nodeName === 'BR' && currentParent.nodeName !== 'P' && currentParent.nodeName !== 'DIV') {
                                            newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, currentParent, false).cloneNode(true);
                                            _this.parent.formatter.editorManager.domNode.insertAfter(outerBRElem, currentParent);
                                            _this.insertFocusContent();
                                            var currentFocusElem = outerBRElem.nextSibling;
                                            while (!ej2_base_1.isNullOrUndefined(currentFocusElem) && currentFocusElem.nodeName !== '#text') {
                                                currentFocusElem = currentFocusElem.lastChild;
                                            }
                                            _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), currentFocusElem, 0);
                                            isEmptyBrInserted = true;
                                        }
                                        else if (currentParent !== _this.parent.inputElement &&
                                            (currentParentStyle.display === 'inline' || currentParentStyle.display === 'inline-block')) {
                                            newElem = _this.parent.formatter.editorManager.nodeCutter.SplitNode(_this.range, currentParent, true).cloneNode(true);
                                            currentParent.parentElement.insertBefore(outerBRElem, currentParent);
                                            _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), currentParent, 0);
                                            _this.insertFocusContent();
                                        }
                                        else {
                                            _this.insertBRElement();
                                        }
                                    }
                                }
                                else {
                                    _this.insertBRElement();
                                }
                                e.args.preventDefault();
                            }
                            _this.triggerActionComplete(e, shiftKey_1);
                            _this.parent.inputElement.dispatchEvent(new Event('input'));
                        }
                    });
                }
            }
        };
        EnterKeyAction.prototype.removeBRElement = function (currentElement) {
            if (ej2_base_1.Browser.userAgent.indexOf('Firefox') !== -1 &&
                this.range.endOffset === currentElement.textContent.length && (currentElement.textContent.length !== 0 ||
                currentElement.querySelectorAll('BR').length > 1) &&
                !ej2_base_1.isNullOrUndefined(currentElement.lastChild) && currentElement.lastChild.nodeName === 'BR') {
                ej2_base_1.detach(currentElement.lastChild);
            }
        };
        EnterKeyAction.prototype.insertBRElement = function () {
            var isEmptyBrInserted = false;
            var isFocusTextNode = true;
            if (this.range.endContainer.textContent.length === 0 && this.range.startContainer.nodeName === 'BR') {
                isFocusTextNode = false;
            }
            var brElm = this.parent.createElement('br');
            var findAnchorLastChild = this.startNode;
            while (findAnchorLastChild.lastChild) {
                findAnchorLastChild = findAnchorLastChild.lastChild;
            }
            var findAnchorElement = this.startNode.nodeName === 'A' && this.endNode.nodeName === 'A' &&
                !ej2_base_1.isNullOrUndefined(this.range.startContainer.parentElement) && this.range.startOffset === this.range.endOffset &&
                this.range.startContainer.textContent.trim().length === findAnchorLastChild.textContent.trim().length;
            if (this.startNode.nodeName === 'BR' && this.endNode.nodeName === 'BR' && this.range.startOffset === 0 && this.range.startOffset === this.range.endOffset) {
                this.parent.formatter.editorManager.domNode.insertAfter(brElm, this.startNode);
                isEmptyBrInserted = true;
            }
            else {
                if (this.startNode === this.parent.inputElement && !ej2_base_1.isNullOrUndefined(this.range.startContainer.previousSibling) &&
                    this.range.startContainer.previousSibling.nodeName === 'BR' && this.range.startContainer.textContent.length === 0) {
                    isEmptyBrInserted = true;
                }
                if (findAnchorElement) {
                    this.parent.formatter.editorManager.domNode.insertAfter(brElm, this.startNode);
                }
                else {
                    this.range.insertNode(brElm);
                }
            }
            if (isEmptyBrInserted || (!ej2_base_1.isNullOrUndefined(brElm.nextElementSibling) && brElm.nextElementSibling.tagName === 'BR') || (!ej2_base_1.isNullOrUndefined(brElm.nextSibling) && (brElm.nextSibling.textContent.length > 0 || (brElm.nextSibling.nodeName === '#text' && brElm.nextSibling.textContent.trim().length === 0 && !ej2_base_1.isNullOrUndefined(brElm.nextSibling.nextSibling) && brElm.nextSibling.nextSibling.textContent.trim().length > 0)))) {
                this.parent.formatter.editorManager.nodeSelection.setCursorPoint(this.parent.contentModule.getDocument(), !ej2_base_1.isNullOrUndefined(brElm.nextSibling) && isFocusTextNode ? brElm.nextSibling : brElm, 0);
                isEmptyBrInserted = false;
            }
            else {
                var brElements = this.parent.createElement('br');
                if (findAnchorElement) {
                    this.parent.formatter.editorManager.domNode.insertAfter(brElements, this.startNode);
                }
                else {
                    this.range.insertNode(brElements);
                }
                this.parent.formatter.editorManager.nodeSelection.setCursorPoint(this.parent.contentModule.getDocument(), brElm, 0);
            }
        };
        EnterKeyAction.prototype.insertFocusContent = function () {
            if (this.range.startContainer.textContent.length === 0) {
                if (this.range.startContainer.nodeName === '#text') {
                    this.range.startContainer.parentElement.innerHTML = '&#8203;';
                }
                else {
                    this.range.startContainer.innerHTML = '&#8203;';
                }
            }
        };
        EnterKeyAction.prototype.createInsertElement = function (shiftKey) {
            var insertElem;
            if ((this.parent.enterKey === 'DIV' && !shiftKey) || (this.parent.shiftEnterKey === 'DIV' && shiftKey)) {
                insertElem = this.parent.createElement('div');
            }
            else Eif ((this.parent.enterKey === 'P' && !shiftKey) || (this.parent.shiftEnterKey === 'P' && shiftKey)) {
                insertElem = this.parent.createElement('p');
            }
            var previousBlockNode = this.parent.formatter.editorManager.domNode.blockNodes()[0].previousSibling;
            var nextBlockNode = this.parent.formatter.editorManager.domNode.blockNodes()[0].nextSibling;
            if (!ej2_base_1.isNullOrUndefined(previousBlockNode) && previousBlockNode.nodeName !== '#text' && previousBlockNode.hasAttribute('style') && previousBlockNode.nodeName !== 'TABLE') {
                insertElem.setAttribute('style', previousBlockNode.getAttribute('style'));
            }
            if (ej2_base_1.isNullOrUndefined(previousBlockNode) && !ej2_base_1.isNullOrUndefined(nextBlockNode) && nextBlockNode.nodeName !== '#text' && nextBlockNode.hasAttribute('style') && nextBlockNode.nodeName !== 'TABLE') {
                insertElem.setAttribute('style', nextBlockNode.getAttribute('style'));
            }
            return insertElem;
        };
        EnterKeyAction.prototype.triggerActionComplete = function (e, shiftKey) {
            this.parent.trigger(events.actionComplete, { requestType: shiftKey ? 'ShiftEnterAction' : 'EnterAction', args: e.args });
        };
        EnterKeyAction.prototype.handleCursorAtTableSide = function (e, isStart, isEnd) {
            var _this = this;
            Eif (this.parent.enterKey !== 'BR') {
                var shiftKey_2 = e.args.shiftKey;
                var actionBeginArgs = {
                    cancel: false,
                    name: events.actionBegin,
                    requestType: shiftKey_2 ? 'ShiftEnterAction' : 'EnterAction',
                    originalEvent: e.args
                };
                this.parent.trigger(events.actionBegin, actionBeginArgs, function (actionBeginArgs) {
                    Eif (!actionBeginArgs.cancel) {
                        var newElement = _this.parent.createElement(_this.parent.enterKey);
                        newElement.innerHTML = '<br>';
                        var tableElement = void 0;
                        if (isStart) {
                            tableElement = _this.range.startContainer.childNodes[_this.range.startOffset];
                            tableElement.parentElement.insertBefore(newElement, tableElement);
                        }
                        if (isEnd) {
                            tableElement = _this.range.startContainer.childNodes[_this.range.startOffset - 1];
                            if (!ej2_base_1.isNullOrUndefined(tableElement.nextSibling)) {
                                tableElement.parentElement.insertBefore(newElement, tableElement.nextSibling);
                            }
                            else Eif (ej2_base_1.isNullOrUndefined(tableElement.nextSibling)) {
                                tableElement.parentElement.appendChild(newElement);
                            }
                        }
                        _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), newElement, 0);
                        e.args.preventDefault();
                        _this.triggerActionComplete(e, shiftKey_2);
                    }
                });
            }
        };
        EnterKeyAction.prototype.handleEnterKeyAtImageSide = function (e, isStart, isEnd) {
            var _this = this;
            var actionBeginArgs = {
                cancel: false,
                name: events.actionBegin,
                requestType: e.args.shiftKey ? 'ShiftEnterAction' : 'EnterAction',
                originalEvent: e.args
            };
            var directRange = false;
            if (this.range.startContainer.nodeName === 'IMG' && this.range.startOffset === 0) {
                directRange = true;
            }
            this.parent.trigger(events.actionBegin, actionBeginArgs, function (actionBeginArgs) {
                Eif (!actionBeginArgs.cancel) {
                    Eif (_this.parent.enterKey === 'BR') {
                        var newElement = _this.parent.createElement('BR');
                        var imageElement = void 0;
                        if (directRange) {
                            imageElement = _this.range.startContainer;
                            imageElement.parentElement.insertBefore(newElement, imageElement);
                            _this.parent.formatter.editorManager.nodeSelection.
                                setCursorPoint(_this.parent.contentModule.getDocument(), imageElement, 0);
                        }
                        Iif (isStart) {
                            imageElement = _this.range.startContainer.childNodes[_this.range.startOffset];
                            imageElement.parentElement.insertBefore(newElement, imageElement);
                            _this.parent.formatter.editorManager.nodeSelection.
                                setCursorPoint(_this.parent.contentModule.getDocument(), imageElement, 0);
                        }
                        if (isEnd) {
                            imageElement = _this.range.startContainer.childNodes[_this.range.startOffset - 1];
                            Iif (!ej2_base_1.isNullOrUndefined(imageElement.nextSibling)) {
                                imageElement.parentElement.insertBefore(newElement, imageElement.nextSibling);
                                _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), newElement.nextSibling, 0);
                            }
                            else Eif (ej2_base_1.isNullOrUndefined(imageElement.nextSibling)) {
                                imageElement.parentElement.appendChild(newElement);
                                var brElement = _this.parent.createElement('BR');
                                imageElement.parentElement.appendChild(brElement);
                                _this.parent.formatter.editorManager.nodeSelection.setCursorPoint(_this.parent.contentModule.getDocument(), brElement, 0);
                            }
                        }
                        e.args.preventDefault();
                        _this.triggerActionComplete(e, e.args.shiftKey);
                    }
                }
            });
        };
        EnterKeyAction.prototype.isTableOrImageStart = function () {
            var customHandlerElements = ['IMG', 'TABLE'];
            var startContainer = this.range.startContainer;
            var startOffset = this.range.startOffset;
            var isCursorAtStart = this.range.collapsed && (startContainer.nodeType === 1) &&
                startContainer.isContentEditable && startContainer.childNodes[startOffset] &&
                (customHandlerElements.indexOf(startContainer.childNodes[startOffset].nodeName) > -1);
            if (isCursorAtStart) {
                return { start: isCursorAtStart, startNodeName: startContainer.childNodes[startOffset].nodeName };
            }
            else {
                return { start: false, startNodeName: '' };
            }
        };
        EnterKeyAction.prototype.isTableOrImageEnd = function () {
            var customHandlerElements = ['IMG', 'TABLE'];
            var startContainer = this.range.startContainer;
            var startOffset = this.range.startOffset;
            var isCursorAtEnd = this.range.collapsed && (startContainer.nodeType === 1) &&
                startContainer.isContentEditable && startContainer.childNodes[startOffset - 1] &&
                (customHandlerElements.indexOf(startContainer.childNodes[startOffset - 1].nodeName) > -1);
            if (isCursorAtEnd) {
                return { end: isCursorAtEnd, endNodeName: startContainer.childNodes[startOffset - 1].nodeName };
            }
            else {
                return { end: false, endNodeName: '' };
            }
        };
        EnterKeyAction.prototype.processedTableImageCursor = function () {
            var _a = this.isTableOrImageStart(), start = _a.start, startNodeName = _a.startNodeName;
            var _b = this.isTableOrImageEnd(), end = _b.end, endNodeName = _b.endNodeName;
            return { start: start, startName: startNodeName, end: end, endName: endNodeName };
        };
        return EnterKeyAction;
    }());
    exports.EnterKeyAction = EnterKeyAction;
});