Skip to content

Commit

Permalink
refactor: DRY out tool typing (excalidraw#7086)
Browse files Browse the repository at this point in the history
  • Loading branch information
dwelle authored Oct 4, 2023
1 parent fa33aa0 commit e6f7435
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 42 deletions.
10 changes: 3 additions & 7 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ import {
import Scene from "../scene/Scene";
import { RenderInteractiveSceneCallback, ScrollBars } from "../scene/types";
import { getStateForZoom } from "../scene/zoom";
import { findShapeByKey, SHAPES } from "../shapes";
import { findShapeByKey } from "../shapes";
import {
AppClassProperties,
AppProps,
Expand All @@ -230,6 +230,7 @@ import {
SidebarName,
SidebarTabName,
KeyboardModifiersObject,
ToolType,
} from "../types";
import {
debounce,
Expand Down Expand Up @@ -3113,12 +3114,7 @@ class App extends React.Component<AppProps, AppState> {
setActiveTool = (
tool:
| {
type:
| typeof SHAPES[number]["value"]
| "eraser"
| "hand"
| "frame"
| "embeddable";
type: ToolType;
}
| { type: "custom"; customType: string },
) => {
Expand Down
46 changes: 20 additions & 26 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
ExcalidrawFrameElement,
ExcalidrawEmbeddableElement,
} from "./element/types";
import { SHAPES } from "./shapes";
import { Point as RoughPoint } from "roughjs/bin/geometry";
import { LinearElementEditor } from "./element/linearElementEditor";
import { SuggestedBinding } from "./element/binding";
Expand Down Expand Up @@ -86,21 +85,30 @@ export type BinaryFileMetadata = Omit<BinaryFileData, "dataURL">;

export type BinaryFiles = Record<ExcalidrawElement["id"], BinaryFileData>;

export type LastActiveTool =
export type ToolType =
| "selection"
| "rectangle"
| "diamond"
| "ellipse"
| "arrow"
| "line"
| "freedraw"
| "text"
| "image"
| "eraser"
| "hand"
| "frame"
| "embeddable";

export type ActiveTool =
| {
type:
| typeof SHAPES[number]["value"]
| "eraser"
| "hand"
| "frame"
| "embeddable";
type: ToolType;
customType: null;
}
| {
type: "custom";
customType: string;
}
| null;
};

export type SidebarName = string;
export type SidebarTabName = string;
Expand Down Expand Up @@ -195,23 +203,9 @@ export type AppState = {
* indicates a previous tool we should revert back to if we deselect the
* currently active tool. At the moment applies to `eraser` and `hand` tool.
*/
lastActiveTool: LastActiveTool;
lastActiveTool: ActiveTool | null;
locked: boolean;
} & (
| {
type:
| typeof SHAPES[number]["value"]
| "eraser"
| "hand"
| "frame"
| "embeddable";
customType: null;
}
| {
type: "custom";
customType: string;
}
);
} & ActiveTool;
penMode: boolean;
penDetected: boolean;
exportBackground: boolean;
Expand Down
12 changes: 3 additions & 9 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ import {
FontString,
NonDeletedExcalidrawElement,
} from "./element/types";
import { AppState, DataURL, LastActiveTool, Zoom } from "./types";
import { ActiveTool, AppState, DataURL, ToolType, Zoom } from "./types";
import { unstable_batchedUpdates } from "react-dom";
import { SHAPES } from "./shapes";
import { isEraserActive, isHandToolActive } from "./appState";
import { ResolutionType } from "./utility-types";
import React from "react";
Expand Down Expand Up @@ -371,15 +370,10 @@ export const updateActiveTool = (
appState: Pick<AppState, "activeTool">,
data: (
| {
type:
| typeof SHAPES[number]["value"]
| "eraser"
| "hand"
| "frame"
| "embeddable";
type: ToolType;
}
| { type: "custom"; customType: string }
) & { lastActiveToolBeforeEraser?: LastActiveTool },
) & { lastActiveToolBeforeEraser?: ActiveTool | null },
): AppState["activeTool"] => {
if (data.type === "custom") {
return {
Expand Down

0 comments on commit e6f7435

Please sign in to comment.