all files / block-manager/actions/ methods.js

97.6% Statements 203/208
88.57% Branches 93/105
100% Functions 35/35
97.57% Lines 201/206
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   1390×   73× 73× 73×                   11× 11×                 30×                   11×                           75× 75× 74×         1550× 1550× 1550×   1548×   1548× 43×   1505× 1505× 1505× 1505× 1505×     1505× 1505× 4501× 4501× 4501× 4501×       4501× 4501×   4501×   4501× 1707×     1505×   1551×   26× 26× 25× 25×         18× 18× 18×             36× 36× 36× 36× 174×   36×                   36× 36×                   15× 12× 12×         31× 31×   13× 13× 12× 12× 12× 12× 12×     10×           12× 12×             12×       10×           10× 10× 10×           10×                  
define(["require", "exports", "@syncfusion/ej2-base", "../../models/enums", "../../common/utils/index", "../../common/utils/html-parser", "../../common/constant", "../services/index"], function (require, exports, ej2_base_1, enums_1, index_1, html_parser_1, constants, index_2) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var BlockEditorMethods = (function () {
        function BlockEditorMethods(manager) {
            this.parent = manager;
        }
        BlockEditorMethods.prototype.addBlock = function (block, targetId, isAfter, preventUIUpdate) {
            var targetBlockModel = index_1.getBlockModelById(targetId, this.parent.getEditorBlocks());
            var populatedBlock = index_2.BlockFactory.populateBlockProperties([block], targetBlockModel ? targetBlockModel.parentId : '');
            this.parent.execCommand({
                command: 'AddBlock',
                state: {
                    block: populatedBlock[0],
                    targetBlock: this.parent.blockContainer.querySelector("#" + targetId),
                    isAfter: isAfter,
                    preventUIUpdate: preventUIUpdate
                }
            });
        };
        BlockEditorMethods.prototype.removeBlock = function (blockId) {
            var blockElement = this.parent.blockContainer.querySelector("#" + blockId);
            this.parent.execCommand({
                command: 'DeleteBlock',
                state: {
                    blockElement: blockElement,
                    isMethod: true,
                    isUndoRedoAction: false
                }
            });
        };
        BlockEditorMethods.prototype.getBlock = function (blockId) {
            return index_1.getBlockModelById(blockId, this.parent.getEditorBlocks());
        };
        BlockEditorMethods.prototype.moveBlock = function (fromBlockId, toBlockId) {
            this.parent.execCommand({
                command: 'MoveBlock',
                state: {
                    fromBlockIds: [fromBlockId],
                    toBlockId: toBlockId,
                    isInteracted: false
                }
            });
        };
        BlockEditorMethods.prototype.updateBlock = function (blockId, properties) {
            if (!blockId || !properties) {
                return false;
            }
            var block = this.getBlock(blockId);
            if (!block) {
                return false;
            }
            this.parent.blockService.updateBlock(blockId, properties);
            this.parent.stateManager.updateManagerBlocks();
            var updatedBlockModel = this.getBlock(blockId);
            var oldBlockElement = this.parent.getBlockElementById(blockId);
            var newBlockElement = this.parent.blockRenderer.createBlockElement(updatedBlockModel);
            var parentBlock = index_1.getBlockModelById(updatedBlockModel.parentId, this.parent.getEditorBlocks());
            var wrapper = this.parent.blockContainer;
            if (parentBlock) {
                var parentBlockElement = this.parent.getBlockElementById(parentBlock.id);
                var selector = parentBlock.blockType === enums_1.BlockType.Callout
                    ? '.' + constants.CALLOUT_CONTENT_CLS
                    : '.' + constants.TOGGLE_CONTENT_CLS;
                wrapper = parentBlockElement.querySelector(selector);
            }
            wrapper.insertBefore(newBlockElement, oldBlockElement);
            ej2_base_1.detach(oldBlockElement);
            if (index_1.isListTypeBlock(updatedBlockModel.blockType)) {
                this.parent.listPlugin.recalculateMarkersForListItems();
                Iif (block.blockType === enums_1.BlockType.Checklist) {
                    if (this.parent.blockRenderer.listRenderer) {
                        this.parent.blockRenderer.listRenderer.toggleCheckedState(updatedBlockModel, updatedBlockModel.properties.isChecked);
                    }
                }
            }
            return true;
        };
        BlockEditorMethods.prototype.executeToolbarAction = function (command, value) {
            var builtInCommands = Object.keys(enums_1.CommandName);
            if (builtInCommands.indexOf(command) !== -1) {
                var convertedCommand = command.toLowerCase();
                this.parent.formattingAction.execCommand({ command: convertedCommand, value: value });
            }
        };
        BlockEditorMethods.prototype.setSelection = function (contentId, start, end) {
            var contentElement = this.parent.blockContainer.querySelector("#" + contentId);
            if (contentElement) {
                index_1.setSelectionRange(contentElement.lastChild, start, end);
            }
        };
        BlockEditorMethods.prototype.setCursorPosition = function (blockId, position) {
            var blockElement = this.parent.getBlockElementById(blockId);
            if (blockElement) {
                var contentElement = index_1.getBlockContentElement(blockElement);
                index_1.setCursorPosition(contentElement, position);
            }
        };
        BlockEditorMethods.prototype.getSelectedBlocks = function () {
            var _this = this;
            var range = this.getRange();
            if (!range) {
                return null;
            }
            var tableBlk = this.parent.currentFocusedBlock &&
                this.parent.currentFocusedBlock.closest("." + constants.TABLE_BLOCK_CLS);
            if (tableBlk && this.parent.tableSelectionManager.hasActiveTableSelection(tableBlk)) {
                return this.parent.tableSelectionManager.getSelectedCellBlocks(tableBlk);
            }
            var selectedBlocks = [];
            var editorBlocks = this.parent.getEditorBlocks();
            var blockElements = this.parent.blockContainer.querySelectorAll('.' + constants.BLOCK_CLS);
            var parent = range.commonAncestorContainer;
            var element = parent.nodeType === Node.ELEMENT_NODE
                ? parent
                : parent.parentElement;
            var isSelectionInsideChild = !!(element && element.closest("." + constants.CALLOUT_CONTENT_CLS + ", ." + constants.TOGGLE_CONTENT_CLS + ", ." + constants.TABLE_CELL_BLK_CONTAINER));
            blockElements.forEach(function (blockElement) {
                var blockRange = document.createRange();
                blockRange.selectNodeContents(blockElement);
                var block = index_1.getBlockModelById(blockElement.id, editorBlocks);
                var isChildrenRootParent = isSelectionInsideChild &&
                    (blockElement.classList.contains(constants.CALLOUT_BLOCK_CLS) ||
                        blockElement.classList.contains(constants.TOGGLE_BLOCK_CLS) ||
                        blockElement.classList.contains(constants.TABLE_BLOCK_CLS));
                var tableRootBlk = blockElement.closest("." + constants.TABLE_BLOCK_CLS);
                var isSelectionInsideTableCell = _this.parent.currentFocusedBlock &&
                    !ej2_base_1.isNullOrUndefined(_this.parent.currentFocusedBlock.closest('.e-cell-blocks-container'));
                var canAllowTableBlks = (!tableRootBlk || blockElement.classList.contains('e-table-block')
                    || isSelectionInsideTableCell);
                if (block && range.intersectsNode(blockElement) && !isChildrenRootParent && canAllowTableBlks) {
                    selectedBlocks.push(block);
                }
            });
            return selectedBlocks;
        };
        BlockEditorMethods.prototype.getRange = function () {
            return index_1.getSelectedRange();
        };
        BlockEditorMethods.prototype.selectRange = function (range) {
            var selection = window.getSelection();
            if (selection) {
                selection.removeAllRanges();
                selection.addRange(range);
            }
        };
        BlockEditorMethods.prototype.selectBlock = function (blockId) {
            var blockElement = this.parent.getBlockElementById(blockId);
            if (blockElement) {
                var range = document.createRange();
                range.selectNodeContents(blockElement);
                this.selectRange(range);
            }
        };
        BlockEditorMethods.prototype.selectAllBlocks = function () {
            var range = document.createRange();
            range.selectNodeContents(this.parent.blockContainer);
            this.selectRange(range);
        };
        BlockEditorMethods.prototype.focusIn = function () {
            var startBlock = this.parent.getEditorBlocks()[0];
            var startBlkEle = this.parent.getBlockElementById(startBlock.id);
            Eif (startBlkEle) {
                this.parent.setFocusAndUIForNewBlock(startBlkEle);
            }
        };
        BlockEditorMethods.prototype.focusOut = function () {
            this.parent.removeFocusAndUIForBlock(this.parent.currentFocusedBlock);
            Eif (this.parent.blockContainer) {
                this.parent.blockContainer.blur();
                var selection = window.getSelection();
                selection.removeAllRanges();
            }
        };
        BlockEditorMethods.prototype.getBlockCount = function () {
            return this.parent.blocks.length;
        };
        BlockEditorMethods.prototype.enableDisableToolbarItems = function (itemId, enable) {
            var toolbarPopup = document.querySelector('#' + this.parent.rootEditorElement.id + constants.INLINE_TBAR_POPUP_ID);
            var ids = typeof itemId === 'string' ? [itemId] : itemId;
            var parentToolbarElements = [];
            var tbarItemModels = [];
            var items = this.parent.inlineToolbarSettings.items;
            ids.forEach(function (id) {
                items.forEach(function (it) {
                    Eif (typeof it === 'string') {
                        Eif (it.toLowerCase === id.toLowerCase) {
                            var defaults = index_1.getInlineToolbarItems();
                            var match = defaults.find(function (d) {
                                return d.command && d.command.toLowerCase() === it.toLowerCase();
                            });
                            tbarItemModels.push(match);
                        }
                    }
                    else {
                        if (it.id === id) {
                            tbarItemModels.push(it);
                        }
                    }
                });
            });
            tbarItemModels.forEach(function (item) {
                var element = toolbarPopup.querySelector("[data-command=" + item.command + "]");
                parentToolbarElements.push(element);
            });
            this.parent.observer.notify('enableDisableTbarItems', {
                items: parentToolbarElements,
                isEnable: enable
            });
        };
        BlockEditorMethods.prototype.getDataAsJson = function (blockId) {
            if (blockId) {
                var block = index_1.getBlockModelById(blockId, this.parent.getEditorBlocks());
                return block ? index_1.sanitizeBlock(block) : null;
            }
            else {
                return this.parent.getEditorBlocks().map(function (block) { return index_1.sanitizeBlock(block); });
            }
        };
        BlockEditorMethods.prototype.getDataAsHtml = function (blockId) {
            if (blockId) {
                var block = index_1.getBlockModelById(blockId, this.parent.getEditorBlocks());
                return block ? html_parser_1.getBlockDataAsHTML([block], this.parent.rootEditorElement.id) : null;
            }
            else {
                return html_parser_1.getBlockDataAsHTML(this.parent.getEditorBlocks(), this.parent.rootEditorElement.id);
            }
        };
        BlockEditorMethods.prototype.parseHtmlToBlocks = function (html) {
            var container = ej2_base_1.createElement('div', { innerHTML: html });
            return html_parser_1.convertHtmlElementToBlocks(container, true);
        };
        BlockEditorMethods.prototype.renderBlocksFromJson = function (json, replace, targetBlockId) {
            try {
                var blocksJson = typeof json === 'string' ? JSON.parse(json) : json;
                var blocks = this.extractBlocks(blocksJson);
                var sanitizedBlocks = blocks.map(function (block) { return index_1.sanitizeBlock(block); });
                this.parent.stateManager.populateUniqueIds(sanitizedBlocks);
                var populatedBlocks = index_2.BlockFactory.populateBlockProperties(sanitizedBlocks);
                if (replace) {
                    return this.replaceAllBlocks(populatedBlocks);
                }
                else {
                    return this.insertBlocksAtPosition(populatedBlocks, targetBlockId);
                }
            }
            catch (e) {
                console.error('Error rendering blocks from JSON:', e);
                return false;
            }
        };
        BlockEditorMethods.prototype.extractBlocks = function (blocksJson) {
            var blocks = [];
            if (Array.isArray(blocksJson)) {
                blocks = blocksJson;
            }
            else Eif (blocksJson && typeof blocksJson === 'object') {
                if (Array.isArray(blocksJson.blocks)) {
                    blocks = blocksJson.blocks;
                }
                else {
                    if (blocksJson.blockType) {
                        blocks = [blocksJson];
                    }
                }
            }
            return blocks;
        };
        BlockEditorMethods.prototype.replaceAllBlocks = function (blocks) {
            this.parent.setEditorBlocks([]);
            this.parent.blockContainer.innerHTML = '';
            if (blocks.length === 0) {
                this.parent.blockCommand.createDefaultEmptyBlock(true);
                return true;
            }
            this.parent.setEditorBlocks(blocks);
            this.parent.stateManager.updateManagerBlocks();
            this.parent.blockRenderer.renderBlocks(this.parent.getEditorBlocks());
            return true;
        };
        BlockEditorMethods.prototype.insertBlocksAtPosition = function (blocks, targetBlockId) {
            if (blocks.length === 0) {
                return false;
            }
            var insertionPointId = targetBlockId;
            if (!insertionPointId) {
                if (this.parent.currentFocusedBlock) {
                    insertionPointId = this.parent.currentFocusedBlock.id;
                }
                else {
                    insertionPointId = this.parent.getEditorBlocks()[this.parent.getEditorBlocks().length - 1].id;
                    var blockElement = this.parent.getBlockElementById(insertionPointId);
                    this.parent.setFocusToBlock(blockElement);
                }
            }
            var lastInsertedElement;
            for (var i = 0; i < blocks.length; i++) {
                var block = blocks[i];
                var targetId = i === 0 ? insertionPointId : lastInsertedElement.id;
                var addedBlock = this.parent.blockCommand.addBlock({
                    block: block,
                    targetBlock: this.parent.getBlockElementById(targetId),
                    isAfter: true,
                    preventEventTrigger: true
                });
                lastInsertedElement = this.parent.getBlockElementById(addedBlock.id);
            }
            Eif (lastInsertedElement) {
                var contentElement = index_1.getBlockContentElement(lastInsertedElement);
                this.parent.setFocusToBlock(lastInsertedElement);
                index_1.setCursorPosition(contentElement, contentElement.innerText.length);
            }
            return true;
        };
        BlockEditorMethods.prototype.print = function () {
            var blockHtml = html_parser_1.getBlockDataAsHTML(this.parent.blocks, this.parent.rootEditorElement.id);
            var tempDiv = ej2_base_1.createElement('div');
            tempDiv.innerHTML = blockHtml;
            var printWind = window.open('', 'print', 'height=' + window.outerHeight + ',width=' + window.outerWidth);
            Iif (ej2_base_1.Browser.info.name === 'msie') {
                printWind.resizeTo(screen.availWidth, screen.availHeight);
            }
            printWind = ej2_base_1.print(tempDiv, printWind);
        };
        return BlockEditorMethods;
    }());
    exports.BlockEditorMethods = BlockEditorMethods;
});