refactor: Update generic constraint Point to use GenericPoint

- Changed existing generic constraints that used LocalPoint | GlobalPoint as Point to now use the unified GenericPoint type.
This commit is contained in:
sunub 2025-04-17 13:16:35 +09:00
parent 71e55dba9f
commit 85cb973936
21 changed files with 153 additions and 211 deletions

View file

@ -19,9 +19,9 @@ import { isPointInShape, isPointOnShape } from "@excalidraw/utils/collision";
import { type GeometricShape, getPolygonShape } from "@excalidraw/utils/shape";
import type {
GenericPoint,
GlobalPoint,
LineSegment,
LocalPoint,
Polygon,
Radians,
} from "@excalidraw/math";
@ -72,7 +72,7 @@ export const shouldTestInside = (element: ExcalidrawElement) => {
return isDraggableFromInside || isImageElement(element);
};
export type HitTestArgs<Point extends GlobalPoint | LocalPoint> = {
export type HitTestArgs<Point extends GenericPoint> = {
x: number;
y: number;
element: ExcalidrawElement;
@ -81,7 +81,7 @@ export type HitTestArgs<Point extends GlobalPoint | LocalPoint> = {
frameNameBound?: FrameNameBounds | null;
};
export const hitElementItself = <Point extends GlobalPoint | LocalPoint>({
export const hitElementItself = <Point extends GenericPoint>({
x,
y,
element,
@ -127,9 +127,7 @@ export const hitElementBoundingBox = (
);
};
export const hitElementBoundingBoxOnly = <
Point extends GlobalPoint | LocalPoint,
>(
export const hitElementBoundingBoxOnly = <Point extends GenericPoint>(
hitArgs: HitTestArgs<Point>,
elementsMap: ElementsMap,
) => {
@ -145,7 +143,7 @@ export const hitElementBoundingBoxOnly = <
);
};
export const hitElementBoundText = <Point extends GlobalPoint | LocalPoint>(
export const hitElementBoundText = <Point extends GenericPoint>(
x: number,
y: number,
textShape: GeometricShape<Point> | null,

View file

@ -9,6 +9,7 @@ import {
vectorCross,
vectorFromPoint,
vectorScale,
type GenericPoint,
type GlobalPoint,
type LocalPoint,
} from "@excalidraw/math";
@ -1642,7 +1643,7 @@ const pathTo = (start: Node, node: Node) => {
return path;
};
const m_dist = (a: GlobalPoint | LocalPoint, b: GlobalPoint | LocalPoint) =>
const m_dist = (a: GenericPoint, b: GenericPoint) =>
Math.abs(a[0] - b[0]) + Math.abs(a[1] - b[1]);
/**
@ -2291,7 +2292,7 @@ const getHoveredElement = (
const gridAddressesEqual = (a: GridAddress, b: GridAddress): boolean =>
a[0] === b[0] && a[1] === b[1];
export const validateElbowPoints = <P extends GlobalPoint | LocalPoint>(
export const validateElbowPoints = <P extends GenericPoint>(
points: readonly P[],
tolerance: number = DEDUP_TRESHOLD,
) =>

View file

@ -13,10 +13,10 @@ import {
} from "@excalidraw/math";
import type {
LocalPoint,
GlobalPoint,
Triangle,
Vector,
GenericPoint,
} from "@excalidraw/math";
import { getCenterForBounds, type Bounds } from "./bounds";
@ -43,12 +43,10 @@ export const vectorToHeading = (vec: Vector): Heading => {
return HEADING_UP;
};
export const headingForPoint = <P extends GlobalPoint | LocalPoint>(
p: P,
o: P,
) => vectorToHeading(vectorFromPoint<P>(p, o));
export const headingForPoint = <P extends GenericPoint>(p: P, o: P) =>
vectorToHeading(vectorFromPoint<P>(p, o));
export const headingForPointIsHorizontal = <P extends GlobalPoint | LocalPoint>(
export const headingForPointIsHorizontal = <P extends GenericPoint>(
p: P,
o: P,
) => headingIsHorizontal(headingForPoint<P>(p, o));

View file

@ -7,7 +7,7 @@ import {
import { SIDE_RESIZING_THRESHOLD } from "@excalidraw/common";
import type { GlobalPoint, LineSegment, LocalPoint } from "@excalidraw/math";
import type { GenericPoint, LineSegment } from "@excalidraw/math";
import type { AppState, Device, Zoom } from "@excalidraw/excalidraw/types";
@ -43,7 +43,7 @@ const isInsideTransformHandle = (
y >= transformHandle[1] &&
y <= transformHandle[1] + transformHandle[3];
export const resizeTest = <Point extends GlobalPoint | LocalPoint>(
export const resizeTest = <Point extends GenericPoint>(
element: NonDeletedExcalidrawElement,
elementsMap: ElementsMap,
appState: AppState,
@ -152,9 +152,7 @@ export const getElementWithTransformHandleType = (
}, null as { element: NonDeletedExcalidrawElement; transformHandleType: MaybeTransformHandleType } | null);
};
export const getTransformHandleTypeFromCoords = <
Point extends GlobalPoint | LocalPoint,
>(
export const getTransformHandleTypeFromCoords = <Point extends GenericPoint>(
[x1, y1, x2, y2]: Bounds,
scenePointerX: number,
scenePointerY: number,
@ -271,7 +269,7 @@ export const getCursorForResizingElement = (resizingElement: {
return cursor ? `${cursor}-resize` : "";
};
const getSelectionBorders = <Point extends LocalPoint | GlobalPoint>(
const getSelectionBorders = <Point extends GenericPoint>(
[x1, y1]: Point,
[x2, y2]: Point,
center: Point,

View file

@ -13,8 +13,7 @@ import {
pointFromPair,
pointRotateRads,
pointsEqual,
type GlobalPoint,
type LocalPoint,
type GenericPoint,
} from "@excalidraw/math";
import {
getClosedCurveShape,
@ -46,7 +45,7 @@ import type {
* get the pure geometric shape of an excalidraw elementw
* which is then used for hit detection
*/
export const getElementShape = <Point extends GlobalPoint | LocalPoint>(
export const getElementShape = <Point extends GenericPoint>(
element: ExcalidrawElement,
elementsMap: ElementsMap,
): GeometricShape<Point> => {
@ -98,7 +97,7 @@ export const getElementShape = <Point extends GlobalPoint | LocalPoint>(
}
};
export const getBoundTextShape = <Point extends GlobalPoint | LocalPoint>(
export const getBoundTextShape = <Point extends GenericPoint>(
element: ExcalidrawElement,
elementsMap: ElementsMap,
): GeometricShape<Point> | null => {
@ -126,9 +125,7 @@ export const getBoundTextShape = <Point extends GlobalPoint | LocalPoint>(
return null;
};
export const getControlPointsForBezierCurve = <
P extends GlobalPoint | LocalPoint,
>(
export const getControlPointsForBezierCurve = <P extends GenericPoint>(
element: NonDeleted<ExcalidrawLinearElement>,
endPoint: P,
) => {
@ -170,7 +167,7 @@ export const getControlPointsForBezierCurve = <
return controlPoints;
};
export const getBezierXY = <P extends GlobalPoint | LocalPoint>(
export const getBezierXY = <P extends GenericPoint>(
p0: P,
p1: P,
p2: P,
@ -187,7 +184,7 @@ export const getBezierXY = <P extends GlobalPoint | LocalPoint>(
return pointFrom(tx, ty);
};
const getPointsInBezierCurve = <P extends GlobalPoint | LocalPoint>(
const getPointsInBezierCurve = <P extends GenericPoint>(
element: NonDeleted<ExcalidrawLinearElement>,
endPoint: P,
) => {
@ -217,7 +214,7 @@ const getPointsInBezierCurve = <P extends GlobalPoint | LocalPoint>(
return pointsOnCurve;
};
const getBezierCurveArcLengths = <P extends GlobalPoint | LocalPoint>(
const getBezierCurveArcLengths = <P extends GenericPoint>(
element: NonDeleted<ExcalidrawLinearElement>,
endPoint: P,
) => {
@ -236,7 +233,7 @@ const getBezierCurveArcLengths = <P extends GlobalPoint | LocalPoint>(
return arcLengths;
};
export const getBezierCurveLength = <P extends GlobalPoint | LocalPoint>(
export const getBezierCurveLength = <P extends GenericPoint>(
element: NonDeleted<ExcalidrawLinearElement>,
endPoint: P,
) => {
@ -245,7 +242,7 @@ export const getBezierCurveLength = <P extends GlobalPoint | LocalPoint>(
};
// This maps interval to actual interval t on the curve so that when t = 0.5, its actually the point at 50% of the length
export const mapIntervalToBezierT = <P extends GlobalPoint | LocalPoint>(
export const mapIntervalToBezierT = <P extends GenericPoint>(
element: NonDeleted<ExcalidrawLinearElement>,
endPoint: P,
interval: number, // The interval between 0 to 1 for which you want to find the point on the curve,
@ -340,7 +337,7 @@ export const aabbForElement = (
return bounds;
};
export const pointInsideBounds = <P extends GlobalPoint | LocalPoint>(
export const pointInsideBounds = <P extends GenericPoint>(
p: P,
bounds: Bounds,
): boolean =>