all files / blockeditor/base/ blockeditor.js

97.12% Statements 270/278
89.29% Branches 75/84
96.72% Functions 59/61
97.4% Lines 262/269
5 statements, 2 functions, 11 branches Ignored     
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 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434    14× 14× 14×                           102× 102× 102× 102×     1390×   1391×       8341×     1390×   1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390×   1390× 1390× 1390× 1390× 1389×   1390×   1390× 1390×   1393×   1390× 1390× 1390× 1390× 1390× 1390× 1390×   1455×                 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390×   1397× 1397×   1392×     1397×       1390× 1390× 1390×   1390×       1390×   1390× 1390×         1398× 1398×       1398× 1398× 96240×   1398× 774×   1398×   62×     13×     11×         75×             16×           13× 13×     15×   31×   1391×   1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390× 1390×   66× 66×   65× 65× 73× 73×                                                                                                                                                                            
/* istanbul ignore next */ 
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __assign = (this && this.__assign) || function () {
    __assign = Object.assign || function(t) {
        for (var s, i = 1, n = arguments.length; i < n; i++) {
            s = arguments[i];
            for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
                t[p] = s[p];
        }
        return t;
    };
    return __assign.apply(this, arguments);
};
/* istanbul ignore next */ 
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    Iif (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) Eif (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
define(["require", "exports", "@syncfusion/ej2-base", "../../models/common/user", "../../models/menus/command-menu-settings", "../../models/menus/inline-toolbar-settings", "../../models/menus/context-menu-settings", "../../models/menus/blockaction-menu-settings", "../../models/common/paste-settings", "../../models/common/label-settings", "../../common/utils/block", "../../common/utils/common", "../../common/utils/data", "../../common/constant", "../../common/constant", "../renderer/index", "../managers/index", "../renderer/index", "../../block-manager/base/block-manager", "../../models/common/index"], function (require, exports, ej2_base_1, user_1, command_menu_settings_1, inline_toolbar_settings_1, context_menu_settings_1, blockaction_menu_settings_1, paste_settings_1, label_settings_1, block_1, common_1, data_1, constant_1, constants, index_1, index_2, index_3, block_manager_1, index_4) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var BlockEditor = (function (_super) {
        __extends(BlockEditor, _super);
        function BlockEditor(options, element) {
            return _super.call(this, options, element) || this;
        }
        BlockEditor.prototype.preRender = function () {
            if (!this.element.id) {
                this.element.id = ej2_base_1.getUniqueID('e-' + this.getModuleName());
            }
        };
        BlockEditor.prototype.getDirective = function () {
            return 'EJS-BLOCKEDITOR';
        };
        BlockEditor.prototype.getModuleName = function () {
            return 'blockeditor';
        };
        BlockEditor.prototype.getPersistData = function () {
            return this.addOnPersist(['blocks']);
        };
        BlockEditor.prototype.render = function () {
            this.initialize();
        };
        BlockEditor.prototype.initialize = function () {
            this.initializeLocale();
            this.initializeManagers();
            this.intializeEngines();
            this.setDimension();
            this.setCssClass();
            this.setRtlClass();
            this.blockManager.stateManager.populateUniqueIds(this.blockManager.getEditorBlocks());
            Eif (!this.floatingIconRenderer.floatingIconContainer) {
                this.floatingIconRenderer.createFloatingIcons();
            }
            this.initializeMentionModules();
            this.renderBlocks(this.blockManager.getEditorBlocks());
            this.updateEditorReadyOnlyState();
            if (this.enableDragAndDrop) {
                this.intermediate.processActions('wireUnWireDragEvents', { enable: true });
            }
            this.eventManager.wireGlobalEvents();
        };
        BlockEditor.prototype.initializeLocale = function () {
            this.l10n = new ej2_base_1.L10n(this.getModuleName(), data_1.getLocaleItems(), this.locale);
            this.updateInternalLocaleCollection();
        };
        BlockEditor.prototype.updateInternalLocaleCollection = function () {
            this.localeJson = data_1.getCurrentLocaleJson(this.l10n);
        };
        BlockEditor.prototype.initializeManagers = function () {
            this.blockManager = new block_manager_1.BlockManager();
            this.intermediate = new index_2.Intermediate(this);
            this.renderblockContainer();
            this.blockManager.updateContext(__assign({ localeJson: this.localeJson, blocks: this.blocks, blockContainer: this.blockContainer, rootEditorElement: this.element, pasteCleanupSettings: this.pasteCleanupSettings, imageBlockSettings: this.imageBlockSettings, codeBlockSettings: this.codeBlockSettings, labelSettings: this.labelSettings, users: this.users, blockActionMenuSettings: this.blockActionMenuSettings, contextMenuSettings: this.contextMenuSettings, commandMenuSettings: this.commandMenuSettings, inlineToolbarSettings: this.inlineToolbarSettings }, this.getEditorProps()));
            this.blockManager.initialize();
            this.floatingIconRenderer = new index_1.FloatingIconRenderer(this);
            this.eventManager = new index_2.EventManager(this);
        };
        BlockEditor.prototype.getEditorProps = function () {
            return {
                readOnly: this.readOnly,
                undoRedoStack: this.undoRedoStack,
                enableHtmlEncode: this.enableHtmlEncode,
                enableHtmlSanitizer: this.enableHtmlSanitizer,
                enableDragAndDrop: this.enableDragAndDrop,
                keyConfig: this.keyConfig
            };
        };
        BlockEditor.prototype.intializeEngines = function () {
            this.menubarRenderer = new index_1.MenuBarRenderer(this);
            this.mentionRenderer = new index_1.MentionRenderer(this);
            this.tooltipRenderer = new index_1.TooltipRenderer(this);
            this.dialogRenderer = new index_1.DialogRenderer(this);
            this.dropdownListRenderer = new index_1.DropDownListRenderer(this);
            this.inlineContentInsertionModule = new index_3.InlineContentInsertionModule(this);
            this.inlineToolbarModule = new index_3.InlineToolbarModule(this);
            this.blockActionMenuModule = new index_3.BlockActionMenuModule(this);
            this.contextMenuModule = new index_3.ContextMenuModule(this);
            this.linkModule = new index_3.LinkModule(this);
        };
        BlockEditor.prototype.setDimension = function () {
            var cssText = "\n          width: " + (!ej2_base_1.isNullOrUndefined(this.width) ? ej2_base_1.formatUnit(this.width) : this.element.style.width) + ";\n          height: " + (!ej2_base_1.isNullOrUndefined(this.height) ? ej2_base_1.formatUnit(this.height) : this.element.style.height) + ";\n         ";
            ej2_base_1.updateCSSText(this.element, cssText);
        };
        BlockEditor.prototype.setCssClass = function () {
            if (this.cssClass) {
                ej2_base_1.addClass([this.element], this.cssClass.trim().split(' '));
            }
        };
        BlockEditor.prototype.setRtlClass = function () {
            this.element.classList.toggle(constants.RTL_CLS, this.enableRtl);
        };
        BlockEditor.prototype.updateLocale = function () {
            this.l10n.setLocale(this.locale);
            this.updateInternalLocaleCollection();
            this.blockManager.updateContext({ localeJson: this.localeJson });
            Eif (this.blockManager.currentFocusedBlock) {
                this.blockManager.togglePlaceholder(this.blockManager.currentFocusedBlock, true);
            }
            this.floatingIconRenderer.updateFloatingIconTooltipContent();
            this.notify(constant_1.events.localeChanged, {});
            this.blockManager.observer.notify(constant_1.events.localeChanged, {});
        };
        BlockEditor.prototype.initializeMentionModules = function () {
            this.slashCommandModule = new index_3.SlashCommandModule(this);
            this.inlineContentInsertionModule.initializeUserMention();
            this.inlineContentInsertionModule.initializeLabelContent();
        };
        BlockEditor.prototype.renderblockContainer = function () {
            this.blockContainer = this.createElement('div', {
                id: this.element.id + constants.BLOCK_CONTAINER_ID,
                className: constants.BLOCK_CONTAINER_CLS
            });
            this.element.appendChild(this.blockContainer);
        };
        BlockEditor.prototype.renderBlocks = function (blocks) {
            this.blockManager.blockCommand.createDefaultEmptyBlock();
            this.blockManager.blockRenderer.renderBlocks(blocks);
        };
        BlockEditor.prototype.getCurrentFocusedBlockModel = function () {
            if (!this.blockManager.currentFocusedBlock) {
                return null;
            }
            return block_1.getBlockModelById(this.blockManager.currentFocusedBlock.id, this.blockManager.getEditorBlocks());
        };
        BlockEditor.prototype.renderTemplate = function (args) {
            var templateName = args.block.id + 'template';
            this.clearTemplate([templateName]);
            var templateFunction = common_1.getTemplateFunction(args.block.template);
            ej2_base_1.append(templateFunction({}, this, templateName, 'template', this.isStringTemplate), args.templateElement);
            this.renderReactTemplates();
        };
        BlockEditor.prototype.updateEditorReadyOnlyState = function () {
            var _this = this;
            var defaultNonEditableElements = [
                'e-callout-icon', 'e-toggle-icon', 'e-image-container', 'e-checkmark-container', 'e-divider-block',
                'e-code-block-toolbar', 'e-code-block-copy-button', 'e-mention-chip'
            ].concat(this.blockManager.blockRenderer.tableRenderer.nonEditableElements);
            var editableElements = Array.from(this.element.querySelectorAll("[contenteditable='" + this.readOnly + "']:not([data-table-readonly-processed]"));
            editableElements = editableElements.filter(function (element) {
                return !defaultNonEditableElements.some(function (className) { return element.classList.contains(className); });
            });
            editableElements.forEach(function (element) {
                element.contentEditable = (!_this.readOnly).toString();
            });
            this.element.classList.toggle('e-readonly', this.readOnly);
        };
        BlockEditor.prototype.addBlock = function (block, targetId, isAfter) {
            this.blockManager.editorMethods.addBlock(block, targetId, isAfter);
        };
        BlockEditor.prototype.removeBlock = function (blockId) {
            this.blockManager.editorMethods.removeBlock(blockId);
        };
        BlockEditor.prototype.getBlock = function (blockId) {
            return this.blockManager.editorMethods.getBlock(blockId);
        };
        BlockEditor.prototype.moveBlock = function (fromBlockId, toBlockId) {
            this.blockManager.editorMethods.moveBlock(fromBlockId, toBlockId);
        };
        BlockEditor.prototype.updateBlock = function (blockId, properties) {
            return this.blockManager.editorMethods.updateBlock(blockId, properties);
        };
        BlockEditor.prototype.enableToolbarItems = function (itemId) {
            this.blockManager.editorMethods.enableDisableToolbarItems(itemId, true);
        };
        BlockEditor.prototype.disableToolbarItems = function (itemId) {
            this.blockManager.editorMethods.enableDisableToolbarItems(itemId, false);
        };
        BlockEditor.prototype.executeToolbarAction = function (action, value) {
            this.blockManager.editorMethods.executeToolbarAction(action, value);
        };
        BlockEditor.prototype.setSelection = function (contentId, startIndex, endIndex) {
            this.blockManager.editorMethods.setSelection(contentId, startIndex, endIndex);
        };
        BlockEditor.prototype.setCursorPosition = function (blockId, position) {
            this.blockManager.editorMethods.setCursorPosition(blockId, position);
        };
        BlockEditor.prototype.getSelectedBlocks = function () {
            return this.blockManager.editorMethods.getSelectedBlocks();
        };
        BlockEditor.prototype.getRange = function () {
            return this.blockManager.editorMethods.getRange();
        };
        BlockEditor.prototype.selectRange = function (range) {
            this.blockManager.editorMethods.selectRange(range);
        };
        BlockEditor.prototype.selectBlock = function (blockId) {
            this.blockManager.editorMethods.selectBlock(blockId);
        };
        BlockEditor.prototype.selectAllBlocks = function () {
            this.blockManager.editorMethods.selectAllBlocks();
        };
        BlockEditor.prototype.focusIn = function () {
            this.blockManager.editorMethods.focusIn();
        };
        BlockEditor.prototype.focusOut = function () {
            this.blockManager.editorMethods.focusOut();
        };
        BlockEditor.prototype.getBlockCount = function () {
            return this.blockManager.editorMethods.getBlockCount();
        };
        BlockEditor.prototype.print = function () {
            this.blockManager.editorMethods.print();
        };
        BlockEditor.prototype.renderBlocksFromJson = function (json, replace, targetBlockId) {
            if (replace === void 0) { replace = false; }
            return this.blockManager.editorMethods.renderBlocksFromJson(json, replace, targetBlockId);
        };
        BlockEditor.prototype.getDataAsJson = function (blockId) {
            return this.blockManager.editorMethods.getDataAsJson(blockId);
        };
        BlockEditor.prototype.getDataAsHtml = function (blockId) {
            return this.blockManager.editorMethods.getDataAsHtml(blockId);
        };
        BlockEditor.prototype.parseHtmlToBlocks = function (html) {
            return this.blockManager.editorMethods.parseHtmlToBlocks(html);
        };
        BlockEditor.prototype.destroy = function () {
            if (this.isDestroyed) {
                return;
            }
            this.notify(constant_1.events.destroy, {});
            this.blockManager.observer.notify(constant_1.events.destroy, {});
            this.mentionRenderer = null;
            this.menubarRenderer = null;
            this.tooltipRenderer = null;
            this.dialogRenderer = null;
            this.dropdownListRenderer = null;
            this.inlineToolbarModule = null;
            this.inlineContentInsertionModule = null;
            this.slashCommandModule = null;
            this.contextMenuModule = null;
            this.blockActionMenuModule = null;
            this.linkModule = null;
            this.floatingIconRenderer = null;
            this.eventManager = null;
            this.l10n = null;
            this.blockContainer = null;
            this.isRendered = false;
            this.intermediate = null;
            _super.prototype.destroy.call(this);
        };
        BlockEditor.prototype.onPropertyChanged = function (newProp, oldProp) {
            var prevProp = oldProp;
            if (!prevProp) {
                return;
            }
            this.blockManager.updateContext(this.getEditorProps());
            for (var _i = 0, _a = Object.keys(newProp); _i < _a.length; _i++) {
                var prop = _a[_i];
                switch (prop) {
                    case 'width':
                    case 'height':
                        this.setDimension();
                        break;
                    case 'cssClass':
                        this.setCssClass();
                        break;
                    case 'locale':
                        this.updateLocale();
                        break;
                    case 'enableRtl':
                        this.setRtlClass();
                        this.notify(constant_1.events.rtlChanged, {});
                        break;
                    case 'readOnly':
                        this.updateEditorReadyOnlyState();
                        this.intermediate.processActions('wireUnWireDragEvents', { enable: !this.readOnly });
                        break;
                    case 'keyConfig':
                        this.blockManager.initializeKeyBindings();
                        break;
                    case 'undoRedoStack':
                        this.blockManager.undoRedoAction.adjustUndoRedoStacks();
                        break;
                    case 'enableDragAndDrop':
                        this.intermediate.processActions('wireUnWireDragEvents', { enable: this.enableDragAndDrop });
                        break;
                    case 'enableHtmlSanitizer':
                    case 'enableHtmlEncode':
                    case 'blocks':
                        this.blockManager.editorMethods.replaceAllBlocks(prop === 'blocks' ? newProp.blocks : this.blockManager.getEditorBlocks());
                        break;
                    case 'labelSettings':
                    case 'users':
                        this.notify(constant_1.events.moduleChanged, { module: 'inlineContent', newProp: newProp, oldProp: oldProp });
                        break;
                    case 'commandMenuSettings':
                        this.notify(constant_1.events.moduleChanged, { module: 'slashCommand', newProp: newProp, oldProp: oldProp });
                        break;
                    case 'inlineToolbarSettings':
                        this.notify(constant_1.events.moduleChanged, { module: 'inlineToolbarSettings', newProp: newProp, oldProp: oldProp });
                        break;
                    case 'blockActionMenuSettings':
                        this.notify(constant_1.events.moduleChanged, { module: 'blockActionMenuSettings', newProp: newProp, oldProp: oldProp });
                        break;
                    case 'contextMenuSettings':
                        this.notify(constant_1.events.moduleChanged, { module: 'contextMenuSettings', newProp: newProp, oldProp: oldProp });
                        break;
                }
            }
        };
        __decorate([
            ej2_base_1.Property('auto')
        ], BlockEditor.prototype, "height", void 0);
        __decorate([
            ej2_base_1.Property('100%')
        ], BlockEditor.prototype, "width", void 0);
        __decorate([
            ej2_base_1.Property('')
        ], BlockEditor.prototype, "cssClass", void 0);
        __decorate([
            ej2_base_1.Property('en-US')
        ], BlockEditor.prototype, "locale", void 0);
        __decorate([
            ej2_base_1.Property(null)
        ], BlockEditor.prototype, "keyConfig", void 0);
        __decorate([
            ej2_base_1.Property(30)
        ], BlockEditor.prototype, "undoRedoStack", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], BlockEditor.prototype, "readOnly", void 0);
        __decorate([
            ej2_base_1.Property(false)
        ], BlockEditor.prototype, "enableHtmlEncode", void 0);
        __decorate([
            ej2_base_1.Property(true)
        ], BlockEditor.prototype, "enableHtmlSanitizer", void 0);
        __decorate([
            ej2_base_1.Property(true)
        ], BlockEditor.prototype, "enableDragAndDrop", void 0);
        __decorate([
            ej2_base_1.Property([])
        ], BlockEditor.prototype, "blocks", void 0);
        __decorate([
            ej2_base_1.Collection([], user_1.User)
        ], BlockEditor.prototype, "users", void 0);
        __decorate([
            ej2_base_1.Complex({}, command_menu_settings_1.CommandMenuSettings)
        ], BlockEditor.prototype, "commandMenuSettings", void 0);
        __decorate([
            ej2_base_1.Complex({}, inline_toolbar_settings_1.InlineToolbarSettings)
        ], BlockEditor.prototype, "inlineToolbarSettings", void 0);
        __decorate([
            ej2_base_1.Complex({}, blockaction_menu_settings_1.BlockActionMenuSettings)
        ], BlockEditor.prototype, "blockActionMenuSettings", void 0);
        __decorate([
            ej2_base_1.Complex({}, context_menu_settings_1.ContextMenuSettings)
        ], BlockEditor.prototype, "contextMenuSettings", void 0);
        __decorate([
            ej2_base_1.Complex({}, paste_settings_1.PasteCleanupSettings)
        ], BlockEditor.prototype, "pasteCleanupSettings", void 0);
        __decorate([
            ej2_base_1.Complex({ items: [], triggerChar: '$' }, label_settings_1.LabelSettings)
        ], BlockEditor.prototype, "labelSettings", void 0);
        __decorate([
            ej2_base_1.Complex({}, index_4.ImageBlockSettings)
        ], BlockEditor.prototype, "imageBlockSettings", void 0);
        __decorate([
            ej2_base_1.Complex({ languages: [], defaultLanguage: 'plaintext' }, index_4.CodeBlockSettings)
        ], BlockEditor.prototype, "codeBlockSettings", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "created", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "blockChanged", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "selectionChanged", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "blockDragging", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "blockDragStart", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "blockDropped", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "focus", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "blur", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "beforePasteCleanup", void 0);
        __decorate([
            ej2_base_1.Event()
        ], BlockEditor.prototype, "afterPasteCleanup", void 0);
        BlockEditor = __decorate([
            ej2_base_1.NotifyPropertyChanges
        ], BlockEditor);
        return BlockEditor;
    }(ej2_base_1.Component));
    exports.BlockEditor = BlockEditor;
});