all files / block-manager/actions/ inline-toolbar.js

97.44% Statements 190/195
87.36% Branches 76/87
100% Functions 31/31
97.4% Lines 187/192
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   1390× 1390×   1390× 1390× 1390× 1390× 1390× 1390× 1390×   1390× 1390× 1390× 1390× 1390× 1390× 1390×   1390× 1390×   1390× 1390×         747× 747× 176×   571× 571× 571×   571×           33×     33×     33× 33× 33×         35× 35× 35× 35×   33×     33× 33× 33×   674× 673×     10× 10×     514× 514×     514× 3026× 514× 514× 514× 58×   456× 456× 2708× 2708× 2708×     514× 514× 514× 565× 565× 565× 565× 2788× 1418× 529×       514×   514× 514× 514× 514×   511× 473× 473×   38× 38× 54× 54×   38× 38×   92×   565× 565×   1920× 1920× 1920× 1920× 1418×     502×     565× 1418×   565×   2708× 2708×   452× 452×   452× 452×   452× 452×   452× 452×           448× 448×   448× 448×   2708×   458×     453× 453× 453× 453× 1056× 1056× 1242× 1056×   186× 186×   1056× 1054×     453× 1056× 1056×   453×   898× 898× 898× 898× 898×     898× 898×                   1390× 1390× 1390× 1390× 1390×          
define(["require", "exports", "@syncfusion/ej2-base", "../../models/enums", "../../common/utils/index", "../../common/constant", "../../common/utils/dom", "../../common/constant"], function (require, exports, ej2_base_1, enums_1, index_1, constant_1, dom_1, constants) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var InlineToolbarModule = (function () {
        function InlineToolbarModule(manager) {
            this.parent = manager;
            this.addEventListeners();
        }
        InlineToolbarModule.prototype.addEventListeners = function () {
            this.parent.observer.on(constant_1.events.inlineToolbarItemClick, this.handleInlineToolbarItemClick, this);
            this.parent.observer.on('toolbarCreated', this.handleToolbarCreated, this);
            this.parent.observer.on('handleColorpickerChange', this.handleColorChange, this);
            this.parent.observer.on('popupWidthChanged', this.handlePopupWidthChanges, this);
            this.parent.observer.on(constant_1.events.formattingPerformed, this.toggleToolbarActiveState, this);
            this.parent.observer.on(constant_1.events.keydown, this.onKeydown, this);
            this.parent.observer.on(constant_1.events.destroy, this.destroy, this);
        };
        InlineToolbarModule.prototype.removeEventListeners = function () {
            this.parent.observer.off(constant_1.events.inlineToolbarItemClick, this.handleInlineToolbarItemClick);
            this.parent.observer.off('toolbarCreated', this.handleToolbarCreated);
            this.parent.observer.off('handleColorpickerChange', this.handleColorChange);
            this.parent.observer.off('popupWidthChanged', this.handlePopupWidthChanges);
            this.parent.observer.off(constant_1.events.formattingPerformed, this.toggleToolbarActiveState);
            this.parent.observer.off(constant_1.events.keydown, this.onKeydown);
            this.parent.observer.off(constant_1.events.destroy, this.destroy);
        };
        InlineToolbarModule.prototype.handleToolbarCreated = function () {
            this.toolbarEle = this.parent.rootEditorElement.querySelector('#' + this.parent.rootEditorElement.id + constants.BLOCKEDITOR_INLINETBAR_ID);
            this.init();
        };
        InlineToolbarModule.prototype.init = function () {
            this.popupElement = this.parent.rootEditorElement.querySelector('#' + this.parent.rootEditorElement.id + constants.INLINE_TBAR_POPUP_ID);
            this.popupObj = this.parent.popupRenderer.renderPopup({
                element: this.popupElement,
                content: this.toolbarEle
            });
        };
        InlineToolbarModule.prototype.onKeydown = function (e) {
            var normalizedKey = index_1.getNormalizedKey(e);
            if (!normalizedKey) {
                return;
            }
            var command = this.parent.keyCommandMap.get(normalizedKey);
            var allowedCommands = ['bold', 'italic', 'underline', 'strikethrough'];
            var tableBlk = this.parent.currentFocusedBlock &&
                this.parent.currentFocusedBlock.closest("." + constants.TABLE_BLOCK_CLS);
            if (allowedCommands.indexOf(command) !== -1) {
                e.preventDefault();
                if (window.getSelection().isCollapsed && !(tableBlk && this.parent.tableSelectionManager.hasActiveTableSelection(tableBlk))) {
                    this.parent.formattingAction.toggleActiveFormats(command);
                }
                else {
                    this.parent.execCommand({ command: 'FormattingAction', state: { command: command } });
                }
            }
        };
        InlineToolbarModule.prototype.isSelectionInTableHeader = function (range) {
            var startEl = (range.startContainer.nodeType === Node.ELEMENT_NODE
                ? range.startContainer
                : range.startContainer.parentElement);
            Iif (!startEl) {
                return false;
            }
            var headerCell = startEl.closest('th,[role="columnheader"]');
            Eif (!headerCell) {
                return false;
            }
            var inThead = headerCell.closest('thead');
            return !!inThead;
        };
        InlineToolbarModule.prototype.showInlineToolbar = function (range, event) {
            var notAllowedTypes = [enums_1.BlockType.Code];
            var blockElement = dom_1.findClosestParent(range.startContainer, '.' + constants.BLOCK_CLS);
            var blockType = blockElement.getAttribute('data-block-type');
            if (!this.parent.inlineToolbarSettings.enable || (notAllowedTypes.indexOf(blockType) !== -1)) {
                return;
            }
            Iif (this.isSelectionInTableHeader(range)) {
                return;
            }
            this.toggleToolbarActiveState();
            this.parent.popupRenderer.adjustPopupPositionRelativeToTarget(range, this.popupObj);
            this.popupObj.show();
        };
        InlineToolbarModule.prototype.hideInlineToolbar = function (e) {
            if (this.popupElement && !this.popupElement.classList.contains('e-popup-open')) {
                return;
            }
            this.popupObj.hide();
        };
        InlineToolbarModule.prototype.handleInlineToolbarItemClick = function (args) {
            var selectedItem = args.item.id;
            if (selectedItem === 'color' || selectedItem === 'bgColor') {
                return;
            }
            this.parent.execCommand({ command: 'FormattingAction', state: { command: selectedItem } });
        };
        InlineToolbarModule.prototype.toggleToolbarActiveState = function () {
            var range = index_1.getSelectedRange();
            Iif (!range) {
                return;
            }
            var toolbarItems = this.popupElement.querySelectorAll('.e-toolbar-item');
            toolbarItems.forEach(function (item) { return item.classList.remove('e-active'); });
            var blockElements = this.getBlocksInRange(range);
            var selectedContentModels = this.getSelectedContentModels(range, blockElements);
            if (!selectedContentModels || !selectedContentModels.length) {
                return;
            }
            var commonStyles = this.getCommonStyles(selectedContentModels);
            for (var _i = 0, _a = Array.from(toolbarItems); _i < _a.length; _i++) {
                var item = _a[_i];
                var command = item.getAttribute('data-command');
                this.toggleActiveState(item, command, commonStyles);
            }
        };
        InlineToolbarModule.prototype.getSelectedContentModels = function (range, blockElements) {
            var _this = this;
            var selectedContentModels = [];
            blockElements.forEach(function (blockElement) {
                var block = index_1.getBlockModelById(blockElement.id, _this.parent.getEditorBlocks());
                var contentElement = index_1.getBlockContentElement(blockElement);
                var selectedNodes = _this.getNodesInRange(index_1.normalizeRange(range), contentElement);
                selectedNodes.forEach(function (node) {
                    var model = block.content.find(function (content) { return content.id === node.id; });
                    if (model) {
                        selectedContentModels.push(model);
                    }
                });
            });
            return selectedContentModels;
        };
        InlineToolbarModule.prototype.getBlocksInRange = function (range) {
            var blocks = [];
            var startBlock = index_1.getParentBlock(range.startContainer);
            var endBlock = index_1.getParentBlock(range.endContainer);
            if (!startBlock) {
                return blocks;
            }
            if (startBlock === endBlock) {
                blocks.push(startBlock);
                return blocks;
            }
            var current = startBlock;
            while (current && current !== endBlock) {
                blocks.push(current);
                current = current.nextElementSibling;
            }
            Eif (endBlock) {
                blocks.push(endBlock);
            }
            return blocks.filter(function (block) { return block.hasAttribute('data-block-type'); });
        };
        InlineToolbarModule.prototype.getNodesInRange = function (range, container) {
            var selectedNodes = [];
            var walker = document.createTreeWalker(container, NodeFilter.SHOW_ELEMENT, {
                acceptNode: function (node) {
                    Eif (node.nodeType === Node.ELEMENT_NODE) {
                        var nodeStart = range.comparePoint(node, 0);
                        var nodeEnd = range.comparePoint(node, node.childNodes.length);
                        if (nodeStart <= 0 && nodeEnd >= 0) {
                            return NodeFilter.FILTER_ACCEPT;
                        }
                    }
                    return NodeFilter.FILTER_REJECT;
                }
            });
            while (walker.nextNode()) {
                selectedNodes.push(walker.currentNode);
            }
            return selectedNodes;
        };
        InlineToolbarModule.prototype.toggleActiveState = function (item, command, styles) {
            var isActive = false;
            switch (command) {
                case 'Bold':
                    isActive = styles.bold === true;
                    break;
                case 'Italic':
                    isActive = styles.italic === true;
                    break;
                case 'Underline':
                    isActive = styles.underline === true;
                    break;
                case 'Strikethrough':
                    isActive = styles.strikethrough === true;
                    break;
                case 'Superscript':
                    isActive = styles.superscript === true;
                    break;
                case 'Subscript':
                    isActive = styles.subscript === true;
                    break;
                case 'Uppercase':
                    isActive = styles.uppercase === true;
                    break;
                case 'Lowercase':
                    isActive = styles.lowercase === true;
                    break;
                case 'Color':
                    this.setColors('color', styles.color);
                    break;
                case 'BackgroundColor':
                    this.setColors('bgColor', styles.backgroundColor);
                    break;
            }
            item.classList.toggle('e-active', isActive);
        };
        InlineToolbarModule.prototype.getCommonStyles = function (contentModels) {
            if (!contentModels.length || !contentModels[0].properties ||
                !contentModels[0].properties.styles) {
                return {};
            }
            var firstStyles = contentModels[0].properties.styles;
            var styleKeys = Object.keys(firstStyles);
            var result = {};
            var _loop_1 = function (key) {
                var firstValue = firstStyles[key];
                var isCommon = contentModels.every(function (model, i) {
                    if (i === 0) {
                        return true;
                    }
                    var styles = model.properties.styles || {};
                    return styles[key] === firstValue;
                });
                if (isCommon) {
                    result[key] = firstValue;
                }
            };
            for (var _i = 0, styleKeys_1 = styleKeys; _i < styleKeys_1.length; _i++) {
                var key = styleKeys_1[_i];
                _loop_1(key);
            }
            return result;
        };
        InlineToolbarModule.prototype.setColors = function (type, value) {
            var colorBtn = this.toolbarEle.querySelector('#toolbar-color-dropdown');
            var bgColorBtn = this.toolbarEle.querySelector('#toolbar-bgcolor-dropdown');
            var currentColor = value ? value : '#000000';
            Eif (currentColor) {
                var button = type === 'color' ?
                    colorBtn.querySelector('.e-inline-color-icon') :
                    bgColorBtn.querySelector('.e-inline-bgColor-icon');
                Eif (button) {
                    ej2_base_1.updateCSSText(button, "border-bottom-color: " + currentColor + ";");
                }
            }
        };
        InlineToolbarModule.prototype.handleColorChange = function (args) {
            var command = args.type === 'bgColor' ? 'backgroundColor' : args.type;
            this.parent.formattingAction.execCommand({
                command: command,
                value: args.value
            });
            this.setColors(args.type, args.value);
        };
        InlineToolbarModule.prototype.handlePopupWidthChanges = function (data) {
            this.popupObj.width = this.parent.inlineToolbarSettings.popupWidth = data.value;
        };
        InlineToolbarModule.prototype.isPopupOpen = function () {
            var inlineTlbrPopupElement = document.querySelector('.e-popup.e-blockeditor-inline-toolbar-popup');
            return (inlineTlbrPopupElement.classList.contains('e-popup-open'));
        };
        InlineToolbarModule.prototype.destroy = function () {
            this.removeEventListeners();
            Eif (this.popupObj) {
                this.popupObj.destroy();
                ej2_base_1.detach(this.popupElement);
                this.popupObj = null;
            }
        };
        return InlineToolbarModule;
    }());
    exports.InlineToolbarModule = InlineToolbarModule;
});