UNPKG

@aurigma/design-atoms

Version:

Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.

146 lines 7.3 kB
import { RgbColors } from "@aurigma/design-atoms-model"; import { ListType, NumberingFormatType } from "@aurigma/design-atoms-text/TextEditor"; import { ListStyleSheetManagerFactory } from "@aurigma/design-atoms-text/TextEditor/Services"; import { GmXmlParser, GmXmlSerializer } from "@aurigma/design-atoms-text/Serialization"; import { ColorPalette } from "@aurigma/design-atoms-text/Serialization/Model/ColorPalette"; import { ListStyleType, NumberMode, NumericValue, Paragraph, ParagraphListStyle, ParagraphMetadata, Units } from "@aurigma/design-atoms-text/Model"; import { Utils } from "../Utils/Common"; import { ItemUtils } from "../Utils/ItemUtils"; const paragraphStylenameKey = "stylename"; export class ListsHandler { constructor(itemsContext, colorParser, colorPreviewService, listSettings) { this._itemsContext = itemsContext; this._colorParser = colorParser; this._colorPreviewService = colorPreviewService; this._listSettings = listSettings; this._listStyleSheetManagerFactory = new ListStyleSheetManagerFactory(Utils.getListSettingFromConfig(this._listSettings)); } async applyList(listType) { for (const item of this._itemsContext.items) { await this._applyListToItem(item, listType); } } isBulletedList() { return this._itemsContext.items.every(item => this._isBulletedList(item)); } isNumberedList() { return this._itemsContext.items.every(item => this._isNumberedList(item)); } async _applyListToItem(item, listType) { const { textModel, colorPalette } = await this._getTextModelAndColorPalette(item); const textSerializer = new GmXmlSerializer(this._colorParser); this._applyList(textModel, listType); const updatedText = textSerializer.serialize(textModel, colorPalette); item.text = updatedText; } _isBulletedList(item) { const textModel = this._getTextModel(item, this._getStubColorPalette(item)); return this._allParagraphsAreListItems(textModel, ListType.bullets); } _isNumberedList(item) { const textModel = this._getTextModel(item, this._getStubColorPalette(item)); return this._allParagraphsAreListItems(textModel, ListType.numbers); } async _getTextModelAndColorPalette(item) { const textParser = new GmXmlParser(this._colorParser); const colorPalette = new ColorPalette(await ItemUtils.getColorPalette(item, this._colorPreviewService)); const listStyleSheetManager = this._listStyleSheetManagerFactory.create(); const textModel = textParser.parse(item.text, colorPalette, item.font.size, listStyleSheetManager); return { textModel: textModel, colorPalette: colorPalette }; } _getTextModel(item, colorPalette) { const textParser = new GmXmlParser(this._colorParser); const listStyleSheetManager = this._listStyleSheetManagerFactory.create(); const textModel = textParser.parse(item.text, colorPalette, item.font.size, listStyleSheetManager); return textModel; } _applyList(textModel, listType) { if (this._allParagraphsAreListItems(textModel, listType)) { this._removeList(textModel); } else { this._makeList(textModel, listType); } } _allParagraphsAreListItems(textModel, listType) { var _a; for (const block of textModel.blocks) { if (block instanceof Paragraph && ((_a = block.listStyle) === null || _a === void 0 ? void 0 : _a.type) !== this._convertListType(listType)) { return false; } } return true; } _removeList(textModel) { var _a; for (const block of textModel.blocks) { if (block instanceof Paragraph && block.listStyle != null && block.listStyle.type !== ListStyleType.none) { block.listStyle = null; (_a = block.metadata) === null || _a === void 0 ? void 0 : _a.delete(paragraphStylenameKey); block.style.firstLineIndent = null; block.style.leftIndent = null; } } } _makeList(textModel, listType) { textModel.blocks.forEach((block, index) => { var _a; const listStyleType = this._convertListType(listType); if (block instanceof Paragraph) { if (((_a = block.listStyle) === null || _a === void 0 ? void 0 : _a.type) !== listStyleType) { if (listStyleType === ListStyleType.bullets) { this._makeUnorderedListItem(block); } else if (listStyleType === ListStyleType.numbers) { this._makeOrderedListItem(block, index); } } else if (listStyleType === ListStyleType.numbers) { this._makeOrderedListItem(block, index); } } }); } _makeUnorderedListItem(paragraph) { const listStyleSheetManager = this._listStyleSheetManagerFactory.create(); const styleSheet = listStyleSheetManager.createDefaultBulletedStyleSheet(this._listSettings.bulletChar.charCodeAt(0)); paragraph.listStyle = new ParagraphListStyle(); paragraph.listStyle.type = ListStyleType.bullets; paragraph.listStyle.tabPosition = this._listSettings.tabOffset; paragraph.listStyle.mode = NumberMode.startAt; if (paragraph.metadata == null) { paragraph.metadata = new ParagraphMetadata(); } paragraph.metadata.set(paragraphStylenameKey, styleSheet.name); paragraph.style.firstLineIndent = new NumericValue(this._listSettings.firstLineIndent, Units.point); paragraph.style.leftIndent = new NumericValue(this._listSettings.listIndent, Units.point); } _makeOrderedListItem(paragraph, index) { const listStyleSheetManager = this._listStyleSheetManagerFactory.create(); const styleSheet = listStyleSheetManager.createDefaultNumberedStyleSheet(NumberingFormatType.number); paragraph.listStyle = new ParagraphListStyle(); paragraph.listStyle.type = ListStyleType.numbers; paragraph.listStyle.tabPosition = this._listSettings.tabOffset; paragraph.listStyle.mode = NumberMode.startAt; paragraph.listStyle.startAt = index + 1; if (paragraph.metadata == null) { paragraph.metadata = new ParagraphMetadata(); } paragraph.metadata.set(paragraphStylenameKey, styleSheet.name); paragraph.style.firstLineIndent = new NumericValue(this._listSettings.firstLineIndent, Units.point); paragraph.style.leftIndent = new NumericValue(this._listSettings.listIndent, Units.point); } _convertListType(listType) { return listType; } _getStubColorPalette(item) { return new ColorPalette(item.colorPalette.toArray().map(color => ({ color: color, preview: RgbColors.black }))); } } //# sourceMappingURL=ListsHandler.js.map