@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
81 lines • 4.21 kB
JavaScript
import { NotImplementedException } from "@aurigma/design-atoms-model/Exception";
import { BaseItemsCommand } from "./BaseItemsCommand";
import { ArchedTextItem, BoundedTextItem, PlainTextItem, ResizeGripsPermissions, TextAlignment } from "@aurigma/design-atoms-model/Product/Items";
import { LayoutItemHandler } from "../../ItemHandlers";
import { ItemUtils } from "../../Utils/ItemUtils";
import { RotatedRectangleF } from "@aurigma/design-atoms-model/Math";
import { assignProperties } from "@aurigma/design-atoms-model/Utils/Utils";
import { ItemsCommand, ItemType, SelectionCommand } from "@aurigma/design-atoms-interfaces";
export class ConvertTextItemCommand extends BaseItemsCommand {
constructor(productHandler, historyArgs, args, _commandManager) {
super(productHandler, historyArgs, args);
this._commandManager = _commandManager;
}
async _executeCommandBody() {
const { item, to } = this._args;
if (item == null || to == null)
return;
if ((item instanceof BoundedTextItem && this._args.to === ItemType.BoundedTextItem) ||
(item instanceof PlainTextItem && this._args.to === ItemType.PlainTextItem) ||
(item instanceof ArchedTextItem && this._args.to === ItemType.ArchedTextItem)) {
console.warn(`ConvertTextItemCommand: item type ${item.type} to ${this._args.to} is not supported`);
return;
}
const rectangle = this._productHandler.getHandler(item).rectangle;
let newItem = null;
switch (this._args.to) {
case ItemType.PlainTextItem:
newItem = new PlainTextItem(item.text, rectangle.location);
break;
case ItemType.BoundedTextItem:
newItem = new BoundedTextItem(item.text, rectangle.toRectangleF());
newItem.manipulationPermissions.resizeGrips.corner = [ResizeGripsPermissions.arbitrary, ResizeGripsPermissions.proportional];
break;
case ItemType.ArchedTextItem:
newItem = assignProperties(new ArchedTextItem(item.text, rectangle.location), { bend: 0.25 });
break;
default:
console.warn(`ConvertTextItemCommand: destination item type ${this._args.to} is not supported`);
return;
}
item.copyTo(newItem);
this._productHandler.exitEditMode();
await this._commandManager.execute(ItemsCommand.replaceItemCommand, { item: newItem, oldItem: item });
await this._productHandler.waitUpdate();
const newHandler = this._productHandler.getHandler(newItem);
if (newHandler.parentGroupItemHandler instanceof LayoutItemHandler) {
newHandler.parentGroupItemHandler.applyLayout();
}
if (item instanceof ArchedTextItem) {
newItem.textPermissions.allowChangeLeading = true;
newItem.textPermissions.allowChangeTextAlignment = true;
}
if (to === ItemType.BoundedTextItem) {
const r = rectangle.toRectangleF();
if (item instanceof PlainTextItem) {
const hShift = r.width * 0.02;
r.width += hShift;
if (item.alignment == TextAlignment.Right || item.alignment == TextAlignment.LastRight)
r.left -= hShift;
else if (item.alignment == TextAlignment.Center || item.alignment == TextAlignment.LastCenter)
r.left -= hShift / 2;
r.height *= 1.1;
}
newHandler.setRectangle(RotatedRectangleF.fromRectangleF(r, rectangle.angle));
}
else {
ItemUtils.alignHandlerToRect(newHandler, rectangle, item);
}
newHandler.update();
await this._productHandler.waitUpdate();
await this._commandManager.execute(SelectionCommand.selectItems, { items: [newItem] });
return newItem;
}
redo() {
throw new NotImplementedException();
}
undo() {
throw new NotImplementedException();
}
}
//# sourceMappingURL=ConvertTextItemCommand.js.map