excalidraw/src/element/handlerRectangles.ts

88 lines
2.0 KiB
TypeScript
Raw Normal View History

import { ExcalidrawElement } from "./types";
import { SceneScroll } from "../scene/types";
2020-01-08 16:55:13 +01:00
type Sides = "n" | "s" | "w" | "e" | "nw" | "ne" | "sw" | "se";
export function handlerRectangles(
element: ExcalidrawElement,
2020-01-24 12:04:54 +02:00
{ scrollX, scrollY }: SceneScroll,
) {
const elementX1 = element.x;
const elementX2 = element.x + element.width;
const elementY1 = element.y;
const elementY2 = element.y + element.height;
const margin = 4;
const minimumSize = 40;
2020-01-08 16:55:13 +01:00
const handlers = {} as { [T in Sides]: number[] };
const marginX = element.width < 0 ? 8 : -8;
const marginY = element.height < 0 ? 8 : -8;
if (Math.abs(elementX2 - elementX1) > minimumSize) {
handlers["n"] = [
elementX1 + (elementX2 - elementX1) / 2 + scrollX - 4,
elementY1 - margin + scrollY + marginY,
8,
2020-01-24 12:04:54 +02:00
8,
];
handlers["s"] = [
elementX1 + (elementX2 - elementX1) / 2 + scrollX - 4,
elementY2 - margin + scrollY - marginY,
8,
2020-01-24 12:04:54 +02:00
8,
];
}
if (Math.abs(elementY2 - elementY1) > minimumSize) {
handlers["w"] = [
elementX1 - margin + scrollX + marginX,
elementY1 + (elementY2 - elementY1) / 2 + scrollY - 4,
8,
2020-01-24 12:04:54 +02:00
8,
];
handlers["e"] = [
elementX2 - margin + scrollX - marginX,
elementY1 + (elementY2 - elementY1) / 2 + scrollY - 4,
8,
2020-01-24 12:04:54 +02:00
8,
];
}
handlers["nw"] = [
elementX1 - margin + scrollX + marginX,
elementY1 - margin + scrollY + marginY,
8,
2020-01-24 12:04:54 +02:00
8,
]; // nw
handlers["ne"] = [
elementX2 - margin + scrollX - marginX,
elementY1 - margin + scrollY + marginY,
8,
2020-01-24 12:04:54 +02:00
8,
]; // ne
handlers["sw"] = [
elementX1 - margin + scrollX + marginX,
elementY2 - margin + scrollY - marginY,
8,
2020-01-24 12:04:54 +02:00
8,
]; // sw
handlers["se"] = [
elementX2 - margin + scrollX - marginX,
elementY2 - margin + scrollY - marginY,
8,
2020-01-24 12:04:54 +02:00
8,
]; // se
if (element.type === "arrow" || element.type === "line") {
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;
}
return handlers;
}