@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
272 lines • 14.3 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __generator = (this && this.__generator) || function (thisArg, body) {
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
function verb(n) { return function (v) { return step([n, v]); }; }
function step(op) {
if (f) throw new TypeError("Generator is already executing.");
while (_) try {
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
if (y = 0, t) op = [op[0] & 2, t.value];
switch (op[0]) {
case 0: case 1: t = op; break;
case 4: _.label++; return { value: op[1], done: false };
case 5: _.label++; y = op[1]; op = [0]; continue;
case 7: op = _.ops.pop(); _.trys.pop(); continue;
default:
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
if (t[2]) _.ops.pop();
_.trys.pop(); continue;
}
op = body.call(thisArg, _);
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
}
};
var __values = (this && this.__values) || function(o) {
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
if (m) return m.call(o);
if (o && typeof o.length === "number") return {
next: function () {
if (o && i >= o.length) o = void 0;
return { value: o && o[i++], done: !o };
}
};
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
};
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";
var paragraphStylenameKey = "stylename";
var ListsHandler = /** @class */ (function () {
function ListsHandler(itemsContext, colorParser, colorPreviewService, listSettings) {
this._itemsContext = itemsContext;
this._colorParser = colorParser;
this._colorPreviewService = colorPreviewService;
this._listSettings = listSettings;
this._listStyleSheetManagerFactory = new ListStyleSheetManagerFactory(Utils.getListSettingFromConfig(this._listSettings));
}
ListsHandler.prototype.applyList = function (listType) {
return __awaiter(this, void 0, void 0, function () {
var _a, _b, item, e_1_1;
var e_1, _c;
return __generator(this, function (_d) {
switch (_d.label) {
case 0:
_d.trys.push([0, 5, 6, 7]);
_a = __values(this._itemsContext.items), _b = _a.next();
_d.label = 1;
case 1:
if (!!_b.done) return [3 /*break*/, 4];
item = _b.value;
return [4 /*yield*/, this._applyListToItem(item, listType)];
case 2:
_d.sent();
_d.label = 3;
case 3:
_b = _a.next();
return [3 /*break*/, 1];
case 4: return [3 /*break*/, 7];
case 5:
e_1_1 = _d.sent();
e_1 = { error: e_1_1 };
return [3 /*break*/, 7];
case 6:
try {
if (_b && !_b.done && (_c = _a.return)) _c.call(_a);
}
finally { if (e_1) throw e_1.error; }
return [7 /*endfinally*/];
case 7: return [2 /*return*/];
}
});
});
};
ListsHandler.prototype.isBulletedList = function () {
var _this = this;
return this._itemsContext.items.every(function (item) { return _this._isBulletedList(item); });
};
ListsHandler.prototype.isNumberedList = function () {
var _this = this;
return this._itemsContext.items.every(function (item) { return _this._isNumberedList(item); });
};
ListsHandler.prototype._applyListToItem = function (item, listType) {
return __awaiter(this, void 0, void 0, function () {
var _a, textModel, colorPalette, textSerializer, updatedText;
return __generator(this, function (_b) {
switch (_b.label) {
case 0: return [4 /*yield*/, this._getTextModelAndColorPalette(item)];
case 1:
_a = _b.sent(), textModel = _a.textModel, colorPalette = _a.colorPalette;
textSerializer = new GmXmlSerializer(this._colorParser);
this._applyList(textModel, listType);
updatedText = textSerializer.serialize(textModel, colorPalette);
item.text = updatedText;
return [2 /*return*/];
}
});
});
};
ListsHandler.prototype._isBulletedList = function (item) {
var textModel = this._getTextModel(item, this._getStubColorPalette(item));
return this._allParagraphsAreListItems(textModel, ListType.bullets);
};
ListsHandler.prototype._isNumberedList = function (item) {
var textModel = this._getTextModel(item, this._getStubColorPalette(item));
return this._allParagraphsAreListItems(textModel, ListType.numbers);
};
ListsHandler.prototype._getTextModelAndColorPalette = function (item) {
return __awaiter(this, void 0, void 0, function () {
var textParser, colorPalette, _a, listStyleSheetManager, textModel;
return __generator(this, function (_b) {
switch (_b.label) {
case 0:
textParser = new GmXmlParser(this._colorParser);
_a = ColorPalette.bind;
return [4 /*yield*/, ItemUtils.getColorPalette(item, this._colorPreviewService)];
case 1:
colorPalette = new (_a.apply(ColorPalette, [void 0, _b.sent()]))();
listStyleSheetManager = this._listStyleSheetManagerFactory.create();
textModel = textParser.parse(item.text, colorPalette, item.font.size, listStyleSheetManager);
return [2 /*return*/, {
textModel: textModel,
colorPalette: colorPalette
}];
}
});
});
};
ListsHandler.prototype._getTextModel = function (item, colorPalette) {
var textParser = new GmXmlParser(this._colorParser);
var listStyleSheetManager = this._listStyleSheetManagerFactory.create();
var textModel = textParser.parse(item.text, colorPalette, item.font.size, listStyleSheetManager);
return textModel;
};
ListsHandler.prototype._applyList = function (textModel, listType) {
if (this._allParagraphsAreListItems(textModel, listType)) {
this._removeList(textModel);
}
else {
this._makeList(textModel, listType);
}
};
ListsHandler.prototype._allParagraphsAreListItems = function (textModel, listType) {
var e_2, _a;
var _b;
try {
for (var _c = __values(textModel.blocks), _d = _c.next(); !_d.done; _d = _c.next()) {
var block = _d.value;
if (block instanceof Paragraph && ((_b = block.listStyle) === null || _b === void 0 ? void 0 : _b.type) !== this._convertListType(listType)) {
return false;
}
}
}
catch (e_2_1) { e_2 = { error: e_2_1 }; }
finally {
try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
}
finally { if (e_2) throw e_2.error; }
}
return true;
};
ListsHandler.prototype._removeList = function (textModel) {
var e_3, _a;
var _b;
try {
for (var _c = __values(textModel.blocks), _d = _c.next(); !_d.done; _d = _c.next()) {
var block = _d.value;
if (block instanceof Paragraph && block.listStyle != null && block.listStyle.type !== ListStyleType.none) {
block.listStyle = null;
(_b = block.metadata) === null || _b === void 0 ? void 0 : _b.delete(paragraphStylenameKey);
block.style.firstLineIndent = null;
block.style.leftIndent = null;
}
}
}
catch (e_3_1) { e_3 = { error: e_3_1 }; }
finally {
try {
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
}
finally { if (e_3) throw e_3.error; }
}
};
ListsHandler.prototype._makeList = function (textModel, listType) {
var _this = this;
textModel.blocks.forEach(function (block, index) {
var _a;
var 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);
}
}
});
};
ListsHandler.prototype._makeUnorderedListItem = function (paragraph) {
var listStyleSheetManager = this._listStyleSheetManagerFactory.create();
var 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);
};
ListsHandler.prototype._makeOrderedListItem = function (paragraph, index) {
var listStyleSheetManager = this._listStyleSheetManagerFactory.create();
var 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);
};
ListsHandler.prototype._convertListType = function (listType) {
return listType;
};
ListsHandler.prototype._getStubColorPalette = function (item) {
return new ColorPalette(item.colorPalette.toArray().map(function (color) { return ({
color: color,
preview: RgbColors.black
}); }));
};
return ListsHandler;
}());
export { ListsHandler };
//# sourceMappingURL=ListsHandler.js.map