all files / block-manager/services/ block-factory.js

94.42% Statements 186/197
94.27% Branches 148/157
90.63% Functions 29/32
94.38% Lines 151/160
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                         6624×   4934×   171×   103×   162×   329×   84×   121×   135×   20×   113×   43×   49×   349×         8144×   7250×   385×   249×   256×       105× 105× 105×   7531× 7531× 7531×   180× 180× 180× 180×   63× 63× 63×   88× 88× 88×   167× 167× 167×   334× 334× 334×   125× 125× 125×   115× 115× 115× 115×             45× 45× 45× 45× 45×             137× 137× 137× 137×             25× 25× 25×     349× 349× 349× 349× 349× 872×     872× 856×   872× 126×     349× 799× 294×   799× 2149× 608×   2149×     2149×     349×   349× 349× 349× 349× 349× 349× 349× 349× 349×                                             4385× 4385× 6122× 3046×   6122× 6122× 5535× 7242×     6122× 6122× 271×   6122×   4385×   9977× 9977× 9977×   430× 430× 430×   270× 270× 270×   276× 276× 276×                            
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);
};
define(["require", "exports", "../../common/utils/index", "../../common/constant", "../../models/enums"], function (require, exports, index_1, constants, enums_1) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    var BlockFactory = (function () {
        function BlockFactory() {
        }
        BlockFactory.createBlockFromPartial = function (block) {
            switch (block.blockType) {
                case enums_1.BlockType.Paragraph:
                    return this.createParagraphBlock(block, block.properties);
                case enums_1.BlockType.Heading:
                    return this.createHeadingBlock(block, block.properties);
                case enums_1.BlockType.Checklist:
                    return this.createChecklistBlock(block, block.properties);
                case enums_1.BlockType.BulletList:
                    return this.createBulletListBlock(block, block.properties);
                case enums_1.BlockType.NumberedList:
                    return this.createNumberedListBlock(block, block.properties);
                case enums_1.BlockType.Code:
                    return this.createCodeBlock(block, block.properties);
                case enums_1.BlockType.Quote:
                    return this.createQuoteBlock(block, block.properties);
                case enums_1.BlockType.Callout:
                    return this.createCalloutBlock(block, block.properties);
                case enums_1.BlockType.Divider:
                    return this.createDividerBlock(block, block.properties);
                case enums_1.BlockType.CollapsibleParagraph:
                    return this.createCollapsibleParagraphBlock(block, block.properties);
                case enums_1.BlockType.CollapsibleHeading:
                    return this.createCollapsibleHeadingBlock(block, block.properties);
                case enums_1.BlockType.Image:
                    return this.createImageBlock(block, block.properties);
                case enums_1.BlockType.Table:
                    return this.createTableBlock(block, block.properties);
                case enums_1.BlockType.Template:
                    return this.createTemplateBlock(block, block.properties);
                default:
                    return null;
            }
        };
        BlockFactory.createContentFromPartial = function (content) {
            switch (content.contentType) {
                case enums_1.ContentType.Text:
                    return this.createTextContent(content, content.properties);
                case enums_1.ContentType.Link:
                    return this.createLinkContent(content, content.properties);
                case enums_1.ContentType.Mention:
                    return this.createMentionContent(content, content.properties);
                case enums_1.ContentType.Label:
                    return this.createLabelContent(content, content.properties);
                default:
                    return null;
            }
        };
        BlockFactory.createChecklistBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ blockType: enums_1.BlockType.Checklist }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { properties: __assign({ isChecked: false }, this.defaultInnerBlockProps, innerProps) });
        };
        BlockFactory.createParagraphBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ blockType: enums_1.BlockType.Paragraph }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { properties: __assign({}, this.defaultInnerBlockProps, innerProps) });
        };
        BlockFactory.createHeadingBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            var sanitizedInnerProps = index_1.sanitizeHeadingProps(innerProps);
            return __assign({ blockType: enums_1.BlockType.Heading }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { properties: __assign({ level: 1 }, this.defaultInnerBlockProps, sanitizedInnerProps) });
        };
        BlockFactory.createImageBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ blockType: enums_1.BlockType.Image }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { content: [], properties: __assign({ src: '', altText: '', width: '', height: '' }, innerProps) });
        };
        BlockFactory.createCodeBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ blockType: enums_1.BlockType.Code }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { properties: __assign({ language: 'javascript' }, innerProps) });
        };
        BlockFactory.createBulletListBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ blockType: enums_1.BlockType.BulletList }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { properties: __assign({}, this.defaultInnerBlockProps, innerProps) });
        };
        BlockFactory.createNumberedListBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ blockType: enums_1.BlockType.NumberedList }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { properties: __assign({}, this.defaultInnerBlockProps, innerProps) });
        };
        BlockFactory.createQuoteBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ blockType: enums_1.BlockType.Quote }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { properties: __assign({}, this.defaultInnerBlockProps, innerProps) });
        };
        BlockFactory.createCollapsibleParagraphBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            var blockId = index_1.isEmptyString(rootProps.id) ? index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) : rootProps.id;
            return __assign({ blockType: enums_1.BlockType.CollapsibleParagraph }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), { id: blockId, properties: __assign({ isExpanded: false, children: [
                        BlockFactory.createParagraphBlock({
                            parentId: blockId,
                            content: [BlockFactory.createTextContent()]
                        })
                    ] }, this.defaultInnerBlockProps, innerProps) });
        };
        BlockFactory.createCollapsibleHeadingBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            var sanitizedInnerProps = index_1.sanitizeHeadingProps(innerProps);
            var blockId = index_1.isEmptyString(rootProps.id) ? index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) : rootProps.id;
            return __assign({ blockType: enums_1.BlockType.CollapsibleHeading }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), { id: blockId, properties: __assign({ isExpanded: false, level: 1, children: [
                        BlockFactory.createParagraphBlock({
                            parentId: blockId,
                            content: [BlockFactory.createTextContent()]
                        })
                    ] }, this.defaultInnerBlockProps, sanitizedInnerProps) });
        };
        BlockFactory.createCalloutBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            var blockId = index_1.isEmptyString(rootProps.id) ? index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) : rootProps.id;
            return __assign({ blockType: enums_1.BlockType.Callout }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), { id: blockId, content: [], properties: __assign({ children: [
                        BlockFactory.createParagraphBlock({
                            parentId: blockId,
                            content: [BlockFactory.createTextContent()]
                        })
                    ] }, innerProps) });
        };
        BlockFactory.createDividerBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ blockType: enums_1.BlockType.Divider }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { content: [], properties: __assign({}, innerProps) });
        };
        BlockFactory.createTemplateBlock = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            Eif (innerProps === void 0) { innerProps = {}; }
            return __assign({ blockType: 'Template' }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { content: [], properties: __assign({}, innerProps) });
        };
        BlockFactory.createTableBlock = function (rootProps, innerProps) {
            var _this = this;
            Iif (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            var tableBlock = __assign({ blockType: enums_1.BlockType.Table }, this.defaultRootBlockProps, index_1.sanitizeBlock(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.BLOCK_ID_PREFIX) } : {}), { properties: __assign({ width: '100%', cssClass: '', enableHeader: true, enableRowNumbers: true, readOnly: false }, this.getDefaultRowsAndColumns(), innerProps) });
            tableBlock.properties.columns.forEach(function (column, idx) {
                Iif (!column.id) {
                    column.id = index_1.generateUniqueId('col_');
                }
                if (!column.type) {
                    column.type = 'Text';
                }
                if (!column.headerText) {
                    column.headerText = "Column " + (idx + 1);
                }
            });
            tableBlock.properties.rows.forEach(function (r) {
                if (!r.id) {
                    r.id = index_1.generateUniqueId('row_');
                }
                r.cells.forEach(function (cell, idx) {
                    if (!cell.id) {
                        cell.id = index_1.generateUniqueId('cell_');
                    }
                    Iif (!cell.columnId) {
                        cell.columnId = tableBlock.properties.columns[idx].id;
                    }
                    cell.blocks = _this.populateBlockProperties(cell.blocks, cell.id);
                });
            });
            return tableBlock;
        };
        BlockFactory.getDefaultRowsAndColumns = function () {
            var col1Id = index_1.generateUniqueId('col_');
            var col2Id = index_1.generateUniqueId('col_');
            var row1Id = index_1.generateUniqueId('row_');
            var row2Id = index_1.generateUniqueId('row_');
            var cell11Id = index_1.generateUniqueId('cell_');
            var cell12Id = index_1.generateUniqueId('cell_');
            var cell21Id = index_1.generateUniqueId('cell_');
            var cell22Id = index_1.generateUniqueId('cell_');
            return {
                columns: [
                    { id: col1Id, type: 'Text', headerText: 'Column 1' },
                    { id: col2Id, type: 'Text', headerText: 'Column 2' }
                ],
                rows: [
                    {
                        id: row1Id,
                        cells: [
                            { id: cell11Id, columnId: col1Id, blocks: [this.createParagraphBlock({ content: [this.createTextContent()] })] },
                            { id: cell12Id, columnId: col2Id, blocks: [this.createParagraphBlock({ content: [this.createTextContent()] })] }
                        ]
                    },
                    {
                        id: row2Id,
                        cells: [
                            { id: cell21Id, columnId: col1Id, blocks: [this.createParagraphBlock({ content: [this.createTextContent()] })] },
                            { id: cell22Id, columnId: col2Id, blocks: [this.createParagraphBlock({ content: [this.createTextContent()] })] }
                        ]
                    }
                ]
            };
        };
        BlockFactory.populateBlockProperties = function (blocks, parentId) {
            var _this = this;
            var populatedBlocks = blocks.map(function (block) {
                if (parentId) {
                    block.parentId = parentId;
                }
                var updatedBlock = BlockFactory.createBlockFromPartial(block);
                if (updatedBlock.content && updatedBlock.content.length > 0) {
                    updatedBlock.content = updatedBlock.content.map(function (originalContent) {
                        return BlockFactory.createContentFromPartial(originalContent);
                    });
                }
                var props = updatedBlock.properties;
                if ((index_1.isChildrenProp(updatedBlock)) && props.children.length > 0) {
                    props.children = _this.populateBlockProperties(props.children, block.id);
                }
                return updatedBlock;
            });
            return populatedBlocks;
        };
        BlockFactory.createTextContent = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ contentType: enums_1.ContentType.Text }, this.defaultRootContentProps, index_1.sanitizeContent(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.CONTENT_ID_PREFIX) } : {}), { properties: __assign({ styles: {} }, innerProps) });
        };
        BlockFactory.createLinkContent = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ contentType: enums_1.ContentType.Link }, this.defaultRootContentProps, index_1.sanitizeContent(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.CONTENT_ID_PREFIX) } : {}), { properties: __assign({ styles: {}, url: '' }, innerProps) });
        };
        BlockFactory.createMentionContent = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ contentType: enums_1.ContentType.Mention }, this.defaultRootContentProps, index_1.sanitizeContent(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.CONTENT_ID_PREFIX) } : {}), { properties: __assign({ userId: '' }, innerProps) });
        };
        BlockFactory.createLabelContent = function (rootProps, innerProps) {
            if (rootProps === void 0) { rootProps = {}; }
            if (innerProps === void 0) { innerProps = {}; }
            return __assign({ contentType: enums_1.ContentType.Label }, this.defaultRootContentProps, index_1.sanitizeContent(rootProps), (index_1.isEmptyString(rootProps.id) ? { id: index_1.generateUniqueId(constants.CONTENT_ID_PREFIX) } : {}), { properties: __assign({ labelId: '' }, innerProps) });
        };
        BlockFactory.defaultRootBlockProps = {
            parentId: '',
            indent: 0,
            content: [],
            cssClass: '',
            template: ''
        };
        BlockFactory.defaultInnerBlockProps = {
            placeholder: ''
        };
        BlockFactory.defaultRootContentProps = {
            content: ''
        };
        return BlockFactory;
    }());
    exports.BlockFactory = BlockFactory;
});