@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
101 lines • 5.55 kB
JavaScript
import { NotImplementedException } from "@aurigma/design-atoms-model/Exception";
import { BaseSurfaceCommand } from "./BaseSurfaceCommand";
import { ProductThemeApplier } from "../../Services/ProductTheme/ProductThemeApplier";
import { ItemUtils } from "../../Utils/ItemUtils";
import { GroupItem, ImageItem, BaseTextItem, PlainTextItem, BoundedTextItem, PathBoundedTextItem, LayoutItem } from "@aurigma/design-atoms-model/Product/Items";
import { SelectionCommand } from "@aurigma/design-atoms-interfaces";
export class AddItemsCommand extends BaseSurfaceCommand {
constructor(_productHandler, historyArgs, args, _productThemeManager, _canvas, _variableItemHelper, _commandManager, _eventManager) {
super(args.surface, historyArgs, args);
this._productHandler = _productHandler;
this._productThemeManager = _productThemeManager;
this._canvas = _canvas;
this._variableItemHelper = _variableItemHelper;
this._commandManager = _commandManager;
this._eventManager = _eventManager;
}
async _executeCommandBody() {
const { items, targetGroupItem, index, orderIndex } = this._args;
const normalizedArgs = Object.assign({ items: items, targetContainer: this._productHandler.userEditContainer, surface: this._productHandler.currentSurface, selectOnCanvas: true, index: null, orderIndex: null, ignoreOrderRules: false, ignoreCanvasRotate: true, createCloneName: false }, this._args);
this._historyArgs.history.pause();
if (targetGroupItem != null) {
await this._insertItemsIntoGroupItem(items, targetGroupItem, index, orderIndex);
this._eventManager.itemCollectionChangedEvent.notify();
}
else {
for (let item of items) {
await this._addItem(normalizedArgs, item);
}
this._canvas.updateTexts();
await ItemUtils.waitForItemsUpdated(items, this._canvas);
}
//PlainTextItem получает свои размеры только после ответа с сервера, до ответа формируется пустой Item.
//Чтобы избежать выделения пустого Item - перерисовываем канвас только после ответа с сервера.
if (!items.some(item => item instanceof PlainTextItem))
this._canvas.redraw();
this._historyArgs.history.resume();
if (normalizedArgs.selectOnCanvas)
this._commandManager.execute(SelectionCommand.selectItems, { items: items.filter(item => !item.locked) });
}
async _addItem(options, item) {
if (this._productThemeManager.currentProductTheme != null) {
await new ProductThemeApplier(this._canvas.viewer.colorPreviewService, this._canvas.viewer.colorParser)
.applyToItem(item, this._productThemeManager.currentProductTheme);
}
if (options.index == null)
options.index = -1;
const targetContainer = options.targetContainer;
if (this._args.createCloneName)
item.name = ItemUtils.createCloneName(item, this._variableItemHelper, this._productHandler);
const items = targetContainer.items;
items.add(item);
if (options.index !== -1) {
targetContainer.items.move(items.length - 1, options.index);
}
else if (!options.ignoreOrderRules && item instanceof ImageItem && items.length > 1) {
// put image over the highest bounded/pathBounded or under the lowest text (not rich)
const highestBounded = items.toArray()
.slice()
.reverse()
.find(i => (i instanceof BoundedTextItem || i instanceof PathBoundedTextItem));
const lowestText = items.toArray().find(i => (i instanceof BaseTextItem));
let newImageItemIndex = -1;
if (highestBounded != null)
newImageItemIndex = items.indexOf(highestBounded) + 1;
else if (lowestText != null)
newImageItemIndex = items.indexOf(lowestText);
if (newImageItemIndex !== -1) {
targetContainer.items.move(items.length - 1, newImageItemIndex);
}
}
}
async _insertItemsIntoGroupItem(items, targetGroupItem, targetIndex, targetOrderIndex) {
if (targetGroupItem instanceof LayoutItem)
targetGroupItem.addItems(items, targetIndex, targetOrderIndex);
else
targetGroupItem.addItems(items, targetIndex);
const updatingItems = [
...items,
...items
.filter(item => item instanceof GroupItem)
.reduce((arr, item) => [...arr, ...item.getNestedItems(true)], [])
];
if (updatingItems.length > 0) {
let topGroupItem = targetGroupItem;
while (topGroupItem.parentGroupItem != null)
topGroupItem = topGroupItem.parentGroupItem;
const { locked: originalLockedState } = topGroupItem;
topGroupItem.locked = true;
await ItemUtils.waitForItemsUpdated(updatingItems, this._canvas);
topGroupItem.locked = originalLockedState;
}
;
}
redo() {
throw new NotImplementedException();
}
undo() {
throw new NotImplementedException();
}
}
//# sourceMappingURL=AddItemsCommand.js.map