change appState.suggestedBindings -> suggestedBinding & remove unused code

This commit is contained in:
dwelle
2025-08-27 10:00:01 +02:00
parent b9a3c6246a
commit 03a6a327a8
17 changed files with 615 additions and 1962 deletions

View File

@@ -3,7 +3,6 @@ import {
arrayToMap,
invariant,
isAlwaysInsideBinding,
tupleToCoors,
} from "@excalidraw/common";
import {
@@ -33,7 +32,6 @@ import {
getElementBounds,
} from "./bounds";
import {
bindingBorderTest,
getHoveredElementForBinding,
hitElementItself,
intersectElementWithLineSegment,
@@ -82,16 +80,6 @@ import type {
BindMode,
} from "./types";
export type SuggestedBinding =
| NonDeleted<ExcalidrawBindableElement>
| SuggestedPointBinding;
export type SuggestedPointBinding = [
NonDeleted<ExcalidrawArrowElement>,
"start" | "end" | "both",
NonDeleted<ExcalidrawBindableElement>,
];
export type BindingStrategy =
// Create a new binding with this mode
| {
@@ -197,39 +185,6 @@ const bindOrUnbindBindingElementEdge = (
}
};
const getOriginalBindingsIfStillCloseToBindingEnds = (
linearElement: NonDeleted<ExcalidrawArrowElement>,
elementsMap: NonDeletedSceneElementsMap,
): (NonDeleted<ExcalidrawElement> | null)[] =>
(["start", "end"] as const).map((edge) => {
const coors = tupleToCoors(
LinearElementEditor.getPointAtIndexGlobalCoordinates(
linearElement,
edge === "start" ? 0 : -1,
elementsMap,
),
);
const elementId =
edge === "start"
? linearElement.startBinding?.elementId
: linearElement.endBinding?.elementId;
if (elementId) {
const element = elementsMap.get(elementId);
if (
isBindableElement(element) &&
bindingBorderTest(
element,
pointFrom<GlobalPoint>(coors.x, coors.y),
elementsMap,
)
) {
return element;
}
}
return null;
});
export const getStartGlobalEndLocalPointsForSimpleArrowBinding = (
arrow: NonDeleted<ExcalidrawArrowElement>,
start: BindingStrategy,
@@ -665,43 +620,12 @@ export const bindOrUnbindBindingElements = (
});
};
export const getSuggestedBindingsForBindingElements = (
selectedElements: NonDeleted<ExcalidrawElement>[],
elementsMap: NonDeletedSceneElementsMap,
): SuggestedBinding[] => {
// HOT PATH: Bail out if selected elements list is too large
if (selectedElements.length > 50) {
return [];
}
return (
selectedElements
.filter(isArrowElement)
.flatMap((element) =>
getOriginalBindingsIfStillCloseToBindingEnds(element, elementsMap),
)
.filter(
(element): element is NonDeleted<ExcalidrawBindableElement> =>
element !== null,
)
// Filter out bind candidates which are in the
// same selection / group with the arrow
//
// TODO: Is it worth turning the list into a set to avoid dupes?
.filter(
(element) =>
selectedElements.filter((selected) => selected.id === element?.id)
.length === 0,
)
);
};
export const maybeSuggestBindingsForBindingElementAtCoords = (
linearElement: NonDeleted<ExcalidrawArrowElement>,
startOrEndOrBoth: "start" | "end" | "both",
scene: Scene,
pointerCoords: GlobalPoint,
): ExcalidrawBindableElement[] => {
): AppState["suggestedBinding"] => {
const startCoords =
startOrEndOrBoth === "start"
? pointerCoords
@@ -729,7 +653,7 @@ export const maybeSuggestBindingsForBindingElementAtCoords = (
scene.getNonDeletedElementsMap(),
);
const suggestedBindings = [];
let suggestedBinding: AppState["suggestedBinding"] = null;
if (startHovered != null && startHovered.id === endHovered?.id) {
const hitStart = hitElementItself({
@@ -747,15 +671,15 @@ export const maybeSuggestBindingsForBindingElementAtCoords = (
overrideShouldTestInside: true,
});
if (hitStart && hitEnd) {
suggestedBindings.push(startHovered);
suggestedBinding = startHovered;
}
} else if (startOrEndOrBoth === "start" && startHovered != null) {
suggestedBindings.push(startHovered);
suggestedBinding = startHovered;
} else if (startOrEndOrBoth === "end" && endHovered != null) {
suggestedBindings.push(endHovered);
suggestedBinding = endHovered;
}
return suggestedBindings;
return suggestedBinding;
};
export const bindBindingElement = (

View File

@@ -421,7 +421,7 @@ export class LinearElementEditor {
}
// suggest bindings for first and last point if selected
let suggestedBindings: ExcalidrawBindableElement[] = [];
let suggestedBinding: AppState["suggestedBinding"] = null;
if (isBindingElement(element, false)) {
const firstIndexIsSelected = selectedPointsIndices[0] === 0;
const lastIndexIsSelected =
@@ -458,7 +458,7 @@ export class LinearElementEditor {
}
if (coords.length) {
suggestedBindings = maybeSuggestBindingsForBindingElementAtCoords(
suggestedBinding = maybeSuggestBindingsForBindingElementAtCoords(
element,
firstIndexIsSelected && lastIndexIsSelected
? "both"
@@ -494,7 +494,7 @@ export class LinearElementEditor {
return {
selectedLinearElement: newLinearElementEditor,
suggestedBindings,
suggestedBinding,
} as Pick<AppState, keyof AppState>;
}

View File

@@ -44,14 +44,3 @@ exports[`Test Linear Elements > Test bound text element > should resize and posi
"Online whiteboard
collaboration made easy"
`;
exports[`Test Linear Elements > Test bound text element > should wrap the bound text when arrow bound container moves 1`] = `
"Online whiteboard
collaboration made easy"
`;
exports[`Test Linear Elements > Test bound text element > should wrap the bound text when arrow bound container moves 2`] = `
"Online whiteboard
collaboration made
easy"
`;