improve typing for handlerRectangles

This commit is contained in:
dwelle 2020-01-08 16:55:13 +01:00
parent e7bf034fef
commit 009412a093
3 changed files with 10 additions and 6 deletions

View File

@ -1,6 +1,8 @@
import { SceneState } from "../scene/types"; import { SceneState } from "../scene/types";
import { ExcalidrawElement } from "./types"; import { ExcalidrawElement } from "./types";
type Sides = "n" | "s" | "w" | "e" | "nw" | "ne" | "sw" | "se";
export function handlerRectangles( export function handlerRectangles(
element: ExcalidrawElement, element: ExcalidrawElement,
sceneState: SceneState sceneState: SceneState
@ -12,7 +14,7 @@ export function handlerRectangles(
const margin = 4; const margin = 4;
const minimumSize = 40; const minimumSize = 40;
const handlers: { [handler: string]: number[] } = {}; const handlers = {} as { [T in Sides]: number[] };
const marginX = element.width < 0 ? 8 : -8; const marginX = element.width < 0 ? 8 : -8;
const marginY = element.height < 0 ? 8 : -8; const marginY = element.height < 0 ? 8 : -8;
@ -78,7 +80,7 @@ export function handlerRectangles(
return { return {
nw: handlers.nw, nw: handlers.nw,
se: handlers.se se: handlers.se
}; } as typeof handlers;
} }
return handlers; return handlers;

View File

@ -3,18 +3,20 @@ import { SceneState } from "../scene/types";
import { handlerRectangles } from "./handlerRectangles"; import { handlerRectangles } from "./handlerRectangles";
type HandlerRectanglesRet = keyof ReturnType<typeof handlerRectangles>;
export function resizeTest( export function resizeTest(
element: ExcalidrawElement, element: ExcalidrawElement,
x: number, x: number,
y: number, y: number,
sceneState: SceneState sceneState: SceneState
): string | false { ): HandlerRectanglesRet | false {
if (element.type === "text") return false; if (element.type === "text") return false;
const handlers = handlerRectangles(element, sceneState); const handlers = handlerRectangles(element, sceneState);
const filter = Object.keys(handlers).filter(key => { const filter = Object.keys(handlers).filter(key => {
const handler = handlers[key]; const handler = handlers[key as HandlerRectanglesRet]!;
return ( return (
x + sceneState.scrollX >= handler[0] && x + sceneState.scrollX >= handler[0] &&
@ -25,7 +27,7 @@ export function resizeTest(
}); });
if (filter.length > 0) { if (filter.length > 0) {
return filter[0]; return filter[0] as HandlerRectanglesRet;
} }
return false; return false;

View File

@ -638,7 +638,7 @@ class App extends React.Component<{}, AppState> {
1, 1,
100 100
); );
let resizeHandle: string | false = false; let resizeHandle: ReturnType<typeof resizeTest> = false;
let isDraggingElements = false; let isDraggingElements = false;
let isResizingElements = false; let isResizingElements = false;
if (this.state.elementType === "selection") { if (this.state.elementType === "selection") {