refactor: DRY out tool typing (#7086)

This commit is contained in:
David Luzar 2023-10-04 23:39:00 +02:00 committed by GitHub
parent fa33aa08ab
commit e6f74350ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 42 deletions

View File

@ -211,7 +211,7 @@ import {
import Scene from "../scene/Scene"; import Scene from "../scene/Scene";
import { RenderInteractiveSceneCallback, ScrollBars } from "../scene/types"; import { RenderInteractiveSceneCallback, ScrollBars } from "../scene/types";
import { getStateForZoom } from "../scene/zoom"; import { getStateForZoom } from "../scene/zoom";
import { findShapeByKey, SHAPES } from "../shapes"; import { findShapeByKey } from "../shapes";
import { import {
AppClassProperties, AppClassProperties,
AppProps, AppProps,
@ -230,6 +230,7 @@ import {
SidebarName, SidebarName,
SidebarTabName, SidebarTabName,
KeyboardModifiersObject, KeyboardModifiersObject,
ToolType,
} from "../types"; } from "../types";
import { import {
debounce, debounce,
@ -3113,12 +3114,7 @@ class App extends React.Component<AppProps, AppState> {
setActiveTool = ( setActiveTool = (
tool: tool:
| { | {
type: type: ToolType;
| typeof SHAPES[number]["value"]
| "eraser"
| "hand"
| "frame"
| "embeddable";
} }
| { type: "custom"; customType: string }, | { type: "custom"; customType: string },
) => { ) => {

View File

@ -18,7 +18,6 @@ import {
ExcalidrawFrameElement, ExcalidrawFrameElement,
ExcalidrawEmbeddableElement, ExcalidrawEmbeddableElement,
} from "./element/types"; } from "./element/types";
import { SHAPES } from "./shapes";
import { Point as RoughPoint } from "roughjs/bin/geometry"; import { Point as RoughPoint } from "roughjs/bin/geometry";
import { LinearElementEditor } from "./element/linearElementEditor"; import { LinearElementEditor } from "./element/linearElementEditor";
import { SuggestedBinding } from "./element/binding"; import { SuggestedBinding } from "./element/binding";
@ -86,21 +85,30 @@ export type BinaryFileMetadata = Omit<BinaryFileData, "dataURL">;
export type BinaryFiles = Record<ExcalidrawElement["id"], BinaryFileData>; 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: type: ToolType;
| typeof SHAPES[number]["value"]
| "eraser"
| "hand"
| "frame"
| "embeddable";
customType: null; customType: null;
} }
| { | {
type: "custom"; type: "custom";
customType: string; customType: string;
} };
| null;
export type SidebarName = string; export type SidebarName = string;
export type SidebarTabName = string; export type SidebarTabName = string;
@ -195,23 +203,9 @@ export type AppState = {
* indicates a previous tool we should revert back to if we deselect the * 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. * currently active tool. At the moment applies to `eraser` and `hand` tool.
*/ */
lastActiveTool: LastActiveTool; lastActiveTool: ActiveTool | null;
locked: boolean; locked: boolean;
} & ( } & ActiveTool;
| {
type:
| typeof SHAPES[number]["value"]
| "eraser"
| "hand"
| "frame"
| "embeddable";
customType: null;
}
| {
type: "custom";
customType: string;
}
);
penMode: boolean; penMode: boolean;
penDetected: boolean; penDetected: boolean;
exportBackground: boolean; exportBackground: boolean;

View File

@ -15,9 +15,8 @@ import {
FontString, FontString,
NonDeletedExcalidrawElement, NonDeletedExcalidrawElement,
} from "./element/types"; } 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 { unstable_batchedUpdates } from "react-dom";
import { SHAPES } from "./shapes";
import { isEraserActive, isHandToolActive } from "./appState"; import { isEraserActive, isHandToolActive } from "./appState";
import { ResolutionType } from "./utility-types"; import { ResolutionType } from "./utility-types";
import React from "react"; import React from "react";
@ -371,15 +370,10 @@ export const updateActiveTool = (
appState: Pick<AppState, "activeTool">, appState: Pick<AppState, "activeTool">,
data: ( data: (
| { | {
type: type: ToolType;
| typeof SHAPES[number]["value"]
| "eraser"
| "hand"
| "frame"
| "embeddable";
} }
| { type: "custom"; customType: string } | { type: "custom"; customType: string }
) & { lastActiveToolBeforeEraser?: LastActiveTool }, ) & { lastActiveToolBeforeEraser?: ActiveTool | null },
): AppState["activeTool"] => { ): AppState["activeTool"] => {
if (data.type === "custom") { if (data.type === "custom") {
return { return {