UNPKG

core-cde

Version:

287 lines 12.5 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Text = exports.ListStyle = exports.ETextBgColor = exports.ETextColor = exports.ETextStyl = exports.ETextAlign = exports.ETextSize = void 0; const Text_actions_1 = require("../actions/Text.actions"); const BaseWidget_1 = require("../BaseWidget"); const Errors_1 = require("../Errors"); const Operations_1 = require("../Operations"); var ETextSize; (function (ETextSize) { ETextSize["s14px"] = "14"; ETextSize["s16px"] = "16"; ETextSize["s18px"] = "18"; ETextSize["s20px"] = "20"; ETextSize["s22px"] = "22"; ETextSize["s24px"] = "24"; ETextSize["s26px"] = "26"; ETextSize["s28px"] = "28"; ETextSize["s30px"] = "30"; ETextSize["s32px"] = "32"; ETextSize["s34px"] = "34"; ETextSize["s36px"] = "36"; ETextSize["s38px"] = "38"; ETextSize["s40px"] = "40"; })(ETextSize = exports.ETextSize || (exports.ETextSize = {})); var ETextAlign; (function (ETextAlign) { ETextAlign["left"] = "left"; ETextAlign["center"] = "center"; ETextAlign["right"] = "right"; ETextAlign["justify"] = "justify"; })(ETextAlign = exports.ETextAlign || (exports.ETextAlign = {})); var ETextStyl; (function (ETextStyl) { ETextStyl["bold"] = "bold"; ETextStyl["italic"] = "italic"; ETextStyl["underline"] = "underline"; ETextStyl["strikethrough"] = "strikethrough"; ETextStyl["inlineCode"] = "inlineCode"; })(ETextStyl = exports.ETextStyl || (exports.ETextStyl = {})); var ETextColor; (function (ETextColor) { ETextColor["black"] = "black"; ETextColor["red"] = "red"; ETextColor["green"] = "green"; ETextColor["orange"] = "orange"; ETextColor["blue"] = "blue"; ETextColor["yellow"] = "yellow"; ETextColor["orangeDark"] = "orangeDark"; ETextColor["yellowDark"] = "yellowDark"; ETextColor["purpleDark"] = "purpleDark"; ETextColor["pinkDark"] = "pinkDark"; ETextColor["grayDark"] = "grayDark"; ETextColor["redDark"] = "redDark"; })(ETextColor = exports.ETextColor || (exports.ETextColor = {})); var ETextBgColor; (function (ETextBgColor) { ETextBgColor["bgRed"] = "bgRed"; ETextBgColor["bgBlue"] = "bgBlue"; ETextBgColor["bgGreen"] = "bgGreen"; ETextBgColor["bgOrange"] = "bgOrange"; ETextBgColor["bgYellow"] = "bgYellow"; ETextBgColor["bgRedDark"] = "bgRedDark"; ETextBgColor["bgOrangeDark"] = "bgOrangeDark"; ETextBgColor["bgYellowDark"] = "bgYellowDark"; ETextBgColor["bgPurpleDark"] = "bgPurpleDark"; ETextBgColor["bgPinkDark"] = "bgPinkDark"; ETextBgColor["bgGrayDark"] = "bgGrayDark"; })(ETextBgColor = exports.ETextBgColor || (exports.ETextBgColor = {})); class ListStyle { } exports.ListStyle = ListStyle; class Text extends BaseWidget_1.BaseWidget { constructor(type, id) { super(type, id); this.textAlign = ETextAlign.left; this.value = ''; this.valueHTML = ''; this.styles = []; } /** * Получить объект класса Text из json * @param json валидная json строка * @returns объект класса Text */ static fromJSON(json) { try { return Object.assign(new Text(null, null), JSON.parse(json)); } catch (error) { throw new Errors_1.JsonParseError('Text widget'); } } /** * Получить объект для сериализации * @returns Объект для сериализации */ toJSON() { return Object.assign({}, null, { id: this.id, type: this.type, styles: this.styles, value: this.value, textAlign: this.textAlign }); } /** * Пересчитать позиции стилей * @param text Текст * @param listStyle Список стилей для текста * @returns Пересчитанный список стилей */ recPositions(text, listStyle) { const posCharStyl = Text.getPosCharStl(text, listStyle); const resMap = Text.groupPosByStyle(posCharStyl); resMap.sort((a, b) => a.position[0] - b.position[0]); return resMap; } applyStyles() { var _a, _b; console.time('applyStyles'); let content = this.value; let end = null; if (this.styles && ((_a = this.styles) === null || _a === void 0 ? void 0 : _a.length)) { for (let i = this.styles.length - 1; i >= 0; i--) { if (end) { content = `${content.substring(0, this.styles[i].position[1])}${this.replaceInRange(content.substring(this.styles[i].position[1], end), ' ', '&nbsp;')}${content.substring(end)}`; } else { content = `${content.substring(0, this.styles[i].position[1])}${this.replaceInRange(content.substring(this.styles[i].position[1]), ' ', '&nbsp;')}`; } end = this.styles[i].position[0]; let contentText = content.substring(this.styles[i].position[0], this.styles[i].position[1]); contentText = this.replaceInRange(contentText, ' ', '&nbsp;'); const contentMod = `<span class="${(_b = this.styles[i].style) === null || _b === void 0 ? void 0 : _b.join(' ')}">${contentText}</span>`; const temp = content.split(''); temp.splice(this.styles[i].position[0], this.styles[i].position[1] - this.styles[i].position[0], contentMod); content = temp.join(''); } if (end) { content = `${this.replaceInRange(content.substring(0, end), ' ', '&nbsp;')}${content.substring(end, content.length)}`; } } else { content = this.replaceInRange(content, ' ', '&nbsp;'); } this.valueHTML = content; console.timeEnd('applyStyles'); } replaceInRange(source, partDelete, partReplace) { let text = source; while (text.indexOf(partDelete) !== -1) { text = text.replace(partDelete, partReplace); } return text; } /** * Получить из массива символов со стилями список стилей * @param posCharStyl массив позиций символов со стилями * @returns список стилей */ static groupPosByStyle(posCharStyl) { let start = 0; const res = []; for (let index = 0; index < posCharStyl.length; index++) { const prevElement = posCharStyl[index - 1]; const nextElement = posCharStyl[index + 1]; const element = posCharStyl[index]; if (element.length !== 0) { if (JSON.stringify(element) !== JSON.stringify(prevElement)) { start = index; } if (JSON.stringify(element) !== JSON.stringify(nextElement)) { res.push({ position: [start, index + 1], style: element }); } } } return res; } /** * Разбиваем текст на позиции со своими стилями * @param text Текст * @param listStyle Стили текста * @returns Массив стилей для каждого символа */ static getPosCharStl(text, listStyle) { if (!text || !listStyle) { return []; } const splitText = text.split(''); const between = (x, range) => { return x >= range[0] && x < range[1]; }; // TODO Переписать код return splitText.map((value, index) => { const a = listStyle.filter(style => between(index, style.position)); const b = a.map(val => val.style); let res = []; b.forEach(val => { res = [...res, ...val]; }); return res; }); } /** * Добавить стиль к массиву символов со стилями * @param listTextStyl массив позиций символов и их стили * @param position позиция вставки символов * @param style стиль * @returns массив позиций символов со стилями */ static addStyle(listTextStyl, position, style) { const [start, end] = position; const selectedElements = listTextStyl.slice(start, end); const actionRemove = selectedElements.every(val => val.findIndex(item => item === style) >= 0); selectedElements.forEach(styles => { const indexStylFind = styles.findIndex(val => val === style); if (actionRemove && indexStylFind >= 0) { styles.splice(indexStylFind, 1); } if (!actionRemove && indexStylFind <= 0) { styles.push(style); } }); return listTextStyl; } static composeUnWrapAction(widgetId, store) { var _a, _b, _c; const widgetNode = store.document.pageSelect.widgets.getNodeById(widgetId); if (!widgetNode) { throw new Errors_1.ElementNotFoundError('composeUnWrapAction()'); } const widget = JSON.parse(JSON.stringify(widgetNode.value)); if (!widget) { throw new Errors_1.ElementNotFoundError('remove()'); } const prevWidget = JSON.parse(JSON.stringify((_a = widgetNode.previous) === null || _a === void 0 ? void 0 : _a.value)); if (!prevWidget) { throw new Error('Previous widget missing!'); } if (widget.type !== prevWidget.type) { throw new Error('Different types of widgets!'); } const textWidget = prevWidget.value + widget.value; Operations_1.Operations.shiftStylePosRight(widget, 0, prevWidget.value.length); const arrSymbols = Text.getPosCharStl(textWidget, [...prevWidget.styles, ...widget.styles]); const listStyleWidget = Text.groupPosByStyle(arrSymbols); const textUnWrappAction = new Text_actions_1.TextUnWrappingAction({ positionWrapText: prevWidget.value.length, listStyleWidget, textWidget, }, store.document.id, store.document.pageSelect.id, (_c = (_b = widgetNode.previous) === null || _b === void 0 ? void 0 : _b.value) === null || _c === void 0 ? void 0 : _c.id); textUnWrappAction.removeWidgetId = widgetNode.value.id; return textUnWrappAction; } static composeWrapAction(newIdWidget, position, widgetId, store) { const widgetNode = store.document.pageSelect.widgets.getNodeById(widgetId); if (!widgetNode) { throw new Errors_1.ElementNotFoundError('composeUnWrapAction()'); } const widget = JSON.parse(JSON.stringify(widgetNode.value)); // const newIdWidget = UUID.get(); const textWidget = widget.value.substr(0, position); const textNewWidget = widget.value.substr(position); // Разбиваем текст по символам со стилями на каждый символ const arrSymbols = Text.getPosCharStl(widget.value, widget.styles); // Пересчитываем стили для текущего виджета const listStyleWidget = Text.groupPosByStyle(arrSymbols.slice(0, textWidget.length)); const listStyleNewWidget = Text.groupPosByStyle(arrSymbols.slice(textWidget.length)); const textWrapAction = new Text_actions_1.TextWrappingAction({ listStyleNewWidget, listStyleWidget, textNewWidget, textWidget, typeNewWidget: widget.type }, store.document.id, store.document.pageSelect.id, widget.id); textWrapAction.newWidgetId = newIdWidget; return textWrapAction; } static isText(state) { return state instanceof Text; } } exports.Text = Text; //# sourceMappingURL=Text.js.map