2020-01-06 19:34:22 +04:00
|
|
|
import { ExcalidrawElement } from "./types";
|
|
|
|
|
|
|
|
import { handlerRectangles } from "./handlerRectangles";
|
2020-01-08 23:56:35 -03:00
|
|
|
import { SceneScroll } from "../scene/types";
|
2020-01-06 19:34:22 +04:00
|
|
|
|
2020-01-08 16:55:13 +01:00
|
|
|
type HandlerRectanglesRet = keyof ReturnType<typeof handlerRectangles>;
|
|
|
|
|
2020-01-06 19:34:22 +04:00
|
|
|
export function resizeTest(
|
|
|
|
element: ExcalidrawElement,
|
|
|
|
x: number,
|
|
|
|
y: number,
|
2020-01-08 23:56:35 -03:00
|
|
|
{ scrollX, scrollY }: SceneScroll
|
2020-01-08 16:55:13 +01:00
|
|
|
): HandlerRectanglesRet | false {
|
2020-01-08 18:06:04 +01:00
|
|
|
if (!element.isSelected || element.type === "text") return false;
|
2020-01-06 19:34:22 +04:00
|
|
|
|
2020-01-08 23:56:35 -03:00
|
|
|
const handlers = handlerRectangles(element, { scrollX, scrollY });
|
2020-01-06 19:34:22 +04:00
|
|
|
|
|
|
|
const filter = Object.keys(handlers).filter(key => {
|
2020-01-08 16:55:13 +01:00
|
|
|
const handler = handlers[key as HandlerRectanglesRet]!;
|
2020-01-06 19:34:22 +04:00
|
|
|
|
|
|
|
return (
|
2020-01-08 23:56:35 -03:00
|
|
|
x + scrollX >= handler[0] &&
|
|
|
|
x + scrollX <= handler[0] + handler[2] &&
|
|
|
|
y + scrollY >= handler[1] &&
|
|
|
|
y + scrollY <= handler[1] + handler[3]
|
2020-01-06 19:34:22 +04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (filter.length > 0) {
|
2020-01-08 16:55:13 +01:00
|
|
|
return filter[0] as HandlerRectanglesRet;
|
2020-01-06 19:34:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2020-01-08 23:56:35 -03:00
|
|
|
|
|
|
|
export function getElementWithResizeHandler(
|
2020-01-09 19:22:04 +04:00
|
|
|
elements: readonly ExcalidrawElement[],
|
2020-01-08 23:56:35 -03:00
|
|
|
{ x, y }: { x: number; y: number },
|
|
|
|
{ scrollX, scrollY }: SceneScroll
|
|
|
|
) {
|
|
|
|
return elements.reduce((result, element) => {
|
|
|
|
if (result) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
const resizeHandle = resizeTest(element, x, y, {
|
|
|
|
scrollX,
|
|
|
|
scrollY
|
|
|
|
});
|
|
|
|
return resizeHandle ? { element, resizeHandle } : null;
|
|
|
|
}, null as { element: ExcalidrawElement; resizeHandle: ReturnType<typeof resizeTest> } | null);
|
|
|
|
}
|