UNPKG

fox-block-builder

Version:

Maintainable code for loop slack-block-kit-like modal builder

152 lines 7.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PluginFormBase = void 0; const class_transformer_1 = require("class-transformer"); const loop_1 = require("../../internal/constants/loop"); const plugin_form_block_dto_1 = require("./plugin-form-block.dto"); const BLOCK_VALUES_JOIN_ARG = loop_1.LOOP_JOIN_ARG; class PluginFormBase { constructor() { this.blocks = []; this.private_metadata = JSON.stringify({}); } setBlocks(blocks) { this.blocks = (0, class_transformer_1.plainToInstance)(plugin_form_block_dto_1.PluginFormBlockDto, blocks, { exposeDefaultValues: true, }); return this; } setBlockById(block) { const index = this.blocks.findIndex(({ id }) => id === block.id); if (index) { this.blocks[index] = (0, class_transformer_1.plainToInstance)(plugin_form_block_dto_1.PluginFormBlockDto, block, { exposeDefaultValues: true, }); } return this; } addBlocks(index = this.blocks.length - 1, del = 0, ...blocks) { this.blocks.splice(index, del, ...(0, class_transformer_1.plainToInstance)(plugin_form_block_dto_1.PluginFormBlockDto, blocks)); return this; } /** Получение блока по ID */ getBlockById(blockId) { var _a; return (_a = this.blocks) === null || _a === void 0 ? void 0 : _a.find(({ id }) => id === null || id === void 0 ? void 0 : id.includes(blockId)); } /** Получение custom value блока */ getCustomBlock(id) { var _a; return (_a = this.blocks) === null || _a === void 0 ? void 0 : _a.find((block) => { var _a; return block.custom && ((_a = block.id) === null || _a === void 0 ? void 0 : _a.includes(String(id))); }); } setBlockParam(id, name, value) { var _a; (_a = this.getBlockById(id)) === null || _a === void 0 ? void 0 : _a.setParam(name, value); return this; } /** Установка label */ setBlockLabel(blockId, label) { var _a; (_a = this.getBlockById(blockId)) === null || _a === void 0 ? void 0 : _a.setLabel(label); return this; } /** Установка value */ setBlockValue(blockId, value) { var _a; (_a = this.getBlockById(blockId)) === null || _a === void 0 ? void 0 : _a.setValue(value); return this; } /** Установка text */ setBlockText(blockId, text) { var _a; (_a = this.getBlockById(blockId)) === null || _a === void 0 ? void 0 : _a.setText(text); return this; } /** Получение значений мультиселектов в виде массивов */ getMultiSelectBlock(blockId) { var _a, _b; const block = this.getBlockById(blockId); const result = []; if (block === null || block === void 0 ? void 0 : block.multi) { /** Забиваем значения блока в массив */ (_a = block.value) === null || _a === void 0 ? void 0 : _a.split(BLOCK_VALUES_JOIN_ARG).forEach((value) => result.push({ value })); /** Если есть текстовые значения - их тоже забиваем в соотв. обьект */ if (block.text) { (_b = block.text) === null || _b === void 0 ? void 0 : _b.split(BLOCK_VALUES_JOIN_ARG).forEach((text, i) => (result[i].text = text)); } } else { /** На случай если блок не мультиселект */ result.push({ text: block === null || block === void 0 ? void 0 : block.text, value: (block === null || block === void 0 ? void 0 : block.value) || '' }); } return result; } /** Получение названия задачи из значения блока */ getTitleFromBlocks(title) { /** Ищем блоки с asTitle */ const titleBlocksTexts = this.blocks .filter((block) => block.asTitle) .map((block) => block.text || block.value); /** Добавляем название */ const titleTexts = title ? [title, ...titleBlocksTexts] : titleBlocksTexts; return (titleTexts && (titleTexts === null || titleTexts === void 0 ? void 0 : titleTexts.length) > 0 ? titleTexts === null || titleTexts === void 0 ? void 0 : titleTexts.join(' | ') : 'Без названия').substring(0, 100); } /** Получение значения блока "другое" из соотв. блока */ getBlockOtherText(blockId) { var _a, _b, _c; return ((_b = (_a = this.blocks) === null || _a === void 0 ? void 0 : _a.find((block) => block.id.includes(blockId + '_OtherText'))) !== null && _b !== void 0 ? _b : (_c = this.blocks) === null || _c === void 0 ? void 0 : _c.find((block) => block.id.includes(blockId + 'OtherText'))); } /** Получение обычных блоков */ getRegularBlocks() { var _a; return (_a = this.blocks) === null || _a === void 0 ? void 0 : _a.filter((block) => !block.custom); } /** Получение custom value блоков */ getCustomBlocks() { var _a; return (_a = this.blocks) === null || _a === void 0 ? void 0 : _a.filter((block) => block.custom); } /** Получение custom value блоков с файлами */ getCustomFileBlocks() { var _a; return (_a = this.getCustomBlocks()) === null || _a === void 0 ? void 0 : _a.filter((block) => { var _a; return ((_a = block.files) === null || _a === void 0 ? void 0 : _a.length) !== 0; }); } /** Получение обычных блоков с файлами */ getFileBlocks() { var _a; return (_a = this.getRegularBlocks()) === null || _a === void 0 ? void 0 : _a.filter((block) => { var _a; return ((_a = block.files) === null || _a === void 0 ? void 0 : _a.length) !== 0; }); } /** Удаление блоков custom value */ removeCustomBlocks() { var _a; this.blocks = (_a = this.blocks) === null || _a === void 0 ? void 0 : _a.filter((block) => !block.custom); } /** Удаление блоков с флагом exclude */ removeExcludedBlocks() { var _a; this.blocks = ((_a = this.blocks) === null || _a === void 0 ? void 0 : _a.filter((block) => !block.exclude)) || []; return this; } /** Форматирование в Fields c опцональным выбором блоков */ toFields(...includeIds) { var _a; let blocks = this.blocks; if (includeIds.length > 0) { blocks = (_a = this.blocks) === null || _a === void 0 ? void 0 : _a.filter(({ id }) => includeIds === null || includeIds === void 0 ? void 0 : includeIds.includes(id)); } return blocks === null || blocks === void 0 ? void 0 : blocks.map(({ label, text }) => ({ title: label, value: text })); } /** Форматирование в строку markdown c опцональным выбором блоков */ asString({ includeIds = [], md = false, }) { var _a; return (_a = this.toFields(...includeIds)) === null || _a === void 0 ? void 0 : _a.map(({ title, value }) => `${md ? '**' : ''}${title}:${md ? '**' : ''} ${value}`).join(`\n`); } /** Форматирование в строку markdown c опцональным выбором блоков */ toMarkdown(...includeIds) { return this.asString({ includeIds, md: true }); } } exports.PluginFormBase = PluginFormBase; //# sourceMappingURL=plugin-form-base.dto.js.map