2020-01-06 20:24:54 +04:00
|
|
|
import { ExcalidrawElement } from "../element/types";
|
2020-01-07 19:04:52 +04:00
|
|
|
import { getElementAbsoluteCoords } from "../element";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
|
|
|
export function setSelection(
|
|
|
|
elements: ExcalidrawElement[],
|
|
|
|
selection: ExcalidrawElement
|
|
|
|
) {
|
2020-01-07 19:04:52 +04:00
|
|
|
const [
|
|
|
|
selectionX1,
|
|
|
|
selectionY1,
|
|
|
|
selectionX2,
|
|
|
|
selectionY2
|
|
|
|
] = getElementAbsoluteCoords(selection);
|
2020-01-06 20:24:54 +04:00
|
|
|
elements.forEach(element => {
|
2020-01-07 19:04:52 +04:00
|
|
|
const [
|
|
|
|
elementX1,
|
|
|
|
elementY1,
|
|
|
|
elementX2,
|
|
|
|
elementY2
|
|
|
|
] = getElementAbsoluteCoords(element);
|
2020-01-06 20:24:54 +04:00
|
|
|
element.isSelected =
|
|
|
|
element.type !== "selection" &&
|
|
|
|
selectionX1 <= elementX1 &&
|
|
|
|
selectionY1 <= elementY1 &&
|
|
|
|
selectionX2 >= elementX2 &&
|
|
|
|
selectionY2 >= elementY2;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function clearSelection(elements: ExcalidrawElement[]) {
|
|
|
|
elements.forEach(element => {
|
|
|
|
element.isSelected = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function deleteSelectedElements(elements: ExcalidrawElement[]) {
|
|
|
|
for (let i = elements.length - 1; i >= 0; --i) {
|
|
|
|
if (elements[i].isSelected) {
|
|
|
|
elements.splice(i, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function getSelectedIndices(elements: ExcalidrawElement[]) {
|
|
|
|
const selectedIndices: number[] = [];
|
|
|
|
elements.forEach((element, index) => {
|
|
|
|
if (element.isSelected) {
|
|
|
|
selectedIndices.push(index);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return selectedIndices;
|
|
|
|
}
|
|
|
|
|
|
|
|
export const someElementIsSelected = (elements: ExcalidrawElement[]) =>
|
|
|
|
elements.some(element => element.isSelected);
|
|
|
|
|
|
|
|
export function getSelectedAttribute<T>(
|
|
|
|
elements: ExcalidrawElement[],
|
|
|
|
getAttribute: (element: ExcalidrawElement) => T
|
|
|
|
): T | null {
|
|
|
|
const attributes = Array.from(
|
|
|
|
new Set(
|
|
|
|
elements
|
|
|
|
.filter(element => element.isSelected)
|
|
|
|
.map(element => getAttribute(element))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
return attributes.length === 1 ? attributes[0] : null;
|
|
|
|
}
|