refactor: remove points function from snapping and move to linear editor

This commit is contained in:
Ryan Di
2025-08-04 18:04:59 +10:00
parent 7332e76d56
commit 60a459b135
2 changed files with 57 additions and 64 deletions

View File

@@ -1319,18 +1319,53 @@ export class LinearElementEditor {
static getPointsGlobalCoordinates(
element: NonDeleted<ExcalidrawLinearElement>,
elementsMap: ElementsMap,
options: {
dragOffset?: { x: number; y: number };
excludePointsIndices?: readonly number[];
} = {},
): GlobalPoint[] {
const { dragOffset, excludePointsIndices } = options;
if (!element.points || element.points.length === 0) {
return [];
}
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
const cx = (x1 + x2) / 2;
const cy = (y1 + y2) / 2;
return element.points.map((p) => {
const { x, y } = element;
return pointRotateRads(
pointFrom(x + p[0], y + p[1]),
let elementX = element.x;
let elementY = element.y;
if (dragOffset) {
elementX += dragOffset.x;
elementY += dragOffset.y;
}
const globalPoints: GlobalPoint[] = [];
for (let i = 0; i < element.points.length; i++) {
// Skip the point being edited if specified
if (
excludePointsIndices?.length &&
excludePointsIndices.find((index) => index === i) !== undefined
) {
continue;
}
const p = element.points[i];
const globalX = elementX + p[0];
const globalY = elementY + p[1];
const rotated = pointRotateRads<GlobalPoint>(
pointFrom(globalX, globalY),
pointFrom(cx, cy),
element.angle,
);
});
globalPoints.push(rotated);
}
return globalPoints;
}
static getPointAtIndexGlobalCoordinates(

View File

@@ -35,6 +35,7 @@ import type {
ExcalidrawElement,
ExcalidrawLinearElement,
NonDeletedExcalidrawElement,
NonDeleted,
} from "@excalidraw/element/types";
import type {
@@ -43,6 +44,8 @@ import type {
KeyboardModifiersObject,
} from "@excalidraw/excalidraw/types";
import { LinearElementEditor } from "./linearElementEditor";
const SNAP_DISTANCE = 8;
// do not comput more gaps per axis than this limit
@@ -194,59 +197,6 @@ export const areRoughlyEqual = (a: number, b: number, precision = 0.01) => {
return Math.abs(a - b) <= precision;
};
export const getLinearElementPoints = (
element: ExcalidrawLinearElement,
options: {
dragOffset?: Vector2D;
excludePointsIndices?: readonly number[];
} = {},
): GlobalPoint[] => {
const { dragOffset, excludePointsIndices } = options;
if (isElbowArrow(element)) {
return [];
}
if (!element.points || element.points.length === 0) {
return [];
}
let elementX = element.x;
let elementY = element.y;
if (dragOffset) {
elementX += dragOffset.x;
elementY += dragOffset.y;
}
const globalPoints: GlobalPoint[] = [];
for (let i = 0; i < element.points.length; i++) {
// Skip the point being edited if specified
if (
excludePointsIndices?.length &&
excludePointsIndices.find((index) => index === i) !== undefined
) {
continue;
}
const point = element.points[i];
const globalX = elementX + point[0];
const globalY = elementY + point[1];
const cx = elementX + element.width / 2;
const cy = elementY + element.height / 2;
const rotated = pointRotateRads<GlobalPoint>(
pointFrom(globalX, globalY),
pointFrom(cx, cy),
element.angle,
);
globalPoints.push(pointFrom(round(rotated[0]), round(rotated[1])));
}
return globalPoints;
};
export const getElementsCorners = (
elements: ExcalidrawElement[],
elementsMap: ElementsMap,
@@ -291,9 +241,13 @@ export const getElementsCorners = (
!boundingBoxCorners
) {
// For linear elements, use actual points instead of bounding box
const linearPoints = getLinearElementPoints(element, {
dragOffset,
});
const linearPoints = LinearElementEditor.getPointsGlobalCoordinates(
element as NonDeleted<ExcalidrawLinearElement>,
elementsMap,
{
dragOffset,
},
);
result = linearPoints;
} else if (
(element.type === "diamond" || element.type === "ellipse") &&
@@ -732,9 +686,13 @@ export const getReferenceSnapPointsForLinearElementPoint = (
// Include other points from the same linear element when creating new points or in editing mode
if (includeSelfPoints) {
const elementPoints = getLinearElementPoints(editingElement, {
excludePointsIndices: options.selectedPointsIndices,
});
const elementPoints = LinearElementEditor.getPointsGlobalCoordinates(
editingElement as NonDeleted<ExcalidrawLinearElement>,
elementsMap,
{
excludePointsIndices: options.selectedPointsIndices,
},
);
allSnapPoints.push(...elementPoints);
}