@aurigma/design-atoms
Version:
Design Atoms is a part of Customer's Canvas SDK which allows for manipulating individual design elements through your code.
94 lines • 6.25 kB
JavaScript
import { LineItemHandler, PlaceholderItemHandler, NewBaseTextItemHandler } from "../ItemHandlers";
import { ResizeGripsPermissions } from "@aurigma/design-atoms-model/Product/Items";
import { ResizeHelper } from "../Utils/Grips";
import { CoordinateSystem } from "../Viewer/CoordinateSystem";
export class SelectionHitTestManager {
constructor(_selectionHandler, _hitTestManager) {
this._selectionHandler = _selectionHandler;
this._hitTestManager = _hitTestManager;
}
hitTest(point) {
var _a;
if (!this._selectionHandler.enabled)
return {};
const rectangle = this._selectionHandler.visibleRectangle;
const itemHandlersByHitTest = this._hitTestManager.getItemHandlersByHitTest(point, CoordinateSystem.workspace);
if (rectangle == null) {
const selection = itemHandlersByHitTest.length === 0;
return { selection: selection };
}
let tolerance;
let currentItemHandler;
const itemHandlers = this._selectionHandler.selectedItemHandlers;
if (itemHandlers.length > 1) {
tolerance = itemHandlers.toArray().reduce((previousSelectionTolerance, currentItem) => Math.min(previousSelectionTolerance, currentItem.getSelectionTolerance()), itemHandlers.getItem(0).getSelectionTolerance());
}
else {
currentItemHandler = itemHandlers.getItem(0);
tolerance = (_a = currentItemHandler === null || currentItemHandler === void 0 ? void 0 : currentItemHandler.getSelectionTolerance()) !== null && _a !== void 0 ? _a : 0;
}
const onlyWidthResizeEnabled = itemHandlers.length === 1 && this._selectionHandler.currentItemHandler instanceof LineItemHandler;
const result = this._hitTestManager.hitTestSelection(rectangle, point, onlyWidthResizeEnabled, tolerance);
let isPlaceholderWithCoverModeEditing = false;
let isPlaceholderWithCropMode = false;
if (currentItemHandler instanceof PlaceholderItemHandler) {
const currentPlaceholder = currentItemHandler;
if (currentItemHandler.editing && itemHandlersByHitTest[0] == currentItemHandler)
result.selection = false;
if (currentPlaceholder.isContentUpdating() && (result.body && !result.resize)) {
result.drag = true;
return result;
}
isPlaceholderWithCoverModeEditing = currentItemHandler.editing && currentItemHandler.isCoverMode();
isPlaceholderWithCropMode = currentItemHandler.isCropMode;
}
if (currentItemHandler instanceof NewBaseTextItemHandler && currentItemHandler.isInEdit) {
result.resize = false;
result.rotate = false;
result.body = false;
}
const permissions = this._selectionHandler.manipulationPermissions;
result.rotate = result.rotate && permissions.allowRotate && !isPlaceholderWithCoverModeEditing && this._selectionHandler.allowManipulation;
if (result.resize) {
const index = result.resizeIndex;
const edgeArbitraryResize = ResizeHelper.isEdgeResizeGrip(index) &&
this._getEdgeResize(permissions, currentItemHandler);
const cornerArbitraryResize = isPlaceholderWithCropMode ? false : ResizeHelper.isCornerResizeGrip(index) &&
this._getCornerResize(permissions, currentItemHandler, false, ResizeGripsPermissions.arbitrary);
const cornerArbitraryResizeInvertedShift = ResizeHelper.isCornerResizeGrip(index) &&
this._getCornerResize(permissions, currentItemHandler, true, ResizeGripsPermissions.arbitrary);
const cornerProportionalResize = ResizeHelper.isCornerResizeGrip(index) &&
this._getCornerResize(permissions, currentItemHandler, false, ResizeGripsPermissions.proportional);
const cornerProportionalResizeInvertedShift = ResizeHelper.isCornerResizeGrip(index) &&
this._getCornerResize(permissions, currentItemHandler, true, ResizeGripsPermissions.proportional);
result.arbitraryResize = edgeArbitraryResize || cornerArbitraryResize;
const resultCornerArbitraryResize = cornerArbitraryResize || cornerArbitraryResizeInvertedShift;
result.cornerArbitraryResize = isPlaceholderWithCropMode ? false : resultCornerArbitraryResize;
const resultCornerProportionalResize = cornerProportionalResize || cornerProportionalResizeInvertedShift;
result.cornerProportionalResize = isPlaceholderWithCropMode || resultCornerProportionalResize;
result.proportionalResize = isPlaceholderWithCropMode || (cornerProportionalResize && !onlyWidthResizeEnabled);
result.resize = result.arbitraryResize || result.proportionalResize;
}
result.dragX = result.body && permissions.allowMoveHorizontal && this._selectionHandler.allowManipulation;
result.dragY = result.body && permissions.allowMoveVertical && this._selectionHandler.allowManipulation;
result.drag = result.dragX || result.dragY;
return result;
}
_getEdgeResize(permissions, currentItemHandler) {
if (currentItemHandler != null && currentItemHandler.getTypeName() === "PlainTextItemHandler" || !this._selectionHandler.allowManipulation)
return false;
const resizeGrips = permissions.resizeGrips;
return resizeGrips.edge;
}
_getCornerResize(permissions, currentItemHandler, shift, resizeType) {
if (resizeType === ResizeGripsPermissions.arbitrary &&
(currentItemHandler != null && currentItemHandler.getTypeName() === "PlainTextItemHandler") || !this._selectionHandler.allowManipulation)
return false;
const resizeGrips = permissions.resizeGrips;
if (resizeGrips.corner == null || resizeGrips.corner.length === 0)
return false;
const result = shift && resizeGrips.corner.length > 1 ? resizeGrips.corner[1] : resizeGrips.corner[0];
return result === resizeType;
}
}
//# sourceMappingURL=SelectionHitTestManager.js.map