@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
70 lines • 3.43 kB
JavaScript
import { BaseItemsCommand } from "./BaseItemsCommand";
import { NotImplementedException } from "@aurigma/design-atoms-model/Exception";
import { PathBoundedTextItem } from "@aurigma/design-atoms-model/Product/Items";
import { Path, RectangleF } from "@aurigma/design-atoms-model/Math";
export class ChangeColumnCountCommand extends BaseItemsCommand {
constructor(productHandler, args, historyArgs) {
super(productHandler, historyArgs, args);
}
async _executeCommandBody() {
var _a;
const items = (_a = this._getTargetItems(this._args.items, this._args.query, this._args.queryOptions)) !== null && _a !== void 0 ? _a : this._productHandler.selectedItems;
items.filter(i => i instanceof PathBoundedTextItem).forEach(i => this._setColumnCount(i, this._args.value));
}
_setColumnCount(textItem, value) {
//TODO доделать http://app-server-1/mantis/view.php?id=22662
const margin = 10;
const textRectangle = getBounds(textItem.boundingPaths);
const width = (textRectangle.width - margin * (value - 1)) / value;
const newBoundingPaths = [];
for (let i = 0; i < value; i++) {
const left = textRectangle.left + (width + margin) * i;
const top = textRectangle.top;
const right = left + width;
const bottom = top + textRectangle.height;
const path = new Path();
path.moveTo(left, top);
path.lineTo(right, top);
path.lineTo(right, bottom);
path.lineTo(left, bottom);
path.lineTo(left, top);
newBoundingPaths.push(path);
}
textItem.boundingPaths = newBoundingPaths;
function getBounds(paths) {
return union(paths.map(getBoundingBox));
function union(rectangles) {
if (rectangles.length === 0)
return RectangleF.empty;
const minLeft = Math.min(...rectangles.map(r => r.left));
const maxRight = Math.max(...rectangles.map(r => r.right));
const minTop = Math.min(...rectangles.map(r => r.top));
const maxBottom = Math.max(...rectangles.map(r => r.bottom));
return RectangleF.FromLTRB(minLeft, minTop, maxRight, maxBottom);
}
function getBoundingBox(path) {
const svg = document.createElementNS("http://www.w3.org/2000/svg", "svg");
svg.style.position = "absolute";
svg.style.visibility = "hidden";
const svgPath = document.createElementNS("http://www.w3.org/2000/svg", "path");
svg.appendChild(svgPath);
svgPath.setAttribute("d", path.toString());
const body = document.querySelector("body");
body.appendChild(svg);
const pathBounds = toRectangleF(svgPath.getBBox());
body.removeChild(svg);
return pathBounds;
}
function toRectangleF(svgRect) {
return new RectangleF(svgRect.x, svgRect.y, svgRect.width, svgRect.height);
}
}
}
redo() {
throw new NotImplementedException();
}
undo() {
throw new NotImplementedException();
}
}
//# sourceMappingURL=ChangeColumnCountCommand.js.map