2020-01-06 20:24:54 +04:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
|
|
|
import { hitTest } from "../element/collision";
|
2020-01-09 01:09:09 +05:00
|
|
|
import { getElementAbsoluteCoords } from "../element";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
export const hasBackground = (elements: ExcalidrawElement[]) =>
|
|
|
|
elements.some(
|
|
|
|
element =>
|
|
|
|
element.isSelected &&
|
|
|
|
(element.type === "rectangle" ||
|
|
|
|
element.type === "ellipse" ||
|
|
|
|
element.type === "diamond")
|
|
|
|
);
|
|
|
|
|
|
|
|
export const hasStroke = (elements: ExcalidrawElement[]) =>
|
|
|
|
elements.some(
|
|
|
|
element =>
|
|
|
|
element.isSelected &&
|
|
|
|
(element.type === "rectangle" ||
|
|
|
|
element.type === "ellipse" ||
|
|
|
|
element.type === "diamond" ||
|
|
|
|
element.type === "arrow")
|
|
|
|
);
|
|
|
|
|
2020-01-09 02:29:41 +01:00
|
|
|
export const hasText = (elements: ExcalidrawElement[]) =>
|
2020-01-09 12:34:46 +01:00
|
|
|
elements.some(element => element.isSelected && element.type === "text");
|
2020-01-09 02:29:41 +01:00
|
|
|
|
2020-01-06 20:24:54 +04:00
|
|
|
export function getElementAtPosition(
|
|
|
|
elements: ExcalidrawElement[],
|
|
|
|
x: number,
|
|
|
|
y: number
|
|
|
|
) {
|
|
|
|
let hitElement = null;
|
|
|
|
// We need to to hit testing from front (end of the array) to back (beginning of the array)
|
|
|
|
for (let i = elements.length - 1; i >= 0; --i) {
|
|
|
|
if (hitTest(elements[i], x, y)) {
|
|
|
|
hitElement = elements[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return hitElement;
|
|
|
|
}
|
2020-01-09 01:09:09 +05:00
|
|
|
|
|
|
|
export function getElementContainingPosition(
|
|
|
|
elements: ExcalidrawElement[],
|
|
|
|
x: number,
|
|
|
|
y: number
|
|
|
|
) {
|
|
|
|
let hitElement = null;
|
|
|
|
// We need to to hit testing from front (end of the array) to back (beginning of the array)
|
|
|
|
for (let i = elements.length - 1; i >= 0; --i) {
|
|
|
|
const [x1, y1, x2, y2] = getElementAbsoluteCoords(elements[i]);
|
|
|
|
if (x1 < x && x < x2 && y1 < y && y < y2) {
|
|
|
|
hitElement = elements[i];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hitElement;
|
|
|
|
}
|