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.

574 lines 23.7 kB
import { EventWithAutoSenderArg, EventWithSenderArg } from "@aurigma/design-atoms-model/EventObject"; import { ItemHandler } from "./ItemHandler"; import { ContentType } from "./ContentType"; import { Graphics } from "../Graphics"; import { WrappingMode } from "@aurigma/design-atoms-model/Product/Items"; import { RotatedRectangleF, PointF, RectangleF, EqualsOfFloatNumbers, Path } from "@aurigma/design-atoms-model/Math"; import { PathHandler } from "./PathHandler"; export class BaseRectangleItemHandler extends ItemHandler { constructor(left, top, width, height, item, _textWhizz = null, colorPreviewService) { super(item); this._textWhizz = _textWhizz; this._transformChanging = new EventWithAutoSenderArg(this); this._violationWarningButtonClickEvent = new EventWithSenderArg(); this._violationWarningButtonClick = new EventWithSenderArg(); this._doubleClickEvent = new EventWithSenderArg(); this._clickEvent = new EventWithSenderArg(); this._onColorPreviewLoaded = (args) => { var _a; this._colorPreviewService.unsubscribeFromPreviewLoaded(args.color, this._onColorPreviewLoaded); (_a = this.canvas) === null || _a === void 0 ? void 0 : _a.redraw(); this._dispatchReadyEvent(); }; this._applyMatrix = (matrix, finished, newAngle = null) => { this.setRectangle(this.rectangle.transformByMatrix(matrix, newAngle), !finished); }; var l = (left) ? left : 0; var t = (top) ? top : 0; var w = (width) ? width : 0; var h = (height) ? height : 0; this._controlPoints = [new PointF(l, t), new PointF(l + w, t + h)]; this._allowNegativeResize = false; this._selectionTolerance = 0; this._violationWarningButton = null; this._violationWarningButtonClickHandler = this._onViolationWarningButtonClick.bind(this); this._colorPreviewService = colorPreviewService; this._violationContainer = null; this._updateVectorMaskCenterSync(); } get allowNegativeResize() { return this._allowNegativeResize; } get transformChanging() { return this._transformChanging; } get rectangle() { return this._getRectangle(); } get bounds() { var r = this.rectangle; var margin = this._getBoundsMargin(); r.width += r.width > 0 ? margin : -margin; r.height += r.height > 0 ? margin : -margin; return r.bounds; } get drawingRectangle() { return this.rectangle; } get angle() { return this.item.transform.angle; } set angle(angle) { this.item.transform.setAngle(angle); } get height() { return this.rectangle.height; } set height(height) { var r = this.rectangle; r.height = height; this.setRectangle(r); } get width() { return this.rectangle.width; } set width(width) { var r = this.rectangle; r.width = width; this.setRectangle(r); } get startRectangle() { return this._startRectangle != null ? this._startRectangle : null; } static transformRectangleByTwoRectanglesDiff(rectangle, startRectangle, endRectangle) { var transform = endRectangle.getTransform(startRectangle); var angle = transform.angle; var scaleX = transform.scaleX; var scaleY = transform.scaleY; if (!EqualsOfFloatNumbers(endRectangle.angle, 0)) rectangle.rotateAt(-endRectangle.angle, startRectangle.center); if (!EqualsOfFloatNumbers(scaleX, 1) || !EqualsOfFloatNumbers(scaleY, 1)) { //Swap scales if the angle is 90 or 270 //TODO FIXIT var swapScales = false; rectangle.width = rectangle.width * (swapScales ? scaleY : scaleX); rectangle.height = rectangle.height * (swapScales ? scaleX : scaleY); rectangle.centerX = endRectangle.centerX + (rectangle.centerX - startRectangle.centerX) * scaleX; rectangle.centerY = endRectangle.centerY + (rectangle.centerY - startRectangle.centerY) * scaleY; } else { rectangle.centerX += transform.translateX; rectangle.centerY += transform.translateY; } if (!EqualsOfFloatNumbers(endRectangle.angle, 0)) rectangle.rotateAt(endRectangle.angle, endRectangle.center); if (!EqualsOfFloatNumbers(angle, 0)) { var center = rectangle.center; center.rotateAt(angle, startRectangle.center); rectangle.angle = rectangle.angle + angle; rectangle.center = center; } return rectangle; } setRectangle(rectangle, supressOnChanged) { const controlBounds = RotatedRectangleF.fromRectangleF(this.getControlBounds()); const transform = rectangle.getTransform(controlBounds); this.item.setTransform(transform, supressOnChanged); } getControlBounds() { const maxX = Math.max(this._controlPoints[0].x, this._controlPoints[1].x); const maxY = Math.max(this._controlPoints[0].y, this._controlPoints[1].y); const minX = Math.min(this._controlPoints[0].x, this._controlPoints[1].x); const minY = Math.min(this._controlPoints[0].y, this._controlPoints[1].y); const width = maxX - minX; const height = maxY - minY; return new RectangleF(minX, minY, width, height); } getControlCenter() { var r = this.getControlBounds(); var centerX = r.left + (r.width / 2); var centerY = r.top + (r.height / 2); return new PointF(centerX, centerY); } transformByMatrix(matrix, finished, newAngle = null) { if (!finished && this._startRectangle == null) { this.startTransform(); } this._applyMatrix(matrix, finished, newAngle); this.transformChanged(); if (finished && this._startRectangle != null) { const rectangle = this.rectangle; const changed = !rectangle.equals(this._startRectangle); const resized = !EqualsOfFloatNumbers(rectangle.width, this._startRectangle.width) || !EqualsOfFloatNumbers(rectangle.height, this._startRectangle.height); if (resized) this.onResized(); this._endTransform(changed, resized, true); this._startRectangle = null; } var canvas = this.canvas; if (canvas != null) { this.updateViolationContainerPosition(); canvas.redraw(); } if (this.item.parentGroupItem != null) { this.raiseChanging(this.item); } else { this._throttleSizeChanging(); } } getTransformedRectangle(relativeToPrintArea = true, withMargins) { const transformedRectangle = this.rectangle; const printAreaBounds = this._getPrintAreaBounds(); if (printAreaBounds != null && relativeToPrintArea) transformedRectangle.location = transformedRectangle.location.translate(-printAreaBounds.left, -printAreaBounds.top); if (withMargins) { const margin = this.getBorderMargin(); transformedRectangle.width += margin * 2; transformedRectangle.height += margin * 2; } return transformedRectangle; } setTransformedRectangle(value, suppressOnChanged) { this.setRectangle(value, suppressOnChanged); } getTextWrappingPath() { return this.item.textWrappingMode !== WrappingMode.None ? Path.rotatedRectangle(this.rectangle) : null; } setLocation(location) { var r = this.rectangle; r.location = location; this.setRectangle(r); } onTextWhizzInit() { this._updateVectorMaskCenter(); } hitTest(point) { return this.canvas.hitTestManager.hitTestSelection(this._getHighlightRectangle(), point); } addViolationWarningButtonClick(handler) { this._violationWarningButtonClick.add(handler); } removeViolationWarningButtonClick(handler) { this._violationWarningButtonClick.remove(handler); } getViolationWarningButton() { return this._violationWarningButton; } setViolationWarningButton(value) { this._violationWarningButton = value; } getViolationWarningButtonClickHandler() { return this._violationWarningButtonClickHandler; } getViolationContainer() { return this._violationContainer; } setViolationContainer(value) { this._violationContainer = value; } getViolationWarningButtonElement() { return this._violationWarningButton; } transformRectangle(startRectangle, endRectangle, highlightOnly = false) { this._onTransformRectangle(startRectangle, endRectangle, highlightOnly); this.updateViolationContainerPosition(); if (this.item.parentGroupItem != null) { this.raiseChanging(this.item); } else { this._throttleSizeChanging(); } } startTransform() { super.startTransform(); this._startRectangle = this.rectangle; if (this.canvas != null) this.canvas.updateViolationContainer(this, false); } endTransform(changed, resized, supressOnChanged = false) { super.endTransform(changed, resized, supressOnChanged); this._endTransform(changed, resized, supressOnChanged); } transformChanged() { super.transformChanged(); this.updateViolationContainerPosition(); } onResized() { delete this._startRectangle; return true; } dispatchDoubleClickEvent(e) { this._doubleClickEvent.notify(this, e); } addDoubleClick(handler) { this._doubleClickEvent.add(handler); } removeDoubleClick(handler) { this._doubleClickEvent.remove(handler); } dispatchClickEvent(e) { this._clickEvent.notify(e); } addClick(handler) { this._clickEvent.add(handler); } removeClick(handler) { this._clickEvent.remove(handler); } getHighlightRectangles(options = { includeChildren: true, includeParent: true }) { return [this._getHighlightRectangle()]; } getSelectionRectangle() { if (!this.isVisible()) return null; return this.rectangle; } getBorderMargin() { return this._getBoundsMargin() / 2; } getSelectionTolerance() { return this._selectionTolerance; } showDeleteToolbarButton() { return this.getPermissions().allowDelete && this.getPermissions().showDeleteToolbarButton; } showHandleToolbarButton() { return false; } showEditToolbarButton() { return this.getPermissions().showEditToolbarButton; } showSelectToolbarButton() { return this.getPermissions().showSelectToolbarButton; } updateViolationContainerPosition() { var _a; (_a = this.canvas) === null || _a === void 0 ? void 0 : _a.updateViolationContainerPosition(this); } isGoodViolationIcon() { if (this._violationWarningButton == null) return false; var goodIconCssClass = this.canvas.goodViolationIconCssClass; return this._violationWarningButton.classList.contains(goodIconCssClass); } setGoodViolationIcon(title) { if (this._violationWarningButton == null) return false; this._showViolationButton(); if (title != null) this._violationWarningButton.title = title; if (this.isGoodViolationIcon()) return; var canvas = this.canvas; var goodIconCssClass = canvas.goodViolationIconCssClass; var warningIconCssClass = canvas.warningViolationIconCssClass; var badIconCssClass = canvas.badViolationIconCssClass; this._violationWarningButton.classList.remove(warningIconCssClass); this._violationWarningButton.classList.remove(badIconCssClass); this._violationWarningButton.classList.add(goodIconCssClass); this._violationWarningButton.classList.toggle("active", canvas.isOnlyThisItemHandlerSelected(this)); } isWarningViolationIcon() { if (this._violationWarningButton == null) return false; var warningIconCssClass = this.canvas.warningViolationIconCssClass; return this._violationWarningButton.classList.contains(warningIconCssClass); } setWarningViolationIcon(title) { if (this._violationWarningButton == null) return; this._showViolationButton(); if (title != null) this._violationWarningButton.title = title; if (this.isWarningViolationIcon()) return; var canvas = this.canvas; var goodIconCssClass = canvas.goodViolationIconCssClass; var warningIconCssClass = canvas.warningViolationIconCssClass; var badIconCssClass = canvas.badViolationIconCssClass; this._violationWarningButton.classList.remove(goodIconCssClass); this._violationWarningButton.classList.remove(badIconCssClass); this._violationWarningButton.classList.add(warningIconCssClass); this._violationWarningButton.classList.toggle("active", canvas.isOnlyThisItemHandlerSelected(this)); } isBadViolationIcon() { if (this._violationWarningButton == null) return false; var badIconCssClass = this.canvas.badViolationIconCssClass; return this._violationWarningButton.classList.contains(badIconCssClass); } setBadViolationIcon(title) { if (this._violationWarningButton == null) return; this._showViolationButton(); if (title != null) this._violationWarningButton.title = title; if (this.isBadViolationIcon()) return; var canvas = this.canvas; var goodIconCssClass = canvas.goodViolationIconCssClass; var warningIconCssClass = canvas.warningViolationIconCssClass; var badIconCssClass = canvas.badViolationIconCssClass; this._violationWarningButton.classList.remove(goodIconCssClass); this._violationWarningButton.classList.remove(warningIconCssClass); this._violationWarningButton.classList.add(badIconCssClass); this._violationWarningButton.classList.toggle("active", canvas.isOnlyThisItemHandlerSelected(this)); } isNoneViolationIcon() { if (this._violationWarningButton == null) return false; var canvas = this.canvas; var goodIconCssClass = canvas.goodViolationIconCssClass; var warningIconCssClass = canvas.warningViolationIconCssClass; var badIconCssClass = canvas.badViolationIconCssClass; var isButtonHasViClass = this._violationWarningButton.classList.contains(goodIconCssClass) || this._violationWarningButton.classList.contains(warningIconCssClass) || this._violationWarningButton.classList.contains(badIconCssClass); return this._violationWarningButton.style.visibility === "hidden" || !isButtonHasViClass; } setNoneViolationIcon() { if (this._violationWarningButton == null) return; var canvas = this.canvas; var goodIconCssClass = canvas.goodViolationIconCssClass; var warningIconCssClass = canvas.warningViolationIconCssClass; var badIconCssClass = canvas.badViolationIconCssClass; this._violationWarningButton.classList.remove(goodIconCssClass); this._violationWarningButton.classList.remove(warningIconCssClass); this._violationWarningButton.classList.remove(badIconCssClass); this._violationWarningButton.style.visibility = "hidden"; } _isReady() { return this._areColorPreviewsReady; } get _areColorPreviewsReady() { return true; } get _isReadyToDraw() { return true; } async _updateImpl(beforeUpdate, afterUpdate) { this._updateViolationContainersVisibility(); } _getItemColorPreviews() { return {}; } _getHighlightRectangle() { var rect = this.rectangle; var margin = parseInt(this._getBoundsMargin().toString()); rect.width += rect.width >= 0 ? margin : -margin; rect.height += rect.height >= 0 ? margin : -margin; return rect; } _updateVisibility() { this._updateViolationContainersVisibility(); } _onViolationWarningButtonClick(e) { this._violationWarningButtonClick.notify(this, e); e.preventDefault(); } _updateViolationContainersVisibility() { var _a; (_a = this.canvas) === null || _a === void 0 ? void 0 : _a.updateViolationContainer(this, false); } _endTransform(changed, resized, supressOnChanged = false) { if (!supressOnChanged && changed && (resized || this._startRectangle != null)) this._itemPropertyChangedEvent.notify(this, "transform"); if (!changed || !resized) delete this._startRectangle; const canvas = this.canvas; if (canvas != null) { canvas.updateViolationContainer(this, true); } } _calculateTransformedRectangle(startRectangle, endRectangle) { let newRectangle; if (this._startRectangle.equals(startRectangle)) { newRectangle = endRectangle.clone(); } else { var rectangle = this._startRectangle.clone(); newRectangle = BaseRectangleItemHandler.transformRectangleByTwoRectanglesDiff(rectangle, startRectangle, endRectangle); } return newRectangle; } _onTransformRectangle(startRectangle, endRectangle, highlightOnly) { //Transform rectangle relatively global start and end rectangles if (startRectangle == null || endRectangle == null || this._startRectangle == null) return; const newRectangle = this._calculateTransformedRectangle(startRectangle, endRectangle); this.setRectangle(newRectangle, true); } _onAddedOnCanvas(canvas, supressUpdate) { //TODO: fix and move to descendants super._onAddedOnCanvas(canvas, supressUpdate); if (canvas == null) return; if (this["_contentType"] == null || this["_contentType"] === ContentType.NotContent) { canvas.addViolationContainer(this); } this._updateVectorMaskCenter(); } _onRemovedFromCanvas(canvas) { super._onRemovedFromCanvas(canvas); if (canvas != null) { canvas.removeViolationContainer(this); } } _getRectangle() { var rectangle = RotatedRectangleF.fromRectangleF(this.getControlBounds()); rectangle.setTransform(this.item.transform); return rectangle; } _getBoundsMargin() { return 0; } _clip(itemHandlerCtx) { if (this._hasVectorMask()) { const clippingPath = this._getTransformedClippingPath(); if (clippingPath != null) Graphics.clipPath(itemHandlerCtx, clippingPath); } } _onItemPropertyChanged(sender, propertyName) { switch (propertyName) { case "opacity": case "transform": break; case "locked": this._updateVisibility(); break; case "textWrappingMode": if (this.canvas != null) this.canvas.updateTexts(); break; case "mask": this._updateVectorMaskCenter(); break; case "violationSettings": this.canvas.updateViolationContainer(this); break; default: } super._onItemPropertyChanged(sender, propertyName); } _onItemVisibilityChanged() { super._onItemVisibilityChanged(); this._updateVisibility(); } async _updateVectorMaskCenter() { if (this._hasVectorMask()) { const bounds = await PathHandler.getBounds(this.item.mask.vectorMask); this._vectorMaskCenter = RotatedRectangleF.fromRectangleF(bounds).center; } else { this._vectorMaskCenter = null; } } _updateVectorMaskCenterSync() { if (this._hasVectorMask()) { const bounds = PathHandler.getBoundsSync(this.item.mask.vectorMask); if (bounds == null) return; this._vectorMaskCenter = RotatedRectangleF.fromRectangleF(bounds).center; } else { this._vectorMaskCenter = null; } } _getTransformedClippingPath(transform) { if (!this._hasVectorMask() || this._vectorMaskCenter == null) return null; if (transform == null) transform = this.item.transform.clone(); const mask = this.item.mask; const path = mask.vectorMask.clone(); const center = this._vectorMaskCenter; const controlCenter = this.getControlCenter(); if (transform.angle !== 0) { path.rotateAt(transform.angle, controlCenter); transform.setAngle(0, true); } if (!EqualsOfFloatNumbers(transform.scaleX, 1) || !EqualsOfFloatNumbers(transform.scaleY, 1)) { const centerXDelta = center.x - controlCenter.x; const centerYDelta = center.y - controlCenter.y; controlCenter.translate(transform.translateX, transform.translateY); transform.setTranslateX(controlCenter.x + centerXDelta * transform.scaleX - center.x); transform.setTranslateY(controlCenter.y + centerYDelta * transform.scaleY - center.y); } path.transform(transform, center); return path; } _hasVectorMask() { return this.item.mask != null && this.item.mask.vectorMask != null; } _refreshState() { super._refreshState(); if (!this._isIgnorePermissionsMode() || this.canvas == null) { return; } if (!this.visible || this.locked) { this.canvas.removeSelectedItemHandler(this); } this.canvas.updateViolationContainer(this, true); this.canvas.redraw(); } _showViolationButton() { var _a; const isVisible = this.isVisible() && ((_a = this.canvas) === null || _a === void 0 ? void 0 : _a.previewMode) != true; this._violationWarningButton.style.visibility = isVisible ? "visible" : "hidden"; } _getPrintAreaBounds() { if (this.layer == null || this.layer.container.parentComponent == null) return null; const surface = this.layer.container.parentComponent; if (surface.printAreas == null || surface.printAreas.length < 1) return null; return surface.printAreas.get(0).bounds; } } BaseRectangleItemHandler.typeName = "BaseRectangleItemHandler"; //# sourceMappingURL=BaseRectangleItemHandler.js.map