Skip to content

Commit

Permalink
feat(core): add isHitElement and isHitSelectedRectangle function (#887)
Browse files Browse the repository at this point in the history
  • Loading branch information
huanhuanwa authored May 22, 2024
1 parent ce5ee71 commit 4192c48
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 25 deletions.
8 changes: 8 additions & 0 deletions .changeset/slow-cups-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@plait/core': patch
'@plait/draw': patch
---

add isHitElement and isHitSelectedRectangle function

fix rotate error
2 changes: 1 addition & 1 deletion packages/core/src/plugins/with-moving.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export function withMoving(board: PlaitBoard) {
x: activeElementsRectangle.x + offsetX,
y: activeElementsRectangle.y + offsetY
};
const activeRectangle = getRectangleByAngle(newRectangle, getSelectionAngle(activeElements)) || newRectangle;
const activeRectangle = getRectangleByAngle(newRectangle, getSelectionAngle(activeElements));
const ref = getSnapMovingRef(board, activeRectangle, activeElements);
offsetX += ref.deltaX;
offsetY += ref.deltaY;
Expand Down
10 changes: 2 additions & 8 deletions packages/core/src/plugins/with-selection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import { RectangleClient } from '../interfaces/rectangle-client';
import {
cacheSelectedElements,
clearSelectedElement,
getHitElementByPoint,
getHitElementsBySelection,
getHitSelectedElements,
getSelectedElements,
isHitElement,
removeSelectedElement
} from '../utils/selected-element';
import { PlaitElement, PlaitPointerType, SELECTION_BORDER_COLOR, SELECTION_FILL_COLOR } from '../interfaces';
Expand Down Expand Up @@ -66,18 +65,13 @@ export function withSelection(board: PlaitBoard) {
}

const point = toViewBoxPoint(board, toHostPoint(board, event.x, event.y));
const selectedElements = getSelectedElements(board);
const hitElement = getHitElementByPoint(board, point);
const hitSelectedElements = selectedElements.length > 1 ? getHitSelectedElements(board, point) : [];
const isHitTarget = hitElement || hitSelectedElements.length > 0;
const isHitTarget = isHitElement(board, point);
const options = (board as PlaitOptionsBoard).getPluginOptions<WithPluginOptions>(PlaitPluginKey.withSelection);

if (PlaitBoard.isPointer(board, PlaitPointerType.selection) && !isHitTarget && options.isMultiple && !options.isDisabledSelect) {
preventTouchMove(board, event, true);
// start rectangle selection
start = toViewBoxPoint(board, toHostPoint(board, event.x, event.y));
}

pointerDown(event);
};

Expand Down
5 changes: 2 additions & 3 deletions packages/core/src/utils/angle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,8 @@ export const getRectangleByAngle = (rectangle: RectangleClient, angle: number) =
const cornerPoints = RectangleClient.getCornerPoints(rectangle);
const centerPoint = RectangleClient.getCenterPoint(rectangle);
return RectangleClient.getRectangleByPoints(rotatePoints(cornerPoints, centerPoint, angle));
} else {
return null;
}
return rectangle;
};

export const isAxisChangedByAngle = (angle: number) => {
Expand All @@ -121,7 +120,7 @@ export function rotateElements(board: PlaitBoard, elements: PlaitElement[], angl
const originAngle = item.angle;
const points = rotatedDataPoints(item.points!, selectionCenterPoint, normalizeAngle(angle));
const path = PlaitBoard.findPath(board, item);
Transforms.setNode(board, { points, angle: normalizeAngle(originAngle || 0 + angle) }, path);
Transforms.setNode(board, { points, angle: normalizeAngle((originAngle || 0) + angle) }, path);
});
}

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/utils/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function getRectangleByElements(board: PlaitBoard, elements: PlaitElement
export function getBoundingRectangleByElements(board: PlaitBoard, elements: PlaitElement[], recursion: boolean): RectangleClient {
const rectangle = getRectangleByElements(board, elements, recursion)!;
const angle = getSelectionAngle(elements);
return getRectangleByAngle(rectangle, angle) || rectangle;
return getRectangleByAngle(rectangle, angle);
}

export function getBoardRectangle(board: PlaitBoard): RectangleClient {
Expand Down
10 changes: 10 additions & 0 deletions packages/core/src/utils/selected-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,13 @@ export const temporaryDisableSelection = (board: PlaitOptionsBoard) => {
board.setPluginOptions<WithPluginOptions>(PlaitPluginKey.withSelection, { ...currentOptions });
}, 0);
};

export const isHitSelectedRectangle = (board: PlaitBoard, point: Point) => {
const hitSelectedElements = getHitSelectedElements(board, point);
return hitSelectedElements.length > 0;
};

export const isHitElement = (board: PlaitBoard, point: Point) => {
const hitElement = getHitElementByPoint(board, point);
return !!hitElement || isHitSelectedRectangle(board, point);
};
5 changes: 4 additions & 1 deletion packages/core/src/utils/snap/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ export function getSnapRectangles(board: PlaitBoard, activeElements: PlaitElemen
recursion: () => true,
isReverse: false
});
return elements.map(item => getRectangleByAngle(board.getRectangle(item)!, item.angle || 0) || board.getRectangle(item)!);
return elements.map(item => {
const rectangle = board.getRectangle(item)!;
return getRectangleByAngle(rectangle, item.angle || 0);
});
}

export function getBarPoint(point: Point, isHorizontal: boolean) {
Expand Down
17 changes: 6 additions & 11 deletions packages/draw/src/utils/snap-resizing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ export function getSnapResizingRefOptions(
const points = resizeOriginPoint.map(p => {
return movePointByZoomAndOriginPoint(p, originPoint, xZoom, yZoom);
}) as [Point, Point];
const activeRectangle =
getRectangleByAngle(RectangleClient.getRectangleByPoints(points), getSelectionAngle(activeElements)) ||
RectangleClient.getRectangleByPoints(points);
const rectangle = RectangleClient.getRectangleByPoints(points);
const activeRectangle = getRectangleByAngle(rectangle, getSelectionAngle(activeElements));
const resizeHandlePoint = movePointByZoomAndOriginPoint(handlePoint, originPoint, xZoom, yZoom);
const [x, y] = getUnitVectorByPointAndPoint(originPoint, resizeHandlePoint);
return {
Expand Down Expand Up @@ -232,10 +231,8 @@ function drawResizingPointSnapLines(
angle: number
) {
debugGenerator.isDebug() && debugGenerator.clear();
const activeRectangle =
getRectangleByAngle(RectangleClient.getRectangleByPoints(activePoints), angle) ||
RectangleClient.getRectangleByPoints(activePoints);

const rectangle = RectangleClient.getRectangleByPoints(activePoints);
const activeRectangle = getRectangleByAngle(rectangle, angle);
const { isAspectRatio, directionFactors } = resizeSnapOptions;
const drawHorizontal = directionFactors[0] !== 0 || isAspectRatio;
const drawVertical = directionFactors[1] !== 0 || isAspectRatio;
Expand All @@ -254,9 +251,8 @@ function drawIsometricSnapLines(

const drawHorizontalLine = resizeSnapOptions.directionFactors[0] !== 0 || resizeSnapOptions.isAspectRatio;
const drawVerticalLine = resizeSnapOptions.directionFactors[1] !== 0 || resizeSnapOptions.isAspectRatio;
const activeRectangle =
getRectangleByAngle(RectangleClient.getRectangleByPoints(activePoints), angle) ||
RectangleClient.getRectangleByPoints(activePoints);
const rectangle = RectangleClient.getRectangleByPoints(activePoints);
const activeRectangle = getRectangleByAngle(rectangle, angle);
for (let snapRectangle of snapRectangles) {
if (activeRectangle.width === snapRectangle.width && drawHorizontalLine) {
widthIsometricPoints.push(getIsometricLinePoints(snapRectangle, true));
Expand All @@ -275,4 +271,3 @@ function drawIsometricSnapLines(
const isometricLines = [...widthIsometricPoints, ...heightIsometricPoints];
return drawSolidLines(board, isometricLines);
}

0 comments on commit 4192c48

Please sign in to comment.