refactor(line-snap): rename axis snap helpers
This commit is contained in:
@@ -130,9 +130,9 @@ export type SnapLine = PointSnapLine | GapSnapLine | PointerSnapLine;
|
|||||||
export class SnapCache {
|
export class SnapCache {
|
||||||
private static referenceSnapPoints: GlobalPoint[] | null = null;
|
private static referenceSnapPoints: GlobalPoint[] | null = null;
|
||||||
|
|
||||||
private static linearElementPointReferenceSnapPoints: {
|
private static linearElementAxisSnapTargets: {
|
||||||
editingElementId: ExcalidrawElement["id"];
|
editingElementId: ExcalidrawElement["id"];
|
||||||
snapPoints: GlobalPoint[];
|
snapTargets: GlobalPoint[];
|
||||||
} | null = null;
|
} | null = null;
|
||||||
|
|
||||||
private static visibleGaps: {
|
private static visibleGaps: {
|
||||||
@@ -148,24 +148,24 @@ export class SnapCache {
|
|||||||
return SnapCache.referenceSnapPoints;
|
return SnapCache.referenceSnapPoints;
|
||||||
};
|
};
|
||||||
|
|
||||||
public static setLinearElementPointReferenceSnapPoints = (
|
public static setLinearElementAxisSnapTargets = (
|
||||||
editingElementId: ExcalidrawElement["id"],
|
editingElementId: ExcalidrawElement["id"],
|
||||||
snapPoints: GlobalPoint[] | null,
|
snapTargets: GlobalPoint[] | null,
|
||||||
) => {
|
) => {
|
||||||
SnapCache.linearElementPointReferenceSnapPoints = snapPoints
|
SnapCache.linearElementAxisSnapTargets = snapTargets
|
||||||
? {
|
? {
|
||||||
editingElementId,
|
editingElementId,
|
||||||
snapPoints,
|
snapTargets,
|
||||||
}
|
}
|
||||||
: null;
|
: null;
|
||||||
};
|
};
|
||||||
|
|
||||||
public static getLinearElementPointReferenceSnapPoints = (
|
public static getLinearElementAxisSnapTargets = (
|
||||||
editingElementId: ExcalidrawElement["id"],
|
editingElementId: ExcalidrawElement["id"],
|
||||||
) => {
|
) => {
|
||||||
return SnapCache.linearElementPointReferenceSnapPoints?.editingElementId ===
|
return SnapCache.linearElementAxisSnapTargets?.editingElementId ===
|
||||||
editingElementId
|
editingElementId
|
||||||
? SnapCache.linearElementPointReferenceSnapPoints.snapPoints
|
? SnapCache.linearElementAxisSnapTargets.snapTargets
|
||||||
: null;
|
: null;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -184,7 +184,7 @@ export class SnapCache {
|
|||||||
|
|
||||||
public static destroy = () => {
|
public static destroy = () => {
|
||||||
SnapCache.referenceSnapPoints = null;
|
SnapCache.referenceSnapPoints = null;
|
||||||
SnapCache.linearElementPointReferenceSnapPoints = null;
|
SnapCache.linearElementAxisSnapTargets = null;
|
||||||
SnapCache.visibleGaps = null;
|
SnapCache.visibleGaps = null;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
@@ -681,30 +681,31 @@ export const getReferenceSnapPoints = (
|
|||||||
.flatMap((elementGroup) => getElementsCorners(elementGroup, elementsMap));
|
.flatMap((elementGroup) => getElementsCorners(elementGroup, elementsMap));
|
||||||
};
|
};
|
||||||
|
|
||||||
const getExternalReferenceSnapPointsForLinearElementPoint = (
|
const getExternalAxisSnapTargets = (
|
||||||
elements: readonly NonDeletedExcalidrawElement[],
|
elements: readonly NonDeletedExcalidrawElement[],
|
||||||
editingElement: ExcalidrawLinearElement,
|
editingElement: ExcalidrawLinearElement,
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
) => {
|
) => {
|
||||||
const cachedReferenceSnapPoints =
|
const cachedAxisSnapTargets = SnapCache.getLinearElementAxisSnapTargets(
|
||||||
SnapCache.getLinearElementPointReferenceSnapPoints(editingElement.id);
|
editingElement.id,
|
||||||
|
);
|
||||||
|
|
||||||
const externalReferenceSnapPoints =
|
const externalAxisSnapTargets =
|
||||||
cachedReferenceSnapPoints ??
|
cachedAxisSnapTargets ??
|
||||||
getReferenceSnapPoints(elements, [editingElement], appState, elementsMap);
|
getReferenceSnapPoints(elements, [editingElement], appState, elementsMap);
|
||||||
|
|
||||||
if (!cachedReferenceSnapPoints) {
|
if (!cachedAxisSnapTargets) {
|
||||||
SnapCache.setLinearElementPointReferenceSnapPoints(
|
SnapCache.setLinearElementAxisSnapTargets(
|
||||||
editingElement.id,
|
editingElement.id,
|
||||||
externalReferenceSnapPoints,
|
externalAxisSnapTargets,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return externalReferenceSnapPoints;
|
return externalAxisSnapTargets;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getSelfReferenceSnapPointsForLinearElementPoint = (
|
const getOwnAxisSnapTargets = (
|
||||||
editingElement: ExcalidrawLinearElement,
|
editingElement: ExcalidrawLinearElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
selectedPointsIndices?: readonly number[],
|
selectedPointsIndices?: readonly number[],
|
||||||
@@ -718,7 +719,7 @@ const getSelfReferenceSnapPointsForLinearElementPoint = (
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getReferenceSnapPointsForLinearElementPoint = (
|
export const getAxisSnapTargets = (
|
||||||
elements: readonly NonDeletedExcalidrawElement[],
|
elements: readonly NonDeletedExcalidrawElement[],
|
||||||
editingElement: ExcalidrawLinearElement,
|
editingElement: ExcalidrawLinearElement,
|
||||||
appState: AppState,
|
appState: AppState,
|
||||||
@@ -728,20 +729,19 @@ export const getReferenceSnapPointsForLinearElementPoint = (
|
|||||||
selectedPointsIndices?: readonly number[];
|
selectedPointsIndices?: readonly number[];
|
||||||
} = {},
|
} = {},
|
||||||
) => {
|
) => {
|
||||||
const externalReferenceSnapPoints =
|
const externalAxisSnapTargets = getExternalAxisSnapTargets(
|
||||||
getExternalReferenceSnapPointsForLinearElementPoint(
|
elements,
|
||||||
elements,
|
editingElement,
|
||||||
editingElement,
|
appState,
|
||||||
appState,
|
elementsMap,
|
||||||
elementsMap,
|
);
|
||||||
);
|
|
||||||
|
|
||||||
if (!options.includeSelfPoints) {
|
if (!options.includeSelfPoints) {
|
||||||
return externalReferenceSnapPoints;
|
return externalAxisSnapTargets;
|
||||||
}
|
}
|
||||||
|
|
||||||
return externalReferenceSnapPoints.concat(
|
return externalAxisSnapTargets.concat(
|
||||||
getSelfReferenceSnapPointsForLinearElementPoint(
|
getOwnAxisSnapTargets(
|
||||||
editingElement,
|
editingElement,
|
||||||
elementsMap,
|
elementsMap,
|
||||||
options.selectedPointsIndices,
|
options.selectedPointsIndices,
|
||||||
@@ -749,16 +749,16 @@ export const getReferenceSnapPointsForLinearElementPoint = (
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const addNearestPointSnaps = (
|
const collectNearestAxisSnapCandidates = (
|
||||||
referenceSnapPoints: readonly GlobalPoint[],
|
axisSnapTargets: readonly GlobalPoint[],
|
||||||
pointerPosition: GlobalPoint,
|
pointerPosition: GlobalPoint,
|
||||||
nearestSnapsX: Snaps,
|
nearestSnapsX: Snaps,
|
||||||
nearestSnapsY: Snaps,
|
nearestSnapsY: Snaps,
|
||||||
minOffset: Vector2D,
|
minOffset: Vector2D,
|
||||||
) => {
|
) => {
|
||||||
for (const referencePoint of referenceSnapPoints) {
|
for (const snapTarget of axisSnapTargets) {
|
||||||
const offsetX = referencePoint[0] - pointerPosition[0];
|
const offsetX = snapTarget[0] - pointerPosition[0];
|
||||||
const offsetY = referencePoint[1] - pointerPosition[1];
|
const offsetY = snapTarget[1] - pointerPosition[1];
|
||||||
const absOffsetX = Math.abs(offsetX);
|
const absOffsetX = Math.abs(offsetX);
|
||||||
const absOffsetY = Math.abs(offsetY);
|
const absOffsetY = Math.abs(offsetY);
|
||||||
|
|
||||||
@@ -773,7 +773,7 @@ const addNearestPointSnaps = (
|
|||||||
|
|
||||||
nearestSnapsX.push({
|
nearestSnapsX.push({
|
||||||
type: "point",
|
type: "point",
|
||||||
points: [pointerPosition, referencePoint],
|
points: [pointerPosition, snapTarget],
|
||||||
offset: offsetX,
|
offset: offsetX,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -787,7 +787,7 @@ const addNearestPointSnaps = (
|
|||||||
|
|
||||||
nearestSnapsY.push({
|
nearestSnapsY.push({
|
||||||
type: "point",
|
type: "point",
|
||||||
points: [pointerPosition, referencePoint],
|
points: [pointerPosition, snapTarget],
|
||||||
offset: offsetY,
|
offset: offsetY,
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -827,8 +827,8 @@ export const snapLinearElementPoint = (
|
|||||||
const nearestSnapsX: Snaps = [];
|
const nearestSnapsX: Snaps = [];
|
||||||
const nearestSnapsY: Snaps = [];
|
const nearestSnapsY: Snaps = [];
|
||||||
|
|
||||||
addNearestPointSnaps(
|
collectNearestAxisSnapCandidates(
|
||||||
getExternalReferenceSnapPointsForLinearElementPoint(
|
getExternalAxisSnapTargets(
|
||||||
elements,
|
elements,
|
||||||
editingElement,
|
editingElement,
|
||||||
app.state,
|
app.state,
|
||||||
@@ -841,8 +841,8 @@ export const snapLinearElementPoint = (
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (options.includeSelfPoints) {
|
if (options.includeSelfPoints) {
|
||||||
addNearestPointSnaps(
|
collectNearestAxisSnapCandidates(
|
||||||
getSelfReferenceSnapPointsForLinearElementPoint(
|
getOwnAxisSnapTargets(
|
||||||
editingElement,
|
editingElement,
|
||||||
elementsMap,
|
elementsMap,
|
||||||
options.selectedPointsIndices,
|
options.selectedPointsIndices,
|
||||||
@@ -870,9 +870,7 @@ export const snapLinearElementPoint = (
|
|||||||
|
|
||||||
const snappedSnapsX = nearestSnapsX
|
const snappedSnapsX = nearestSnapsX
|
||||||
.filter(
|
.filter(
|
||||||
(
|
(snap): snap is PointSnap =>
|
||||||
snap,
|
|
||||||
): snap is PointSnap =>
|
|
||||||
snap.type === "point" && isCloseTo(snap.offset, snapOffset.x, 0.01),
|
snap.type === "point" && isCloseTo(snap.offset, snapOffset.x, 0.01),
|
||||||
)
|
)
|
||||||
.map((snap) => ({
|
.map((snap) => ({
|
||||||
@@ -883,9 +881,7 @@ export const snapLinearElementPoint = (
|
|||||||
|
|
||||||
const snappedSnapsY = nearestSnapsY
|
const snappedSnapsY = nearestSnapsY
|
||||||
.filter(
|
.filter(
|
||||||
(
|
(snap): snap is PointSnap =>
|
||||||
snap,
|
|
||||||
): snap is PointSnap =>
|
|
||||||
snap.type === "point" && isCloseTo(snap.offset, snapOffset.y, 0.01),
|
snap.type === "point" && isCloseTo(snap.offset, snapOffset.y, 0.01),
|
||||||
)
|
)
|
||||||
.map((snap) => ({
|
.map((snap) => ({
|
||||||
|
|||||||
Reference in New Issue
Block a user