2020-01-06 19:34:22 +04:00
|
|
|
import { ExcalidrawElement } from "./types";
|
2020-01-08 23:56:35 -03:00
|
|
|
import { SceneScroll } from "../scene/types";
|
2020-02-01 15:49:18 +04:00
|
|
|
import { getArrowAbsoluteBounds } from "./bounds";
|
2020-01-06 19:34:22 +04:00
|
|
|
|
2020-01-08 16:55:13 +01:00
|
|
|
type Sides = "n" | "s" | "w" | "e" | "nw" | "ne" | "sw" | "se";
|
|
|
|
|
2020-01-06 19:34:22 +04:00
|
|
|
export function handlerRectangles(
|
|
|
|
element: ExcalidrawElement,
|
2020-01-24 12:04:54 +02:00
|
|
|
{ scrollX, scrollY }: SceneScroll,
|
2020-01-06 19:34:22 +04:00
|
|
|
) {
|
2020-02-01 15:49:18 +04:00
|
|
|
let elementX2 = 0;
|
|
|
|
let elementY2 = 0;
|
|
|
|
let elementX1 = Infinity;
|
|
|
|
let elementY1 = Infinity;
|
|
|
|
let marginX = -8;
|
|
|
|
let marginY = -8;
|
|
|
|
|
|
|
|
let minimumSize = 40;
|
|
|
|
if (element.type === "arrow") {
|
|
|
|
[elementX1, elementY1, elementX2, elementY2] = getArrowAbsoluteBounds(
|
|
|
|
element,
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
elementX1 = element.x;
|
|
|
|
elementX2 = element.x + element.width;
|
|
|
|
elementY1 = element.y;
|
|
|
|
elementY2 = element.y + element.height;
|
|
|
|
|
|
|
|
marginX = element.width < 0 ? 8 : -8;
|
|
|
|
marginY = element.height < 0 ? 8 : -8;
|
|
|
|
}
|
2020-01-06 19:34:22 +04:00
|
|
|
|
|
|
|
const margin = 4;
|
2020-01-08 16:55:13 +01:00
|
|
|
const handlers = {} as { [T in Sides]: number[] };
|
2020-01-06 19:34:22 +04:00
|
|
|
|
|
|
|
if (Math.abs(elementX2 - elementX1) > minimumSize) {
|
|
|
|
handlers["n"] = [
|
2020-01-08 23:56:35 -03:00
|
|
|
elementX1 + (elementX2 - elementX1) / 2 + scrollX - 4,
|
|
|
|
elementY1 - margin + scrollY + marginY,
|
2020-01-06 19:34:22 +04:00
|
|
|
8,
|
2020-01-24 12:04:54 +02:00
|
|
|
8,
|
2020-01-06 19:34:22 +04:00
|
|
|
];
|
|
|
|
|
|
|
|
handlers["s"] = [
|
2020-01-08 23:56:35 -03:00
|
|
|
elementX1 + (elementX2 - elementX1) / 2 + scrollX - 4,
|
|
|
|
elementY2 - margin + scrollY - marginY,
|
2020-01-06 19:34:22 +04:00
|
|
|
8,
|
2020-01-24 12:04:54 +02:00
|
|
|
8,
|
2020-01-06 19:34:22 +04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Math.abs(elementY2 - elementY1) > minimumSize) {
|
|
|
|
handlers["w"] = [
|
2020-01-08 23:56:35 -03:00
|
|
|
elementX1 - margin + scrollX + marginX,
|
|
|
|
elementY1 + (elementY2 - elementY1) / 2 + scrollY - 4,
|
2020-01-06 19:34:22 +04:00
|
|
|
8,
|
2020-01-24 12:04:54 +02:00
|
|
|
8,
|
2020-01-06 19:34:22 +04:00
|
|
|
];
|
|
|
|
|
|
|
|
handlers["e"] = [
|
2020-01-08 23:56:35 -03:00
|
|
|
elementX2 - margin + scrollX - marginX,
|
|
|
|
elementY1 + (elementY2 - elementY1) / 2 + scrollY - 4,
|
2020-01-06 19:34:22 +04:00
|
|
|
8,
|
2020-01-24 12:04:54 +02:00
|
|
|
8,
|
2020-01-06 19:34:22 +04:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
handlers["nw"] = [
|
2020-01-08 23:56:35 -03:00
|
|
|
elementX1 - margin + scrollX + marginX,
|
|
|
|
elementY1 - margin + scrollY + marginY,
|
2020-01-06 19:34:22 +04:00
|
|
|
8,
|
2020-01-24 12:04:54 +02:00
|
|
|
8,
|
2020-01-06 19:34:22 +04:00
|
|
|
]; // nw
|
|
|
|
handlers["ne"] = [
|
2020-01-08 23:56:35 -03:00
|
|
|
elementX2 - margin + scrollX - marginX,
|
|
|
|
elementY1 - margin + scrollY + marginY,
|
2020-01-06 19:34:22 +04:00
|
|
|
8,
|
2020-01-24 12:04:54 +02:00
|
|
|
8,
|
2020-01-06 19:34:22 +04:00
|
|
|
]; // ne
|
|
|
|
handlers["sw"] = [
|
2020-01-08 23:56:35 -03:00
|
|
|
elementX1 - margin + scrollX + marginX,
|
|
|
|
elementY2 - margin + scrollY - marginY,
|
2020-01-06 19:34:22 +04:00
|
|
|
8,
|
2020-01-24 12:04:54 +02:00
|
|
|
8,
|
2020-01-06 19:34:22 +04:00
|
|
|
]; // sw
|
|
|
|
handlers["se"] = [
|
2020-01-08 23:56:35 -03:00
|
|
|
elementX2 - margin + scrollX - marginX,
|
|
|
|
elementY2 - margin + scrollY - marginY,
|
2020-01-06 19:34:22 +04:00
|
|
|
8,
|
2020-01-24 12:04:54 +02:00
|
|
|
8,
|
2020-01-06 19:34:22 +04:00
|
|
|
]; // se
|
|
|
|
|
2020-02-01 15:49:18 +04:00
|
|
|
if (element.type === "line") {
|
2020-01-06 19:34:22 +04:00
|
|
|
return {
|
|
|
|
nw: handlers.nw,
|
2020-01-24 12:04:54 +02:00
|
|
|
se: handlers.se,
|
2020-01-08 16:55:13 +01:00
|
|
|
} as typeof handlers;
|
2020-02-01 15:49:18 +04:00
|
|
|
} else if (element.type === "arrow") {
|
|
|
|
if (element.points.length === 2) {
|
|
|
|
// only check the last point because starting point is always (0,0)
|
|
|
|
const [, p1] = element.points;
|
|
|
|
|
|
|
|
if (p1[0] === 0 || p1[1] === 0) {
|
|
|
|
return {
|
|
|
|
nw: handlers.nw,
|
|
|
|
se: handlers.se,
|
|
|
|
} as typeof handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p1[0] > 0 && p1[1] < 0) {
|
|
|
|
return {
|
|
|
|
ne: handlers.ne,
|
|
|
|
sw: handlers.sw,
|
|
|
|
} as typeof handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p1[0] > 0 && p1[1] > 0) {
|
|
|
|
return {
|
|
|
|
nw: handlers.nw,
|
|
|
|
se: handlers.se,
|
|
|
|
} as typeof handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p1[0] < 0 && p1[1] > 0) {
|
|
|
|
return {
|
|
|
|
ne: handlers.ne,
|
|
|
|
sw: handlers.sw,
|
|
|
|
} as typeof handlers;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p1[0] < 0 && p1[1] < 0) {
|
|
|
|
return {
|
|
|
|
nw: handlers.nw,
|
|
|
|
se: handlers.se,
|
|
|
|
} as typeof handlers;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
n: handlers.n,
|
|
|
|
s: handlers.s,
|
|
|
|
w: handlers.w,
|
|
|
|
e: handlers.e,
|
|
|
|
} as typeof handlers;
|
2020-01-06 19:34:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return handlers;
|
|
|
|
}
|