mirror of
https://github.com/excalidraw/excalidraw.git
synced 2025-05-03 10:00:07 -04:00
fix: remove scene from getElementAbsoluteCoords and dependent functions and use elementsMap (#7663)
* fix: remove scene from getElementAbsoluteCoords and dependent functions and use elementsMap * lint * fix * use non deleted elements where possible * use non deleted elements map in actions * pass elementsMap instead of array to elementOverlapsWithFrame * lint * fix * pass elementsMap to getElementsCorners * pass elementsMap to getEligibleElementsForBinding * pass elementsMap in bindOrUnbindSelectedElements and unbindLinearElements * pass elementsMap in elementsAreInFrameBounds,elementOverlapsWithFrame,isCursorInFrame,getElementsInResizingFrame * pass elementsMap in getElementsWithinSelection, getElementsCompletelyInFrame, isElementContainingFrame, getElementsInNewFrame * pass elementsMap to getElementWithTransformHandleType * pass elementsMap to getVisibleGaps, getMaximumGroups,getReferenceSnapPoints,snapDraggedElements * lint * pass elementsMap to bindTextToShapeAfterDuplication,bindLinearElementToElement,getTextBindableContainerAtPosition * revert changes for bindTextToShapeAfterDuplication
This commit is contained in:
parent
73bf50e8a8
commit
47f87f4ecb
36 changed files with 779 additions and 270 deletions
|
@ -8,15 +8,18 @@ import {
|
|||
import { MaybeTransformHandleType } from "./element/transformHandles";
|
||||
import { isBoundToContainer, isFrameLikeElement } from "./element/typeChecks";
|
||||
import {
|
||||
ElementsMap,
|
||||
ExcalidrawElement,
|
||||
NonDeletedExcalidrawElement,
|
||||
} from "./element/types";
|
||||
import { getMaximumGroups } from "./groups";
|
||||
import { KEYS } from "./keys";
|
||||
import { rangeIntersection, rangesOverlap, rotatePoint } from "./math";
|
||||
import { getVisibleAndNonSelectedElements } from "./scene/selection";
|
||||
import {
|
||||
getSelectedElements,
|
||||
getVisibleAndNonSelectedElements,
|
||||
} from "./scene/selection";
|
||||
import { AppState, KeyboardModifiersObject, Point } from "./types";
|
||||
import { arrayToMap } from "./utils";
|
||||
|
||||
const SNAP_DISTANCE = 8;
|
||||
|
||||
|
@ -167,6 +170,7 @@ export const areRoughlyEqual = (a: number, b: number, precision = 0.01) => {
|
|||
|
||||
export const getElementsCorners = (
|
||||
elements: ExcalidrawElement[],
|
||||
elementsMap: ElementsMap,
|
||||
{
|
||||
omitCenter,
|
||||
boundingBoxCorners,
|
||||
|
@ -185,7 +189,10 @@ export const getElementsCorners = (
|
|||
if (elements.length === 1) {
|
||||
const element = elements[0];
|
||||
|
||||
let [x1, y1, x2, y2, cx, cy] = getElementAbsoluteCoords(element);
|
||||
let [x1, y1, x2, y2, cx, cy] = getElementAbsoluteCoords(
|
||||
element,
|
||||
elementsMap,
|
||||
);
|
||||
|
||||
if (dragOffset) {
|
||||
x1 += dragOffset.x;
|
||||
|
@ -280,6 +287,7 @@ export const getVisibleGaps = (
|
|||
elements: readonly NonDeletedExcalidrawElement[],
|
||||
selectedElements: ExcalidrawElement[],
|
||||
appState: AppState,
|
||||
elementsMap: ElementsMap,
|
||||
) => {
|
||||
const referenceElements: ExcalidrawElement[] = getReferenceElements(
|
||||
elements,
|
||||
|
@ -287,10 +295,7 @@ export const getVisibleGaps = (
|
|||
appState,
|
||||
);
|
||||
|
||||
const referenceBounds = getMaximumGroups(
|
||||
referenceElements,
|
||||
arrayToMap(elements),
|
||||
)
|
||||
const referenceBounds = getMaximumGroups(referenceElements, elementsMap)
|
||||
.filter(
|
||||
(elementsGroup) =>
|
||||
!(elementsGroup.length === 1 && isBoundToContainer(elementsGroup[0])),
|
||||
|
@ -569,19 +574,19 @@ export const getReferenceSnapPoints = (
|
|||
elements: readonly NonDeletedExcalidrawElement[],
|
||||
selectedElements: ExcalidrawElement[],
|
||||
appState: AppState,
|
||||
elementsMap: ElementsMap,
|
||||
) => {
|
||||
const referenceElements = getReferenceElements(
|
||||
elements,
|
||||
selectedElements,
|
||||
appState,
|
||||
);
|
||||
|
||||
return getMaximumGroups(referenceElements, arrayToMap(elements))
|
||||
return getMaximumGroups(referenceElements, elementsMap)
|
||||
.filter(
|
||||
(elementsGroup) =>
|
||||
!(elementsGroup.length === 1 && isBoundToContainer(elementsGroup[0])),
|
||||
)
|
||||
.flatMap((elementGroup) => getElementsCorners(elementGroup));
|
||||
.flatMap((elementGroup) => getElementsCorners(elementGroup, elementsMap));
|
||||
};
|
||||
|
||||
const getPointSnaps = (
|
||||
|
@ -641,11 +646,13 @@ const getPointSnaps = (
|
|||
};
|
||||
|
||||
export const snapDraggedElements = (
|
||||
selectedElements: ExcalidrawElement[],
|
||||
elements: ExcalidrawElement[],
|
||||
dragOffset: Vector2D,
|
||||
appState: AppState,
|
||||
event: KeyboardModifiersObject,
|
||||
elementsMap: ElementsMap,
|
||||
) => {
|
||||
const selectedElements = getSelectedElements(elements, appState);
|
||||
if (
|
||||
!isSnappingEnabled({ appState, event, selectedElements }) ||
|
||||
selectedElements.length === 0
|
||||
|
@ -658,7 +665,6 @@ export const snapDraggedElements = (
|
|||
snapLines: [],
|
||||
};
|
||||
}
|
||||
|
||||
dragOffset.x = round(dragOffset.x);
|
||||
dragOffset.y = round(dragOffset.y);
|
||||
const nearestSnapsX: Snaps = [];
|
||||
|
@ -669,7 +675,7 @@ export const snapDraggedElements = (
|
|||
y: snapDistance,
|
||||
};
|
||||
|
||||
const selectionPoints = getElementsCorners(selectedElements, {
|
||||
const selectionPoints = getElementsCorners(selectedElements, elementsMap, {
|
||||
dragOffset,
|
||||
});
|
||||
|
||||
|
@ -719,7 +725,7 @@ export const snapDraggedElements = (
|
|||
|
||||
getPointSnaps(
|
||||
selectedElements,
|
||||
getElementsCorners(selectedElements, {
|
||||
getElementsCorners(selectedElements, elementsMap, {
|
||||
dragOffset: newDragOffset,
|
||||
}),
|
||||
appState,
|
||||
|
@ -1204,6 +1210,7 @@ export const snapNewElement = (
|
|||
event: KeyboardModifiersObject,
|
||||
origin: Vector2D,
|
||||
dragOffset: Vector2D,
|
||||
elementsMap: ElementsMap,
|
||||
) => {
|
||||
if (
|
||||
!isSnappingEnabled({ event, selectedElements: [draggingElement], appState })
|
||||
|
@ -1248,7 +1255,7 @@ export const snapNewElement = (
|
|||
nearestSnapsX.length = 0;
|
||||
nearestSnapsY.length = 0;
|
||||
|
||||
const corners = getElementsCorners([draggingElement], {
|
||||
const corners = getElementsCorners([draggingElement], elementsMap, {
|
||||
boundingBoxCorners: true,
|
||||
omitCenter: true,
|
||||
});
|
||||
|
@ -1276,6 +1283,7 @@ export const getSnapLinesAtPointer = (
|
|||
appState: AppState,
|
||||
pointer: Vector2D,
|
||||
event: KeyboardModifiersObject,
|
||||
elementsMap: ElementsMap,
|
||||
) => {
|
||||
if (!isSnappingEnabled({ event, selectedElements: [], appState })) {
|
||||
return {
|
||||
|
@ -1301,7 +1309,7 @@ export const getSnapLinesAtPointer = (
|
|||
const verticalSnapLines: PointerSnapLine[] = [];
|
||||
|
||||
for (const referenceElement of referenceElements) {
|
||||
const corners = getElementsCorners([referenceElement]);
|
||||
const corners = getElementsCorners([referenceElement], elementsMap);
|
||||
|
||||
for (const corner of corners) {
|
||||
const offsetX = corner[0] - pointer.x;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue