Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b92f585ce7 |
@@ -1,5 +1,3 @@
|
|||||||
import type { Radians } from "@excalidraw/math";
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* x and y position of top left corner, x and y position of bottom right corner
|
* x and y position of top left corner, x and y position of bottom right corner
|
||||||
*/
|
*/
|
||||||
@@ -8,31 +6,7 @@ export type Bounds = readonly [
|
|||||||
minY: number,
|
minY: number,
|
||||||
maxX: number,
|
maxX: number,
|
||||||
maxY: number,
|
maxY: number,
|
||||||
] & { _brand: "excalidraw__bounds" };
|
];
|
||||||
|
|
||||||
export type RotatedBounds = readonly [
|
|
||||||
minX: number,
|
|
||||||
minY: number,
|
|
||||||
maxX: number,
|
|
||||||
maxY: number,
|
|
||||||
angle: Radians,
|
|
||||||
] & {
|
|
||||||
_brand_rotated: "excalidraw__rotated_bounds";
|
|
||||||
};
|
|
||||||
|
|
||||||
export const bounds = <T extends Radians | undefined = undefined>(
|
|
||||||
minX: number,
|
|
||||||
minY: number,
|
|
||||||
maxX: number,
|
|
||||||
maxY: number,
|
|
||||||
angle: T = undefined as T,
|
|
||||||
) => {
|
|
||||||
return (
|
|
||||||
angle
|
|
||||||
? ([minX, minY, maxX, maxY, angle] as unknown)
|
|
||||||
: ([minX, minY, maxX, maxY] as unknown)
|
|
||||||
) as T extends Radians ? RotatedBounds : Bounds;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const isBounds = (box: unknown): box is Bounds =>
|
export const isBounds = (box: unknown): box is Bounds =>
|
||||||
Array.isArray(box) &&
|
Array.isArray(box) &&
|
||||||
@@ -41,12 +15,3 @@ export const isBounds = (box: unknown): box is Bounds =>
|
|||||||
typeof box[1] === "number" &&
|
typeof box[1] === "number" &&
|
||||||
typeof box[2] === "number" &&
|
typeof box[2] === "number" &&
|
||||||
typeof box[3] === "number";
|
typeof box[3] === "number";
|
||||||
|
|
||||||
export const isRotatedBounds = (box: unknown): box is RotatedBounds =>
|
|
||||||
Array.isArray(box) &&
|
|
||||||
box.length === 5 &&
|
|
||||||
typeof box[0] === "number" &&
|
|
||||||
typeof box[1] === "number" &&
|
|
||||||
typeof box[2] === "number" &&
|
|
||||||
typeof box[3] === "number" &&
|
|
||||||
typeof box[4] === "number";
|
|
||||||
|
|||||||
@@ -2,11 +2,9 @@ import rough from "roughjs/bin/rough";
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
arrayToMap,
|
arrayToMap,
|
||||||
bounds,
|
|
||||||
type Bounds,
|
type Bounds,
|
||||||
invariant,
|
invariant,
|
||||||
rescalePoints,
|
rescalePoints,
|
||||||
type RotatedBounds,
|
|
||||||
sizeOf,
|
sizeOf,
|
||||||
} from "@excalidraw/common";
|
} from "@excalidraw/common";
|
||||||
|
|
||||||
@@ -92,7 +90,7 @@ export class ElementBounds {
|
|||||||
private static boundsCache = new WeakMap<
|
private static boundsCache = new WeakMap<
|
||||||
ExcalidrawElement,
|
ExcalidrawElement,
|
||||||
{
|
{
|
||||||
bounds: RotatedBounds;
|
bounds: Bounds;
|
||||||
version: ExcalidrawElement["version"];
|
version: ExcalidrawElement["version"];
|
||||||
}
|
}
|
||||||
>();
|
>();
|
||||||
@@ -104,11 +102,11 @@ export class ElementBounds {
|
|||||||
}
|
}
|
||||||
>();
|
>();
|
||||||
|
|
||||||
static getBounds<T extends boolean = false>(
|
static getBounds(
|
||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
nonRotated: T = false as T,
|
nonRotated: boolean = false,
|
||||||
): T extends true ? Bounds : RotatedBounds {
|
) {
|
||||||
const cachedBounds =
|
const cachedBounds =
|
||||||
nonRotated && element.angle !== 0
|
nonRotated && element.angle !== 0
|
||||||
? ElementBounds.nonRotatedBoundsCache.get(element)
|
? ElementBounds.nonRotatedBoundsCache.get(element)
|
||||||
@@ -121,41 +119,40 @@ export class ElementBounds {
|
|||||||
// which is causing problems down the line. Fix TBA.
|
// which is causing problems down the line. Fix TBA.
|
||||||
!isBoundToContainer(element)
|
!isBoundToContainer(element)
|
||||||
) {
|
) {
|
||||||
return cachedBounds.bounds as T extends true ? Bounds : RotatedBounds;
|
return cachedBounds.bounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nonRotated && element.angle !== 0) {
|
if (nonRotated && element.angle !== 0) {
|
||||||
const [minX, minY, maxX, maxY] = ElementBounds.calculateBounds(
|
const nonRotatedBounds = ElementBounds.calculateBounds(
|
||||||
{
|
{
|
||||||
...element,
|
...element,
|
||||||
angle: 0 as Radians,
|
angle: 0 as Radians,
|
||||||
},
|
},
|
||||||
elementsMap,
|
elementsMap,
|
||||||
);
|
);
|
||||||
const nonRotatedBounds = bounds(minX, minY, maxX, maxY);
|
|
||||||
ElementBounds.nonRotatedBoundsCache.set(element, {
|
ElementBounds.nonRotatedBoundsCache.set(element, {
|
||||||
version: element.version,
|
version: element.version,
|
||||||
bounds: nonRotatedBounds,
|
bounds: nonRotatedBounds,
|
||||||
});
|
});
|
||||||
|
|
||||||
return nonRotatedBounds as T extends true ? Bounds : RotatedBounds;
|
return nonRotatedBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
const _bounds = ElementBounds.calculateBounds(element, elementsMap);
|
const bounds = ElementBounds.calculateBounds(element, elementsMap);
|
||||||
|
|
||||||
ElementBounds.boundsCache.set(element, {
|
ElementBounds.boundsCache.set(element, {
|
||||||
version: element.version,
|
version: element.version,
|
||||||
bounds: _bounds,
|
bounds,
|
||||||
});
|
});
|
||||||
|
|
||||||
return _bounds as T extends true ? Bounds : RotatedBounds;
|
return bounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static calculateBounds(
|
private static calculateBounds(
|
||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
): RotatedBounds {
|
): Bounds {
|
||||||
let _bounds: RotatedBounds;
|
let bounds: Bounds;
|
||||||
|
|
||||||
const [x1, y1, x2, y2, cx, cy] = getElementAbsoluteCoords(
|
const [x1, y1, x2, y2, cx, cy] = getElementAbsoluteCoords(
|
||||||
element,
|
element,
|
||||||
@@ -172,15 +169,14 @@ export class ElementBounds {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
return bounds(
|
return [
|
||||||
minX + element.x,
|
minX + element.x,
|
||||||
minY + element.y,
|
minY + element.y,
|
||||||
maxX + element.x,
|
maxX + element.x,
|
||||||
maxY + element.y,
|
maxY + element.y,
|
||||||
element.angle,
|
];
|
||||||
);
|
|
||||||
} else if (isLinearElement(element)) {
|
} else if (isLinearElement(element)) {
|
||||||
_bounds = getLinearElementRotatedBounds(element, cx, cy, elementsMap);
|
bounds = getLinearElementRotatedBounds(element, cx, cy, elementsMap);
|
||||||
} else if (element.type === "diamond") {
|
} else if (element.type === "diamond") {
|
||||||
const [x11, y11] = pointRotateRads(
|
const [x11, y11] = pointRotateRads(
|
||||||
pointFrom(cx, y1),
|
pointFrom(cx, y1),
|
||||||
@@ -206,7 +202,7 @@ export class ElementBounds {
|
|||||||
const minY = Math.min(y11, y12, y22, y21);
|
const minY = Math.min(y11, y12, y22, y21);
|
||||||
const maxX = Math.max(x11, x12, x22, x21);
|
const maxX = Math.max(x11, x12, x22, x21);
|
||||||
const maxY = Math.max(y11, y12, y22, y21);
|
const maxY = Math.max(y11, y12, y22, y21);
|
||||||
_bounds = bounds(minX, minY, maxX, maxY, element.angle);
|
bounds = [minX, minY, maxX, maxY];
|
||||||
} else if (element.type === "ellipse") {
|
} else if (element.type === "ellipse") {
|
||||||
const w = (x2 - x1) / 2;
|
const w = (x2 - x1) / 2;
|
||||||
const h = (y2 - y1) / 2;
|
const h = (y2 - y1) / 2;
|
||||||
@@ -214,7 +210,7 @@ export class ElementBounds {
|
|||||||
const sin = Math.sin(element.angle);
|
const sin = Math.sin(element.angle);
|
||||||
const ww = Math.hypot(w * cos, h * sin);
|
const ww = Math.hypot(w * cos, h * sin);
|
||||||
const hh = Math.hypot(h * cos, w * sin);
|
const hh = Math.hypot(h * cos, w * sin);
|
||||||
_bounds = bounds(cx - ww, cy - hh, cx + ww, cy + hh, element.angle);
|
bounds = [cx - ww, cy - hh, cx + ww, cy + hh];
|
||||||
} else {
|
} else {
|
||||||
const [x11, y11] = pointRotateRads(
|
const [x11, y11] = pointRotateRads(
|
||||||
pointFrom(x1, y1),
|
pointFrom(x1, y1),
|
||||||
@@ -240,10 +236,10 @@ export class ElementBounds {
|
|||||||
const minY = Math.min(y11, y12, y22, y21);
|
const minY = Math.min(y11, y12, y22, y21);
|
||||||
const maxX = Math.max(x11, x12, x22, x21);
|
const maxX = Math.max(x11, x12, x22, x21);
|
||||||
const maxY = Math.max(y11, y12, y22, y21);
|
const maxY = Math.max(y11, y12, y22, y21);
|
||||||
_bounds = bounds(minX, minY, maxX, maxY, element.angle);
|
bounds = [minX, minY, maxX, maxY];
|
||||||
}
|
}
|
||||||
|
|
||||||
return _bounds;
|
return bounds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -628,7 +624,7 @@ export const getCubicBezierCurveBound = (
|
|||||||
minY = Math.min(minY, ...ys);
|
minY = Math.min(minY, ...ys);
|
||||||
maxY = Math.max(maxY, ...ys);
|
maxY = Math.max(maxY, ...ys);
|
||||||
}
|
}
|
||||||
return bounds(minX, minY, maxX, maxY);
|
return [minX, minY, maxX, maxY];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getMinMaxXYFromCurvePathOps = (
|
export const getMinMaxXYFromCurvePathOps = (
|
||||||
@@ -681,7 +677,7 @@ export const getMinMaxXYFromCurvePathOps = (
|
|||||||
},
|
},
|
||||||
{ minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity },
|
{ minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity },
|
||||||
);
|
);
|
||||||
return bounds(minX, minY, maxX, maxY);
|
return [minX, minY, maxX, maxY];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBoundsFromPoints = (
|
export const getBoundsFromPoints = (
|
||||||
@@ -699,7 +695,7 @@ export const getBoundsFromPoints = (
|
|||||||
maxY = Math.max(maxY, y);
|
maxY = Math.max(maxY, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bounds(minX, minY, maxX, maxY);
|
return [minX, minY, maxX, maxY];
|
||||||
};
|
};
|
||||||
|
|
||||||
const getFreeDrawElementAbsoluteCoords = (
|
const getFreeDrawElementAbsoluteCoords = (
|
||||||
@@ -942,7 +938,7 @@ const getLinearElementRotatedBounds = (
|
|||||||
cx: number,
|
cx: number,
|
||||||
cy: number,
|
cy: number,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
): RotatedBounds => {
|
): Bounds => {
|
||||||
const boundTextElement = getBoundTextElement(element, elementsMap);
|
const boundTextElement = getBoundTextElement(element, elementsMap);
|
||||||
|
|
||||||
if (element.points.length < 2) {
|
if (element.points.length < 2) {
|
||||||
@@ -953,21 +949,20 @@ const getLinearElementRotatedBounds = (
|
|||||||
element.angle,
|
element.angle,
|
||||||
);
|
);
|
||||||
|
|
||||||
let coords: RotatedBounds = bounds(x, y, x, y, element.angle);
|
let coords: Bounds = [x, y, x, y];
|
||||||
if (boundTextElement) {
|
if (boundTextElement) {
|
||||||
const coordsWithBoundText = LinearElementEditor.getMinMaxXYWithBoundText(
|
const coordsWithBoundText = LinearElementEditor.getMinMaxXYWithBoundText(
|
||||||
element,
|
element,
|
||||||
elementsMap,
|
elementsMap,
|
||||||
bounds(x, y, x, y, element.angle),
|
[x, y, x, y],
|
||||||
boundTextElement,
|
boundTextElement,
|
||||||
);
|
);
|
||||||
coords = bounds(
|
coords = [
|
||||||
coordsWithBoundText[0],
|
coordsWithBoundText[0],
|
||||||
coordsWithBoundText[1],
|
coordsWithBoundText[1],
|
||||||
coordsWithBoundText[2],
|
coordsWithBoundText[2],
|
||||||
coordsWithBoundText[3],
|
coordsWithBoundText[3],
|
||||||
element.angle,
|
];
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return coords;
|
return coords;
|
||||||
}
|
}
|
||||||
@@ -983,13 +978,7 @@ const getLinearElementRotatedBounds = (
|
|||||||
element.angle,
|
element.angle,
|
||||||
);
|
);
|
||||||
const res = getMinMaxXYFromCurvePathOps(ops, transformXY);
|
const res = getMinMaxXYFromCurvePathOps(ops, transformXY);
|
||||||
let coords: RotatedBounds = bounds(
|
let coords: Bounds = [res[0], res[1], res[2], res[3]];
|
||||||
res[0],
|
|
||||||
res[1],
|
|
||||||
res[2],
|
|
||||||
res[3],
|
|
||||||
element.angle,
|
|
||||||
);
|
|
||||||
if (boundTextElement) {
|
if (boundTextElement) {
|
||||||
const coordsWithBoundText = LinearElementEditor.getMinMaxXYWithBoundText(
|
const coordsWithBoundText = LinearElementEditor.getMinMaxXYWithBoundText(
|
||||||
element,
|
element,
|
||||||
@@ -997,23 +986,22 @@ const getLinearElementRotatedBounds = (
|
|||||||
coords,
|
coords,
|
||||||
boundTextElement,
|
boundTextElement,
|
||||||
);
|
);
|
||||||
coords = bounds(
|
coords = [
|
||||||
coordsWithBoundText[0],
|
coordsWithBoundText[0],
|
||||||
coordsWithBoundText[1],
|
coordsWithBoundText[1],
|
||||||
coordsWithBoundText[2],
|
coordsWithBoundText[2],
|
||||||
coordsWithBoundText[3],
|
coordsWithBoundText[3],
|
||||||
element.angle,
|
];
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return coords;
|
return coords;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getElementBounds = <T extends boolean = false>(
|
export const getElementBounds = (
|
||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
nonRotated: T = false as T,
|
nonRotated: boolean = false,
|
||||||
) => {
|
): Bounds => {
|
||||||
return ElementBounds.getBounds<T>(element, elementsMap, nonRotated);
|
return ElementBounds.getBounds(element, elementsMap, nonRotated);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getCommonBounds = (
|
export const getCommonBounds = (
|
||||||
@@ -1021,7 +1009,7 @@ export const getCommonBounds = (
|
|||||||
elementsMap?: ElementsMap,
|
elementsMap?: ElementsMap,
|
||||||
): Bounds => {
|
): Bounds => {
|
||||||
if (!sizeOf(elements)) {
|
if (!sizeOf(elements)) {
|
||||||
return bounds(0, 0, 0, 0);
|
return [0, 0, 0, 0];
|
||||||
}
|
}
|
||||||
|
|
||||||
let minX = Infinity;
|
let minX = Infinity;
|
||||||
@@ -1039,7 +1027,7 @@ export const getCommonBounds = (
|
|||||||
maxY = Math.max(maxY, y2);
|
maxY = Math.max(maxY, y2);
|
||||||
});
|
});
|
||||||
|
|
||||||
return bounds(minX, minY, maxX, maxY);
|
return [minX, minY, maxX, maxY];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDraggedElementsBounds = (
|
export const getDraggedElementsBounds = (
|
||||||
@@ -1062,12 +1050,12 @@ export const getResizedElementAbsoluteCoords = (
|
|||||||
normalizePoints: boolean,
|
normalizePoints: boolean,
|
||||||
): Bounds => {
|
): Bounds => {
|
||||||
if (!(isLinearElement(element) || isFreeDrawElement(element))) {
|
if (!(isLinearElement(element) || isFreeDrawElement(element))) {
|
||||||
return bounds(
|
return [
|
||||||
element.x,
|
element.x,
|
||||||
element.y,
|
element.y,
|
||||||
element.x + nextWidth,
|
element.x + nextWidth,
|
||||||
element.y + nextHeight,
|
element.y + nextHeight,
|
||||||
);
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
const points = rescalePoints(
|
const points = rescalePoints(
|
||||||
@@ -1077,11 +1065,11 @@ export const getResizedElementAbsoluteCoords = (
|
|||||||
normalizePoints,
|
normalizePoints,
|
||||||
);
|
);
|
||||||
|
|
||||||
let _bounds: Bounds;
|
let bounds: Bounds;
|
||||||
|
|
||||||
if (isFreeDrawElement(element)) {
|
if (isFreeDrawElement(element)) {
|
||||||
// Free Draw
|
// Free Draw
|
||||||
_bounds = getBoundsFromPoints(points);
|
bounds = getBoundsFromPoints(points);
|
||||||
} else {
|
} else {
|
||||||
// Line
|
// Line
|
||||||
const gen = rough.generator();
|
const gen = rough.generator();
|
||||||
@@ -1093,16 +1081,16 @@ export const getResizedElementAbsoluteCoords = (
|
|||||||
: gen.curve(points as [number, number][], generateRoughOptions(element));
|
: gen.curve(points as [number, number][], generateRoughOptions(element));
|
||||||
|
|
||||||
const ops = getCurvePathOps(curve);
|
const ops = getCurvePathOps(curve);
|
||||||
_bounds = getMinMaxXYFromCurvePathOps(ops);
|
bounds = getMinMaxXYFromCurvePathOps(ops);
|
||||||
}
|
}
|
||||||
|
|
||||||
const [minX, minY, maxX, maxY] = _bounds;
|
const [minX, minY, maxX, maxY] = bounds;
|
||||||
return bounds(
|
return [
|
||||||
minX + element.x,
|
minX + element.x,
|
||||||
minY + element.y,
|
minY + element.y,
|
||||||
maxX + element.x,
|
maxX + element.x,
|
||||||
maxY + element.y,
|
maxY + element.y,
|
||||||
);
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getElementPointsCoords = (
|
export const getElementPointsCoords = (
|
||||||
@@ -1120,20 +1108,20 @@ export const getElementPointsCoords = (
|
|||||||
: gen.curve(points as [number, number][], generateRoughOptions(element));
|
: gen.curve(points as [number, number][], generateRoughOptions(element));
|
||||||
const ops = getCurvePathOps(curve);
|
const ops = getCurvePathOps(curve);
|
||||||
const [minX, minY, maxX, maxY] = getMinMaxXYFromCurvePathOps(ops);
|
const [minX, minY, maxX, maxY] = getMinMaxXYFromCurvePathOps(ops);
|
||||||
return bounds(
|
return [
|
||||||
minX + element.x,
|
minX + element.x,
|
||||||
minY + element.y,
|
minY + element.y,
|
||||||
maxX + element.x,
|
maxX + element.x,
|
||||||
maxY + element.y,
|
maxY + element.y,
|
||||||
);
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getClosestElementBounds = (
|
export const getClosestElementBounds = (
|
||||||
elements: readonly ExcalidrawElement[],
|
elements: readonly ExcalidrawElement[],
|
||||||
from: { x: number; y: number },
|
from: { x: number; y: number },
|
||||||
): RotatedBounds => {
|
): Bounds => {
|
||||||
if (!elements.length) {
|
if (!elements.length) {
|
||||||
return bounds(0, 0, 0, 0, 0 as Radians);
|
return [0, 0, 0, 0];
|
||||||
}
|
}
|
||||||
|
|
||||||
let minDistance = Infinity;
|
let minDistance = Infinity;
|
||||||
@@ -1202,9 +1190,7 @@ export const getVisibleSceneBounds = ({
|
|||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getCenterForBounds = (
|
export const getCenterForBounds = (bounds: Bounds): GlobalPoint =>
|
||||||
bounds: Bounds | RotatedBounds,
|
|
||||||
): GlobalPoint =>
|
|
||||||
pointFrom(
|
pointFrom(
|
||||||
bounds[0] + (bounds[2] - bounds[0]) / 2,
|
bounds[0] + (bounds[2] - bounds[0]) / 2,
|
||||||
bounds[1] + (bounds[3] - bounds[1]) / 2,
|
bounds[1] + (bounds[3] - bounds[1]) / 2,
|
||||||
@@ -1249,24 +1235,24 @@ export const aabbForElement = (
|
|||||||
element.angle,
|
element.angle,
|
||||||
);
|
);
|
||||||
|
|
||||||
const _bounds = bounds(
|
const bounds = [
|
||||||
Math.min(topLeftX, topRightX, bottomRightX, bottomLeftX),
|
Math.min(topLeftX, topRightX, bottomRightX, bottomLeftX),
|
||||||
Math.min(topLeftY, topRightY, bottomRightY, bottomLeftY),
|
Math.min(topLeftY, topRightY, bottomRightY, bottomLeftY),
|
||||||
Math.max(topLeftX, topRightX, bottomRightX, bottomLeftX),
|
Math.max(topLeftX, topRightX, bottomRightX, bottomLeftX),
|
||||||
Math.max(topLeftY, topRightY, bottomRightY, bottomLeftY),
|
Math.max(topLeftY, topRightY, bottomRightY, bottomLeftY),
|
||||||
);
|
] as Bounds;
|
||||||
|
|
||||||
if (offset) {
|
if (offset) {
|
||||||
const [topOffset, rightOffset, downOffset, leftOffset] = offset;
|
const [topOffset, rightOffset, downOffset, leftOffset] = offset;
|
||||||
return bounds(
|
return [
|
||||||
_bounds[0] - leftOffset,
|
bounds[0] - leftOffset,
|
||||||
_bounds[1] - topOffset,
|
bounds[1] - topOffset,
|
||||||
_bounds[2] + rightOffset,
|
bounds[2] + rightOffset,
|
||||||
_bounds[3] + downOffset,
|
bounds[3] + downOffset,
|
||||||
);
|
] as Bounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
return _bounds;
|
return bounds;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const pointInsideBounds = <P extends GlobalPoint | LocalPoint>(
|
export const pointInsideBounds = <P extends GlobalPoint | LocalPoint>(
|
||||||
@@ -1275,7 +1261,7 @@ export const pointInsideBounds = <P extends GlobalPoint | LocalPoint>(
|
|||||||
): boolean =>
|
): boolean =>
|
||||||
p[0] > bounds[0] && p[0] < bounds[2] && p[1] > bounds[1] && p[1] < bounds[3];
|
p[0] > bounds[0] && p[0] < bounds[2] && p[1] > bounds[1] && p[1] < bounds[3];
|
||||||
|
|
||||||
export const doNonRotatedBoundsIntersect = (
|
export const doBoundsIntersect = (
|
||||||
bounds1: Bounds | null,
|
bounds1: Bounds | null,
|
||||||
bounds2: Bounds | null,
|
bounds2: Bounds | null,
|
||||||
): boolean => {
|
): boolean => {
|
||||||
@@ -1295,7 +1281,7 @@ export const elementCenterPoint = (
|
|||||||
xOffset: number = 0,
|
xOffset: number = 0,
|
||||||
yOffset: number = 0,
|
yOffset: number = 0,
|
||||||
) => {
|
) => {
|
||||||
if (isLinearElement(element) || isFreeDrawElement(element)) {
|
if (isLinearElement(element)) {
|
||||||
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(element, elementsMap);
|
||||||
const [x, y] = pointFrom<GlobalPoint>((x1 + x2) / 2, (y1 + y2) / 2);
|
const [x, y] = pointFrom<GlobalPoint>((x1 + x2) / 2, (y1 + y2) / 2);
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,4 @@
|
|||||||
import {
|
import { invariant, isTransparent, type Bounds } from "@excalidraw/common";
|
||||||
bounds,
|
|
||||||
invariant,
|
|
||||||
isTransparent,
|
|
||||||
type Bounds,
|
|
||||||
} from "@excalidraw/common";
|
|
||||||
import {
|
import {
|
||||||
curveIntersectLineSegment,
|
curveIntersectLineSegment,
|
||||||
isPointWithinBounds,
|
isPointWithinBounds,
|
||||||
@@ -34,7 +29,7 @@ import type { FrameNameBounds } from "@excalidraw/excalidraw/types";
|
|||||||
|
|
||||||
import { isPathALoop } from "./utils";
|
import { isPathALoop } from "./utils";
|
||||||
import {
|
import {
|
||||||
doNonRotatedBoundsIntersect,
|
doBoundsIntersect,
|
||||||
elementCenterPoint,
|
elementCenterPoint,
|
||||||
getCenterForBounds,
|
getCenterForBounds,
|
||||||
getCubicBezierCurveBound,
|
getCubicBezierCurveBound,
|
||||||
@@ -159,11 +154,14 @@ export const hitElementItself = ({
|
|||||||
|
|
||||||
// Hit test against the extended, rotated bounding box of the element first
|
// Hit test against the extended, rotated bounding box of the element first
|
||||||
const bounds = getElementBounds(element, elementsMap, true);
|
const bounds = getElementBounds(element, elementsMap, true);
|
||||||
const hitBounds = isPointInRotatedBounds(
|
const hitBounds = isPointWithinBounds(
|
||||||
point,
|
pointFrom(bounds[0] - threshold, bounds[1] - threshold),
|
||||||
bounds,
|
pointRotateRads(
|
||||||
element.angle,
|
point,
|
||||||
threshold,
|
getCenterForBounds(bounds),
|
||||||
|
-element.angle as Radians,
|
||||||
|
),
|
||||||
|
pointFrom(bounds[2] + threshold, bounds[3] + threshold),
|
||||||
);
|
);
|
||||||
|
|
||||||
// PERF: Bail out early if the point is not even in the
|
// PERF: Bail out early if the point is not even in the
|
||||||
@@ -194,108 +192,18 @@ export const hitElementItself = ({
|
|||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
export function getBoundsCorners(
|
|
||||||
bounds: Bounds,
|
|
||||||
): readonly [GlobalPoint, GlobalPoint, GlobalPoint, GlobalPoint];
|
|
||||||
export function getBoundsCorners(
|
|
||||||
bounds: Bounds,
|
|
||||||
angle: Radians,
|
|
||||||
): readonly [GlobalPoint, GlobalPoint, GlobalPoint, GlobalPoint];
|
|
||||||
export function getBoundsCorners(
|
|
||||||
bounds: Bounds,
|
|
||||||
angle: Radians = 0 as Radians,
|
|
||||||
) {
|
|
||||||
const [x1, y1, x2, y2] = bounds;
|
|
||||||
const center = getCenterForBounds(bounds);
|
|
||||||
const corners = [
|
|
||||||
pointFrom<GlobalPoint>(x1, y1),
|
|
||||||
pointFrom<GlobalPoint>(x2, y1),
|
|
||||||
pointFrom<GlobalPoint>(x2, y2),
|
|
||||||
pointFrom<GlobalPoint>(x1, y2),
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
if (angle === 0) {
|
|
||||||
return corners;
|
|
||||||
}
|
|
||||||
|
|
||||||
return corners.map((point) => pointRotateRads(point, center, angle)) as [
|
|
||||||
GlobalPoint,
|
|
||||||
GlobalPoint,
|
|
||||||
GlobalPoint,
|
|
||||||
GlobalPoint,
|
|
||||||
];
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getBoundsEdges = (
|
|
||||||
corners: readonly [GlobalPoint, GlobalPoint, GlobalPoint, GlobalPoint],
|
|
||||||
) =>
|
|
||||||
[
|
|
||||||
lineSegment(corners[0], corners[1]),
|
|
||||||
lineSegment(corners[1], corners[2]),
|
|
||||||
lineSegment(corners[2], corners[3]),
|
|
||||||
lineSegment(corners[3], corners[0]),
|
|
||||||
] as const;
|
|
||||||
|
|
||||||
const isPointInRotatedBounds = (
|
|
||||||
point: GlobalPoint,
|
|
||||||
bounds: Bounds,
|
|
||||||
angle: Radians,
|
|
||||||
tolerance = 0,
|
|
||||||
) => {
|
|
||||||
const adjustedPoint =
|
|
||||||
angle === 0
|
|
||||||
? point
|
|
||||||
: pointRotateRads(point, getCenterForBounds(bounds), -angle as Radians);
|
|
||||||
|
|
||||||
return isPointWithinBounds(
|
|
||||||
pointFrom(bounds[0] - tolerance, bounds[1] - tolerance),
|
|
||||||
adjustedPoint,
|
|
||||||
pointFrom(bounds[2] + tolerance, bounds[3] + tolerance),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const hitElementBoundingBox = (
|
export const hitElementBoundingBox = (
|
||||||
point: GlobalPoint,
|
point: GlobalPoint,
|
||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
tolerance = 0,
|
tolerance = 0,
|
||||||
) => {
|
) => {
|
||||||
const bounds = getElementBounds(element, elementsMap, true);
|
let [x1, y1, x2, y2] = getElementBounds(element, elementsMap);
|
||||||
return isPointInRotatedBounds(point, bounds, element.angle, tolerance);
|
x1 -= tolerance;
|
||||||
};
|
y1 -= tolerance;
|
||||||
|
x2 += tolerance;
|
||||||
export const doBoundsIntersectElementBoundingBox = (
|
y2 += tolerance;
|
||||||
intersectorBounds: Bounds,
|
return isPointWithinBounds(pointFrom(x1, y1), point, pointFrom(x2, y2));
|
||||||
element: ExcalidrawElement,
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
) => {
|
|
||||||
const [x1, y1, x2, y2] = intersectorBounds;
|
|
||||||
const intersectorCorners = [
|
|
||||||
pointFrom<GlobalPoint>(x1, y1),
|
|
||||||
pointFrom<GlobalPoint>(x2, y1),
|
|
||||||
pointFrom<GlobalPoint>(x2, y2),
|
|
||||||
pointFrom<GlobalPoint>(x1, y2),
|
|
||||||
] as const;
|
|
||||||
const intersectorEdges = getBoundsEdges(intersectorCorners);
|
|
||||||
|
|
||||||
const elementBounds = getElementBounds(element, elementsMap, true);
|
|
||||||
const elementBoundsCorners = getBoundsCorners(elementBounds, element.angle);
|
|
||||||
const elementBoundsEdges = getBoundsEdges(elementBoundsCorners);
|
|
||||||
|
|
||||||
return (
|
|
||||||
elementBoundsCorners.some((point) =>
|
|
||||||
isPointWithinBounds(intersectorCorners[0], point, intersectorCorners[2]),
|
|
||||||
) ||
|
|
||||||
intersectorCorners.some((point) =>
|
|
||||||
isPointInRotatedBounds(point, elementBounds, element.angle),
|
|
||||||
) ||
|
|
||||||
intersectorEdges.some((selectionEdge) =>
|
|
||||||
elementBoundsEdges.some(
|
|
||||||
(elementBoundsEdge) =>
|
|
||||||
!!lineSegmentIntersectionPoints(selectionEdge, elementBoundsEdge),
|
|
||||||
),
|
|
||||||
)
|
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const hitElementBoundingBoxOnly = (
|
export const hitElementBoundingBoxOnly = (
|
||||||
@@ -348,13 +256,9 @@ const bindingBorderTest = (
|
|||||||
// PERF: Run a cheap test to see if the binding element
|
// PERF: Run a cheap test to see if the binding element
|
||||||
// is even close to the element
|
// is even close to the element
|
||||||
const t = Math.max(1, tolerance);
|
const t = Math.max(1, tolerance);
|
||||||
const elementBounds = getElementBounds(element, elementsMap, true);
|
const bounds = [x - t, y - t, x + t, y + t] as Bounds;
|
||||||
if (
|
const elementBounds = getElementBounds(element, elementsMap);
|
||||||
!doNonRotatedBoundsIntersect(
|
if (!doBoundsIntersect(bounds, elementBounds)) {
|
||||||
bounds(x - t, y - t, x + t, y + t),
|
|
||||||
elementBounds,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,7 +269,6 @@ const bindingBorderTest = (
|
|||||||
const enclosingFrameBounds = getElementBounds(
|
const enclosingFrameBounds = getElementBounds(
|
||||||
enclosingFrame,
|
enclosingFrame,
|
||||||
elementsMap,
|
elementsMap,
|
||||||
true,
|
|
||||||
);
|
);
|
||||||
if (!pointInsideBounds(p, enclosingFrameBounds)) {
|
if (!pointInsideBounds(p, enclosingFrameBounds)) {
|
||||||
return false;
|
return false;
|
||||||
@@ -515,15 +418,15 @@ export const intersectElementWithLineSegment = (
|
|||||||
): GlobalPoint[] => {
|
): GlobalPoint[] => {
|
||||||
// First check if the line intersects the element's axis-aligned bounding box
|
// First check if the line intersects the element's axis-aligned bounding box
|
||||||
// as it is much faster than checking intersection against the element's shape
|
// as it is much faster than checking intersection against the element's shape
|
||||||
const intersectorBounds = bounds(
|
const intersectorBounds = [
|
||||||
Math.min(line[0][0] - offset, line[1][0] - offset),
|
Math.min(line[0][0] - offset, line[1][0] - offset),
|
||||||
Math.min(line[0][1] - offset, line[1][1] - offset),
|
Math.min(line[0][1] - offset, line[1][1] - offset),
|
||||||
Math.max(line[0][0] + offset, line[1][0] + offset),
|
Math.max(line[0][0] + offset, line[1][0] + offset),
|
||||||
Math.max(line[0][1] + offset, line[1][1] + offset),
|
Math.max(line[0][1] + offset, line[1][1] + offset),
|
||||||
);
|
] as Bounds;
|
||||||
const elementBounds = getElementBounds(element, elementsMap, true);
|
const elementBounds = getElementBounds(element, elementsMap);
|
||||||
|
|
||||||
if (!doNonRotatedBoundsIntersect(intersectorBounds, elementBounds)) {
|
if (!doBoundsIntersect(intersectorBounds, elementBounds)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -582,14 +485,14 @@ const curveIntersections = (
|
|||||||
for (const c of curves) {
|
for (const c of curves) {
|
||||||
// Optimize by doing a cheap bounding box check first
|
// Optimize by doing a cheap bounding box check first
|
||||||
const b1 = getCubicBezierCurveBound(c[0], c[1], c[2], c[3]);
|
const b1 = getCubicBezierCurveBound(c[0], c[1], c[2], c[3]);
|
||||||
const b2 = bounds(
|
const b2 = [
|
||||||
Math.min(segment[0][0], segment[1][0]),
|
Math.min(segment[0][0], segment[1][0]),
|
||||||
Math.min(segment[0][1], segment[1][1]),
|
Math.min(segment[0][1], segment[1][1]),
|
||||||
Math.max(segment[0][0], segment[1][0]),
|
Math.max(segment[0][0], segment[1][0]),
|
||||||
Math.max(segment[0][1], segment[1][1]),
|
Math.max(segment[0][1], segment[1][1]),
|
||||||
);
|
] as Bounds;
|
||||||
|
|
||||||
if (!doNonRotatedBoundsIntersect(b1, b2)) {
|
if (!doBoundsIntersect(b1, b2)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -659,14 +562,14 @@ const intersectLinearOrFreeDrawWithLineSegment = (
|
|||||||
for (const c of curves) {
|
for (const c of curves) {
|
||||||
// Optimize by doing a cheap bounding box check first
|
// Optimize by doing a cheap bounding box check first
|
||||||
const b1 = getCubicBezierCurveBound(c[0], c[1], c[2], c[3]);
|
const b1 = getCubicBezierCurveBound(c[0], c[1], c[2], c[3]);
|
||||||
const b2 = bounds(
|
const b2 = [
|
||||||
Math.min(segment[0][0], segment[1][0]),
|
Math.min(segment[0][0], segment[1][0]),
|
||||||
Math.min(segment[0][1], segment[1][1]),
|
Math.min(segment[0][1], segment[1][1]),
|
||||||
Math.max(segment[0][0], segment[1][0]),
|
Math.max(segment[0][0], segment[1][0]),
|
||||||
Math.max(segment[0][1], segment[1][1]),
|
Math.max(segment[0][1], segment[1][1]),
|
||||||
);
|
] as Bounds;
|
||||||
|
|
||||||
if (!doNonRotatedBoundsIntersect(b1, b2)) {
|
if (!doBoundsIntersect(b1, b2)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -99,7 +99,7 @@ export const isElementContainingFrame = (
|
|||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
frame: ExcalidrawFrameLikeElement,
|
frame: ExcalidrawFrameLikeElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
): boolean => {
|
) => {
|
||||||
return getElementsWithinSelection([frame], element, elementsMap).some(
|
return getElementsWithinSelection([frame], element, elementsMap).some(
|
||||||
(e) => e.id === frame.id,
|
(e) => e.id === frame.id,
|
||||||
);
|
);
|
||||||
@@ -140,7 +140,7 @@ export const elementOverlapsWithFrame = (
|
|||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
frame: ExcalidrawFrameLikeElement,
|
frame: ExcalidrawFrameLikeElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
): boolean => {
|
) => {
|
||||||
return (
|
return (
|
||||||
elementsAreInFrameBounds([element], frame, elementsMap) ||
|
elementsAreInFrameBounds([element], frame, elementsMap) ||
|
||||||
isElementIntersectingFrame(element, frame, elementsMap) ||
|
isElementIntersectingFrame(element, frame, elementsMap) ||
|
||||||
|
|||||||
@@ -21,7 +21,6 @@ import {
|
|||||||
invariant,
|
invariant,
|
||||||
isShallowEqual,
|
isShallowEqual,
|
||||||
getFeatureFlag,
|
getFeatureFlag,
|
||||||
bounds,
|
|
||||||
} from "@excalidraw/common";
|
} from "@excalidraw/common";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -43,7 +42,7 @@ import type {
|
|||||||
NullableGridSize,
|
NullableGridSize,
|
||||||
Zoom,
|
Zoom,
|
||||||
} from "@excalidraw/excalidraw/types";
|
} from "@excalidraw/excalidraw/types";
|
||||||
import type { RotatedBounds } from "@excalidraw/common";
|
import type { Bounds } from "@excalidraw/common";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
calculateFixedPointForNonElbowArrowBinding,
|
calculateFixedPointForNonElbowArrowBinding,
|
||||||
@@ -1880,7 +1879,7 @@ export class LinearElementEditor {
|
|||||||
static getMinMaxXYWithBoundText = (
|
static getMinMaxXYWithBoundText = (
|
||||||
element: ExcalidrawLinearElement,
|
element: ExcalidrawLinearElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
elementBounds: RotatedBounds,
|
elementBounds: Bounds,
|
||||||
boundTextElement: ExcalidrawTextElementWithContainer,
|
boundTextElement: ExcalidrawTextElementWithContainer,
|
||||||
): [number, number, number, number, number, number] => {
|
): [number, number, number, number, number, number] => {
|
||||||
let [x1, y1, x2, y2] = elementBounds;
|
let [x1, y1, x2, y2] = elementBounds;
|
||||||
@@ -2005,7 +2004,7 @@ export class LinearElementEditor {
|
|||||||
return LinearElementEditor.getMinMaxXYWithBoundText(
|
return LinearElementEditor.getMinMaxXYWithBoundText(
|
||||||
element,
|
element,
|
||||||
elementsMap,
|
elementsMap,
|
||||||
bounds(x1, y1, x2, y2, element.angle),
|
[x1, y1, x2, y2],
|
||||||
boundTextElement,
|
boundTextElement,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,35 +1,15 @@
|
|||||||
import { arrayToMap, isShallowEqual, type Bounds } from "@excalidraw/common";
|
import { arrayToMap, isShallowEqual } from "@excalidraw/common";
|
||||||
import {
|
|
||||||
lineSegment,
|
|
||||||
pointFrom,
|
|
||||||
type GlobalPoint,
|
|
||||||
type LineSegment,
|
|
||||||
} from "@excalidraw/math";
|
|
||||||
|
|
||||||
import type {
|
import type {
|
||||||
AppState,
|
AppState,
|
||||||
BoxSelectionMode,
|
|
||||||
InteractiveCanvasAppState,
|
InteractiveCanvasAppState,
|
||||||
} from "@excalidraw/excalidraw/types";
|
} from "@excalidraw/excalidraw/types";
|
||||||
|
|
||||||
import {
|
import { getElementAbsoluteCoords, getElementBounds } from "./bounds";
|
||||||
getElementAbsoluteCoords,
|
|
||||||
getElementBounds,
|
|
||||||
getElementLineSegments,
|
|
||||||
} from "./bounds";
|
|
||||||
import {
|
|
||||||
doBoundsIntersectElementBoundingBox,
|
|
||||||
getBoundsCorners,
|
|
||||||
getBoundsEdges,
|
|
||||||
intersectElementWithLineSegment,
|
|
||||||
isPointInElement,
|
|
||||||
shouldTestInside,
|
|
||||||
} from "./collision";
|
|
||||||
import { isElementInViewport } from "./sizeHelpers";
|
import { isElementInViewport } from "./sizeHelpers";
|
||||||
import {
|
import {
|
||||||
isBoundToContainer,
|
isBoundToContainer,
|
||||||
isFrameLikeElement,
|
isFrameLikeElement,
|
||||||
isFreeDrawElement,
|
|
||||||
isLinearElement,
|
isLinearElement,
|
||||||
isTextElement,
|
isTextElement,
|
||||||
} from "./typeChecks";
|
} from "./typeChecks";
|
||||||
@@ -50,205 +30,6 @@ import type {
|
|||||||
NonDeletedExcalidrawElement,
|
NonDeletedExcalidrawElement,
|
||||||
} from "./types";
|
} from "./types";
|
||||||
|
|
||||||
// Broad-phase only for overlap mode. Rotated closed shapes should not select
|
|
||||||
// from the empty corners of their axis-aligned bounds. Linear elements and
|
|
||||||
// freedraw already rely on the outline-specific path below, so exclude them.
|
|
||||||
const shouldUseRotatedOverlapBroadPhase = (
|
|
||||||
element: NonDeletedExcalidrawElement,
|
|
||||||
) =>
|
|
||||||
element.angle !== 0 &&
|
|
||||||
!isLinearElement(element) &&
|
|
||||||
!isFreeDrawElement(element);
|
|
||||||
|
|
||||||
const clipLineSegmentToBounds = (
|
|
||||||
segment: LineSegment<GlobalPoint>,
|
|
||||||
bounds: Bounds,
|
|
||||||
): LineSegment<GlobalPoint> | null => {
|
|
||||||
const [minX, minY, maxX, maxY] = bounds;
|
|
||||||
const [[x1, y1], [x2, y2]] = segment;
|
|
||||||
const deltaX = x2 - x1;
|
|
||||||
const deltaY = y2 - y1;
|
|
||||||
let tMin = 0;
|
|
||||||
let tMax = 1;
|
|
||||||
|
|
||||||
const clip = (p: number, q: number) => {
|
|
||||||
if (p === 0) {
|
|
||||||
return q >= 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
const ratio = q / p;
|
|
||||||
|
|
||||||
if (p < 0) {
|
|
||||||
if (ratio > tMax) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
tMin = Math.max(tMin, ratio);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ratio < tMin) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
tMax = Math.min(tMax, ratio);
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (
|
|
||||||
!clip(-deltaX, x1 - minX) ||
|
|
||||||
!clip(deltaX, maxX - x1) ||
|
|
||||||
!clip(-deltaY, y1 - minY) ||
|
|
||||||
!clip(deltaY, maxY - y1)
|
|
||||||
) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return lineSegment(
|
|
||||||
pointFrom<GlobalPoint>(x1 + tMin * deltaX, y1 + tMin * deltaY),
|
|
||||||
pointFrom<GlobalPoint>(x1 + tMax * deltaX, y1 + tMax * deltaY),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const isPointWithinAabb = (point: GlobalPoint, bounds: Bounds) =>
|
|
||||||
point[0] >= bounds[0] &&
|
|
||||||
point[0] <= bounds[2] &&
|
|
||||||
point[1] >= bounds[1] &&
|
|
||||||
point[1] <= bounds[3];
|
|
||||||
|
|
||||||
const shouldUsePreciseFilledOverlap = (element: NonDeletedExcalidrawElement) =>
|
|
||||||
element.type === "ellipse" ||
|
|
||||||
element.type === "diamond" ||
|
|
||||||
(element.type === "rectangle" && !!element.roundness);
|
|
||||||
|
|
||||||
const shouldSkipElementFromSelection = (element: NonDeletedExcalidrawElement) =>
|
|
||||||
element.locked || element.type === "selection" || isBoundToContainer(element);
|
|
||||||
|
|
||||||
const getFrameBoundsForSelection = (
|
|
||||||
element: NonDeletedExcalidrawElement,
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
): Bounds | null => {
|
|
||||||
if (!element.frameId) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
const containingFrame = getContainingFrame(element, elementsMap);
|
|
||||||
return containingFrame
|
|
||||||
? (getElementBounds(containingFrame, elementsMap) as Bounds)
|
|
||||||
: null;
|
|
||||||
};
|
|
||||||
|
|
||||||
const finalizeElementsInSelection = (
|
|
||||||
elementsInSelection: NonDeletedExcalidrawElement[],
|
|
||||||
excludeElementsInFrames: boolean,
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
): NonDeletedExcalidrawElement[] => {
|
|
||||||
elementsInSelection = excludeElementsInFrames
|
|
||||||
? excludeElementsInFramesFromSelection(elementsInSelection)
|
|
||||||
: elementsInSelection;
|
|
||||||
|
|
||||||
return elementsInSelection.filter((element) => {
|
|
||||||
const containingFrame = getContainingFrame(element, elementsMap);
|
|
||||||
|
|
||||||
if (containingFrame) {
|
|
||||||
return elementOverlapsWithFrame(element, containingFrame, elementsMap);
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
const getVisibleElementOutlineSegments = (
|
|
||||||
element: NonDeletedExcalidrawElement,
|
|
||||||
frameBounds: Bounds | null,
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
) =>
|
|
||||||
frameBounds
|
|
||||||
? getElementLineSegments(element, elementsMap).flatMap((segment) => {
|
|
||||||
const clippedSegment = clipLineSegmentToBounds(segment, frameBounds);
|
|
||||||
return clippedSegment ? [clippedSegment] : [];
|
|
||||||
})
|
|
||||||
: getElementLineSegments(element, elementsMap);
|
|
||||||
|
|
||||||
const doesSelectionIntersectElementOutline = (
|
|
||||||
element: NonDeletedExcalidrawElement,
|
|
||||||
frameBounds: Bounds | null,
|
|
||||||
selectionEdges: readonly LineSegment<GlobalPoint>[],
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
) =>
|
|
||||||
selectionEdges.some((selectionEdge) =>
|
|
||||||
intersectElementWithLineSegment(
|
|
||||||
element,
|
|
||||||
elementsMap,
|
|
||||||
selectionEdge,
|
|
||||||
0,
|
|
||||||
true,
|
|
||||||
).some((point) => !frameBounds || isPointWithinAabb(point, frameBounds)),
|
|
||||||
);
|
|
||||||
|
|
||||||
const doesSelectionContainElementOutline = (
|
|
||||||
outlineSegments: readonly LineSegment<GlobalPoint>[],
|
|
||||||
selectionBounds: Bounds,
|
|
||||||
) =>
|
|
||||||
outlineSegments.length > 0 &&
|
|
||||||
outlineSegments.every(
|
|
||||||
(outlineSegment) =>
|
|
||||||
isPointWithinAabb(outlineSegment[0], selectionBounds) &&
|
|
||||||
isPointWithinAabb(outlineSegment[1], selectionBounds),
|
|
||||||
);
|
|
||||||
|
|
||||||
const doesSelectionContainElementInterior = (
|
|
||||||
element: NonDeletedExcalidrawElement,
|
|
||||||
frameBounds: Bounds | null,
|
|
||||||
selectionCorners: readonly GlobalPoint[],
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
) =>
|
|
||||||
selectionCorners.some(
|
|
||||||
(selectionCorner) =>
|
|
||||||
(!frameBounds || isPointWithinAabb(selectionCorner, frameBounds)) &&
|
|
||||||
isPointInElement(selectionCorner, element, elementsMap),
|
|
||||||
);
|
|
||||||
|
|
||||||
const doesSelectionOverlapFilledElement = (
|
|
||||||
element: NonDeletedExcalidrawElement,
|
|
||||||
frameBounds: Bounds | null,
|
|
||||||
selectionBounds: Bounds,
|
|
||||||
selectionCorners: readonly GlobalPoint[],
|
|
||||||
selectionEdges: readonly LineSegment<GlobalPoint>[],
|
|
||||||
elementsMap: ElementsMap,
|
|
||||||
) => {
|
|
||||||
if (
|
|
||||||
doesSelectionContainElementInterior(
|
|
||||||
element,
|
|
||||||
frameBounds,
|
|
||||||
selectionCorners,
|
|
||||||
elementsMap,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
doesSelectionIntersectElementOutline(
|
|
||||||
element,
|
|
||||||
frameBounds,
|
|
||||||
selectionEdges,
|
|
||||||
elementsMap,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
const outlineSegments = getVisibleElementOutlineSegments(
|
|
||||||
element,
|
|
||||||
frameBounds,
|
|
||||||
elementsMap,
|
|
||||||
);
|
|
||||||
|
|
||||||
return (
|
|
||||||
outlineSegments.length > 0 &&
|
|
||||||
doesSelectionContainElementOutline(outlineSegments, selectionBounds)
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Frames and their containing elements are not to be selected at the same time.
|
* Frames and their containing elements are not to be selected at the same time.
|
||||||
* Given an array of selected elements, if there are frames and their containing elements
|
* Given an array of selected elements, if there are frames and their containing elements
|
||||||
@@ -281,166 +62,55 @@ export const getElementsWithinSelection = (
|
|||||||
selection: NonDeletedExcalidrawElement,
|
selection: NonDeletedExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
excludeElementsInFrames: boolean = true,
|
excludeElementsInFrames: boolean = true,
|
||||||
boxSelectionMode: BoxSelectionMode = "contain",
|
) => {
|
||||||
): NonDeletedExcalidrawElement[] => {
|
const [selectionX1, selectionY1, selectionX2, selectionY2] =
|
||||||
const [selectionStartX, selectionStartY, selectionEndX, selectionEndY] =
|
|
||||||
getElementAbsoluteCoords(selection, elementsMap);
|
getElementAbsoluteCoords(selection, elementsMap);
|
||||||
const selectionX1 = Math.min(selectionStartX, selectionEndX);
|
|
||||||
const selectionY1 = Math.min(selectionStartY, selectionEndY);
|
|
||||||
const selectionX2 = Math.max(selectionStartX, selectionEndX);
|
|
||||||
const selectionY2 = Math.max(selectionStartY, selectionEndY);
|
|
||||||
const selectionBounds = [
|
|
||||||
selectionX1,
|
|
||||||
selectionY1,
|
|
||||||
selectionX2,
|
|
||||||
selectionY2,
|
|
||||||
] as Bounds;
|
|
||||||
|
|
||||||
if (boxSelectionMode !== "overlap") {
|
let elementsInSelection = elements.filter((element) => {
|
||||||
const elementsInSelection: NonDeletedExcalidrawElement[] = [];
|
let [elementX1, elementY1, elementX2, elementY2] = getElementBounds(
|
||||||
|
element,
|
||||||
for (const element of elements) {
|
|
||||||
if (shouldSkipElementFromSelection(element)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const elementBounds = getElementBounds(element, elementsMap) as Bounds;
|
|
||||||
const frameBounds = getFrameBoundsForSelection(element, elementsMap);
|
|
||||||
let elementX1 = elementBounds[0];
|
|
||||||
let elementY1 = elementBounds[1];
|
|
||||||
let elementX2 = elementBounds[2];
|
|
||||||
let elementY2 = elementBounds[3];
|
|
||||||
|
|
||||||
if (frameBounds) {
|
|
||||||
elementX1 = Math.max(frameBounds[0], elementX1);
|
|
||||||
elementY1 = Math.max(frameBounds[1], elementY1);
|
|
||||||
elementX2 = Math.min(frameBounds[2], elementX2);
|
|
||||||
elementY2 = Math.min(frameBounds[3], elementY2);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
selectionX1 <= elementX1 &&
|
|
||||||
selectionY1 <= elementY1 &&
|
|
||||||
selectionX2 >= elementX2 &&
|
|
||||||
selectionY2 >= elementY2
|
|
||||||
) {
|
|
||||||
elementsInSelection.push(element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return finalizeElementsInSelection(
|
|
||||||
elementsInSelection,
|
|
||||||
excludeElementsInFrames,
|
|
||||||
elementsMap,
|
elementsMap,
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
const selectionCorners = getBoundsCorners(selectionBounds);
|
const containingFrame = getContainingFrame(element, elementsMap);
|
||||||
const selectionEdges = getBoundsEdges(selectionCorners);
|
if (containingFrame) {
|
||||||
const elementsInSelection: NonDeletedExcalidrawElement[] = [];
|
const [fx1, fy1, fx2, fy2] = getElementBounds(
|
||||||
|
containingFrame,
|
||||||
|
elementsMap,
|
||||||
|
);
|
||||||
|
|
||||||
for (const element of elements) {
|
elementX1 = Math.max(fx1, elementX1);
|
||||||
if (shouldSkipElementFromSelection(element)) {
|
elementY1 = Math.max(fy1, elementY1);
|
||||||
continue;
|
elementX2 = Math.min(fx2, elementX2);
|
||||||
|
elementY2 = Math.min(fy2, elementY2);
|
||||||
}
|
}
|
||||||
|
|
||||||
const elementBounds = getElementBounds(element, elementsMap) as Bounds;
|
return (
|
||||||
const frameBounds = getFrameBoundsForSelection(element, elementsMap);
|
element.locked === false &&
|
||||||
let elementX1 = elementBounds[0];
|
element.type !== "selection" &&
|
||||||
let elementY1 = elementBounds[1];
|
!isBoundToContainer(element) &&
|
||||||
let elementX2 = elementBounds[2];
|
|
||||||
let elementY2 = elementBounds[3];
|
|
||||||
|
|
||||||
if (frameBounds) {
|
|
||||||
elementX1 = Math.max(frameBounds[0], elementX1);
|
|
||||||
elementY1 = Math.max(frameBounds[1], elementY1);
|
|
||||||
elementX2 = Math.min(frameBounds[2], elementX2);
|
|
||||||
elementY2 = Math.min(frameBounds[3], elementY2);
|
|
||||||
}
|
|
||||||
|
|
||||||
const isSelectionContainingElement =
|
|
||||||
selectionX1 <= elementX1 &&
|
selectionX1 <= elementX1 &&
|
||||||
selectionY1 <= elementY1 &&
|
selectionY1 <= elementY1 &&
|
||||||
selectionX2 >= elementX2 &&
|
selectionX2 >= elementX2 &&
|
||||||
selectionY2 >= elementY2;
|
selectionY2 >= elementY2
|
||||||
|
|
||||||
const isSelectionOverlappingElementAabb =
|
|
||||||
selectionX1 <= elementX2 &&
|
|
||||||
selectionY1 <= elementY2 &&
|
|
||||||
selectionX2 >= elementX1 &&
|
|
||||||
selectionY2 >= elementY1;
|
|
||||||
const isSelectionOverlappingElement = shouldUseRotatedOverlapBroadPhase(
|
|
||||||
element,
|
|
||||||
)
|
|
||||||
? isSelectionOverlappingElementAabb &&
|
|
||||||
doBoundsIntersectElementBoundingBox(
|
|
||||||
selectionBounds,
|
|
||||||
element,
|
|
||||||
elementsMap,
|
|
||||||
)
|
|
||||||
: isSelectionOverlappingElementAabb;
|
|
||||||
|
|
||||||
const shouldSelectFromInside = shouldTestInside(element);
|
|
||||||
|
|
||||||
if (shouldSelectFromInside) {
|
|
||||||
if (
|
|
||||||
isSelectionOverlappingElement &&
|
|
||||||
(!shouldUsePreciseFilledOverlap(element) ||
|
|
||||||
isSelectionContainingElement ||
|
|
||||||
doesSelectionOverlapFilledElement(
|
|
||||||
element,
|
|
||||||
frameBounds,
|
|
||||||
selectionBounds,
|
|
||||||
selectionCorners,
|
|
||||||
selectionEdges,
|
|
||||||
elementsMap,
|
|
||||||
))
|
|
||||||
) {
|
|
||||||
elementsInSelection.push(element);
|
|
||||||
}
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!isSelectionOverlappingElement) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isSelectionContainingElement) {
|
|
||||||
elementsInSelection.push(element);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
|
||||||
doesSelectionIntersectElementOutline(
|
|
||||||
element,
|
|
||||||
frameBounds,
|
|
||||||
selectionEdges,
|
|
||||||
elementsMap,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
elementsInSelection.push(element);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const outlineSegments = getVisibleElementOutlineSegments(
|
|
||||||
element,
|
|
||||||
frameBounds,
|
|
||||||
elementsMap,
|
|
||||||
);
|
);
|
||||||
|
});
|
||||||
|
|
||||||
if (
|
elementsInSelection = excludeElementsInFrames
|
||||||
outlineSegments.length > 0 &&
|
? excludeElementsInFramesFromSelection(elementsInSelection)
|
||||||
doesSelectionContainElementOutline(outlineSegments, selectionBounds)
|
: elementsInSelection;
|
||||||
) {
|
|
||||||
elementsInSelection.push(element);
|
elementsInSelection = elementsInSelection.filter((element) => {
|
||||||
|
const containingFrame = getContainingFrame(element, elementsMap);
|
||||||
|
|
||||||
|
if (containingFrame) {
|
||||||
|
return elementOverlapsWithFrame(element, containingFrame, elementsMap);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return finalizeElementsInSelection(
|
return true;
|
||||||
elementsInSelection,
|
});
|
||||||
excludeElementsInFrames,
|
|
||||||
elementsMap,
|
return elementsInSelection;
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getVisibleAndNonSelectedElements = (
|
export const getVisibleAndNonSelectedElements = (
|
||||||
|
|||||||
@@ -1,8 +1,7 @@
|
|||||||
import { arrayToMap } from "@excalidraw/common";
|
import { arrayToMap, ROUNDNESS } from "@excalidraw/common";
|
||||||
import { type GlobalPoint, type LocalPoint, pointFrom } from "@excalidraw/math";
|
import { type GlobalPoint, type LocalPoint, pointFrom } from "@excalidraw/math";
|
||||||
import { Excalidraw } from "@excalidraw/excalidraw";
|
import { Excalidraw } from "@excalidraw/excalidraw";
|
||||||
import { API } from "@excalidraw/excalidraw/tests/helpers/api";
|
import { API } from "@excalidraw/excalidraw/tests/helpers/api";
|
||||||
import { UI } from "@excalidraw/excalidraw/tests/helpers/ui";
|
|
||||||
import "@excalidraw/utils/test-utils";
|
import "@excalidraw/utils/test-utils";
|
||||||
import { render } from "@excalidraw/excalidraw/tests/test-utils";
|
import { render } from "@excalidraw/excalidraw/tests/test-utils";
|
||||||
|
|
||||||
@@ -10,29 +9,29 @@ import * as distance from "../src/distance";
|
|||||||
import { hitElementItself } from "../src/collision";
|
import { hitElementItself } from "../src/collision";
|
||||||
|
|
||||||
describe("check rotated elements can be hit:", () => {
|
describe("check rotated elements can be hit:", () => {
|
||||||
beforeEach(async () => {
|
|
||||||
localStorage.clear();
|
|
||||||
await render(<Excalidraw handleKeyboardGlobally={true} />);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("arrow", () => {
|
it("arrow", () => {
|
||||||
UI.createElement("arrow", {
|
const element = API.createElement({
|
||||||
|
type: "arrow",
|
||||||
x: 0,
|
x: 0,
|
||||||
y: 0,
|
y: 0,
|
||||||
width: 124,
|
width: 124,
|
||||||
height: 302,
|
height: 302,
|
||||||
angle: 1.8700426423973724,
|
angle: 1.8700426423973724,
|
||||||
|
roundness: { type: ROUNDNESS.PROPORTIONAL_RADIUS },
|
||||||
|
endArrowhead: "arrow",
|
||||||
points: [
|
points: [
|
||||||
[0, 0],
|
[0, 0],
|
||||||
[120, -198],
|
[120, -198],
|
||||||
[-4, -302],
|
[-4, -302],
|
||||||
] as LocalPoint[],
|
] as LocalPoint[],
|
||||||
});
|
});
|
||||||
|
const elementsMap = arrayToMap([element]);
|
||||||
|
|
||||||
const hit = hitElementItself({
|
const hit = hitElementItself({
|
||||||
point: pointFrom<GlobalPoint>(87, -67),
|
point: pointFrom<GlobalPoint>(88, -68),
|
||||||
element: window.h.elements[0],
|
element,
|
||||||
threshold: 10,
|
threshold: 10,
|
||||||
elementsMap: window.h.scene.getNonDeletedElementsMap(),
|
elementsMap,
|
||||||
});
|
});
|
||||||
expect(hit).toBe(true);
|
expect(hit).toBe(true);
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -193,7 +193,6 @@ const APP_STATE_STORAGE_CONF = (<
|
|||||||
gridModeEnabled: { browser: true, export: true, server: true },
|
gridModeEnabled: { browser: true, export: true, server: true },
|
||||||
height: { browser: false, export: false, server: false },
|
height: { browser: false, export: false, server: false },
|
||||||
isBindingEnabled: { browser: true, export: false, server: false },
|
isBindingEnabled: { browser: true, export: false, server: false },
|
||||||
boxSelectionMode: { browser: true, export: false, server: false },
|
|
||||||
bindingPreference: { browser: true, export: false, server: false },
|
bindingPreference: { browser: true, export: false, server: false },
|
||||||
isMidpointSnappingEnabled: { browser: true, export: false, server: false },
|
isMidpointSnappingEnabled: { browser: true, export: false, server: false },
|
||||||
defaultSidebarDockedPreference: {
|
defaultSidebarDockedPreference: {
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import {
|
|||||||
KEYS,
|
KEYS,
|
||||||
APP_NAME,
|
APP_NAME,
|
||||||
CURSOR_TYPE,
|
CURSOR_TYPE,
|
||||||
DEFAULT_TRANSFORM_HANDLE_SPACING,
|
|
||||||
DEFAULT_MAX_IMAGE_WIDTH_OR_HEIGHT,
|
DEFAULT_MAX_IMAGE_WIDTH_OR_HEIGHT,
|
||||||
DEFAULT_VERTICAL_ALIGN,
|
DEFAULT_VERTICAL_ALIGN,
|
||||||
DRAGGING_THRESHOLD,
|
DRAGGING_THRESHOLD,
|
||||||
@@ -246,7 +245,7 @@ import {
|
|||||||
bindOrUnbindBindingElement,
|
bindOrUnbindBindingElement,
|
||||||
mutateElement,
|
mutateElement,
|
||||||
getElementBounds,
|
getElementBounds,
|
||||||
doNonRotatedBoundsIntersect,
|
doBoundsIntersect,
|
||||||
isPointInElement,
|
isPointInElement,
|
||||||
maxBindingDistance_simple,
|
maxBindingDistance_simple,
|
||||||
convertToExcalidrawElements,
|
convertToExcalidrawElements,
|
||||||
@@ -1154,7 +1153,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
startBounds &&
|
startBounds &&
|
||||||
endBounds &&
|
endBounds &&
|
||||||
startElement.id !== endElement.id &&
|
startElement.id !== endElement.id &&
|
||||||
doNonRotatedBoundsIntersect(startBounds, endBounds)
|
doBoundsIntersect(startBounds, endBounds)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -7240,14 +7239,6 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
this.interactiveCanvas,
|
this.interactiveCanvas,
|
||||||
isTextElement(hitElement) ? CURSOR_TYPE.TEXT : CURSOR_TYPE.CROSSHAIR,
|
isTextElement(hitElement) ? CURSOR_TYPE.TEXT : CURSOR_TYPE.CROSSHAIR,
|
||||||
);
|
);
|
||||||
} else if (
|
|
||||||
!event[KEYS.CTRL_OR_CMD] &&
|
|
||||||
this.isHittingCommonBoundingBoxOfSelectedElements(
|
|
||||||
scenePointer,
|
|
||||||
selectedElements,
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
setCursor(this.interactiveCanvas, CURSOR_TYPE.MOVE);
|
|
||||||
} else if (this.state.viewModeEnabled) {
|
} else if (this.state.viewModeEnabled) {
|
||||||
setCursor(this.interactiveCanvas, CURSOR_TYPE.GRAB);
|
setCursor(this.interactiveCanvas, CURSOR_TYPE.GRAB);
|
||||||
} else if (this.state.openDialog?.name === "elementLinkSelector") {
|
} else if (this.state.openDialog?.name === "elementLinkSelector") {
|
||||||
@@ -7739,24 +7730,17 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
const hitSelectedElement =
|
const hitSelectedElement =
|
||||||
pointerDownState.hit.element &&
|
pointerDownState.hit.element &&
|
||||||
this.isASelectedElement(pointerDownState.hit.element);
|
this.isASelectedElement(pointerDownState.hit.element);
|
||||||
const shouldForceLassoReselect =
|
|
||||||
event.altKey &&
|
|
||||||
event[KEYS.CTRL_OR_CMD] &&
|
|
||||||
!pointerDownState.resize.handleType;
|
|
||||||
const shouldStartLassoSelection =
|
|
||||||
shouldForceLassoReselect ||
|
|
||||||
(!pointerDownState.hit.hasHitCommonBoundingBoxOfSelectedElements &&
|
|
||||||
!pointerDownState.resize.handleType &&
|
|
||||||
!hitSelectedElement);
|
|
||||||
|
|
||||||
if (shouldStartLassoSelection) {
|
if (
|
||||||
if (!this.lassoTrail.hasCurrentTrail) {
|
!pointerDownState.hit.hasHitCommonBoundingBoxOfSelectedElements &&
|
||||||
this.lassoTrail.startPath(
|
!pointerDownState.resize.handleType &&
|
||||||
pointerDownState.origin.x,
|
!hitSelectedElement
|
||||||
pointerDownState.origin.y,
|
) {
|
||||||
event.shiftKey,
|
this.lassoTrail.startPath(
|
||||||
);
|
pointerDownState.origin.x,
|
||||||
}
|
pointerDownState.origin.y,
|
||||||
|
event.shiftKey,
|
||||||
|
);
|
||||||
|
|
||||||
// block dragging after lasso selection on PCs until the next pointer down
|
// block dragging after lasso selection on PCs until the next pointer down
|
||||||
// (on mobile or tablet, we want to allow user to drag immediately)
|
// (on mobile or tablet, we want to allow user to drag immediately)
|
||||||
@@ -8745,14 +8729,12 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
DEFAULT_COLLISION_THRESHOLD / this.state.zoom.value,
|
DEFAULT_COLLISION_THRESHOLD / this.state.zoom.value,
|
||||||
1,
|
1,
|
||||||
);
|
);
|
||||||
const boundsPadding =
|
|
||||||
(DEFAULT_TRANSFORM_HANDLE_SPACING * 2) / this.state.zoom.value;
|
|
||||||
const [x1, y1, x2, y2] = getCommonBounds(selectedElements);
|
const [x1, y1, x2, y2] = getCommonBounds(selectedElements);
|
||||||
return (
|
return (
|
||||||
point.x > x1 - boundsPadding - threshold &&
|
point.x > x1 - threshold &&
|
||||||
point.x < x2 + boundsPadding + threshold &&
|
point.x < x2 + threshold &&
|
||||||
point.y > y1 - boundsPadding - threshold &&
|
point.y > y1 - threshold &&
|
||||||
point.y < y2 + boundsPadding + threshold
|
point.y < y2 + threshold
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10285,7 +10267,6 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
this.state.selectionElement,
|
this.state.selectionElement,
|
||||||
this.scene.getNonDeletedElementsMap(),
|
this.scene.getNonDeletedElementsMap(),
|
||||||
false,
|
false,
|
||||||
this.state.boxSelectionMode,
|
|
||||||
)
|
)
|
||||||
: [];
|
: [];
|
||||||
|
|
||||||
|
|||||||
@@ -26,16 +26,13 @@
|
|||||||
background: var(--RadioGroup-background);
|
background: var(--RadioGroup-background);
|
||||||
border: 1px solid var(--RadioGroup-border);
|
border: 1px solid var(--RadioGroup-border);
|
||||||
|
|
||||||
gap: 2px;
|
|
||||||
|
|
||||||
&__choice {
|
&__choice {
|
||||||
position: relative;
|
position: relative;
|
||||||
display: flex;
|
display: flex;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
min-width: 20px;
|
width: 32px;
|
||||||
height: 24px;
|
height: 24px;
|
||||||
padding: 0 0.375rem;
|
|
||||||
|
|
||||||
color: var(--RadioGroup-choice-color-off);
|
color: var(--RadioGroup-choice-color-off);
|
||||||
background: var(--RadioGroup-choice-background-off);
|
background: var(--RadioGroup-choice-background-off);
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
.excalidraw {
|
.excalidraw {
|
||||||
.dropdown-menu {
|
.dropdown-menu {
|
||||||
max-width: 20rem;
|
max-width: 16rem;
|
||||||
z-index: 1;
|
z-index: 1;
|
||||||
|
|
||||||
&--placement-top {
|
&--placement-top {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
import { useEditorInterface } from "../App";
|
import { useEditorInterface } from "../App";
|
||||||
import { Ellipsify } from "../Ellipsify";
|
|
||||||
import { RadioGroup } from "../RadioGroup";
|
import { RadioGroup } from "../RadioGroup";
|
||||||
|
|
||||||
type Props<T> = {
|
type Props<T> = {
|
||||||
@@ -13,7 +12,6 @@ type Props<T> = {
|
|||||||
onChange: (value: T) => void;
|
onChange: (value: T) => void;
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
name: string;
|
name: string;
|
||||||
icon?: React.ReactNode;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const DropdownMenuItemContentRadio = <T,>({
|
const DropdownMenuItemContentRadio = <T,>({
|
||||||
@@ -23,17 +21,13 @@ const DropdownMenuItemContentRadio = <T,>({
|
|||||||
choices,
|
choices,
|
||||||
children,
|
children,
|
||||||
name,
|
name,
|
||||||
icon,
|
|
||||||
}: Props<T>) => {
|
}: Props<T>) => {
|
||||||
const editorInterface = useEditorInterface();
|
const editorInterface = useEditorInterface();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<div className="dropdown-menu-item-base dropdown-menu-item-bare">
|
<div className="dropdown-menu-item-base dropdown-menu-item-bare">
|
||||||
{icon && <div className="dropdown-menu-item__icon">{icon}</div>}
|
<label className="dropdown-menu-item__text">{children}</label>
|
||||||
<label className="dropdown-menu-item__text">
|
|
||||||
<Ellipsify>{children}</Ellipsify>
|
|
||||||
</label>
|
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
name={name}
|
name={name}
|
||||||
value={value}
|
value={value}
|
||||||
|
|||||||
@@ -39,13 +39,7 @@ import DropdownMenuItemCheckbox from "../dropdownMenu/DropdownMenuItemCheckbox";
|
|||||||
import DropdownMenuItemContentRadio from "../dropdownMenu/DropdownMenuItemContentRadio";
|
import DropdownMenuItemContentRadio from "../dropdownMenu/DropdownMenuItemContentRadio";
|
||||||
import DropdownMenuItemLink from "../dropdownMenu/DropdownMenuItemLink";
|
import DropdownMenuItemLink from "../dropdownMenu/DropdownMenuItemLink";
|
||||||
import DropdownMenuSub from "../dropdownMenu/DropdownMenuSub";
|
import DropdownMenuSub from "../dropdownMenu/DropdownMenuSub";
|
||||||
import {
|
import { GithubIcon, DiscordIcon, XBrandIcon, settingsIcon } from "../icons";
|
||||||
GithubIcon,
|
|
||||||
DiscordIcon,
|
|
||||||
XBrandIcon,
|
|
||||||
settingsIcon,
|
|
||||||
emptyIcon,
|
|
||||||
} from "../icons";
|
|
||||||
import {
|
import {
|
||||||
boltIcon,
|
boltIcon,
|
||||||
DeviceDesktopIcon,
|
DeviceDesktopIcon,
|
||||||
@@ -433,40 +427,6 @@ const PreferencesToggleToolLockItem = () => {
|
|||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
const PreferencesBoxSelectionModeItem = () => {
|
|
||||||
const { t } = useI18n();
|
|
||||||
const appState = useUIAppState();
|
|
||||||
const setAppState = useExcalidrawSetAppState();
|
|
||||||
const boxSelectionMode = appState.boxSelectionMode ?? "contain";
|
|
||||||
|
|
||||||
return (
|
|
||||||
<DropdownMenuItemContentRadio<"contain" | "overlap">
|
|
||||||
name="boxSelectionMode"
|
|
||||||
icon={emptyIcon}
|
|
||||||
value={boxSelectionMode}
|
|
||||||
onChange={(value) => {
|
|
||||||
setAppState({
|
|
||||||
boxSelectionMode: value === "contain" ? undefined : value,
|
|
||||||
});
|
|
||||||
}}
|
|
||||||
choices={[
|
|
||||||
{
|
|
||||||
value: "contain",
|
|
||||||
label: t("labels.boxSelectionContain"),
|
|
||||||
ariaLabel: t("labels.boxSelectionContain"),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
value: "overlap",
|
|
||||||
label: t("labels.boxSelectionOverlap"),
|
|
||||||
ariaLabel: t("labels.boxSelectionOverlap"),
|
|
||||||
},
|
|
||||||
]}
|
|
||||||
>
|
|
||||||
{t("labels.boxSelectionMode")}
|
|
||||||
</DropdownMenuItemContentRadio>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
const PreferencesToggleSnapModeItem = () => {
|
const PreferencesToggleSnapModeItem = () => {
|
||||||
const { t } = useI18n();
|
const { t } = useI18n();
|
||||||
const actionManager = useExcalidrawActionManager();
|
const actionManager = useExcalidrawActionManager();
|
||||||
@@ -608,7 +568,6 @@ export const Preferences = ({
|
|||||||
<DropdownMenuSub.Content className="excalidraw-main-menu-preferences-submenu">
|
<DropdownMenuSub.Content className="excalidraw-main-menu-preferences-submenu">
|
||||||
{children || (
|
{children || (
|
||||||
<>
|
<>
|
||||||
<PreferencesBoxSelectionModeItem />
|
|
||||||
<PreferencesToggleToolLockItem />
|
<PreferencesToggleToolLockItem />
|
||||||
<PreferencesToggleSnapModeItem />
|
<PreferencesToggleSnapModeItem />
|
||||||
<PreferencesToggleGridModeItem />
|
<PreferencesToggleGridModeItem />
|
||||||
@@ -626,7 +585,6 @@ export const Preferences = ({
|
|||||||
};
|
};
|
||||||
|
|
||||||
Preferences.ToggleToolLock = PreferencesToggleToolLockItem;
|
Preferences.ToggleToolLock = PreferencesToggleToolLockItem;
|
||||||
Preferences.BoxSelectionMode = PreferencesBoxSelectionModeItem;
|
|
||||||
Preferences.ToggleSnapMode = PreferencesToggleSnapModeItem;
|
Preferences.ToggleSnapMode = PreferencesToggleSnapModeItem;
|
||||||
Preferences.ToggleArrowBinding = PreferencesToggleArrowBindingItem;
|
Preferences.ToggleArrowBinding = PreferencesToggleArrowBindingItem;
|
||||||
Preferences.ToggleMidpointSnapping = PreferencesToggleMidpointSnappingItem;
|
Preferences.ToggleMidpointSnapping = PreferencesToggleMidpointSnappingItem;
|
||||||
|
|||||||
@@ -936,12 +936,6 @@ export const restoreAppState = (
|
|||||||
: defaultValue;
|
: defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const boxSelectionMode =
|
|
||||||
appState.boxSelectionMode ?? localAppState?.boxSelectionMode;
|
|
||||||
if (boxSelectionMode !== undefined) {
|
|
||||||
nextAppState.boxSelectionMode = boxSelectionMode;
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
...nextAppState,
|
...nextAppState,
|
||||||
cursorButton: localAppState?.cursorButton || "up",
|
cursorButton: localAppState?.cursorButton || "up",
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { arrayToMap, easeOut, THEME } from "@excalidraw/common";
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
computeBoundTextPosition,
|
computeBoundTextPosition,
|
||||||
doNonRotatedBoundsIntersect,
|
doBoundsIntersect,
|
||||||
getBoundTextElement,
|
getBoundTextElement,
|
||||||
getElementBounds,
|
getElementBounds,
|
||||||
getElementLineSegments,
|
getElementLineSegments,
|
||||||
@@ -219,7 +219,7 @@ const eraserTest = (
|
|||||||
origElementBounds[3] + threshold,
|
origElementBounds[3] + threshold,
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!doNonRotatedBoundsIntersect(segmentBounds, elementBounds)) {
|
if (!doBoundsIntersect(segmentBounds, elementBounds)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { type Bounds } from "@excalidraw/common";
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
computeBoundTextPosition,
|
computeBoundTextPosition,
|
||||||
doNonRotatedBoundsIntersect,
|
doBoundsIntersect,
|
||||||
getBoundTextElement,
|
getBoundTextElement,
|
||||||
getElementBounds,
|
getElementBounds,
|
||||||
intersectElementWithLineSegment,
|
intersectElementWithLineSegment,
|
||||||
@@ -66,7 +66,7 @@ export const getLassoSelectedElementIds = (input: {
|
|||||||
const elementBounds = getElementBounds(element, elementsMap);
|
const elementBounds = getElementBounds(element, elementsMap);
|
||||||
|
|
||||||
if (
|
if (
|
||||||
doNonRotatedBoundsIntersect(lassoBounds, elementBounds) &&
|
doBoundsIntersect(lassoBounds, elementBounds) &&
|
||||||
!intersectedElements.has(element.id) &&
|
!intersectedElements.has(element.id) &&
|
||||||
!enclosedElements.has(element.id)
|
!enclosedElements.has(element.id)
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -185,9 +185,6 @@
|
|||||||
"shapeSwitch": "Switch shape",
|
"shapeSwitch": "Switch shape",
|
||||||
"preferences": "Preferences",
|
"preferences": "Preferences",
|
||||||
"preferences_toolLock": "Tool lock",
|
"preferences_toolLock": "Tool lock",
|
||||||
"boxSelectionMode": "Select on",
|
|
||||||
"boxSelectionContain": "Wrap",
|
|
||||||
"boxSelectionOverlap": "Overlap",
|
|
||||||
"arrowBinding": "Arrow binding",
|
"arrowBinding": "Arrow binding",
|
||||||
"midpointSnapping": "Snap to midpoints"
|
"midpointSnapping": "Snap to midpoints"
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -4358,7 +4358,7 @@ exports[`contextMenu element > selecting 'Ungroup selection' in context menu ung
|
|||||||
"type": "rectangle",
|
"type": "rectangle",
|
||||||
"updated": 1,
|
"updated": 1,
|
||||||
"version": 5,
|
"version": 5,
|
||||||
"versionNonce": 1006504105,
|
"versionNonce": 760410951,
|
||||||
"width": 20,
|
"width": 20,
|
||||||
"x": -10,
|
"x": -10,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
@@ -4383,14 +4383,14 @@ exports[`contextMenu element > selecting 'Ungroup selection' in context menu ung
|
|||||||
"opacity": 100,
|
"opacity": 100,
|
||||||
"roughness": 1,
|
"roughness": 1,
|
||||||
"roundness": null,
|
"roundness": null,
|
||||||
"seed": 400692809,
|
"seed": 238820263,
|
||||||
"strokeColor": "#1e1e1e",
|
"strokeColor": "#1e1e1e",
|
||||||
"strokeStyle": "solid",
|
"strokeStyle": "solid",
|
||||||
"strokeWidth": 2,
|
"strokeWidth": 2,
|
||||||
"type": "rectangle",
|
"type": "rectangle",
|
||||||
"updated": 1,
|
"updated": 1,
|
||||||
"version": 5,
|
"version": 5,
|
||||||
"versionNonce": 289600103,
|
"versionNonce": 1006504105,
|
||||||
"width": 20,
|
"width": 20,
|
||||||
"x": 20,
|
"x": 20,
|
||||||
"y": 30,
|
"y": 30,
|
||||||
@@ -6864,7 +6864,7 @@ exports[`contextMenu element > shows 'Ungroup selection' in context menu for gro
|
|||||||
"type": "rectangle",
|
"type": "rectangle",
|
||||||
"updated": 1,
|
"updated": 1,
|
||||||
"version": 4,
|
"version": 4,
|
||||||
"versionNonce": 1723083209,
|
"versionNonce": 747212839,
|
||||||
"width": 10,
|
"width": 10,
|
||||||
"x": -10,
|
"x": -10,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
@@ -6891,14 +6891,14 @@ exports[`contextMenu element > shows 'Ungroup selection' in context menu for gro
|
|||||||
"opacity": 100,
|
"opacity": 100,
|
||||||
"roughness": 1,
|
"roughness": 1,
|
||||||
"roundness": null,
|
"roundness": null,
|
||||||
"seed": 400692809,
|
"seed": 238820263,
|
||||||
"strokeColor": "#1e1e1e",
|
"strokeColor": "#1e1e1e",
|
||||||
"strokeStyle": "solid",
|
"strokeStyle": "solid",
|
||||||
"strokeWidth": 2,
|
"strokeWidth": 2,
|
||||||
"type": "rectangle",
|
"type": "rectangle",
|
||||||
"updated": 1,
|
"updated": 1,
|
||||||
"version": 4,
|
"version": 4,
|
||||||
"versionNonce": 760410951,
|
"versionNonce": 1723083209,
|
||||||
"width": 10,
|
"width": 10,
|
||||||
"x": 12,
|
"x": 12,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
|
|||||||
@@ -1,9 +1,7 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { vi } from "vitest";
|
import { vi } from "vitest";
|
||||||
|
|
||||||
import { KEYS, ROUNDNESS, reseed } from "@excalidraw/common";
|
import { KEYS, reseed } from "@excalidraw/common";
|
||||||
import { getElementBounds, getElementLineSegments } from "@excalidraw/element";
|
|
||||||
import { pointFrom, pointRotateRads, type LocalPoint } from "@excalidraw/math";
|
|
||||||
|
|
||||||
import { SHAPES } from "../components/shapes";
|
import { SHAPES } from "../components/shapes";
|
||||||
|
|
||||||
@@ -14,7 +12,6 @@ import * as StaticScene from "../renderer/staticScene";
|
|||||||
import { API } from "./helpers/api";
|
import { API } from "./helpers/api";
|
||||||
import { Keyboard, Pointer, UI } from "./helpers/ui";
|
import { Keyboard, Pointer, UI } from "./helpers/ui";
|
||||||
import {
|
import {
|
||||||
act,
|
|
||||||
render,
|
render,
|
||||||
fireEvent,
|
fireEvent,
|
||||||
mockBoundingClientRect,
|
mockBoundingClientRect,
|
||||||
@@ -42,19 +39,6 @@ const { h } = window;
|
|||||||
|
|
||||||
const mouse = new Pointer("mouse");
|
const mouse = new Pointer("mouse");
|
||||||
|
|
||||||
const getOutlineBounds = (element: ReturnType<typeof API.createElement>) => {
|
|
||||||
const sceneElement = API.getElement(element);
|
|
||||||
const elementsMap = h.scene.getNonDeletedElementsMap();
|
|
||||||
const points = getElementLineSegments(sceneElement, elementsMap).flat();
|
|
||||||
|
|
||||||
return [
|
|
||||||
Math.min(...points.map((point) => point[0])),
|
|
||||||
Math.min(...points.map((point) => point[1])),
|
|
||||||
Math.max(...points.map((point) => point[0])),
|
|
||||||
Math.max(...points.map((point) => point[1])),
|
|
||||||
] as const;
|
|
||||||
};
|
|
||||||
|
|
||||||
describe("box-selection", () => {
|
describe("box-selection", () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await render(<Excalidraw />);
|
await render(<Excalidraw />);
|
||||||
@@ -124,497 +108,6 @@ describe("box-selection", () => {
|
|||||||
|
|
||||||
assertSelectedElements([]);
|
assertSelectedElements([]);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not select an element when the selection box only partially overlaps it", () => {
|
|
||||||
const rect1 = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 50,
|
|
||||||
height: 50,
|
|
||||||
backgroundColor: "red",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rect1]);
|
|
||||||
|
|
||||||
mouse.downAt(25, -20);
|
|
||||||
mouse.move(-1000, -1000);
|
|
||||||
mouse.moveTo(75, 70);
|
|
||||||
mouse.up();
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("lasso reselection", () => {
|
|
||||||
beforeEach(async () => {
|
|
||||||
await render(<Excalidraw />);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should allow ctrl+alt lasso reselection when starting inside the active common bounds", () => {
|
|
||||||
const rectA = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
backgroundColor: "red",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
const rectB = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 220,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
backgroundColor: "blue",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rectA, rectB]);
|
|
||||||
mouse.select([rectA, rectB]);
|
|
||||||
act(() => {
|
|
||||||
h.app.setActiveTool({ type: "lasso" });
|
|
||||||
});
|
|
||||||
|
|
||||||
Keyboard.withModifierKeys({ ctrl: true, alt: true }, () => {
|
|
||||||
mouse.downAt(110, 50);
|
|
||||||
mouse.moveTo(50, -20);
|
|
||||||
|
|
||||||
expect(h.app.lassoTrail.hasCurrentTrail).toBe(true);
|
|
||||||
|
|
||||||
mouse.moveTo(-20, 50);
|
|
||||||
mouse.moveTo(50, 120);
|
|
||||||
mouse.moveTo(110, 50);
|
|
||||||
mouse.up();
|
|
||||||
});
|
|
||||||
|
|
||||||
assertSelectedElements([rectA.id]);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("box-selection overlap mode", () => {
|
|
||||||
const boxSelect = (
|
|
||||||
startX: number,
|
|
||||||
startY: number,
|
|
||||||
endX: number,
|
|
||||||
endY: number,
|
|
||||||
) => {
|
|
||||||
mouse.downAt(startX, startY);
|
|
||||||
mouse.move(-1000, -1000);
|
|
||||||
mouse.moveTo(endX, endY);
|
|
||||||
mouse.up();
|
|
||||||
};
|
|
||||||
|
|
||||||
const boxSelectTopLeftAabbCorner = (
|
|
||||||
element: ReturnType<typeof API.createElement>,
|
|
||||||
) => {
|
|
||||||
const sceneElement = API.getElement(element);
|
|
||||||
const elementsMap = h.scene.getNonDeletedElementsMap();
|
|
||||||
const [x1, y1] = getElementBounds(sceneElement, elementsMap);
|
|
||||||
|
|
||||||
boxSelect(x1 + 2, y1 + 2, x1 + 12, y1 + 12);
|
|
||||||
};
|
|
||||||
|
|
||||||
const boxSelectTopRightAabbCorner = (
|
|
||||||
element: ReturnType<typeof API.createElement>,
|
|
||||||
) => {
|
|
||||||
const sceneElement = API.getElement(element);
|
|
||||||
const elementsMap = h.scene.getNonDeletedElementsMap();
|
|
||||||
const [, y1, x2] = getElementBounds(sceneElement, elementsMap);
|
|
||||||
|
|
||||||
boxSelect(x2 - 12, y1 + 2, x2 - 2, y1 + 12);
|
|
||||||
};
|
|
||||||
|
|
||||||
const boxSelectTopLeftRotatedLocalBoundsCorner = (
|
|
||||||
element: ReturnType<typeof API.createElement>,
|
|
||||||
) => {
|
|
||||||
const sceneElement = API.getElement(element);
|
|
||||||
const elementsMap = h.scene.getNonDeletedElementsMap();
|
|
||||||
const [x1, y1, x2, y2] = getElementBounds(sceneElement, elementsMap, true);
|
|
||||||
const center = pointFrom((x1 + x2) / 2, (y1 + y2) / 2);
|
|
||||||
const [cornerX, cornerY] = pointRotateRads(
|
|
||||||
pointFrom(x1, y1),
|
|
||||||
center,
|
|
||||||
sceneElement.angle,
|
|
||||||
);
|
|
||||||
|
|
||||||
boxSelect(cornerX - 4, cornerY - 4, cornerX + 4, cornerY + 4);
|
|
||||||
};
|
|
||||||
|
|
||||||
beforeEach(async () => {
|
|
||||||
await render(
|
|
||||||
<Excalidraw
|
|
||||||
initialData={{ appState: { boxSelectionMode: "overlap" } }}
|
|
||||||
/>,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should select an element when the selection box partially overlaps it", () => {
|
|
||||||
const rect1 = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 50,
|
|
||||||
height: 50,
|
|
||||||
backgroundColor: "red",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rect1]);
|
|
||||||
|
|
||||||
boxSelect(25, -20, 75, 70);
|
|
||||||
|
|
||||||
assertSelectedElements([rect1.id]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a transparent rectangle when the selection box stays inside it", () => {
|
|
||||||
const rect1 = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
backgroundColor: "transparent",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rect1]);
|
|
||||||
|
|
||||||
boxSelect(25, 25, 75, 75);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should select a transparent rectangle when the selection box crosses its outline", () => {
|
|
||||||
const rect1 = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
backgroundColor: "transparent",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rect1]);
|
|
||||||
|
|
||||||
boxSelect(25, 25, 125, 75);
|
|
||||||
|
|
||||||
assertSelectedElements([rect1.id]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a rotated transparent rectangle when the selection box stays inside it", () => {
|
|
||||||
const rect1 = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
angle: Math.PI / 4,
|
|
||||||
backgroundColor: "transparent",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rect1]);
|
|
||||||
|
|
||||||
boxSelect(40, 40, 60, 60);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should select a rotated rounded rectangle when the selection box contains its outline but not its bounds", () => {
|
|
||||||
const rect = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 180,
|
|
||||||
angle: Math.PI / 6,
|
|
||||||
backgroundColor: "transparent",
|
|
||||||
fillStyle: "solid",
|
|
||||||
roundness: { type: ROUNDNESS.ADAPTIVE_RADIUS },
|
|
||||||
roughness: 0,
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rect]);
|
|
||||||
|
|
||||||
const sceneRect = API.getElement(rect);
|
|
||||||
const elementsMap = h.scene.getNonDeletedElementsMap();
|
|
||||||
const [boundsX1, boundsY1, boundsX2, boundsY2] = getElementBounds(
|
|
||||||
sceneRect,
|
|
||||||
elementsMap,
|
|
||||||
);
|
|
||||||
const [outlineX1, outlineY1, outlineX2, outlineY2] = getOutlineBounds(rect);
|
|
||||||
|
|
||||||
expect(outlineX1).toBeGreaterThan(boundsX1);
|
|
||||||
expect(outlineY1).toBeGreaterThan(boundsY1);
|
|
||||||
expect(outlineX2).toBeLessThan(boundsX2);
|
|
||||||
expect(outlineY2).toBeLessThan(boundsY2);
|
|
||||||
|
|
||||||
boxSelect(
|
|
||||||
outlineX1 - (outlineX1 - boundsX1) / 2,
|
|
||||||
outlineY1 - (outlineY1 - boundsY1) / 2,
|
|
||||||
outlineX2 + (boundsX2 - outlineX2) / 2,
|
|
||||||
outlineY2 + (boundsY2 - outlineY2) / 2,
|
|
||||||
);
|
|
||||||
|
|
||||||
assertSelectedElements([rect.id]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a filled rotated rectangle when the selection box only overlaps its axis-aligned bounds", () => {
|
|
||||||
const rect = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
angle: Math.PI / 4,
|
|
||||||
backgroundColor: "red",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rect]);
|
|
||||||
|
|
||||||
boxSelectTopLeftAabbCorner(rect);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a filled ellipse when the selection box only overlaps its bounds corner", () => {
|
|
||||||
const ellipse = API.createElement({
|
|
||||||
type: "ellipse",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
backgroundColor: "red",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([ellipse]);
|
|
||||||
|
|
||||||
boxSelectTopRightAabbCorner(ellipse);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a filled diamond when the selection box only overlaps its bounds corner", () => {
|
|
||||||
const diamond = API.createElement({
|
|
||||||
type: "diamond",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
backgroundColor: "red",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([diamond]);
|
|
||||||
|
|
||||||
boxSelectTopRightAabbCorner(diamond);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a filled rotated ellipse when the selection box only overlaps its axis-aligned bounds", () => {
|
|
||||||
const ellipse = API.createElement({
|
|
||||||
type: "ellipse",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
angle: Math.PI / 4,
|
|
||||||
backgroundColor: "red",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([ellipse]);
|
|
||||||
|
|
||||||
boxSelectTopLeftRotatedLocalBoundsCorner(ellipse);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a filled rotated diamond when the selection box only overlaps its rotated local bounds", () => {
|
|
||||||
const diamond = API.createElement({
|
|
||||||
type: "diamond",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
angle: Math.PI / 4,
|
|
||||||
backgroundColor: "red",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([diamond]);
|
|
||||||
|
|
||||||
boxSelectTopLeftRotatedLocalBoundsCorner(diamond);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select rotated text when the selection box only overlaps its axis-aligned bounds", () => {
|
|
||||||
const text = API.createElement({
|
|
||||||
type: "text",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
angle: Math.PI / 4,
|
|
||||||
text: "test",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([text]);
|
|
||||||
|
|
||||||
boxSelect(-18, -18, -8, -8);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select rotated image when the selection box only overlaps its axis-aligned bounds", () => {
|
|
||||||
const image = API.createElement({
|
|
||||||
type: "image",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
angle: Math.PI / 4,
|
|
||||||
fileId: "file_A",
|
|
||||||
status: "saved",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([image]);
|
|
||||||
|
|
||||||
boxSelect(-18, -18, -8, -8);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should deselect a selected rotated rectangle when clicking in the empty corner of its axis-aligned bounds", () => {
|
|
||||||
const rect = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
angle: Math.PI / 4,
|
|
||||||
backgroundColor: "red",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([rect]);
|
|
||||||
|
|
||||||
mouse.clickAt(50, 50);
|
|
||||||
assertSelectedElements([rect.id]);
|
|
||||||
|
|
||||||
const sceneRect = API.getElement(rect);
|
|
||||||
const elementsMap = h.scene.getNonDeletedElementsMap();
|
|
||||||
const [x1, y1] = getElementBounds(sceneRect, elementsMap);
|
|
||||||
|
|
||||||
mouse.clickAt(x1 + 2, y1 + 2);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a line when the selection box only overlaps its bounds", () => {
|
|
||||||
const line = API.createElement({
|
|
||||||
type: "line",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
backgroundColor: "transparent",
|
|
||||||
points: [pointFrom<LocalPoint>(0, 0), pointFrom<LocalPoint>(100, 100)],
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([line]);
|
|
||||||
|
|
||||||
boxSelect(20, 50, 30, 60);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not click-select rotated freedraw in the corner of its axis-aligned bounds", () => {
|
|
||||||
const freedraw = API.createElement({
|
|
||||||
type: "freedraw",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
angle: Math.PI / 4,
|
|
||||||
backgroundColor: "transparent",
|
|
||||||
points: [
|
|
||||||
pointFrom<LocalPoint>(0, 0),
|
|
||||||
pointFrom<LocalPoint>(100, 0),
|
|
||||||
pointFrom<LocalPoint>(100, 100),
|
|
||||||
pointFrom<LocalPoint>(0, 100),
|
|
||||||
pointFrom<LocalPoint>(0, 0),
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([freedraw]);
|
|
||||||
|
|
||||||
const sceneFreedraw = API.getElement(freedraw);
|
|
||||||
const elementsMap = h.scene.getNonDeletedElementsMap();
|
|
||||||
const [x1, y1] = getElementBounds(sceneFreedraw, elementsMap);
|
|
||||||
|
|
||||||
mouse.clickAt(x1 + 2, y1 + 2);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a freedraw when the selection box only overlaps its bounds", () => {
|
|
||||||
const freedraw = API.createElement({
|
|
||||||
type: "freedraw",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
backgroundColor: "transparent",
|
|
||||||
points: [
|
|
||||||
pointFrom<LocalPoint>(0, 0),
|
|
||||||
pointFrom<LocalPoint>(50, 50),
|
|
||||||
pointFrom<LocalPoint>(100, 100),
|
|
||||||
],
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([freedraw]);
|
|
||||||
|
|
||||||
boxSelect(20, 50, 30, 60);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("should not select a transparent framed element when the selection box stays inside its clipped bounds", () => {
|
|
||||||
const frame = API.createElement({
|
|
||||||
type: "frame",
|
|
||||||
x: 0,
|
|
||||||
y: 0,
|
|
||||||
width: 100,
|
|
||||||
height: 100,
|
|
||||||
backgroundColor: "transparent",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
const rect1 = API.createElement({
|
|
||||||
type: "rectangle",
|
|
||||||
x: 50,
|
|
||||||
y: 10,
|
|
||||||
width: 100,
|
|
||||||
height: 80,
|
|
||||||
frameId: frame.id,
|
|
||||||
backgroundColor: "transparent",
|
|
||||||
fillStyle: "solid",
|
|
||||||
});
|
|
||||||
|
|
||||||
API.setElements([frame, rect1]);
|
|
||||||
|
|
||||||
boxSelect(60, 20, 90, 60);
|
|
||||||
|
|
||||||
assertSelectedElements([]);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("inner box-selection", () => {
|
describe("inner box-selection", () => {
|
||||||
|
|||||||
@@ -269,8 +269,6 @@ export type ObservedElementsAppState = {
|
|||||||
activeLockedId: AppState["activeLockedId"];
|
activeLockedId: AppState["activeLockedId"];
|
||||||
};
|
};
|
||||||
|
|
||||||
export type BoxSelectionMode = "contain" | "overlap";
|
|
||||||
|
|
||||||
export interface AppState {
|
export interface AppState {
|
||||||
contextMenu: {
|
contextMenu: {
|
||||||
items: ContextMenuItems;
|
items: ContextMenuItems;
|
||||||
@@ -309,8 +307,6 @@ export interface AppState {
|
|||||||
* `bindingPreference` and keyboard modifiers (ctrl/alt)
|
* `bindingPreference` and keyboard modifiers (ctrl/alt)
|
||||||
*/
|
*/
|
||||||
isBindingEnabled: boolean;
|
isBindingEnabled: boolean;
|
||||||
/** user box selection preference; defaults to "contain" when unset */
|
|
||||||
boxSelectionMode?: BoxSelectionMode;
|
|
||||||
/** user arrow binding preference */
|
/** user arrow binding preference */
|
||||||
bindingPreference: "enabled" | "disabled";
|
bindingPreference: "enabled" | "disabled";
|
||||||
/** user preference whether arrow snap to midpoints while binding */
|
/** user preference whether arrow snap to midpoints while binding */
|
||||||
|
|||||||
@@ -1,14 +1,3 @@
|
|||||||
//
|
|
||||||
// Generic markers
|
|
||||||
//
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Can be used for any type of point-likes to mark them as rotated to enlist
|
|
||||||
* the type checker to weed out subtle bugs due to rotated and non-rotated
|
|
||||||
* versions of the same data point.
|
|
||||||
*/
|
|
||||||
export type Rotated<T> = T & { _brand_rotated: "excalimath_rotated" };
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// Measurements
|
// Measurements
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -1,9 +1,4 @@
|
|||||||
import {
|
import { arrayToMap, type Bounds } from "@excalidraw/common";
|
||||||
arrayToMap,
|
|
||||||
bounds,
|
|
||||||
type RotatedBounds,
|
|
||||||
type Bounds,
|
|
||||||
} from "@excalidraw/common";
|
|
||||||
import { getElementBounds } from "@excalidraw/element";
|
import { getElementBounds } from "@excalidraw/element";
|
||||||
import {
|
import {
|
||||||
isArrowElement,
|
isArrowElement,
|
||||||
@@ -95,7 +90,7 @@ const getMinMaxPoints = (points: Points) => {
|
|||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getRotatedBBox = (element: Element): RotatedBounds => {
|
const getRotatedBBox = (element: Element): Bounds => {
|
||||||
const points = getElementRelativePoints(element);
|
const points = getElementRelativePoints(element);
|
||||||
|
|
||||||
const { cx, cy } = getMinMaxPoints(points);
|
const { cx, cy } = getMinMaxPoints(points);
|
||||||
@@ -106,13 +101,12 @@ const getRotatedBBox = (element: Element): RotatedBounds => {
|
|||||||
);
|
);
|
||||||
const { minX, minY, maxX, maxY } = getMinMaxPoints(rotatedPoints);
|
const { minX, minY, maxX, maxY } = getMinMaxPoints(rotatedPoints);
|
||||||
|
|
||||||
return bounds(
|
return [
|
||||||
minX + element.x,
|
minX + element.x,
|
||||||
minY + element.y,
|
minY + element.y,
|
||||||
maxX + element.x,
|
maxX + element.x,
|
||||||
maxY + element.y,
|
maxY + element.y,
|
||||||
element.angle,
|
];
|
||||||
);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const isElementInsideBBox = (
|
export const isElementInsideBBox = (
|
||||||
@@ -166,12 +160,12 @@ export const elementPartiallyOverlapsWithOrContainsBBox = (
|
|||||||
|
|
||||||
export const elementsOverlappingBBox = ({
|
export const elementsOverlappingBBox = ({
|
||||||
elements,
|
elements,
|
||||||
bounds: _bounds,
|
bounds,
|
||||||
type,
|
type,
|
||||||
errorMargin = 0,
|
errorMargin = 0,
|
||||||
}: {
|
}: {
|
||||||
elements: Elements;
|
elements: Elements;
|
||||||
bounds: RotatedBounds | ExcalidrawElement;
|
bounds: Bounds | ExcalidrawElement;
|
||||||
/** safety offset. Defaults to 0. */
|
/** safety offset. Defaults to 0. */
|
||||||
errorMargin?: number;
|
errorMargin?: number;
|
||||||
/**
|
/**
|
||||||
@@ -181,16 +175,15 @@ export const elementsOverlappingBBox = ({
|
|||||||
**/
|
**/
|
||||||
type: "overlap" | "contain" | "inside";
|
type: "overlap" | "contain" | "inside";
|
||||||
}) => {
|
}) => {
|
||||||
if (isExcalidrawElement(_bounds)) {
|
if (isExcalidrawElement(bounds)) {
|
||||||
_bounds = getElementBounds(_bounds, arrayToMap(elements));
|
bounds = getElementBounds(bounds, arrayToMap(elements));
|
||||||
}
|
}
|
||||||
const adjustedBBox = bounds(
|
const adjustedBBox: Bounds = [
|
||||||
_bounds[0] - errorMargin,
|
bounds[0] - errorMargin,
|
||||||
_bounds[1] - errorMargin,
|
bounds[1] - errorMargin,
|
||||||
_bounds[2] + errorMargin,
|
bounds[2] + errorMargin,
|
||||||
_bounds[3] + errorMargin,
|
bounds[3] + errorMargin,
|
||||||
_bounds[4],
|
];
|
||||||
);
|
|
||||||
|
|
||||||
const includedElementSet = new Set<string>();
|
const includedElementSet = new Set<string>();
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user