all files / block-manager/plugins/menus/ slash-command.js

100% Statements 67/67
100% Branches 17/17
100% Functions 18/18
100% Lines 67/67
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   1390× 1390× 1390× 1390×   1390× 1390× 1390× 1390× 1390×   1390× 1390× 1390× 1390× 1390×   1390×   1390× 1390× 1390× 26240×     747× 747× 176×   571× 571×     50×   10× 10× 10×   52× 52×   51× 51× 51×             144× 144× 69×     886× 886× 886×         1390× 1390×        
define(["require", "exports", "../../../common/utils/block", "../../../models/enums", "../../../common/constant", "../../../common/utils/common"], function (require, exports, block_1, enums_1, constant_1, common_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var SlashCommandModule = (function () {
        function SlashCommandModule(manager) {
            this.isPopupOpened = false;
            this.shortcutMap = new Map();
            this.parent = manager;
            this.addEventListeners();
        }
        SlashCommandModule.prototype.addEventListeners = function () {
            this.parent.observer.on(constant_1.events.keydown, this.onKeyDown, this);
            this.parent.observer.on('slashMenuCreated', this.handleSlashMenuCreated, this);
            this.parent.observer.on('slashCommandChange', this.handleSlashCommandChange, this);
            this.parent.observer.on('updateSlashMenuPopupState', this.updateSlashMenuPopupState, this);
            this.parent.observer.on(constant_1.events.destroy, this.destroy, this);
        };
        SlashCommandModule.prototype.removeEventListeners = function () {
            this.parent.observer.off(constant_1.events.keydown, this.onKeyDown);
            this.parent.observer.off('slashMenuCreated', this.handleSlashMenuCreated);
            this.parent.observer.off('slashCommandChange', this.handleSlashCommandChange);
            this.parent.observer.off('updateSlashMenuPopupState', this.updateSlashMenuPopupState);
            this.parent.observer.off(constant_1.events.destroy, this.destroy);
        };
        SlashCommandModule.prototype.handleSlashMenuCreated = function () {
            this.buildShortcutMap();
        };
        SlashCommandModule.prototype.buildShortcutMap = function () {
            var _this = this;
            this.shortcutMap.clear();
            this.parent.commandMenuSettings.commands.forEach(function (item) {
                _this.shortcutMap.set(item.shortcut.toLowerCase(), item);
            });
        };
        SlashCommandModule.prototype.onKeyDown = function (e) {
            var normalizedKey = common_1.getNormalizedKey(e);
            if (!normalizedKey) {
                return;
            }
            var commandItem = this.shortcutMap.get(normalizedKey);
            if (commandItem) {
                e.preventDefault();
                this.transformBlocks(commandItem);
            }
        };
        SlashCommandModule.prototype.handleSlashCommandChange = function (args) {
            this.transformBlocks(args.itemData);
        };
        SlashCommandModule.prototype.getHeadingProps = function (itemId) {
            var extractedType = itemId.replace('-command', '');
            var level = parseInt(extractedType.slice(-1), 10);
            return { level: level };
        };
        SlashCommandModule.prototype.transformBlocks = function (commandItem) {
            var selectedItem = commandItem.type;
            if (!selectedItem || !this.parent.currentFocusedBlock) {
                return;
            }
            var isHeadingType = selectedItem === enums_1.BlockType.Heading || selectedItem === enums_1.BlockType.CollapsibleHeading;
            var headingProps = isHeadingType ? this.getHeadingProps(commandItem.id) : undefined;
            this.parent.blockCommand.handleBlockTransformation({
                block: block_1.getBlockModelById(this.parent.currentFocusedBlock.id, this.parent.getEditorBlocks()),
                blockElement: this.parent.currentFocusedBlock,
                newBlockType: selectedItem,
                props: headingProps
            });
        };
        SlashCommandModule.prototype.updateSlashMenuPopupState = function (options) {
            this.isPopupOpened = options.isOpen;
            if (!options.isOpen) {
                this.parent.isPopupOpenedOnAddIconClick = false;
            }
        };
        SlashCommandModule.prototype.isPopupOpen = function () {
            var mentionPopupId = this.parent.blockContainer.id + "_popup";
            var commandPopupElement = document.querySelector("#" + mentionPopupId + ".e-blockeditor-command-menu");
            return this.isPopupOpened && (commandPopupElement && commandPopupElement.classList.contains('e-popup-open'));
        };
        SlashCommandModule.prototype.hidePopup = function () {
            this.parent.observer.notify('hideSlashMenuPopup');
        };
        SlashCommandModule.prototype.showPopup = function () {
            this.parent.observer.notify('showSlashMenuPopup');
        };
        SlashCommandModule.prototype.filterCommands = function (text, xOffset, yOffset) {
            this.parent.observer.notify('filterSlashCommands', { text: text, offsetX: xOffset, offsetY: yOffset });
        };
        SlashCommandModule.prototype.destroy = function () {
            this.removeEventListeners();
            this.shortcutMap = null;
        };
        return SlashCommandModule;
    }());
    exports.SlashCommandModule = SlashCommandModule;
});