all files / common/utils/ block.js

96.74% Statements 208/215
93.59% Branches 146/156
96.55% Functions 28/29
96.74% Lines 208/215
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   95×   48080× 117953× 117953× 23430×   94523× 94523× 12899× 12899× 1774×     92749× 3254× 3254× 3253× 5209× 5209× 11162× 11162× 11162× 11162× 2932×               19944×   1590×   1587× 1587×   1584× 1584× 1584×       115× 260× 260× 112×       981× 981×   2572× 2572×   2571× 2207×   364× 364× 203×           161× 161× 46×   115×   10416× 6308×   4108× 56024× 56024× 3273× 3273× 3273× 9547× 9547× 46403× 46403× 3257×             851×   7923× 5344×   7923× 25029× 6867×   18162×   1056×   176×   175×     175×   173× 45×   128× 128×       125×   15433× 42×   15391× 15391× 394×   14997×   1734×   1732×                           20486×   10041×       30×   22× 22× 22×   81×   80× 80× 80× 80× 80×       42×   41× 41× 41× 41× 41× 89× 89×   41×       83×   82×   80×         345× 541× 344×   197×     76× 76×     88803×   9597×   8596× 8596×   37×      
define(["require", "exports", "@syncfusion/ej2-base", "../../models/enums", "./selection", "./dom", "../../common/constant"], function (require, exports, ej2_base_1, enums_1, selection_1, dom_1, constants) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    function extractBlockTypeFromElement(blockElement) {
        return blockElement ? blockElement.getAttribute('data-block-type') : '';
    }
    exports.extractBlockTypeFromElement = extractBlockTypeFromElement;
    function getBlockModelById(blockId, blocks) {
        for (var _i = 0, blocks_1 = blocks; _i < blocks_1.length; _i++) {
            var block = blocks_1[_i];
            if (block.id === blockId) {
                return block;
            }
            var props = block.properties;
            if (props && props.children && props.children.length > 0) {
                var childBlock = getBlockModelById(blockId, props.children);
                if (childBlock) {
                    return childBlock;
                }
            }
            if (block.blockType === enums_1.BlockType.Table && block.properties) {
                var tprops = block.properties;
                if (tprops.rows && Array.isArray(tprops.rows)) {
                    for (var _a = 0, _b = tprops.rows; _a < _b.length; _a++) {
                        var row = _b[_a];
                        for (var _c = 0, _d = row.cells; _c < _d.length; _c++) {
                            var cell = _d[_c];
                            Eif (cell.blocks && cell.blocks.length) {
                                var inner = getBlockModelById(blockId, cell.blocks);
                                if (inner) {
                                    return inner;
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
    exports.getBlockModelById = getBlockModelById;
    function getBlockIndexById(id, blocks) {
        if (blocks.length === 0 || !id) {
            return -1;
        }
        var blockModel = getBlockModelById(id, blocks);
        if (!blockModel) {
            return -1;
        }
        var containerInfo = getContainerInfo(id, blocks);
        Eif (containerInfo && containerInfo.array) {
            return containerInfo.array.indexOf(blockModel);
        }
        return -1;
    }
    exports.getBlockIndexById = getBlockIndexById;
    function getContentModelById(contentId, contents) {
        for (var _i = 0, contents_1 = contents; _i < contents_1.length; _i++) {
            var content = contents_1[_i];
            if (content.id === contentId) {
                return content;
            }
        }
        return null;
    }
    exports.getContentModelById = getContentModelById;
    function getParentBlocksArray(blockId, blocks) {
        var info = getContainerInfo(blockId, blocks);
        return info ? info.array : null;
    }
    exports.getParentBlocksArray = getParentBlocksArray;
    function getContainerInfo(blockId, blocks) {
        var target = getBlockModelById(blockId, blocks);
        if (!target) {
            return null;
        }
        if (!target.parentId) {
            return { array: blocks, containerType: 'root', containerId: '' };
        }
        var parentBlock = getBlockModelById(target.parentId, blocks);
        if (parentBlock && parentBlock.properties.children) {
            return {
                array: parentBlock.properties.children,
                containerType: 'children',
                containerId: parentBlock.id
            };
        }
        var cell = findCellById(target.parentId, blocks);
        if (cell) {
            return { array: cell.blocks, containerType: 'cell', containerId: cell.id };
        }
        return { array: blocks, containerType: 'root', containerId: '' };
    }
    exports.getContainerInfo = getContainerInfo;
    function findCellById(cellId, blocks) {
        if (!cellId) {
            return null;
        }
        for (var _i = 0, blocks_2 = blocks; _i < blocks_2.length; _i++) {
            var block = blocks_2[_i];
            if (block.blockType === enums_1.BlockType.Table) {
                var tprops = block.properties;
                Eif (tprops && tprops.rows) {
                    for (var _a = 0, _b = tprops.rows; _a < _b.length; _a++) {
                        var row = _b[_a];
                        for (var _c = 0, _d = row.cells; _c < _d.length; _c++) {
                            var cell = _d[_c];
                            if (cell.id === cellId) {
                                return cell;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
    exports.findCellById = findCellById;
    function getParentBlock(element) {
        if (element && element.nodeType === Node.TEXT_NODE) {
            element = element.parentElement;
        }
        while (element && element.nodeType === Node.ELEMENT_NODE) {
            if (element.classList.contains('e-block')) {
                return element;
            }
            element = element.parentNode;
        }
        return null;
    }
    exports.getParentBlock = getParentBlock;
    function getAdjacentBlock(currentBlock, direction) {
        if (!currentBlock) {
            return null;
        }
        var adjacentBlock = direction === 'previous'
            ? currentBlock.previousElementSibling
            : currentBlock.nextElementSibling;
        if (adjacentBlock && adjacentBlock.classList.contains(constants.TABLE_BLOCK_CLS)) {
            return adjacentBlock.querySelector('.' + constants.BLOCK_CLS);
        }
        if (adjacentBlock instanceof HTMLElement && adjacentBlock.classList.contains('e-block')) {
            return adjacentBlock;
        }
        var calloutBlock = dom_1.findClosestParent(currentBlock, '.' + constants.CALLOUT_BLOCK_CLS);
        if (calloutBlock) {
            var calloutContent = calloutBlock.querySelector('.' + constants.CALLOUT_CONTENT_CLS);
            if (direction === 'previous' && currentBlock === calloutContent.firstElementChild) {
                return calloutBlock.previousElementSibling;
            }
            else if (direction === 'next' && currentBlock === calloutContent.lastElementChild) {
                return calloutBlock.nextElementSibling;
            }
        }
        return null;
    }
    exports.getAdjacentBlock = getAdjacentBlock;
    function getBlockContentElement(blockElement) {
        if (!blockElement) {
            return null;
        }
        var blockType = blockElement.getAttribute('data-block-type');
        if (blockType && blockType.startsWith('Collapsible')) {
            return blockElement.querySelector('.e-toggle-header').querySelector('.' + constants.CONTENT_CLS);
        }
        return blockElement.querySelector('.' + constants.CONTENT_CLS);
    }
    exports.getBlockContentElement = getBlockContentElement;
    function getContentElementBasedOnId(contentId, wrapper) {
        if (!wrapper || !contentId) {
            return null;
        }
        return wrapper.querySelector("#" + contentId);
    }
    exports.getContentElementBasedOnId = getContentElementBasedOnId;
    function getAdjacentCell(table, direction, currentCell) {
        Iif (!currentCell) {
            return null;
        }
        var rowIndex = parseInt(currentCell.dataset.row, 10);
        var colIndex = parseInt(currentCell.dataset.col, 10);
        var targetRowIndex = rowIndex;
        var targetColIndex = colIndex;
        switch (direction) {
            case 'up':
                targetRowIndex = rowIndex - 1;
                break;
            case 'down':
                targetRowIndex = rowIndex + 1;
                break;
            case 'left':
                targetColIndex = colIndex - 1;
                break;
            case 'right':
                targetColIndex = colIndex + 1;
                break;
        }
        var targetCellSelector = "[data-row=\"" + targetRowIndex + "\"][data-col=\"" + targetColIndex + "\"]";
        var targetCell = table.querySelector(targetCellSelector);
        return targetCell;
    }
    exports.getAdjacentCell = getAdjacentCell;
    function isListTypeBlock(blockType) {
        return blockType === enums_1.BlockType.BulletList || blockType === enums_1.BlockType.NumberedList || blockType === enums_1.BlockType.Checklist;
    }
    exports.isListTypeBlock = isListTypeBlock;
    function isChildrenTypeBlock(blockType) {
        return blockType === enums_1.BlockType.Callout || (blockType && blockType.toString().startsWith('Collapsible'));
    }
    exports.isChildrenTypeBlock = isChildrenTypeBlock;
    function isDividerBlock(blockElement) {
        return blockElement && blockElement.classList.contains('e-divider-block');
    }
    exports.isDividerBlock = isDividerBlock;
    function isNonContentEditableBlock(blockType) {
        return blockType === enums_1.BlockType.Divider || blockType === enums_1.BlockType.Image;
    }
    exports.isNonContentEditableBlock = isNonContentEditableBlock;
    function isCursorAtEdge(contentElement, isStart) {
        var isCursorAtStart = isAtStartOfBlock(contentElement);
        var isCursorAtEnd = isAtEndOfBlock(contentElement);
        return isStart ? isCursorAtStart : isCursorAtEnd;
    }
    exports.isCursorAtEdge = isCursorAtEdge;
    function isAtStartOfBlock(element) {
        if (!element) {
            return false;
        }
        var range = selection_1.getSelectedRange();
        var walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null);
        var firstTextNode = walker.nextNode();
        var startContainer = normalizeIntoContentElement(range.startContainer);
        return (range.collapsed &&
            (startContainer === firstTextNode || startContainer === element) &&
            range.startOffset === 0);
    }
    exports.isAtStartOfBlock = isAtStartOfBlock;
    function isAtEndOfBlock(element) {
        if (!element) {
            return false;
        }
        var range = selection_1.getSelectedRange();
        var walker = document.createTreeWalker(element, NodeFilter.SHOW_TEXT, null);
        var lastTextNode = null;
        var totalLength = 0;
        while (walker.nextNode()) {
            lastTextNode = walker.currentNode;
            totalLength = lastTextNode.textContent.length;
        }
        return (range.collapsed &&
            (range.startContainer === lastTextNode || range.startContainer === element) &&
            range.startOffset === totalLength);
    }
    exports.isAtEndOfBlock = isAtEndOfBlock;
    function normalizeIntoContentElement(element) {
        if (element instanceof HTMLElement && element.classList.contains('e-block')) {
            return element.querySelector('.' + constants.CONTENT_CLS);
        }
        else if (element instanceof HTMLElement && element.nodeName === 'BR') {
            return getClosestContentElementInDocument(element);
        }
        return element;
    }
    exports.normalizeIntoContentElement = normalizeIntoContentElement;
    function removeEmptyTextNodes(element) {
        Array.from(element.childNodes).forEach(function (node) {
            if (node.nodeType === Node.TEXT_NODE && !node.textContent.trim()) {
                ej2_base_1.detach(node);
            }
        });
    }
    exports.removeEmptyTextNodes = removeEmptyTextNodes;
    function getClosestContentElementInDocument(node) {
        while (node && node.nodeType === Node.TEXT_NODE || node.nodeType === Node.ELEMENT_NODE) {
            if (node.nodeType === Node.ELEMENT_NODE && node.id) {
                return node;
            }
            node = node.parentNode;
        }
        return null;
    }
    exports.getClosestContentElementInDocument = getClosestContentElementInDocument;
    function cleanCheckmarkElement(blockElement) {
        var checkmarkElement = blockElement.querySelector('.e-checkmark-container');
        if (checkmarkElement) {
            ej2_base_1.detach(checkmarkElement);
        }
    }
    exports.cleanCheckmarkElement = cleanCheckmarkElement;
    function isEmptyString(id) {
        return !id || id.trim() === '';
    }
    exports.isEmptyString = isEmptyString;
    function isChildrenProp(block) {
        return block.properties && 'children' in block.properties;
    }
    exports.isChildrenProp = isChildrenProp;
    function isAlwaysOnPlaceHolderBlk(blockType) {
        var showPlaceholdersAlwaysFor = [enums_1.BlockType.BulletList, enums_1.BlockType.Checklist, enums_1.BlockType.NumberedList, enums_1.BlockType.Quote];
        return blockType && (showPlaceholdersAlwaysFor.indexOf(blockType) >= 0);
    }
    exports.isAlwaysOnPlaceHolderBlk = isAlwaysOnPlaceHolderBlk;
    var nonMergableBlockTypes = new Set([enums_1.BlockType.Image, enums_1.BlockType.Divider]);
    function isNonMergableBlock(blockElement) {
        return nonMergableBlockTypes.has(extractBlockTypeFromElement(blockElement));
    }
    exports.isNonMergableBlock = isNonMergableBlock;
});