Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f3dba020b0 | |||
| 7c766bdfc4 | |||
| 23959099e1 | |||
| 1e61f1c66a | |||
| 22b0f1f918 | |||
| 379dba47aa |
@@ -1,3 +1,5 @@
|
|||||||
|
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
|
||||||
*/
|
*/
|
||||||
@@ -6,7 +8,31 @@ 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) &&
|
||||||
@@ -15,3 +41,12 @@ 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,9 +2,11 @@ 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";
|
||||||
|
|
||||||
@@ -90,7 +92,7 @@ export class ElementBounds {
|
|||||||
private static boundsCache = new WeakMap<
|
private static boundsCache = new WeakMap<
|
||||||
ExcalidrawElement,
|
ExcalidrawElement,
|
||||||
{
|
{
|
||||||
bounds: Bounds;
|
bounds: RotatedBounds;
|
||||||
version: ExcalidrawElement["version"];
|
version: ExcalidrawElement["version"];
|
||||||
}
|
}
|
||||||
>();
|
>();
|
||||||
@@ -102,11 +104,11 @@ export class ElementBounds {
|
|||||||
}
|
}
|
||||||
>();
|
>();
|
||||||
|
|
||||||
static getBounds(
|
static getBounds<T extends boolean = false>(
|
||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
nonRotated: boolean = false,
|
nonRotated: T = false as T,
|
||||||
) {
|
): 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)
|
||||||
@@ -119,40 +121,41 @@ 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;
|
return cachedBounds.bounds as T extends true ? Bounds : RotatedBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (nonRotated && element.angle !== 0) {
|
if (nonRotated && element.angle !== 0) {
|
||||||
const nonRotatedBounds = ElementBounds.calculateBounds(
|
const [minX, minY, maxX, maxY] = 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;
|
return nonRotatedBounds as T extends true ? Bounds : RotatedBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
return _bounds as T extends true ? Bounds : RotatedBounds;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static calculateBounds(
|
private static calculateBounds(
|
||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
): Bounds {
|
): RotatedBounds {
|
||||||
let bounds: Bounds;
|
let _bounds: RotatedBounds;
|
||||||
|
|
||||||
const [x1, y1, x2, y2, cx, cy] = getElementAbsoluteCoords(
|
const [x1, y1, x2, y2, cx, cy] = getElementAbsoluteCoords(
|
||||||
element,
|
element,
|
||||||
@@ -169,14 +172,15 @@ export class ElementBounds {
|
|||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
return [
|
return bounds(
|
||||||
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),
|
||||||
@@ -202,7 +206,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 = [minX, minY, maxX, maxY];
|
_bounds = bounds(minX, minY, maxX, maxY, element.angle);
|
||||||
} 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;
|
||||||
@@ -210,7 +214,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 = [cx - ww, cy - hh, cx + ww, cy + hh];
|
_bounds = bounds(cx - ww, cy - hh, cx + ww, cy + hh, element.angle);
|
||||||
} else {
|
} else {
|
||||||
const [x11, y11] = pointRotateRads(
|
const [x11, y11] = pointRotateRads(
|
||||||
pointFrom(x1, y1),
|
pointFrom(x1, y1),
|
||||||
@@ -236,10 +240,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 = [minX, minY, maxX, maxY];
|
_bounds = bounds(minX, minY, maxX, maxY, element.angle);
|
||||||
}
|
}
|
||||||
|
|
||||||
return bounds;
|
return _bounds;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -624,7 +628,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 [minX, minY, maxX, maxY];
|
return bounds(minX, minY, maxX, maxY);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getMinMaxXYFromCurvePathOps = (
|
export const getMinMaxXYFromCurvePathOps = (
|
||||||
@@ -677,7 +681,7 @@ export const getMinMaxXYFromCurvePathOps = (
|
|||||||
},
|
},
|
||||||
{ minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity },
|
{ minX: Infinity, minY: Infinity, maxX: -Infinity, maxY: -Infinity },
|
||||||
);
|
);
|
||||||
return [minX, minY, maxX, maxY];
|
return bounds(minX, minY, maxX, maxY);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getBoundsFromPoints = (
|
export const getBoundsFromPoints = (
|
||||||
@@ -695,7 +699,7 @@ export const getBoundsFromPoints = (
|
|||||||
maxY = Math.max(maxY, y);
|
maxY = Math.max(maxY, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
return [minX, minY, maxX, maxY];
|
return bounds(minX, minY, maxX, maxY);
|
||||||
};
|
};
|
||||||
|
|
||||||
const getFreeDrawElementAbsoluteCoords = (
|
const getFreeDrawElementAbsoluteCoords = (
|
||||||
@@ -938,7 +942,7 @@ const getLinearElementRotatedBounds = (
|
|||||||
cx: number,
|
cx: number,
|
||||||
cy: number,
|
cy: number,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
): Bounds => {
|
): RotatedBounds => {
|
||||||
const boundTextElement = getBoundTextElement(element, elementsMap);
|
const boundTextElement = getBoundTextElement(element, elementsMap);
|
||||||
|
|
||||||
if (element.points.length < 2) {
|
if (element.points.length < 2) {
|
||||||
@@ -949,20 +953,21 @@ const getLinearElementRotatedBounds = (
|
|||||||
element.angle,
|
element.angle,
|
||||||
);
|
);
|
||||||
|
|
||||||
let coords: Bounds = [x, y, x, y];
|
let coords: RotatedBounds = bounds(x, y, x, y, element.angle);
|
||||||
if (boundTextElement) {
|
if (boundTextElement) {
|
||||||
const coordsWithBoundText = LinearElementEditor.getMinMaxXYWithBoundText(
|
const coordsWithBoundText = LinearElementEditor.getMinMaxXYWithBoundText(
|
||||||
element,
|
element,
|
||||||
elementsMap,
|
elementsMap,
|
||||||
[x, y, x, y],
|
bounds(x, y, x, y, element.angle),
|
||||||
boundTextElement,
|
boundTextElement,
|
||||||
);
|
);
|
||||||
coords = [
|
coords = bounds(
|
||||||
coordsWithBoundText[0],
|
coordsWithBoundText[0],
|
||||||
coordsWithBoundText[1],
|
coordsWithBoundText[1],
|
||||||
coordsWithBoundText[2],
|
coordsWithBoundText[2],
|
||||||
coordsWithBoundText[3],
|
coordsWithBoundText[3],
|
||||||
];
|
element.angle,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
return coords;
|
return coords;
|
||||||
}
|
}
|
||||||
@@ -978,7 +983,13 @@ const getLinearElementRotatedBounds = (
|
|||||||
element.angle,
|
element.angle,
|
||||||
);
|
);
|
||||||
const res = getMinMaxXYFromCurvePathOps(ops, transformXY);
|
const res = getMinMaxXYFromCurvePathOps(ops, transformXY);
|
||||||
let coords: Bounds = [res[0], res[1], res[2], res[3]];
|
let coords: RotatedBounds = bounds(
|
||||||
|
res[0],
|
||||||
|
res[1],
|
||||||
|
res[2],
|
||||||
|
res[3],
|
||||||
|
element.angle,
|
||||||
|
);
|
||||||
if (boundTextElement) {
|
if (boundTextElement) {
|
||||||
const coordsWithBoundText = LinearElementEditor.getMinMaxXYWithBoundText(
|
const coordsWithBoundText = LinearElementEditor.getMinMaxXYWithBoundText(
|
||||||
element,
|
element,
|
||||||
@@ -986,22 +997,23 @@ const getLinearElementRotatedBounds = (
|
|||||||
coords,
|
coords,
|
||||||
boundTextElement,
|
boundTextElement,
|
||||||
);
|
);
|
||||||
coords = [
|
coords = bounds(
|
||||||
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 = (
|
export const getElementBounds = <T extends boolean = false>(
|
||||||
element: ExcalidrawElement,
|
element: ExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
nonRotated: boolean = false,
|
nonRotated: T = false as T,
|
||||||
): Bounds => {
|
) => {
|
||||||
return ElementBounds.getBounds(element, elementsMap, nonRotated);
|
return ElementBounds.getBounds<T>(element, elementsMap, nonRotated);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getCommonBounds = (
|
export const getCommonBounds = (
|
||||||
@@ -1009,7 +1021,7 @@ export const getCommonBounds = (
|
|||||||
elementsMap?: ElementsMap,
|
elementsMap?: ElementsMap,
|
||||||
): Bounds => {
|
): Bounds => {
|
||||||
if (!sizeOf(elements)) {
|
if (!sizeOf(elements)) {
|
||||||
return [0, 0, 0, 0];
|
return bounds(0, 0, 0, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
let minX = Infinity;
|
let minX = Infinity;
|
||||||
@@ -1027,7 +1039,7 @@ export const getCommonBounds = (
|
|||||||
maxY = Math.max(maxY, y2);
|
maxY = Math.max(maxY, y2);
|
||||||
});
|
});
|
||||||
|
|
||||||
return [minX, minY, maxX, maxY];
|
return bounds(minX, minY, maxX, maxY);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getDraggedElementsBounds = (
|
export const getDraggedElementsBounds = (
|
||||||
@@ -1050,12 +1062,12 @@ export const getResizedElementAbsoluteCoords = (
|
|||||||
normalizePoints: boolean,
|
normalizePoints: boolean,
|
||||||
): Bounds => {
|
): Bounds => {
|
||||||
if (!(isLinearElement(element) || isFreeDrawElement(element))) {
|
if (!(isLinearElement(element) || isFreeDrawElement(element))) {
|
||||||
return [
|
return bounds(
|
||||||
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(
|
||||||
@@ -1065,11 +1077,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();
|
||||||
@@ -1081,16 +1093,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 [
|
return bounds(
|
||||||
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 = (
|
||||||
@@ -1108,20 +1120,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 [
|
return bounds(
|
||||||
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 },
|
||||||
): Bounds => {
|
): RotatedBounds => {
|
||||||
if (!elements.length) {
|
if (!elements.length) {
|
||||||
return [0, 0, 0, 0];
|
return bounds(0, 0, 0, 0, 0 as Radians);
|
||||||
}
|
}
|
||||||
|
|
||||||
let minDistance = Infinity;
|
let minDistance = Infinity;
|
||||||
@@ -1190,7 +1202,9 @@ export const getVisibleSceneBounds = ({
|
|||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getCenterForBounds = (bounds: Bounds): GlobalPoint =>
|
export const getCenterForBounds = (
|
||||||
|
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,
|
||||||
@@ -1235,24 +1249,24 @@ export const aabbForElement = (
|
|||||||
element.angle,
|
element.angle,
|
||||||
);
|
);
|
||||||
|
|
||||||
const bounds = [
|
const _bounds = 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 [
|
return bounds(
|
||||||
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>(
|
||||||
@@ -1261,7 +1275,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 doBoundsIntersect = (
|
export const doNonRotatedBoundsIntersect = (
|
||||||
bounds1: Bounds | null,
|
bounds1: Bounds | null,
|
||||||
bounds2: Bounds | null,
|
bounds2: Bounds | null,
|
||||||
): boolean => {
|
): boolean => {
|
||||||
@@ -1281,7 +1295,7 @@ export const elementCenterPoint = (
|
|||||||
xOffset: number = 0,
|
xOffset: number = 0,
|
||||||
yOffset: number = 0,
|
yOffset: number = 0,
|
||||||
) => {
|
) => {
|
||||||
if (isLinearElement(element)) {
|
if (isLinearElement(element) || isFreeDrawElement(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,4 +1,9 @@
|
|||||||
import { invariant, isTransparent, type Bounds } from "@excalidraw/common";
|
import {
|
||||||
|
bounds,
|
||||||
|
invariant,
|
||||||
|
isTransparent,
|
||||||
|
type Bounds,
|
||||||
|
} from "@excalidraw/common";
|
||||||
import {
|
import {
|
||||||
curveIntersectLineSegment,
|
curveIntersectLineSegment,
|
||||||
isPointWithinBounds,
|
isPointWithinBounds,
|
||||||
@@ -29,7 +34,7 @@ import type { FrameNameBounds } from "@excalidraw/excalidraw/types";
|
|||||||
|
|
||||||
import { isPathALoop } from "./utils";
|
import { isPathALoop } from "./utils";
|
||||||
import {
|
import {
|
||||||
doBoundsIntersect,
|
doNonRotatedBoundsIntersect,
|
||||||
elementCenterPoint,
|
elementCenterPoint,
|
||||||
getCenterForBounds,
|
getCenterForBounds,
|
||||||
getCubicBezierCurveBound,
|
getCubicBezierCurveBound,
|
||||||
@@ -154,14 +159,11 @@ 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 = isPointWithinBounds(
|
const hitBounds = isPointInRotatedBounds(
|
||||||
pointFrom(bounds[0] - threshold, bounds[1] - threshold),
|
point,
|
||||||
pointRotateRads(
|
bounds,
|
||||||
point,
|
element.angle,
|
||||||
getCenterForBounds(bounds),
|
threshold,
|
||||||
-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
|
||||||
@@ -192,18 +194,108 @@ 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,
|
||||||
) => {
|
) => {
|
||||||
let [x1, y1, x2, y2] = getElementBounds(element, elementsMap);
|
const bounds = getElementBounds(element, elementsMap, true);
|
||||||
x1 -= tolerance;
|
return isPointInRotatedBounds(point, bounds, element.angle, tolerance);
|
||||||
y1 -= tolerance;
|
};
|
||||||
x2 += tolerance;
|
|
||||||
y2 += tolerance;
|
export const doBoundsIntersectElementBoundingBox = (
|
||||||
return isPointWithinBounds(pointFrom(x1, y1), point, pointFrom(x2, y2));
|
intersectorBounds: Bounds,
|
||||||
|
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 = (
|
||||||
@@ -256,9 +348,13 @@ 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 bounds = [x - t, y - t, x + t, y + t] as Bounds;
|
const elementBounds = getElementBounds(element, elementsMap, true);
|
||||||
const elementBounds = getElementBounds(element, elementsMap);
|
if (
|
||||||
if (!doBoundsIntersect(bounds, elementBounds)) {
|
!doNonRotatedBoundsIntersect(
|
||||||
|
bounds(x - t, y - t, x + t, y + t),
|
||||||
|
elementBounds,
|
||||||
|
)
|
||||||
|
) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -269,6 +365,7 @@ 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;
|
||||||
@@ -418,15 +515,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 = [
|
const intersectorBounds = bounds(
|
||||||
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);
|
const elementBounds = getElementBounds(element, elementsMap, true);
|
||||||
|
|
||||||
if (!doBoundsIntersect(intersectorBounds, elementBounds)) {
|
if (!doNonRotatedBoundsIntersect(intersectorBounds, elementBounds)) {
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -485,14 +582,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 = [
|
const b2 = bounds(
|
||||||
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 (!doBoundsIntersect(b1, b2)) {
|
if (!doNonRotatedBoundsIntersect(b1, b2)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -562,14 +659,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 = [
|
const b2 = bounds(
|
||||||
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 (!doBoundsIntersect(b1, b2)) {
|
if (!doNonRotatedBoundsIntersect(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,6 +21,7 @@ import {
|
|||||||
invariant,
|
invariant,
|
||||||
isShallowEqual,
|
isShallowEqual,
|
||||||
getFeatureFlag,
|
getFeatureFlag,
|
||||||
|
bounds,
|
||||||
} from "@excalidraw/common";
|
} from "@excalidraw/common";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@@ -42,7 +43,7 @@ import type {
|
|||||||
NullableGridSize,
|
NullableGridSize,
|
||||||
Zoom,
|
Zoom,
|
||||||
} from "@excalidraw/excalidraw/types";
|
} from "@excalidraw/excalidraw/types";
|
||||||
import type { Bounds } from "@excalidraw/common";
|
import type { RotatedBounds } from "@excalidraw/common";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
calculateFixedPointForNonElbowArrowBinding,
|
calculateFixedPointForNonElbowArrowBinding,
|
||||||
@@ -1879,7 +1880,7 @@ export class LinearElementEditor {
|
|||||||
static getMinMaxXYWithBoundText = (
|
static getMinMaxXYWithBoundText = (
|
||||||
element: ExcalidrawLinearElement,
|
element: ExcalidrawLinearElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
elementBounds: Bounds,
|
elementBounds: RotatedBounds,
|
||||||
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;
|
||||||
@@ -2004,7 +2005,7 @@ export class LinearElementEditor {
|
|||||||
return LinearElementEditor.getMinMaxXYWithBoundText(
|
return LinearElementEditor.getMinMaxXYWithBoundText(
|
||||||
element,
|
element,
|
||||||
elementsMap,
|
elementsMap,
|
||||||
[x1, y1, x2, y2],
|
bounds(x1, y1, x2, y2, element.angle),
|
||||||
boundTextElement,
|
boundTextElement,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,35 @@
|
|||||||
import { arrayToMap, isShallowEqual } from "@excalidraw/common";
|
import { arrayToMap, isShallowEqual, type Bounds } 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 { getElementAbsoluteCoords, getElementBounds } from "./bounds";
|
import {
|
||||||
|
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";
|
||||||
@@ -30,6 +50,205 @@ 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
|
||||||
@@ -62,55 +281,166 @@ export const getElementsWithinSelection = (
|
|||||||
selection: NonDeletedExcalidrawElement,
|
selection: NonDeletedExcalidrawElement,
|
||||||
elementsMap: ElementsMap,
|
elementsMap: ElementsMap,
|
||||||
excludeElementsInFrames: boolean = true,
|
excludeElementsInFrames: boolean = true,
|
||||||
) => {
|
boxSelectionMode: BoxSelectionMode = "contain",
|
||||||
const [selectionX1, selectionY1, selectionX2, selectionY2] =
|
): NonDeletedExcalidrawElement[] => {
|
||||||
|
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;
|
||||||
|
|
||||||
let elementsInSelection = elements.filter((element) => {
|
if (boxSelectionMode !== "overlap") {
|
||||||
let [elementX1, elementY1, elementX2, elementY2] = getElementBounds(
|
const elementsInSelection: NonDeletedExcalidrawElement[] = [];
|
||||||
element,
|
|
||||||
elementsMap,
|
|
||||||
);
|
|
||||||
|
|
||||||
const containingFrame = getContainingFrame(element, elementsMap);
|
for (const element of elements) {
|
||||||
if (containingFrame) {
|
if (shouldSkipElementFromSelection(element)) {
|
||||||
const [fx1, fy1, fx2, fy2] = getElementBounds(
|
continue;
|
||||||
containingFrame,
|
}
|
||||||
elementsMap,
|
|
||||||
);
|
|
||||||
|
|
||||||
elementX1 = Math.max(fx1, elementX1);
|
const elementBounds = getElementBounds(element, elementsMap) as Bounds;
|
||||||
elementY1 = Math.max(fy1, elementY1);
|
const frameBounds = getFrameBoundsForSelection(element, elementsMap);
|
||||||
elementX2 = Math.min(fx2, elementX2);
|
let elementX1 = elementBounds[0];
|
||||||
elementY2 = Math.min(fy2, elementY2);
|
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 (
|
return finalizeElementsInSelection(
|
||||||
element.locked === false &&
|
elementsInSelection,
|
||||||
element.type !== "selection" &&
|
excludeElementsInFrames,
|
||||||
!isBoundToContainer(element) &&
|
elementsMap,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
const selectionCorners = getBoundsCorners(selectionBounds);
|
||||||
|
const selectionEdges = getBoundsEdges(selectionCorners);
|
||||||
|
const elementsInSelection: NonDeletedExcalidrawElement[] = [];
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
const isSelectionContainingElement =
|
||||||
selectionX1 <= elementX1 &&
|
selectionX1 <= elementX1 &&
|
||||||
selectionY1 <= elementY1 &&
|
selectionY1 <= elementY1 &&
|
||||||
selectionX2 >= elementX2 &&
|
selectionX2 >= elementX2 &&
|
||||||
selectionY2 >= elementY2
|
selectionY2 >= elementY2;
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
elementsInSelection = excludeElementsInFrames
|
const isSelectionOverlappingElementAabb =
|
||||||
? excludeElementsInFramesFromSelection(elementsInSelection)
|
selectionX1 <= elementX2 &&
|
||||||
: elementsInSelection;
|
selectionY1 <= elementY2 &&
|
||||||
|
selectionX2 >= elementX1 &&
|
||||||
|
selectionY2 >= elementY1;
|
||||||
|
const isSelectionOverlappingElement = shouldUseRotatedOverlapBroadPhase(
|
||||||
|
element,
|
||||||
|
)
|
||||||
|
? isSelectionOverlappingElementAabb &&
|
||||||
|
doBoundsIntersectElementBoundingBox(
|
||||||
|
selectionBounds,
|
||||||
|
element,
|
||||||
|
elementsMap,
|
||||||
|
)
|
||||||
|
: isSelectionOverlappingElementAabb;
|
||||||
|
|
||||||
elementsInSelection = elementsInSelection.filter((element) => {
|
const shouldSelectFromInside = shouldTestInside(element);
|
||||||
const containingFrame = getContainingFrame(element, elementsMap);
|
|
||||||
|
|
||||||
if (containingFrame) {
|
if (shouldSelectFromInside) {
|
||||||
return elementOverlapsWithFrame(element, containingFrame, elementsMap);
|
if (
|
||||||
|
isSelectionOverlappingElement &&
|
||||||
|
(!shouldUsePreciseFilledOverlap(element) ||
|
||||||
|
isSelectionContainingElement ||
|
||||||
|
doesSelectionOverlapFilledElement(
|
||||||
|
element,
|
||||||
|
frameBounds,
|
||||||
|
selectionBounds,
|
||||||
|
selectionCorners,
|
||||||
|
selectionEdges,
|
||||||
|
elementsMap,
|
||||||
|
))
|
||||||
|
) {
|
||||||
|
elementsInSelection.push(element);
|
||||||
|
}
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
if (!isSelectionOverlappingElement) {
|
||||||
});
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
return elementsInSelection;
|
if (isSelectionContainingElement) {
|
||||||
|
elementsInSelection.push(element);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
doesSelectionIntersectElementOutline(
|
||||||
|
element,
|
||||||
|
frameBounds,
|
||||||
|
selectionEdges,
|
||||||
|
elementsMap,
|
||||||
|
)
|
||||||
|
) {
|
||||||
|
elementsInSelection.push(element);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const outlineSegments = getVisibleElementOutlineSegments(
|
||||||
|
element,
|
||||||
|
frameBounds,
|
||||||
|
elementsMap,
|
||||||
|
);
|
||||||
|
|
||||||
|
if (
|
||||||
|
outlineSegments.length > 0 &&
|
||||||
|
doesSelectionContainElementOutline(outlineSegments, selectionBounds)
|
||||||
|
) {
|
||||||
|
elementsInSelection.push(element);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return finalizeElementsInSelection(
|
||||||
|
elementsInSelection,
|
||||||
|
excludeElementsInFrames,
|
||||||
|
elementsMap,
|
||||||
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getVisibleAndNonSelectedElements = (
|
export const getVisibleAndNonSelectedElements = (
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ describe("check rotated elements can be hit:", () => {
|
|||||||
] as LocalPoint[],
|
] as LocalPoint[],
|
||||||
});
|
});
|
||||||
const hit = hitElementItself({
|
const hit = hitElementItself({
|
||||||
point: pointFrom<GlobalPoint>(88, -68),
|
point: pointFrom<GlobalPoint>(87, -67),
|
||||||
element: window.h.elements[0],
|
element: window.h.elements[0],
|
||||||
threshold: 10,
|
threshold: 10,
|
||||||
elementsMap: window.h.scene.getNonDeletedElementsMap(),
|
elementsMap: window.h.scene.getNonDeletedElementsMap(),
|
||||||
|
|||||||
@@ -193,6 +193,7 @@ 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,6 +27,7 @@ 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,
|
||||||
@@ -245,7 +246,7 @@ import {
|
|||||||
bindOrUnbindBindingElement,
|
bindOrUnbindBindingElement,
|
||||||
mutateElement,
|
mutateElement,
|
||||||
getElementBounds,
|
getElementBounds,
|
||||||
doBoundsIntersect,
|
doNonRotatedBoundsIntersect,
|
||||||
isPointInElement,
|
isPointInElement,
|
||||||
maxBindingDistance_simple,
|
maxBindingDistance_simple,
|
||||||
convertToExcalidrawElements,
|
convertToExcalidrawElements,
|
||||||
@@ -1153,7 +1154,7 @@ class App extends React.Component<AppProps, AppState> {
|
|||||||
startBounds &&
|
startBounds &&
|
||||||
endBounds &&
|
endBounds &&
|
||||||
startElement.id !== endElement.id &&
|
startElement.id !== endElement.id &&
|
||||||
doBoundsIntersect(startBounds, endBounds)
|
doNonRotatedBoundsIntersect(startBounds, endBounds)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -7239,6 +7240,14 @@ 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") {
|
||||||
@@ -7730,17 +7739,24 @@ 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 (
|
if (shouldStartLassoSelection) {
|
||||||
!pointerDownState.hit.hasHitCommonBoundingBoxOfSelectedElements &&
|
if (!this.lassoTrail.hasCurrentTrail) {
|
||||||
!pointerDownState.resize.handleType &&
|
this.lassoTrail.startPath(
|
||||||
!hitSelectedElement
|
pointerDownState.origin.x,
|
||||||
) {
|
pointerDownState.origin.y,
|
||||||
this.lassoTrail.startPath(
|
event.shiftKey,
|
||||||
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)
|
||||||
@@ -8729,12 +8745,14 @@ 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 - threshold &&
|
point.x > x1 - boundsPadding - threshold &&
|
||||||
point.x < x2 + threshold &&
|
point.x < x2 + boundsPadding + threshold &&
|
||||||
point.y > y1 - threshold &&
|
point.y > y1 - boundsPadding - threshold &&
|
||||||
point.y < y2 + threshold
|
point.y < y2 + boundsPadding + threshold
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -10267,6 +10285,7 @@ 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,13 +26,16 @@
|
|||||||
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;
|
||||||
width: 32px;
|
min-width: 20px;
|
||||||
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: 16rem;
|
max-width: 20rem;
|
||||||
z-index: 1;
|
z-index: 1;
|
||||||
|
|
||||||
&--placement-top {
|
&--placement-top {
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
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> = {
|
||||||
@@ -12,6 +13,7 @@ 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,>({
|
||||||
@@ -21,13 +23,17 @@ 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">
|
||||||
<label className="dropdown-menu-item__text">{children}</label>
|
{icon && <div className="dropdown-menu-item__icon">{icon}</div>}
|
||||||
|
<label className="dropdown-menu-item__text">
|
||||||
|
<Ellipsify>{children}</Ellipsify>
|
||||||
|
</label>
|
||||||
<RadioGroup
|
<RadioGroup
|
||||||
name={name}
|
name={name}
|
||||||
value={value}
|
value={value}
|
||||||
|
|||||||
@@ -39,7 +39,13 @@ 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 { GithubIcon, DiscordIcon, XBrandIcon, settingsIcon } from "../icons";
|
import {
|
||||||
|
GithubIcon,
|
||||||
|
DiscordIcon,
|
||||||
|
XBrandIcon,
|
||||||
|
settingsIcon,
|
||||||
|
emptyIcon,
|
||||||
|
} from "../icons";
|
||||||
import {
|
import {
|
||||||
boltIcon,
|
boltIcon,
|
||||||
DeviceDesktopIcon,
|
DeviceDesktopIcon,
|
||||||
@@ -427,6 +433,40 @@ 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();
|
||||||
@@ -568,6 +608,7 @@ 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 />
|
||||||
@@ -585,6 +626,7 @@ 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,6 +936,12 @@ 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,
|
||||||
doBoundsIntersect,
|
doNonRotatedBoundsIntersect,
|
||||||
getBoundTextElement,
|
getBoundTextElement,
|
||||||
getElementBounds,
|
getElementBounds,
|
||||||
getElementLineSegments,
|
getElementLineSegments,
|
||||||
@@ -219,7 +219,7 @@ const eraserTest = (
|
|||||||
origElementBounds[3] + threshold,
|
origElementBounds[3] + threshold,
|
||||||
];
|
];
|
||||||
|
|
||||||
if (!doBoundsIntersect(segmentBounds, elementBounds)) {
|
if (!doNonRotatedBoundsIntersect(segmentBounds, elementBounds)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ import { type Bounds } from "@excalidraw/common";
|
|||||||
|
|
||||||
import {
|
import {
|
||||||
computeBoundTextPosition,
|
computeBoundTextPosition,
|
||||||
doBoundsIntersect,
|
doNonRotatedBoundsIntersect,
|
||||||
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 (
|
||||||
doBoundsIntersect(lassoBounds, elementBounds) &&
|
doNonRotatedBoundsIntersect(lassoBounds, elementBounds) &&
|
||||||
!intersectedElements.has(element.id) &&
|
!intersectedElements.has(element.id) &&
|
||||||
!enclosedElements.has(element.id)
|
!enclosedElements.has(element.id)
|
||||||
) {
|
) {
|
||||||
|
|||||||
@@ -185,6 +185,9 @@
|
|||||||
"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": 760410951,
|
"versionNonce": 1006504105,
|
||||||
"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": 238820263,
|
"seed": 400692809,
|
||||||
"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": 1006504105,
|
"versionNonce": 289600103,
|
||||||
"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": 747212839,
|
"versionNonce": 1723083209,
|
||||||
"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": 238820263,
|
"seed": 400692809,
|
||||||
"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": 1723083209,
|
"versionNonce": 760410951,
|
||||||
"width": 10,
|
"width": 10,
|
||||||
"x": 12,
|
"x": 12,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
|
|||||||
@@ -1,7 +1,9 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { vi } from "vitest";
|
import { vi } from "vitest";
|
||||||
|
|
||||||
import { KEYS, reseed } from "@excalidraw/common";
|
import { KEYS, ROUNDNESS, 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";
|
||||||
|
|
||||||
@@ -12,6 +14,7 @@ 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,
|
||||||
@@ -39,6 +42,19 @@ 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 />);
|
||||||
@@ -108,6 +124,497 @@ 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,6 +269,8 @@ 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;
|
||||||
@@ -307,6 +309,8 @@ 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,3 +1,14 @@
|
|||||||
|
//
|
||||||
|
// 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,4 +1,9 @@
|
|||||||
import { arrayToMap, type Bounds } from "@excalidraw/common";
|
import {
|
||||||
|
arrayToMap,
|
||||||
|
bounds,
|
||||||
|
type RotatedBounds,
|
||||||
|
type Bounds,
|
||||||
|
} from "@excalidraw/common";
|
||||||
import { getElementBounds } from "@excalidraw/element";
|
import { getElementBounds } from "@excalidraw/element";
|
||||||
import {
|
import {
|
||||||
isArrowElement,
|
isArrowElement,
|
||||||
@@ -90,7 +95,7 @@ const getMinMaxPoints = (points: Points) => {
|
|||||||
return ret;
|
return ret;
|
||||||
};
|
};
|
||||||
|
|
||||||
const getRotatedBBox = (element: Element): Bounds => {
|
const getRotatedBBox = (element: Element): RotatedBounds => {
|
||||||
const points = getElementRelativePoints(element);
|
const points = getElementRelativePoints(element);
|
||||||
|
|
||||||
const { cx, cy } = getMinMaxPoints(points);
|
const { cx, cy } = getMinMaxPoints(points);
|
||||||
@@ -101,12 +106,13 @@ const getRotatedBBox = (element: Element): Bounds => {
|
|||||||
);
|
);
|
||||||
const { minX, minY, maxX, maxY } = getMinMaxPoints(rotatedPoints);
|
const { minX, minY, maxX, maxY } = getMinMaxPoints(rotatedPoints);
|
||||||
|
|
||||||
return [
|
return bounds(
|
||||||
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 = (
|
||||||
@@ -160,12 +166,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: Bounds | ExcalidrawElement;
|
bounds: RotatedBounds | ExcalidrawElement;
|
||||||
/** safety offset. Defaults to 0. */
|
/** safety offset. Defaults to 0. */
|
||||||
errorMargin?: number;
|
errorMargin?: number;
|
||||||
/**
|
/**
|
||||||
@@ -175,15 +181,16 @@ 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