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.

81 lines 3.76 kB
import { NotImplementedException } from "@aurigma/design-atoms-model/Exception"; import { BaseItemsCommand } from "./BaseItemsCommand"; import { LayoutItem } from "@aurigma/design-atoms-model/Product/Items"; import { ItemsCommand } from "@aurigma/design-atoms-interfaces"; export class DeleteItemsCommand extends BaseItemsCommand { constructor(productHandler, historyArgs, args, _canvas, viewerConf, _commandManager) { var _a; super(productHandler, historyArgs, args); this._canvas = _canvas; this._commandManager = _commandManager; this._conf = (_a = viewerConf === null || viewerConf === void 0 ? void 0 : viewerConf.messages) === null || _a === void 0 ? void 0 : _a.delete; if (this._conf == null) this._conf = { enabled: false }; } async _executeCommandBody() { const normalizedArgs = Object.assign({ force: false, autoUngroup: true, autoParentGroupDelete: true }, this._args); const targetItems = this._getTargetItems(this._args.items, this._args.query, this._args.queryOptions); this.deleteItems(targetItems, normalizedArgs.force, normalizedArgs.autoUngroup, normalizedArgs.autoParentGroupDelete); } deleteItems(items, force, autoUngroup, autoParentGroupDelete) { if (items.length == 0) return; const isAllowDelete = items.every(item => this._productHandler.getHandler(item).getPermissions().allowDelete); if (!isAllowDelete && !force) return; const remove = !force && this._conf.enabled ? confirm(this._conf.message) : true; if (!remove) return; const byParentMap = new Map(); for (let i = 0; i < items.length; i++) { const item = items[i]; const { parentGroupItem } = item; if (parentGroupItem != null) { if (!byParentMap.has(parentGroupItem)) { byParentMap.set(parentGroupItem, []); } let childItems = byParentMap.get(parentGroupItem); childItems.push(item); } else { this.deleteItem(item); } } for (let [parent, children] of byParentMap) { this._removeChildItemsFromGroup(parent, children, autoParentGroupDelete, autoUngroup); } this._canvas.updateTexts(); this._canvas.updateSelection(); this._productHandler.redraw(); } _removeChildItemsFromGroup(parentGroupItem, children, autoParentGroupDelete, autoUngroup) { if (parentGroupItem instanceof LayoutItem) children.forEach(x => parentGroupItem.removeItems([x])); else children.forEach(x => parentGroupItem.items.remove(x)); if (autoParentGroupDelete && parentGroupItem.items.length === 0) this.deleteItem(parentGroupItem); else if (autoUngroup && parentGroupItem.items.length < 2) this._commandManager.execute(ItemsCommand.ungroupItems, { items: parentGroupItem.items.toArray() }); } deleteItem(item) { if (item == null) return; const container = item.parentGroupItem != null ? item.parentGroupItem : item.parentContainer; if (container != null && container.items.contains(item)) { if (container instanceof LayoutItem) container.removeItems([item]); else container.items.remove(item); } } redo() { throw new NotImplementedException(); } undo() { throw new NotImplementedException(); } } //# sourceMappingURL=DeleteItemsCommand.js.map