all files / block-manager/plugins/menus/ context-menu.js

98.95% Statements 94/95
92.11% Branches 35/38
100% Functions 19/19
98.95% Lines 94/95
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   1390× 1390× 1390× 1390× 1390×   1390× 1390× 1390× 1390× 1390× 1390×   1390× 1390× 1390× 1390× 1390× 1390×   1390×   1390× 1390× 1390× 15281×     747× 747× 176×   571× 571× 436× 436×     13× 13×     13× 13× 13× 13× 13× 13×                 441× 441×   208× 208×   170× 170×         56× 56×         14×   13× 13× 13× 13× 13× 13×     13×   13× 13× 13× 13× 13×                   13×   171×   1390× 1390×        
define(["require", "exports", "../../../models/enums", "../../../common/constant", "../../../common/constant", "../../../common/utils/common", "../../../common/utils/block", "../../../common/utils/selection"], function (require, exports, enums_1, constant_1, constants, common_1, block_1, selection_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var ContextMenuModule = (function () {
        function ContextMenuModule(manager) {
            this.isPopupOpened = false;
            this.isClipboardEmptyCache = true;
            this.shortcutMap = new Map();
            this.parent = manager;
            this.addEventListeners();
        }
        ContextMenuModule.prototype.addEventListeners = function () {
            this.parent.observer.on(constant_1.events.keydown, this.onKeyDown, this);
            this.parent.observer.on('contextMenuCreated', this.handleContextMenuCreated, this);
            this.parent.observer.on('contextMenuBeforeOpen', this.handleContextMenuBeforeOpen, this);
            this.parent.observer.on('updateContextMenuState', this.updateContextMenuPopupState, this);
            this.parent.observer.on('contextMenuSelection', this.handleContextMenuSelection, this);
            this.parent.observer.on(constant_1.events.destroy, this.destroy, this);
        };
        ContextMenuModule.prototype.removeEventListeners = function () {
            this.parent.observer.off(constant_1.events.keydown, this.onKeyDown);
            this.parent.observer.off('contextMenuCreated', this.handleContextMenuCreated);
            this.parent.observer.off('contextMenuBeforeOpen', this.handleContextMenuBeforeOpen);
            this.parent.observer.off('updateContextMenuState', this.updateContextMenuPopupState);
            this.parent.observer.off('contextMenuSelection', this.handleContextMenuSelection);
            this.parent.observer.off(constant_1.events.destroy, this.destroy);
        };
        ContextMenuModule.prototype.handleContextMenuCreated = function () {
            this.buildShortcutMap();
        };
        ContextMenuModule.prototype.buildShortcutMap = function () {
            var _this = this;
            this.shortcutMap.clear();
            this.parent.contextMenuSettings.items.forEach(function (item) {
                _this.shortcutMap.set(item.shortcut.toLowerCase(), item);
            });
        };
        ContextMenuModule.prototype.onKeyDown = function (e) {
            var normalizedKey = common_1.getNormalizedKey(e);
            if (!normalizedKey) {
                return;
            }
            var menuItem = this.shortcutMap.get(normalizedKey);
            if (menuItem && menuItem.id !== 'cut' && menuItem.id !== 'copy' && menuItem.id !== 'paste') {
                e.preventDefault();
                this.handleContextMenuActions(menuItem, e);
            }
        };
        ContextMenuModule.prototype.handleContextMenuBeforeOpen = function (args) {
            var _this = this;
            Iif (!this.parent.currentFocusedBlock) {
                this.parent.setFocusAndUIForNewBlock(this.parent.currentHoveredBlock);
            }
            this.toggleDisabledItems();
            this.parent.blockActionMenuModule.toggleBlockActionPopup(true);
            this.parent.linkModule.hideLinkPopup();
            setTimeout(function () {
                Eif (_this.parent.inlineToolbarModule) {
                    _this.parent.inlineToolbarModule.hideInlineToolbar(args.event);
                }
            }, 50);
        };
        ContextMenuModule.prototype.updateContextMenuPopupState = function (value) {
            this.isPopupOpened = value.isOpen;
        };
        ContextMenuModule.prototype.handleContextMenuSelection = function (args) {
            this.handleContextMenuActions(args.item, args.event);
        };
        ContextMenuModule.prototype.handleIndentationAction = function (shouldDecrease) {
            this.parent.execCommand({ command: 'IndentBlock', state: {
                    blockIDs: this.parent.editorMethods.getSelectedBlocks().map(function (block) { return block.id; }),
                    shouldDecrease: shouldDecrease
                } });
        };
        ContextMenuModule.prototype.handleContextMenuActions = function (menuItem, e) {
            var prop = menuItem.id.toLowerCase();
            switch (prop) {
                case 'undo':
                    this.parent.undoRedoAction.undo();
                    break;
                case 'redo':
                    this.parent.undoRedoAction.redo();
                    break;
                case 'cut':
                    this.parent.clipboardAction.handleContextCut();
                    break;
                case 'copy':
                    this.parent.clipboardAction.handleContextCopy();
                    break;
                case 'paste':
                    this.parent.clipboardAction.handleContextPaste();
                    break;
                case 'link':
                    this.parent.linkModule.showLinkPopup(e);
                    break;
                case 'increaseindent':
                case 'decreaseindent':
                    this.handleIndentationAction(prop === 'decreaseindent');
                    break;
            }
        };
        ContextMenuModule.prototype.toggleDisabledItems = function () {
            if (!selection_1.getSelectedRange() || !this.parent.currentFocusedBlock) {
                return;
            }
            var blockModel = block_1.getBlockModelById(this.parent.currentFocusedBlock.id, this.parent.getEditorBlocks());
            var tableBlk = this.parent.currentFocusedBlock.closest("." + constants.TABLE_BLOCK_CLS);
            var notAllowedTypes = [enums_1.BlockType.Image, enums_1.BlockType.Code];
            var isNotAllowedType = notAllowedTypes.indexOf(blockModel.blockType) !== -1;
            var previousBlockElement = block_1.getAdjacentBlock(this.parent.currentFocusedBlock, 'previous');
            var previousBlockModel = previousBlockElement
                ? block_1.getBlockModelById(previousBlockElement.id, this.parent.getEditorBlocks())
                : null;
            var canIndent = (!tableBlk && (!previousBlockModel ||
                (previousBlockModel && blockModel.indent <= previousBlockModel.indent) && !isNotAllowedType));
            var canOutdent = !tableBlk && (blockModel.indent > 0 && !isNotAllowedType);
            var isSelection = selection_1.getSelectedRange().toString().trim().length > 0;
            var selectedBlocks = this.parent.editorMethods.getSelectedBlocks();
            var canAllowLink = isSelection && !isNotAllowedType && (selectedBlocks && selectedBlocks.length === 1);
            var menuState = {
                'increaseindent': canIndent,
                'decreaseindent': canOutdent,
                'undo': this.parent.undoRedoAction.canUndo(),
                'redo': this.parent.undoRedoAction.canRedo(),
                'link': canAllowLink,
                'cut': isSelection,
                'copy': isSelection,
                'paste': true
            };
            this.parent.observer.notify('enableDisableContextMenuItems', menuState);
        };
        ContextMenuModule.prototype.isPopupOpen = function () {
            return this.isPopupOpened;
        };
        ContextMenuModule.prototype.destroy = function () {
            this.removeEventListeners();
            this.shortcutMap = null;
        };
        return ContextMenuModule;
    }());
    exports.ContextMenuModule = ContextMenuModule;
});