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-03-08 10:20:55 -07:00
|
|
|
import { AppState } from "../types";
|
2020-03-14 20:46:57 -07:00
|
|
|
import { newElementWith } from "../element/mutateElement";
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-01-13 04:32:25 +05:00
|
|
|
export function getElementsWithinSelection(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-24 12:04:54 +02:00
|
|
|
selection: ExcalidrawElement,
|
2020-01-06 20:24:54 +04:00
|
|
|
) {
|
2020-01-07 19:04:52 +04:00
|
|
|
const [
|
|
|
|
selectionX1,
|
|
|
|
selectionY1,
|
|
|
|
selectionX2,
|
2020-01-24 12:04:54 +02:00
|
|
|
selectionY2,
|
2020-01-07 19:04:52 +04:00
|
|
|
] = getElementAbsoluteCoords(selection);
|
2020-01-13 04:32:25 +05:00
|
|
|
return elements.filter(element => {
|
2020-01-07 19:04:52 +04:00
|
|
|
const [
|
|
|
|
elementX1,
|
|
|
|
elementY1,
|
|
|
|
elementX2,
|
2020-01-24 12:04:54 +02:00
|
|
|
elementY2,
|
2020-01-07 19:04:52 +04:00
|
|
|
] = getElementAbsoluteCoords(element);
|
2020-01-13 04:32:25 +05:00
|
|
|
|
|
|
|
return (
|
2020-01-06 20:24:54 +04:00
|
|
|
element.type !== "selection" &&
|
|
|
|
selectionX1 <= elementX1 &&
|
|
|
|
selectionY1 <= elementY1 &&
|
|
|
|
selectionX2 >= elementX2 &&
|
2020-01-13 04:32:25 +05:00
|
|
|
selectionY2 >= elementY2
|
|
|
|
);
|
2020-01-06 20:24:54 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
export function deleteSelectedElements(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
) {
|
2020-03-08 14:10:42 -07:00
|
|
|
return {
|
2020-03-14 20:46:57 -07:00
|
|
|
elements: elements.map(el => {
|
2020-03-14 21:25:07 +01:00
|
|
|
if (appState.selectedElementIds[el.id]) {
|
2020-03-14 20:46:57 -07:00
|
|
|
return newElementWith(el, { isDeleted: true });
|
2020-03-14 21:25:07 +01:00
|
|
|
}
|
2020-03-14 20:46:57 -07:00
|
|
|
return el;
|
2020-03-14 21:25:07 +01:00
|
|
|
}),
|
2020-03-08 14:10:42 -07:00
|
|
|
appState: {
|
|
|
|
...appState,
|
|
|
|
selectedElementIds: {},
|
|
|
|
},
|
|
|
|
};
|
2020-01-06 20:24:54 +04:00
|
|
|
}
|
|
|
|
|
2020-03-08 10:20:55 -07:00
|
|
|
export function getSelectedIndices(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
|
|
|
appState: AppState,
|
|
|
|
) {
|
2020-01-06 20:24:54 +04:00
|
|
|
const selectedIndices: number[] = [];
|
|
|
|
elements.forEach((element, index) => {
|
2020-03-08 10:20:55 -07:00
|
|
|
if (appState.selectedElementIds[element.id]) {
|
2020-01-06 20:24:54 +04:00
|
|
|
selectedIndices.push(index);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return selectedIndices;
|
|
|
|
}
|
|
|
|
|
2020-02-16 22:54:50 +01:00
|
|
|
export function isSomeElementSelected(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-02-16 22:54:50 +01:00
|
|
|
): boolean {
|
2020-03-08 10:20:55 -07:00
|
|
|
return elements.some(element => appState.selectedElementIds[element.id]);
|
2020-02-16 22:54:50 +01:00
|
|
|
}
|
2020-01-06 20:24:54 +04:00
|
|
|
|
2020-01-21 00:16:22 +01:00
|
|
|
/**
|
|
|
|
* Returns common attribute (picked by `getAttribute` callback) of selected
|
|
|
|
* elements. If elements don't share the same value, returns `null`.
|
|
|
|
*/
|
|
|
|
export function getCommonAttributeOfSelectedElements<T>(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-01-24 12:04:54 +02:00
|
|
|
getAttribute: (element: ExcalidrawElement) => T,
|
2020-01-06 20:24:54 +04:00
|
|
|
): T | null {
|
|
|
|
const attributes = Array.from(
|
|
|
|
new Set(
|
2020-03-08 10:20:55 -07:00
|
|
|
getSelectedElements(elements, appState).map(element =>
|
|
|
|
getAttribute(element),
|
|
|
|
),
|
2020-01-24 12:04:54 +02:00
|
|
|
),
|
2020-01-06 20:24:54 +04:00
|
|
|
);
|
|
|
|
return attributes.length === 1 ? attributes[0] : null;
|
|
|
|
}
|
2020-02-16 22:54:50 +01:00
|
|
|
|
|
|
|
export function getSelectedElements(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-02-16 22:54:50 +01:00
|
|
|
): readonly ExcalidrawElement[] {
|
2020-03-08 10:20:55 -07:00
|
|
|
return elements.filter(element => appState.selectedElementIds[element.id]);
|
2020-02-16 22:54:50 +01:00
|
|
|
}
|
2020-03-07 10:20:38 -05:00
|
|
|
|
|
|
|
export function getTargetElement(
|
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-03-08 10:20:55 -07:00
|
|
|
appState: AppState,
|
2020-03-07 10:20:38 -05:00
|
|
|
) {
|
2020-03-08 10:20:55 -07:00
|
|
|
return appState.editingElement
|
|
|
|
? [appState.editingElement]
|
|
|
|
: getSelectedElements(elements, appState);
|
2020-03-07 10:20:38 -05:00
|
|
|
}
|