diff --git a/packages/excalidraw/components/App.tsx b/packages/excalidraw/components/App.tsx index abeb27124..e185b91a3 100644 --- a/packages/excalidraw/components/App.tsx +++ b/packages/excalidraw/components/App.tsx @@ -8148,6 +8148,7 @@ class App extends React.Component { ...this.state.selectedLinearElement, pointerDownState: ret.pointerDownState, selectedPointsIndices: ret.selectedPointsIndices, + segmentMidPointHoveredCoords: null, }, }); } @@ -8157,6 +8158,7 @@ class App extends React.Component { ...this.state.editingLinearElement, pointerDownState: ret.pointerDownState, selectedPointsIndices: ret.selectedPointsIndices, + segmentMidPointHoveredCoords: null, }, }); } @@ -8170,7 +8172,7 @@ class App extends React.Component { return; } - const didDrag = LinearElementEditor.handlePointDragging( + const newLinearElementEditor = LinearElementEditor.handlePointDragging( event, this, pointerCoords.x, @@ -8184,29 +8186,18 @@ class App extends React.Component { linearElementEditor, this.scene, ); - if (didDrag) { + if (newLinearElementEditor) { pointerDownState.lastCoords.x = pointerCoords.x; pointerDownState.lastCoords.y = pointerCoords.y; pointerDownState.drag.hasOccurred = true; - if ( - this.state.editingLinearElement && - !this.state.editingLinearElement.isDragging - ) { - this.setState({ - editingLinearElement: { - ...this.state.editingLinearElement, - isDragging: true, - }, - }); - } - if (!this.state.selectedLinearElement.isDragging) { - this.setState({ - selectedLinearElement: { - ...this.state.selectedLinearElement, - isDragging: true, - }, - }); - } + + this.setState({ + editingLinearElement: this.state.editingLinearElement + ? newLinearElementEditor + : null, + selectedLinearElement: newLinearElementEditor, + }); + return; } } diff --git a/packages/excalidraw/element/linearElementEditor.ts b/packages/excalidraw/element/linearElementEditor.ts index f9b23f048..6f0af6d73 100644 --- a/packages/excalidraw/element/linearElementEditor.ts +++ b/packages/excalidraw/element/linearElementEditor.ts @@ -232,15 +232,15 @@ export class LinearElementEditor { ) => void, linearElementEditor: LinearElementEditor, scene: Scene, - ): boolean { + ): LinearElementEditor | null { if (!linearElementEditor) { - return false; + return null; } const { elementId } = linearElementEditor; const elementsMap = scene.getNonDeletedElementsMap(); const element = LinearElementEditor.getElement(elementId, elementsMap); if (!element) { - return false; + return null; } if ( @@ -248,24 +248,18 @@ export class LinearElementEditor { !linearElementEditor.pointerDownState.lastClickedIsEndPoint && linearElementEditor.pointerDownState.lastClickedPoint !== 0 ) { - return false; + return null; } const selectedPointsIndices = isElbowArrow(element) - ? linearElementEditor.selectedPointsIndices - ?.reduce( - (startEnd, index) => - (index === 0 - ? [0, startEnd[1]] - : [startEnd[0], element.points.length - 1]) as [ - boolean | number, - boolean | number, - ], - [false, false] as [number | boolean, number | boolean], - ) - .filter( - (idx: number | boolean): idx is number => typeof idx === "number", - ) + ? [ + !!linearElementEditor.selectedPointsIndices?.includes(0) + ? 0 + : undefined, + !!linearElementEditor.selectedPointsIndices?.find((idx) => idx > 0) + ? element.points.length - 1 + : undefined, + ].filter((idx): idx is number => idx !== undefined) : linearElementEditor.selectedPointsIndices; const lastClickedPoint = isElbowArrow(element) ? linearElementEditor.pointerDownState.lastClickedPoint > 0 @@ -274,9 +268,7 @@ export class LinearElementEditor { : linearElementEditor.pointerDownState.lastClickedPoint; // point that's being dragged (out of all selected points) - const draggingPoint = element.points[lastClickedPoint] as - | [number, number] - | undefined; + const draggingPoint = element.points[lastClickedPoint]; if (selectedPointsIndices && draggingPoint) { if ( @@ -384,10 +376,23 @@ export class LinearElementEditor { } } - return true; + return { + ...linearElementEditor, + selectedPointsIndices, + segmentMidPointHoveredCoords: + lastClickedPoint !== 0 && + lastClickedPoint !== element.points.length - 1 + ? this.getPointGlobalCoordinates( + element, + draggingPoint, + elementsMap, + ) + : null, + isDragging: true, + }; } - return false; + return null; } static handlePointerUp( diff --git a/packages/excalidraw/renderer/interactiveScene.ts b/packages/excalidraw/renderer/interactiveScene.ts index f988dcb39..887ad424a 100644 --- a/packages/excalidraw/renderer/interactiveScene.ts +++ b/packages/excalidraw/renderer/interactiveScene.ts @@ -907,16 +907,14 @@ const _renderInteractiveScene = ({ (el) => el.id === editor.elementId, // Don't forget bound text elements! ); - if (isElbowArrow(firstSelectedLinear)) { - if (editor.segmentMidPointHoveredCoords) { - renderElbowArrowMidPointHighlight(context, appState); - } else if ( - editor.hoverPointIndex !== 0 && - editor.hoverPointIndex !== firstSelectedLinear.points.length - 1 - ) { - renderLinearElementPointHighlight(context, appState, elementsMap); - } - } else if (editor.hoverPointIndex >= 0) { + if (editor.segmentMidPointHoveredCoords) { + renderElbowArrowMidPointHighlight(context, appState); + } else if ( + isElbowArrow(firstSelectedLinear) + ? editor.hoverPointIndex === 0 || + editor.hoverPointIndex === firstSelectedLinear.points.length - 1 + : editor.hoverPointIndex >= 0 + ) { renderLinearElementPointHighlight(context, appState, elementsMap); } }