all files / block-manager/base/ block-manager.js

95.43% Statements 209/219
88.66% Branches 86/97
92.86% Functions 26/28
95.87% Lines 209/218
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                       1390×               1390×   1458×   1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390×   1390×   1390×   1391× 1391× 1391× 8346× 8346× 8346×     1391×   335×   79× 79×   92× 92×   52× 52×   15× 15×     47× 47×   34× 34×   12× 12×       2207× 2207× 2207×     40850×     40850×   44×     44×   4188×   333×   331× 331× 331× 331× 331× 166×   165×     481× 481× 481× 481× 481×     10084× 10084× 10084× 6969×   3115× 3115× 186×   3115×   5679× 5679× 5679× 5679× 5679× 5679×   5679× 5679× 2015×   3664× 3664× 3664× 3664× 3664× 3664× 367×     412× 412× 1587× 1587×   412× 376×     51× 51× 51× 39× 39× 39× 39×   51× 51×   41× 41× 41× 41× 41×     8928× 8101×     8100×     8928×   4170× 2535× 2471×     64×       1390×         1390× 4170× 4170× 4170× 4170×     1390× 1390× 1390× 10× 10×   1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390×        
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
define(["require", "exports", "@syncfusion/ej2-base", "@syncfusion/ej2-popups", "../../common/constant", "../../common/constant", "../plugins/block/block-command", "../services/index", "../services/state-manager", "../../selection/selection", "../renderer/common/popup-renderer", "../plugins/menus/index", "../actions/floating-icon", "../../common/utils/index", "../../models/enums", "../../common/utils/dom", "../plugins/block/list", "../actions/undo", "../actions/methods", "../actions/formatting", "../plugins/inline/link", "../plugins/inline/inline-content", "../actions/clipboard", "../actions/mention", "../actions/drag", "../actions/inline-toolbar", "../actions/event", "../plugins/block/block-renderer", "../plugins/table/selection-manager"], function (require, exports, ej2_base_1, ej2_popups_1, constant_1, constants, block_command_1, index_1, state_manager_1, selection_1, popup_renderer_1, index_2, floating_icon_1, index_3, enums_1, dom_1, list_1, undo_1, methods_1, formatting_1, link_1, inline_content_1, clipboard_1, mention_1, drag_1, inline_toolbar_1, event_1, block_renderer_1, selection_manager_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var BlockManager = (function () {
        function BlockManager() {
            this.defaultKeyConfig = {
                bold: 'ctrl+b',
                italic: 'ctrl+i',
                underline: 'ctrl+u',
                strikethrough: 'ctrl+shift+x',
                link: 'ctrl+k',
                print: 'ctrl+p'
            };
            this.observer = new ej2_base_1.Observer(this);
        }
        BlockManager.prototype.updateContext = function (BlockEditorObj) {
            ej2_base_1.extend(this, this, BlockEditorObj);
        };
        BlockManager.prototype.initialize = function () {
            var populatedBlocks = index_1.BlockFactory.populateBlockProperties(this.blocks);
            this.blocks = populatedBlocks.slice();
            this.observer.notify('updateEditorBlocks', { blocks: this.blocks });
            this.blockCommand = new block_command_1.BlockCommand(this);
            this.stateManager = new state_manager_1.StateManager(this);
            this.blockService = new index_1.BlockService(this.blocks);
            this.eventService = new index_1.EventService(this);
            this.tableService = new index_1.TableService(this);
            this.tableSelectionManager = new selection_manager_1.TableSelectionManager(this);
            this.blockCommand.createDefaultEmptyBlock();
            this.blockRenderer = new block_renderer_1.BlockRenderer(this);
            this.listPlugin = new list_1.ListPlugin(this);
            this.nodeSelection = new selection_1.NodeSelection(this.blockContainer);
            this.popupRenderer = new popup_renderer_1.PopupRenderer(this);
            this.eventAction = new event_1.EventAction(this);
            this.floatingIconAction = new floating_icon_1.FloatingIcon(this);
            this.undoRedoAction = new undo_1.UndoRedoAction(this);
            this.formattingAction = new formatting_1.FormattingAction(this);
            this.clipboardAction = new clipboard_1.ClipboardAction(this);
            this.dragAndDropAction = new drag_1.DragAndDropAction(this);
            this.mentionAction = new mention_1.MentionAction(this);
            this.editorMethods = new methods_1.BlockEditorMethods(this);
            this.inlineContentInsertionModule = new inline_content_1.InlineContentInsertionModule(this);
            this.linkModule = new link_1.LinkModule(this);
            this.inlineToolbarModule = new inline_toolbar_1.InlineToolbarModule(this);
            this.blockActionMenuModule = new index_2.BlockActionMenuModule(this);
            this.contextMenuModule = new index_2.ContextMenuModule(this);
            this.slashCommandModule = new index_2.SlashCommandModule(this);
            this.scrollParentElements = ej2_popups_1.getScrollableParent(this.rootEditorElement);
            this.wireEvents();
            this.initializeKeyBindings();
        };
        BlockManager.prototype.wireEvents = function () {
            this.observer.on(constant_1.events.destroy, this.destroy, this);
        };
        BlockManager.prototype.unwireEvents = function () {
            this.observer.off(constant_1.events.destroy, this.destroy);
        };
        BlockManager.prototype.initializeKeyBindings = function () {
            var config = __assign({}, this.defaultKeyConfig, this.keyConfig);
            var map = new Map();
            for (var command in config) {
                Eif (Object.prototype.hasOwnProperty.call(config, command)) {
                    var keyCombo = config["" + command].toLowerCase().replace(/\s+/g, '');
                    map.set(keyCombo, command);
                }
            }
            this.keyCommandMap = map;
        };
        BlockManager.prototype.execCommand = function (options) {
            switch (options.command) {
                case 'AddBlock':
                    this.observer.notify(constants.ADDBLOCK, options.state);
                    break;
                case 'SplitBlock':
                    this.observer.notify(constants.SPLITBLOCK, options.state);
                    break;
                case 'DeleteBlock':
                    this.observer.notify(constants.DELETEBLOCK, options.state);
                    break;
                case 'DeleteAtCursor':
                    this.observer.notify(constants.DELETEATCURSOR, options.state);
                    break;
                case 'DuplicateBlock':
                    this.observer.notify(constants.DUPLICATEBLOCK, options.state);
                    break;
                case 'IndentBlock':
                    this.observer.notify(constants.INDENTBLOCK, options.state);
                    break;
                case 'MoveBlock':
                    this.observer.notify(constants.MOVEBLOCK, options.state);
                    break;
                case 'FormattingAction':
                    this.observer.notify(constants.FORMATTINGACTION, options.state);
                    break;
                case 'DeleteNonMergableBlock':
                    this.observer.notify(constants.DELETE_NON_MERGABLEBLOCK, options.state);
                    break;
            }
        };
        BlockManager.prototype.setFocusToBlock = function (block) {
            Eif (block) {
                block.focus();
                this.currentFocusedBlock = block;
            }
        };
        BlockManager.prototype.getEditorBlocks = function () {
            Iif (!this.blockService) {
                return [];
            }
            return this.blockService.getBlocks();
        };
        BlockManager.prototype.setEditorBlocks = function (blocks) {
            Iif (!this.blockService) {
                return;
            }
            this.blockService.setBlocks(blocks);
        };
        BlockManager.prototype.getBlockElementById = function (blockId) {
            return this.blockContainer.querySelector("#" + blockId);
        };
        BlockManager.prototype.adjustViewForFocusedBlock = function () {
            if (!this.currentFocusedBlock) {
                return;
            }
            var editorBlocks = this.getEditorBlocks();
            var lastBlock = editorBlocks[editorBlocks.length - 1];
            var containerRect = this.rootEditorElement.getBoundingClientRect();
            var blockRect = this.currentFocusedBlock.getBoundingClientRect();
            if (lastBlock && lastBlock.id === this.currentFocusedBlock.id) {
                this.rootEditorElement.scrollTo({ top: this.rootEditorElement.scrollHeight });
            }
            else if (blockRect.bottom > containerRect.bottom) {
                this.rootEditorElement.scrollTop += blockRect.bottom - containerRect.bottom;
            }
        };
        BlockManager.prototype.setFocusAndUIForNewBlock = function (blockElement) {
            this.togglePlaceholder(this.currentFocusedBlock, false);
            this.setFocusToBlock(blockElement);
            index_3.setCursorPosition(index_3.getBlockContentElement(blockElement), 0);
            this.togglePlaceholder(this.currentFocusedBlock, true);
            this.floatingIconAction.showFloatingIcons(this.currentFocusedBlock);
        };
        BlockManager.prototype.removeFocusAndUIForBlock = function (blockElement) {
            this.togglePlaceholder(blockElement, false);
            this.currentFocusedBlock = null;
            this.floatingIconAction.hideFloatingIcons();
        };
        BlockManager.prototype.getPlaceholderValue = function (block) {
            var props = block.properties;
            var tableCell = index_3.findCellById(block.parentId, this.getEditorBlocks());
            if (tableCell || (props && props.placeholder && props.placeholder !== '')) {
                return props.placeholder;
            }
            var constant = block.blockType.charAt(0).toLowerCase() + block.blockType.slice(1);
            if (block.blockType.endsWith(enums_1.BlockType.Heading) && props && props.level) {
                constant += props.level.toString();
            }
            return this.localeJson[constant];
        };
        BlockManager.prototype.togglePlaceholder = function (blockElement, isFocused) {
            var blockModel = blockElement ? index_3.getBlockModelById(blockElement.id, this.getEditorBlocks()) : null;
            var isTableBlock = blockElement && dom_1.findClosestParent(blockElement, "." + constants.TABLE_BLOCK_CLS);
            var isCalloutBlock = blockElement && dom_1.findClosestParent(blockElement, "." + constants.CALLOUT_BLOCK_CLS);
            var isCalloutHasOneChild = isCalloutBlock && isCalloutBlock.querySelectorAll("." + constants.BLOCK_CLS).length === 1;
            var isAlwaysOnBlock = blockModel && index_3.isAlwaysOnPlaceHolderBlk(blockModel.blockType);
            var tableCriteria = (isTableBlock &&
                ((blockModel && !(blockModel.properties.placeholder)) || !isFocused));
            var isNullPlaceholder = (blockModel && (blockModel && !('placeholder' in blockModel.properties)));
            if (!blockModel || isNullPlaceholder || tableCriteria || (isAlwaysOnBlock && !isFocused) || (isCalloutHasOneChild && !isFocused)) {
                return;
            }
            var blockType = blockElement.getAttribute('data-block-type');
            var placeholderValue = this.getPlaceholderValue(blockModel);
            var contentEle = index_3.getBlockContentElement(blockElement);
            var isEmptyContent = dom_1.isElementEmpty(contentEle);
            contentEle.setAttribute('placeholder', isEmptyContent && isFocused ? placeholderValue : '');
            if (isEmptyContent && blockType !== enums_1.BlockType.Code) {
                dom_1.clearBreakTags(contentEle);
            }
        };
        BlockManager.prototype.refreshPlaceholder = function () {
            var _this = this;
            this.rootEditorElement.querySelectorAll('.' + constants.CONTENT_CLS).forEach(function (el) {
                var blockEle = el.closest("." + constants.BLOCK_CLS);
                _this.togglePlaceholder(blockEle, false);
            });
            if (this.currentFocusedBlock) {
                this.togglePlaceholder(this.currentFocusedBlock, true);
            }
        };
        BlockManager.prototype.setCursorAfterBulkBlockAddition = function (insertionType) {
            var cursorElement = this.currentFocusedBlock;
            var cursorpos = cursorElement.textContent.length;
            if (insertionType === 'blocks') {
                cursorElement = (cursorElement.nextElementSibling || cursorElement);
                cursorpos = 0;
                this.togglePlaceholder(this.currentFocusedBlock, false);
                this.togglePlaceholder(cursorElement, true);
            }
            this.setFocusToBlock(cursorElement);
            index_3.setCursorPosition(index_3.getBlockContentElement(cursorElement), cursorpos);
        };
        BlockManager.prototype.updateFocusAndCursor = function (blockElement) {
            Eif (blockElement) {
                var content = index_3.getBlockContentElement(blockElement);
                this.setFocusToBlock(blockElement);
                index_3.setCursorPosition(content, 0);
                this.floatingIconAction.showFloatingIcons(blockElement);
            }
        };
        BlockManager.prototype.serializeValue = function (value) {
            if (!ej2_base_1.isNullOrUndefined(value) && value.trim() !== '') {
                if (this.enableHtmlEncode) {
                    value = index_3.sanitizeHelper(index_3.decode(value), this.enableHtmlSanitizer);
                    value = index_3.encode(value);
                }
                else {
                    value = index_3.sanitizeHelper(value, this.enableHtmlSanitizer);
                }
            }
            return value;
        };
        BlockManager.prototype.removeAndNullify = function (element) {
            if (element) {
                if (!ej2_base_1.isNullOrUndefined(element.parentNode)) {
                    ej2_base_1.remove(element);
                }
                else {
                    element.innerHTML = '';
                }
            }
        };
        BlockManager.prototype.destroyBlockEditor = function () {
            var properties = [
                'currentHoveredBlock',
                'currentFocusedBlock',
                'blockContainer'
            ];
            for (var _i = 0, properties_1 = properties; _i < properties_1.length; _i++) {
                var prop = properties_1[_i];
                var element = prop;
                this.removeAndNullify(this[element]);
                this[element] = null;
            }
        };
        BlockManager.prototype.destroy = function () {
            this.scrollParentElements = [];
            this.unwireEvents();
            if (!ej2_base_1.isNullOrUndefined(this.updateTimer)) {
                clearInterval(this.updateTimer);
                this.updateTimer = null;
            }
            this.popupRenderer = null;
            this.inlineToolbarModule = null;
            this.inlineContentInsertionModule = null;
            this.slashCommandModule = null;
            this.contextMenuModule = null;
            this.blockActionMenuModule = null;
            this.linkModule = null;
            this.nodeSelection = null;
            this.popupRenderer = null;
            this.eventAction = null;
            this.formattingAction = null;
            this.listPlugin = null;
            this.editorMethods = null;
            this.mentionAction = null;
            this.stateManager = null;
            this.blockService = null;
            this.eventService = null;
            this.blockCommand = null;
            this.blockRenderer = null;
            this.tableService = null;
            this.tableSelectionManager = null;
            this.keyCommandMap = null;
            this.defaultKeyConfig = null;
            this.dragAndDropAction = null;
            this.undoRedoAction = null;
            this.updateTimer = null;
            this.destroyBlockEditor();
        };
        return BlockManager;
    }());
    exports.BlockManager = BlockManager;
});