all files / block-manager/plugins/inline/ inline-content.js

100% Statements 88/88
94.12% Branches 32/34
100% Functions 16/16
100% Lines 85/85
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   1390× 1390×   1390× 1390×   1390× 1390×   35× 35× 35× 35× 35×             35×   37× 37×   35× 35× 35×   36×       35× 35×       35×   38× 38× 38× 38×   37× 37× 37×   39× 36×   35× 35× 35× 35× 35× 35× 35× 62× 22×   35× 35× 35×     35× 35× 35× 35×     70× 70× 70× 50× 37× 37× 37× 37×     70×   35× 35× 35× 35× 35× 18×   17× 17×   35×   1390×        
define(["require", "exports", "../../../models/enums", "../../../common/utils/index", "../../../common/constant", "../../../block-manager/services/block-factory", "../../../common/constant"], function (require, exports, enums_1, index_1, constants, block_factory_1, constant_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var InlineContentInsertionModule = (function () {
        function InlineContentInsertionModule(manager) {
            this.parent = manager;
            this.addEventListeners();
        }
        InlineContentInsertionModule.prototype.addEventListeners = function () {
            this.parent.observer.on('inlineContentInsertion', this.handleInlineContentInsertion, this);
            this.parent.observer.on(constant_1.events.destroy, this.destroy, this);
        };
        InlineContentInsertionModule.prototype.removeEventListeners = function () {
            this.parent.observer.off('inlineContentInsertion', this.handleInlineContentInsertion);
            this.parent.observer.off(constant_1.events.destroy, this.destroy);
        };
        InlineContentInsertionModule.prototype.handleInlineContentInsertion = function (args) {
            this.parent.mentionAction.cleanMentionArtifacts(this.parent.currentFocusedBlock);
            var contentType = (args.value.toString().indexOf('e-user-mention-item-template')) > 0 ? enums_1.ContentType.Mention : enums_1.ContentType.Label;
            var mentionChar = contentType === enums_1.ContentType.Mention ? '@' : this.parent.labelSettings.triggerChar;
            this.parent.mentionAction.removeMentionQueryKeysFromModel(mentionChar);
            var options = {
                block: index_1.getBlockModelById(this.parent.currentFocusedBlock.id, this.parent.getEditorBlocks()),
                blockElement: this.parent.currentFocusedBlock,
                range: index_1.getSelectedRange().cloneRange(),
                contentType: contentType,
                itemData: args.itemData
            };
            this.processInsertion(options);
        };
        InlineContentInsertionModule.prototype.processInsertion = function (options) {
            var range = options.range, contentType = options.contentType, blockElement = options.blockElement;
            if (!range || !blockElement) {
                return;
            }
            var rangeParent = this.getRangeParent(range);
            var insertedNode = this.findInsertedNode(contentType, rangeParent);
            this.splitAndReorganizeContent(insertedNode, contentType, rangeParent, options);
        };
        InlineContentInsertionModule.prototype.getRangeParent = function (range) {
            return range.startContainer.nodeType === Node.TEXT_NODE
                ? range.startContainer.parentElement
                : range.startContainer;
        };
        InlineContentInsertionModule.prototype.findInsertedNode = function (contentType, rangeParent) {
            var _a;
            var contentClassMap = (_a = {},
                _a[enums_1.ContentType.Mention] = 'e-mention-chip',
                _a[enums_1.ContentType.Label] = 'e-mention-chip',
                _a);
            return rangeParent.querySelector("." + contentClassMap["" + contentType]);
        };
        InlineContentInsertionModule.prototype.splitAndReorganizeContent = function (insertedNode, contentType, rangeParent, options) {
            var _a;
            var block = options.block;
            var blockContentElement = rangeParent.closest('.' + constants.CONTENT_CLS);
            if (!blockContentElement) {
                return null;
            }
            var nodesToProcess = Array.from(rangeParent.childNodes);
            var insertionIndex = nodesToProcess.indexOf(insertedNode);
            if (insertionIndex === -1) {
                return null;
            }
            var affectingContent = block.content.find(function (content) { return content.id === index_1.getClosestContentElementInDocument(rangeParent).id; });
            if (!affectingContent) {
                return null;
            }
            var oldBlock = index_1.decoupleReference(index_1.sanitizeBlock(block));
            var beforeContents = this.processContents(nodesToProcess.slice(0, insertionIndex), affectingContent);
            var insertedContent = this.createInlineContentModel(insertedNode, contentType, options);
            var afterContents = this.processContents(nodesToProcess.slice(insertionIndex + 1), affectingContent, true);
            var affectingIndex = block.content.indexOf(affectingContent);
            var newContentModels = beforeContents.concat([insertedContent], afterContents);
            (_a = block.content).splice.apply(_a, [affectingIndex, 1].concat(newContentModels));
            if (block.content.findIndex(function (c) { return c.id === insertedContent.id; }) === block.content.length - 1) {
                block.content.push(block_factory_1.BlockFactory.createTextContent());
            }
            this.parent.blockService.updateContent(block.id, block.content);
            this.parent.stateManager.updateManagerBlocks();
            this.parent.observer.notify('modelChanged', { type: 'ReRenderBlockContent', state: {
                    data: [{ block: block, oldBlock: oldBlock }]
                } });
            this.parent.undoRedoAction.trackContentChangedForUndoRedo(oldBlock, index_1.decoupleReference(index_1.sanitizeBlock(block)));
            var nextSibling = blockContentElement.querySelector('#' + insertedContent.id).nextElementSibling;
            Eif (nextSibling) {
                index_1.setCursorPosition(nextSibling, 0);
            }
        };
        InlineContentInsertionModule.prototype.processContents = function (nodes, baseContentModel, generateNewIds) {
            if (generateNewIds === void 0) { generateNewIds = false; }
            var contentModels = [];
            nodes.forEach(function (node) {
                if (node.nodeType === Node.TEXT_NODE && node.textContent !== '') {
                    var newContent = index_1.decoupleReference(index_1.sanitizeContent(baseContentModel));
                    newContent.id = generateNewIds ? index_1.generateUniqueId(constants.CONTENT_ID_PREFIX) : newContent.id;
                    newContent.content = node.textContent;
                    contentModels.push(newContent);
                }
            });
            return contentModels;
        };
        InlineContentInsertionModule.prototype.createInlineContentModel = function (element, contentType, options) {
            var user = options.itemData;
            var labelItem = options.itemData;
            var contentValue = contentType === enums_1.ContentType.Mention ? user.user : element.innerText;
            var newContent;
            if (contentType === enums_1.ContentType.Mention) {
                newContent = block_factory_1.BlockFactory.createMentionContent({ content: contentValue }, { userId: user.id });
            }
            else Eif (contentType === enums_1.ContentType.Label) {
                newContent = block_factory_1.BlockFactory.createLabelContent({ content: contentValue }, { labelId: labelItem.id });
            }
            return newContent;
        };
        InlineContentInsertionModule.prototype.destroy = function () {
            this.removeEventListeners();
        };
        return InlineContentInsertionModule;
    }());
    exports.InlineContentInsertionModule = InlineContentInsertionModule;
});