Compare commits
10 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a7a3f9d82b | |||
| 895c2b23c7 | |||
| 50099012c6 | |||
| de2ad7cd3f | |||
| d7abb6a309 | |||
| 9fd91d9a59 | |||
| ba087233cb | |||
| 7c58d1f6f4 | |||
| d9ab298526 | |||
| 7b2496bfd7 |
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
pointFrom,
|
||||
pointFromPair,
|
||||
type GlobalPoint,
|
||||
type LocalPoint,
|
||||
@@ -69,12 +70,12 @@ export const getGridPoint = (
|
||||
x: number,
|
||||
y: number,
|
||||
gridSize: NullableGridSize,
|
||||
): [number, number] => {
|
||||
): GlobalPoint => {
|
||||
if (gridSize) {
|
||||
return [
|
||||
return pointFrom<GlobalPoint>(
|
||||
Math.round(x / gridSize) * gridSize,
|
||||
Math.round(y / gridSize) * gridSize,
|
||||
];
|
||||
);
|
||||
}
|
||||
return [x, y];
|
||||
return pointFrom<GlobalPoint>(x, y);
|
||||
};
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import {
|
||||
arrayToMap,
|
||||
getFeatureFlag,
|
||||
getGridPoint,
|
||||
invariant,
|
||||
isTransparent,
|
||||
} from "@excalidraw/common";
|
||||
@@ -22,7 +23,7 @@ import {
|
||||
} from "@excalidraw/math";
|
||||
|
||||
import type { LineSegment, LocalPoint, Radians } from "@excalidraw/math";
|
||||
import type { AppState } from "@excalidraw/excalidraw/types";
|
||||
import type { AppState, NullableGridSize } from "@excalidraw/excalidraw/types";
|
||||
import type { MapEntry, Mutable } from "@excalidraw/common/utility-types";
|
||||
import type { Bounds } from "@excalidraw/common";
|
||||
|
||||
@@ -154,6 +155,7 @@ export const bindOrUnbindBindingElement = (
|
||||
altKey?: boolean;
|
||||
angleLocked?: boolean;
|
||||
initialBinding?: boolean;
|
||||
gridSize?: NullableGridSize;
|
||||
},
|
||||
) => {
|
||||
const { start, end } = getBindingStrategyForDraggingBindingElementEndpoints(
|
||||
@@ -170,12 +172,16 @@ export const bindOrUnbindBindingElement = (
|
||||
},
|
||||
);
|
||||
|
||||
const isMidpointSnappingEnabled =
|
||||
appState.isMidpointSnappingEnabled && !appState.gridModeEnabled;
|
||||
|
||||
bindOrUnbindBindingElementEdge(
|
||||
arrow,
|
||||
start,
|
||||
"start",
|
||||
scene,
|
||||
appState.isBindingEnabled,
|
||||
isMidpointSnappingEnabled,
|
||||
);
|
||||
bindOrUnbindBindingElementEdge(
|
||||
arrow,
|
||||
@@ -183,6 +189,7 @@ export const bindOrUnbindBindingElement = (
|
||||
"end",
|
||||
scene,
|
||||
appState.isBindingEnabled,
|
||||
isMidpointSnappingEnabled,
|
||||
);
|
||||
if (start.focusPoint || end.focusPoint) {
|
||||
// If the strategy dictates a focus point override, then
|
||||
@@ -227,6 +234,7 @@ const bindOrUnbindBindingElementEdge = (
|
||||
startOrEnd: "start" | "end",
|
||||
scene: Scene,
|
||||
shouldSnapToOutline = true,
|
||||
isMidpointSnappingEnabled = true,
|
||||
): void => {
|
||||
if (mode === null) {
|
||||
// null means break the binding
|
||||
@@ -240,6 +248,7 @@ const bindOrUnbindBindingElementEdge = (
|
||||
scene,
|
||||
focusPoint,
|
||||
shouldSnapToOutline,
|
||||
isMidpointSnappingEnabled,
|
||||
);
|
||||
}
|
||||
};
|
||||
@@ -593,6 +602,7 @@ export const getBindingStrategyForDraggingBindingElementEndpoints = (
|
||||
finalize?: boolean;
|
||||
initialBinding?: boolean;
|
||||
zoom?: AppState["zoom"];
|
||||
gridSize?: NullableGridSize;
|
||||
},
|
||||
): { start: BindingStrategy; end: BindingStrategy } => {
|
||||
if (getFeatureFlag("COMPLEX_BINDINGS")) {
|
||||
@@ -633,6 +643,7 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = (
|
||||
finalize?: boolean;
|
||||
initialBinding?: boolean;
|
||||
zoom?: AppState["zoom"];
|
||||
gridSize?: NullableGridSize;
|
||||
},
|
||||
): { start: BindingStrategy; end: BindingStrategy } => {
|
||||
const startIdx = 0;
|
||||
@@ -695,7 +706,9 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = (
|
||||
elementsMap,
|
||||
);
|
||||
const hit = getHoveredElementForBinding(
|
||||
globalPoint,
|
||||
opts?.angleLocked || appState.gridModeEnabled
|
||||
? pointFrom<GlobalPoint>(scenePointerX, scenePointerY)
|
||||
: globalPoint,
|
||||
elements,
|
||||
elementsMap,
|
||||
maxBindingDistance_simple(appState.zoom),
|
||||
@@ -747,7 +760,11 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = (
|
||||
? globalPoint
|
||||
: // NOTE: Can only affect the start point because new arrows always drag the end point
|
||||
opts?.newArrow
|
||||
? appState.selectedLinearElement!.initialState.origin!
|
||||
? getGridPoint(
|
||||
appState.selectedLinearElement!.initialState.origin![0],
|
||||
appState.selectedLinearElement!.initialState.origin![1],
|
||||
opts.gridSize as NullableGridSize,
|
||||
)
|
||||
: LinearElementEditor.getPointAtIndexGlobalCoordinates(
|
||||
arrow,
|
||||
0,
|
||||
@@ -806,12 +823,27 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = (
|
||||
focusPoint:
|
||||
projectFixedPointOntoDiagonal(
|
||||
arrow,
|
||||
globalPoint,
|
||||
opts?.angleLocked || appState.gridModeEnabled
|
||||
? snapBoundPointToGrid(
|
||||
pointFrom<GlobalPoint>(scenePointerX, scenePointerY),
|
||||
hit,
|
||||
elementsMap,
|
||||
appState.gridSize as NullableGridSize,
|
||||
arrow,
|
||||
LinearElementEditor.getPointAtIndexGlobalCoordinates(
|
||||
arrow,
|
||||
startDragged ? 1 : -2,
|
||||
elementsMap,
|
||||
),
|
||||
)
|
||||
: globalPoint,
|
||||
hit,
|
||||
startDragged ? "start" : "end",
|
||||
elementsMap,
|
||||
appState.zoom,
|
||||
appState.isMidpointSnappingEnabled,
|
||||
appState.isMidpointSnappingEnabled &&
|
||||
!opts?.angleLocked &&
|
||||
!appState.gridModeEnabled,
|
||||
) || globalPoint,
|
||||
}
|
||||
: { mode: null };
|
||||
@@ -856,7 +888,7 @@ const getBindingStrategyForDraggingBindingElementEndpoints_simple = (
|
||||
startDragged ? "end" : "start",
|
||||
elementsMap,
|
||||
appState.zoom,
|
||||
appState.isMidpointSnappingEnabled,
|
||||
false,
|
||||
) || otherEndpoint,
|
||||
}
|
||||
: { mode: undefined }
|
||||
@@ -1021,6 +1053,7 @@ export const bindBindingElement = (
|
||||
scene: Scene,
|
||||
focusPoint?: GlobalPoint,
|
||||
shouldSnapToOutline = true,
|
||||
isMidpointSnappingEnabled = true,
|
||||
): void => {
|
||||
const elementsMap = scene.getNonDeletedElementsMap();
|
||||
|
||||
@@ -1036,6 +1069,7 @@ export const bindBindingElement = (
|
||||
startOrEnd,
|
||||
elementsMap,
|
||||
shouldSnapToOutline,
|
||||
isMidpointSnappingEnabled,
|
||||
),
|
||||
};
|
||||
} else {
|
||||
@@ -1740,6 +1774,92 @@ const extractBinding = (
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Snaps a bound arrow endpoint to the grid on the axis parallel to the
|
||||
* bindable element's side, while preserving the binding gap distance on the
|
||||
* perpendicular axis. In other words, the grid axis closest to the side's
|
||||
* perpendicular (normal) is used as the snap axis and the other axis is kept at
|
||||
* the binding gap distance.
|
||||
*/
|
||||
const snapBoundPointToGrid = (
|
||||
outlinePoint: GlobalPoint,
|
||||
bindableElement: ExcalidrawBindableElement,
|
||||
elementsMap: ElementsMap,
|
||||
gridSize: NullableGridSize,
|
||||
arrowElement: ExcalidrawArrowElement,
|
||||
adjacentPoint?: GlobalPoint,
|
||||
): GlobalPoint => {
|
||||
if (!gridSize) {
|
||||
return outlinePoint;
|
||||
}
|
||||
|
||||
const aabb = aabbForElement(bindableElement, elementsMap);
|
||||
// For ellipses and diamonds use the arrow's incoming direction instead of
|
||||
// the position-based heading, which can give the wrong axis when the
|
||||
// outline point is near a cardinal zone or an angled diamond face.
|
||||
const heading =
|
||||
adjacentPoint &&
|
||||
(bindableElement.type === "ellipse" || bindableElement.type === "diamond")
|
||||
? vectorToHeading(vectorFromPoint(adjacentPoint, outlinePoint))
|
||||
: headingForPointFromElement(bindableElement, aabb, outlinePoint);
|
||||
|
||||
const normalLocal = pointFrom<GlobalPoint>(heading[0], heading[1]);
|
||||
const normalGlobal = pointRotateRads(
|
||||
normalLocal,
|
||||
pointFrom<GlobalPoint>(0, 0),
|
||||
bindableElement.angle,
|
||||
);
|
||||
|
||||
const bindingGap = getBindingGap(bindableElement, arrowElement);
|
||||
const extent =
|
||||
Math.max(bindableElement.width, bindableElement.height) + bindingGap * 2;
|
||||
const center = getCenterForBounds(aabb);
|
||||
|
||||
const absNX = Math.abs(normalGlobal[0]);
|
||||
const absNY = Math.abs(normalGlobal[1]);
|
||||
if (absNX >= absNY) {
|
||||
// Global X is closest to the perpendicular so snap Y, intersect horizontal line
|
||||
const [, snappedY] = getGridPoint(
|
||||
outlinePoint[0],
|
||||
outlinePoint[1],
|
||||
gridSize,
|
||||
);
|
||||
const intersector = lineSegment<GlobalPoint>(
|
||||
pointFrom<GlobalPoint>(center[0] - extent, snappedY),
|
||||
pointFrom<GlobalPoint>(center[0] + extent, snappedY),
|
||||
);
|
||||
const intersection = intersectElementWithLineSegment(
|
||||
bindableElement,
|
||||
elementsMap,
|
||||
intersector,
|
||||
bindingGap,
|
||||
).sort(
|
||||
(a, b) =>
|
||||
pointDistanceSq(a, outlinePoint) - pointDistanceSq(b, outlinePoint),
|
||||
)[0];
|
||||
|
||||
return intersection ?? pointFrom<GlobalPoint>(outlinePoint[0], snappedY);
|
||||
}
|
||||
|
||||
// Global Y is closest to the perpendicular so snap X, intersect vertical line
|
||||
const [snappedX] = getGridPoint(outlinePoint[0], outlinePoint[1], gridSize);
|
||||
const intersector = lineSegment<GlobalPoint>(
|
||||
pointFrom<GlobalPoint>(snappedX, center[1] - extent),
|
||||
pointFrom<GlobalPoint>(snappedX, center[1] + extent),
|
||||
);
|
||||
const intersection = intersectElementWithLineSegment(
|
||||
bindableElement,
|
||||
elementsMap,
|
||||
intersector,
|
||||
bindingGap,
|
||||
).sort(
|
||||
(a, b) =>
|
||||
pointDistanceSq(a, outlinePoint) - pointDistanceSq(b, outlinePoint),
|
||||
)[0];
|
||||
|
||||
return intersection ?? pointFrom<GlobalPoint>(snappedX, outlinePoint[1]);
|
||||
};
|
||||
|
||||
const elementArea = (element: ExcalidrawBindableElement) =>
|
||||
element.width * element.height;
|
||||
|
||||
|
||||
@@ -359,6 +359,7 @@ export class LinearElementEditor {
|
||||
linearElementEditor,
|
||||
);
|
||||
|
||||
const angleLocked = shouldRotateWithDiscreteAngle(event);
|
||||
LinearElementEditor.movePoints(
|
||||
element,
|
||||
app.scene,
|
||||
@@ -370,7 +371,10 @@ export class LinearElementEditor {
|
||||
},
|
||||
{
|
||||
isBindingEnabled: app.state.isBindingEnabled,
|
||||
isMidpointSnappingEnabled: app.state.isMidpointSnappingEnabled,
|
||||
isMidpointSnappingEnabled:
|
||||
app.state.isMidpointSnappingEnabled &&
|
||||
!angleLocked &&
|
||||
!app.state.gridModeEnabled,
|
||||
},
|
||||
);
|
||||
// Set the suggested binding from the updates if available
|
||||
@@ -427,7 +431,9 @@ export class LinearElementEditor {
|
||||
"start",
|
||||
elementsMap,
|
||||
app.state.zoom,
|
||||
app.state.isMidpointSnappingEnabled,
|
||||
app.state.isMidpointSnappingEnabled &&
|
||||
!angleLocked &&
|
||||
!app.state.gridModeEnabled,
|
||||
)
|
||||
: linearElementEditor.initialState.altFocusPoint,
|
||||
},
|
||||
@@ -554,6 +560,8 @@ export class LinearElementEditor {
|
||||
linearElementEditor,
|
||||
);
|
||||
|
||||
const angleLocked =
|
||||
shouldRotateWithDiscreteAngle(event) && singlePointDragged;
|
||||
LinearElementEditor.movePoints(
|
||||
element,
|
||||
app.scene,
|
||||
@@ -565,7 +573,10 @@ export class LinearElementEditor {
|
||||
},
|
||||
{
|
||||
isBindingEnabled: app.state.isBindingEnabled,
|
||||
isMidpointSnappingEnabled: app.state.isMidpointSnappingEnabled,
|
||||
isMidpointSnappingEnabled:
|
||||
app.state.isMidpointSnappingEnabled &&
|
||||
!angleLocked &&
|
||||
!app.state.gridModeEnabled,
|
||||
},
|
||||
);
|
||||
|
||||
@@ -661,7 +672,9 @@ export class LinearElementEditor {
|
||||
"start",
|
||||
elementsMap,
|
||||
app.state.zoom,
|
||||
app.state.isMidpointSnappingEnabled,
|
||||
app.state.isMidpointSnappingEnabled &&
|
||||
!angleLocked &&
|
||||
!app.state.gridModeEnabled,
|
||||
)
|
||||
: linearElementEditor.initialState.altFocusPoint,
|
||||
},
|
||||
@@ -2176,6 +2189,7 @@ const pointDraggingUpdates = (
|
||||
newArrow: !!app.state.newElement,
|
||||
angleLocked,
|
||||
altKey,
|
||||
gridSize: app.getEffectiveGridSize(),
|
||||
},
|
||||
);
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ import { isInvisiblySmallElement } from "@excalidraw/element";
|
||||
|
||||
import { CaptureUpdateAction } from "@excalidraw/element";
|
||||
|
||||
import type { GlobalPoint, LocalPoint } from "@excalidraw/math";
|
||||
import type { LocalPoint } from "@excalidraw/math";
|
||||
import type {
|
||||
ExcalidrawElement,
|
||||
ExcalidrawLinearElement,
|
||||
@@ -93,32 +93,40 @@ export const actionFinalize = register<FormData>({
|
||||
? [element.points.length - 1] // New arrow creation
|
||||
: appState.selectedLinearElement.selectedPointsIndices;
|
||||
|
||||
const angleLocked = shouldRotateWithDiscreteAngle(event);
|
||||
const effectiveGridSize = event[KEYS.CTRL_OR_CMD]
|
||||
? null
|
||||
: app.getEffectiveGridSize();
|
||||
|
||||
const draggedPoints: PointsPositionUpdates =
|
||||
selectedPointsIndices.reduce((map, index) => {
|
||||
map.set(index, {
|
||||
point: LinearElementEditor.pointFromAbsoluteCoords(
|
||||
point: angleLocked
|
||||
? element.points[index]
|
||||
: LinearElementEditor.createPointAt(
|
||||
element,
|
||||
pointFrom<GlobalPoint>(
|
||||
elementsMap,
|
||||
sceneCoords.x - linearElementEditor.pointerOffset.x,
|
||||
sceneCoords.y - linearElementEditor.pointerOffset.y,
|
||||
),
|
||||
elementsMap,
|
||||
effectiveGridSize,
|
||||
),
|
||||
});
|
||||
|
||||
return map;
|
||||
}, new Map()) ?? new Map();
|
||||
|
||||
bindOrUnbindBindingElement(
|
||||
element,
|
||||
draggedPoints,
|
||||
sceneCoords.x - linearElementEditor.pointerOffset.x,
|
||||
sceneCoords.y - linearElementEditor.pointerOffset.y,
|
||||
sceneCoords.x,
|
||||
sceneCoords.y,
|
||||
scene,
|
||||
appState,
|
||||
{
|
||||
newArrow,
|
||||
altKey: event.altKey,
|
||||
angleLocked: shouldRotateWithDiscreteAngle(event),
|
||||
angleLocked,
|
||||
gridSize: app.getEffectiveGridSize(),
|
||||
},
|
||||
);
|
||||
} else if (isLineElement(element)) {
|
||||
|
||||
@@ -7263,14 +7263,16 @@ class App extends React.Component<AppProps, AppState> {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this.state.activeTool.type === "arrow") {
|
||||
// Set suggested binding if we're hovering with an arrow tool
|
||||
// and not dragging out a new element
|
||||
if (this.state.activeTool.type === "arrow" && !this.state.newElement) {
|
||||
const scenePointer = pointFrom<GlobalPoint>(scenePointerX, scenePointerY);
|
||||
const hit = getHoveredElementForBinding(
|
||||
pointFrom<GlobalPoint>(scenePointerX, scenePointerY),
|
||||
scenePointer,
|
||||
this.scene.getNonDeletedElements(),
|
||||
this.scene.getNonDeletedElementsMap(),
|
||||
maxBindingDistance_simple(this.state.zoom),
|
||||
);
|
||||
const scenePointer = pointFrom<GlobalPoint>(scenePointerX, scenePointerY);
|
||||
const elementsMap = this.scene.getNonDeletedElementsMap();
|
||||
if (hit && !isPointInElement(scenePointer, hit, elementsMap)) {
|
||||
this.setState({
|
||||
|
||||
@@ -253,6 +253,7 @@ const getRelevantAppStateProps = (
|
||||
newElement: appState.newElement,
|
||||
isBindingEnabled: appState.isBindingEnabled,
|
||||
isMidpointSnappingEnabled: appState.isMidpointSnappingEnabled,
|
||||
gridModeEnabled: appState.gridModeEnabled,
|
||||
suggestedBinding: appState.suggestedBinding,
|
||||
isRotating: appState.isRotating,
|
||||
elementsToHighlight: appState.elementsToHighlight,
|
||||
|
||||
@@ -17,6 +17,7 @@ import {
|
||||
FRAME_STYLE,
|
||||
getFeatureFlag,
|
||||
invariant,
|
||||
shouldRotateWithDiscreteAngle,
|
||||
THEME,
|
||||
} from "@excalidraw/common";
|
||||
|
||||
@@ -229,6 +230,7 @@ const renderBindingHighlightForBindableElement_simple = (
|
||||
elementsMap: ElementsMap,
|
||||
appState: InteractiveCanvasAppState,
|
||||
pointerCoords: GlobalPoint | null,
|
||||
angleLocked = false,
|
||||
) => {
|
||||
const enclosingFrame =
|
||||
suggestedBinding.element.frameId &&
|
||||
@@ -415,6 +417,8 @@ const renderBindingHighlightForBindableElement_simple = (
|
||||
|
||||
if (
|
||||
appState.isMidpointSnappingEnabled &&
|
||||
!appState.gridModeEnabled &&
|
||||
!angleLocked &&
|
||||
(isFrameLikeElement(suggestedBinding.element) ||
|
||||
isBindableElement(suggestedBinding.element))
|
||||
) {
|
||||
@@ -807,7 +811,12 @@ const renderBindingHighlightForBindableElement_complex = (
|
||||
|
||||
context.restore();
|
||||
|
||||
if (appState.isMidpointSnappingEnabled) {
|
||||
if (
|
||||
appState.isMidpointSnappingEnabled &&
|
||||
!appState.gridModeEnabled &&
|
||||
(!app.lastPointerMoveEvent ||
|
||||
!shouldRotateWithDiscreteAngle(app.lastPointerMoveEvent))
|
||||
) {
|
||||
// Draw midpoint indicators
|
||||
context.save();
|
||||
context.translate(
|
||||
@@ -920,12 +929,16 @@ const renderBindingHighlightForBindableElement = (
|
||||
app.lastPointerMoveCoords.y,
|
||||
)
|
||||
: null;
|
||||
const angleLocked =
|
||||
!!app.lastPointerMoveEvent &&
|
||||
shouldRotateWithDiscreteAngle(app.lastPointerMoveEvent);
|
||||
renderBindingHighlightForBindableElement_simple(
|
||||
context,
|
||||
suggestedBinding,
|
||||
allElementsMap,
|
||||
appState,
|
||||
pointerCoords,
|
||||
angleLocked,
|
||||
);
|
||||
context.restore();
|
||||
};
|
||||
|
||||
@@ -224,6 +224,7 @@ export type InteractiveCanvasAppState = Readonly<
|
||||
newElement: AppState["newElement"];
|
||||
isBindingEnabled: AppState["isBindingEnabled"];
|
||||
isMidpointSnappingEnabled: AppState["isMidpointSnappingEnabled"];
|
||||
gridModeEnabled: AppState["gridModeEnabled"];
|
||||
suggestedBinding: AppState["suggestedBinding"];
|
||||
isRotating: AppState["isRotating"];
|
||||
elementsToHighlight: AppState["elementsToHighlight"];
|
||||
@@ -845,6 +846,7 @@ export type AppClassProperties = {
|
||||
onStateChange: App["onStateChange"];
|
||||
|
||||
lastPointerMoveCoords: App["lastPointerMoveCoords"];
|
||||
lastPointerMoveEvent: App["lastPointerMoveEvent"];
|
||||
bindModeHandler: App["bindModeHandler"];
|
||||
|
||||
setAppState: App["setAppState"];
|
||||
|
||||
Reference in New Issue
Block a user