瀏覽代碼

refactor: code clean up (#3681)

* refactor: code clean up
Move types from App.tsx to types.ts
Move excalidrawPackage.test.tsx inside src/tests/package

* import type
vanilla_orig
Aakansha Doshi 4 年之前
父節點
當前提交
15f02ba191
沒有連結到貢獻者的電子郵件帳戶。

+ 2
- 79
src/components/App.tsx 查看文件

111
 import { LinearElementEditor } from "../element/linearElementEditor";
111
 import { LinearElementEditor } from "../element/linearElementEditor";
112
 import { mutateElement } from "../element/mutateElement";
112
 import { mutateElement } from "../element/mutateElement";
113
 import { deepCopyElement, newFreeDrawElement } from "../element/newElement";
113
 import { deepCopyElement, newFreeDrawElement } from "../element/newElement";
114
-import { MaybeTransformHandleType } from "../element/transformHandles";
115
 import {
114
 import {
116
   isBindingElement,
115
   isBindingElement,
117
   isBindingElementType,
116
   isBindingElementType,
167
 import {
166
 import {
168
   AppProps,
167
   AppProps,
169
   AppState,
168
   AppState,
169
+  ExcalidrawImperativeAPI,
170
   Gesture,
170
   Gesture,
171
   GestureEvent,
171
   GestureEvent,
172
   LibraryItems,
172
   LibraryItems,
173
+  PointerDownState,
173
   SceneData,
174
   SceneData,
174
 } from "../types";
175
 } from "../types";
175
 import {
176
 import {
180
   isToolIcon,
181
   isToolIcon,
181
   isWritableElement,
182
   isWritableElement,
182
   resetCursor,
183
   resetCursor,
183
-  ResolvablePromise,
184
   resolvablePromise,
184
   resolvablePromise,
185
   sceneCoordsToViewportCoords,
185
   sceneCoordsToViewportCoords,
186
   setCursor,
186
   setCursor,
222
   initialScale: null,
222
   initialScale: null,
223
 };
223
 };
224
 
224
 
225
-export type PointerDownState = Readonly<{
226
-  // The first position at which pointerDown happened
227
-  origin: Readonly<{ x: number; y: number }>;
228
-  // Same as "origin" but snapped to the grid, if grid is on
229
-  originInGrid: Readonly<{ x: number; y: number }>;
230
-  // Scrollbar checks
231
-  scrollbars: ReturnType<typeof isOverScrollBars>;
232
-  // The previous pointer position
233
-  lastCoords: { x: number; y: number };
234
-  // map of original elements data
235
-  originalElements: Map<string, NonDeleted<ExcalidrawElement>>;
236
-  resize: {
237
-    // Handle when resizing, might change during the pointer interaction
238
-    handleType: MaybeTransformHandleType;
239
-    // This is determined on the initial pointer down event
240
-    isResizing: boolean;
241
-    // This is determined on the initial pointer down event
242
-    offset: { x: number; y: number };
243
-    // This is determined on the initial pointer down event
244
-    arrowDirection: "origin" | "end";
245
-    // This is a center point of selected elements determined on the initial pointer down event (for rotation only)
246
-    center: { x: number; y: number };
247
-  };
248
-  hit: {
249
-    // The element the pointer is "hitting", is determined on the initial
250
-    // pointer down event
251
-    element: NonDeleted<ExcalidrawElement> | null;
252
-    // The elements the pointer is "hitting", is determined on the initial
253
-    // pointer down event
254
-    allHitElements: NonDeleted<ExcalidrawElement>[];
255
-    // This is determined on the initial pointer down event
256
-    wasAddedToSelection: boolean;
257
-    // Whether selected element(s) were duplicated, might change during the
258
-    // pointer interaction
259
-    hasBeenDuplicated: boolean;
260
-    hasHitCommonBoundingBoxOfSelectedElements: boolean;
261
-  };
262
-  withCmdOrCtrl: boolean;
263
-  drag: {
264
-    // Might change during the pointer interation
265
-    hasOccurred: boolean;
266
-    // Might change during the pointer interation
267
-    offset: { x: number; y: number } | null;
268
-  };
269
-  // We need to have these in the state so that we can unsubscribe them
270
-  eventListeners: {
271
-    // It's defined on the initial pointer down event
272
-    onMove: null | ((event: PointerEvent) => void);
273
-    // It's defined on the initial pointer down event
274
-    onUp: null | ((event: PointerEvent) => void);
275
-    // It's defined on the initial pointer down event
276
-    onKeyDown: null | ((event: KeyboardEvent) => void);
277
-    // It's defined on the initial pointer down event
278
-    onKeyUp: null | ((event: KeyboardEvent) => void);
279
-  };
280
-}>;
281
-
282
-export type ExcalidrawImperativeAPI = {
283
-  updateScene: InstanceType<typeof App>["updateScene"];
284
-  resetScene: InstanceType<typeof App>["resetScene"];
285
-  getSceneElementsIncludingDeleted: InstanceType<
286
-    typeof App
287
-  >["getSceneElementsIncludingDeleted"];
288
-  history: {
289
-    clear: InstanceType<typeof App>["resetHistory"];
290
-  };
291
-  scrollToContent: InstanceType<typeof App>["scrollToContent"];
292
-  getSceneElements: InstanceType<typeof App>["getSceneElements"];
293
-  getAppState: () => InstanceType<typeof App>["state"];
294
-  refresh: InstanceType<typeof App>["refresh"];
295
-  importLibrary: InstanceType<typeof App>["importLibraryFromUrl"];
296
-  setToastMessage: InstanceType<typeof App>["setToastMessage"];
297
-  readyPromise: ResolvablePromise<ExcalidrawImperativeAPI>;
298
-  ready: true;
299
-  id: string;
300
-};
301
-
302
 class App extends React.Component<AppProps, AppState> {
225
 class App extends React.Component<AppProps, AppState> {
303
   canvas: HTMLCanvasElement | null = null;
226
   canvas: HTMLCanvasElement | null = null;
304
   rc: RoughCanvas | null = null;
227
   rc: RoughCanvas | null = null;

+ 1
- 1
src/data/library.ts 查看文件

2
 import { LibraryItems, LibraryItem } from "../types";
2
 import { LibraryItems, LibraryItem } from "../types";
3
 import { restoreElements } from "./restore";
3
 import { restoreElements } from "./restore";
4
 import { getNonDeletedElements } from "../element";
4
 import { getNonDeletedElements } from "../element";
5
-import App from "../components/App";
5
+import type App from "../components/App";
6
 
6
 
7
 class Library {
7
 class Library {
8
   private libraryCache: LibraryItems | null = null;
8
   private libraryCache: LibraryItems | null = null;

+ 1
- 1
src/element/dragElements.ts 查看文件

5
 import { getPerfectElementSize } from "./sizeHelpers";
5
 import { getPerfectElementSize } from "./sizeHelpers";
6
 import Scene from "../scene/Scene";
6
 import Scene from "../scene/Scene";
7
 import { NonDeletedExcalidrawElement } from "./types";
7
 import { NonDeletedExcalidrawElement } from "./types";
8
-import { PointerDownState } from "../components/App";
8
+import { PointerDownState } from "../types";
9
 
9
 
10
 export const dragSelectedElements = (
10
 export const dragSelectedElements = (
11
   pointerDownState: PointerDownState,
11
   pointerDownState: PointerDownState,

+ 1
- 2
src/element/resizeElements.ts 查看文件

32
   MaybeTransformHandleType,
32
   MaybeTransformHandleType,
33
   TransformHandleDirection,
33
   TransformHandleDirection,
34
 } from "./transformHandles";
34
 } from "./transformHandles";
35
-import { PointerDownState } from "../components/App";
36
-import { Point } from "../types";
35
+import { Point, PointerDownState } from "../types";
37
 
36
 
38
 export const normalizeAngle = (angle: number): number => {
37
 export const normalizeAngle = (angle: number): number => {
39
   if (angle >= 2 * Math.PI) {
38
   if (angle >= 2 * Math.PI) {

+ 1
- 1
src/excalidraw-app/collab/CollabWrapper.tsx 查看文件

1
 import throttle from "lodash.throttle";
1
 import throttle from "lodash.throttle";
2
 import React, { PureComponent } from "react";
2
 import React, { PureComponent } from "react";
3
-import { ExcalidrawImperativeAPI } from "../../components/App";
3
+import { ExcalidrawImperativeAPI } from "../../types";
4
 import { ErrorDialog } from "../../components/ErrorDialog";
4
 import { ErrorDialog } from "../../components/ErrorDialog";
5
 import { APP_NAME, ENV, EVENT } from "../../constants";
5
 import { APP_NAME, ENV, EVENT } from "../../constants";
6
 import { ImportedDataState } from "../../data/types";
6
 import { ImportedDataState } from "../../data/types";

+ 1
- 2
src/excalidraw-app/index.tsx 查看文件

8
 } from "react";
8
 } from "react";
9
 import { trackEvent } from "../analytics";
9
 import { trackEvent } from "../analytics";
10
 import { getDefaultAppState } from "../appState";
10
 import { getDefaultAppState } from "../appState";
11
-import { ExcalidrawImperativeAPI } from "../components/App";
12
 import { ErrorDialog } from "../components/ErrorDialog";
11
 import { ErrorDialog } from "../components/ErrorDialog";
13
 import { TopErrorBoundary } from "../components/TopErrorBoundary";
12
 import { TopErrorBoundary } from "../components/TopErrorBoundary";
14
 import {
13
 import {
31
   defaultLang,
30
   defaultLang,
32
   languages,
31
   languages,
33
 } from "../packages/excalidraw/index";
32
 } from "../packages/excalidraw/index";
34
-import { AppState, LibraryItems } from "../types";
33
+import { AppState, LibraryItems, ExcalidrawImperativeAPI } from "../types";
35
 import {
34
 import {
36
   debounce,
35
   debounce,
37
   getVersion,
36
   getVersion,

src/tests/__snapshots__/excalidrawPackage.test.tsx.snap → src/tests/packages/__snapshots__/excalidraw.test.tsx.snap 查看文件


src/tests/excalidrawPackage.test.tsx → src/tests/packages/excalidraw.test.tsx 查看文件

1
 import React from "react";
1
 import React from "react";
2
-import { fireEvent, GlobalTestState, render } from "./test-utils";
3
-import Excalidraw from "../packages/excalidraw/index";
2
+import { fireEvent, GlobalTestState, render } from "../test-utils";
3
+import Excalidraw from "../../packages/excalidraw/index";
4
 import { queryByText, queryByTestId } from "@testing-library/react";
4
 import { queryByText, queryByTestId } from "@testing-library/react";
5
-import { GRID_SIZE } from "../constants";
6
-import { t } from "../i18n";
5
+import { GRID_SIZE } from "../../constants";
6
+import { t } from "../../i18n";
7
 
7
 
8
 const { h } = window;
8
 const { h } = window;
9
 
9
 

+ 80
- 1
src/types.ts 查看文件

16
 import { LinearElementEditor } from "./element/linearElementEditor";
16
 import { LinearElementEditor } from "./element/linearElementEditor";
17
 import { SuggestedBinding } from "./element/binding";
17
 import { SuggestedBinding } from "./element/binding";
18
 import { ImportedDataState } from "./data/types";
18
 import { ImportedDataState } from "./data/types";
19
-import { ExcalidrawImperativeAPI } from "./components/App";
19
+import type App from "./components/App";
20
 import type { ResolvablePromise } from "./utils";
20
 import type { ResolvablePromise } from "./utils";
21
 import { Spreadsheet } from "./charts";
21
 import { Spreadsheet } from "./charts";
22
 import { Language } from "./i18n";
22
 import { Language } from "./i18n";
23
 import { ClipboardData } from "./clipboard";
23
 import { ClipboardData } from "./clipboard";
24
+import { isOverScrollBars } from "./scene";
25
+import { MaybeTransformHandleType } from "./element/transformHandles";
24
 
26
 
25
 export type Point = Readonly<RoughPoint>;
27
 export type Point = Readonly<RoughPoint>;
26
 
28
 
249
   detectScroll: boolean;
251
   detectScroll: boolean;
250
   handleKeyboardGlobally: boolean;
252
   handleKeyboardGlobally: boolean;
251
 };
253
 };
254
+
255
+export type PointerDownState = Readonly<{
256
+  // The first position at which pointerDown happened
257
+  origin: Readonly<{ x: number; y: number }>;
258
+  // Same as "origin" but snapped to the grid, if grid is on
259
+  originInGrid: Readonly<{ x: number; y: number }>;
260
+  // Scrollbar checks
261
+  scrollbars: ReturnType<typeof isOverScrollBars>;
262
+  // The previous pointer position
263
+  lastCoords: { x: number; y: number };
264
+  // map of original elements data
265
+  originalElements: Map<string, NonDeleted<ExcalidrawElement>>;
266
+  resize: {
267
+    // Handle when resizing, might change during the pointer interaction
268
+    handleType: MaybeTransformHandleType;
269
+    // This is determined on the initial pointer down event
270
+    isResizing: boolean;
271
+    // This is determined on the initial pointer down event
272
+    offset: { x: number; y: number };
273
+    // This is determined on the initial pointer down event
274
+    arrowDirection: "origin" | "end";
275
+    // This is a center point of selected elements determined on the initial pointer down event (for rotation only)
276
+    center: { x: number; y: number };
277
+  };
278
+  hit: {
279
+    // The element the pointer is "hitting", is determined on the initial
280
+    // pointer down event
281
+    element: NonDeleted<ExcalidrawElement> | null;
282
+    // The elements the pointer is "hitting", is determined on the initial
283
+    // pointer down event
284
+    allHitElements: NonDeleted<ExcalidrawElement>[];
285
+    // This is determined on the initial pointer down event
286
+    wasAddedToSelection: boolean;
287
+    // Whether selected element(s) were duplicated, might change during the
288
+    // pointer interaction
289
+    hasBeenDuplicated: boolean;
290
+    hasHitCommonBoundingBoxOfSelectedElements: boolean;
291
+  };
292
+  withCmdOrCtrl: boolean;
293
+  drag: {
294
+    // Might change during the pointer interation
295
+    hasOccurred: boolean;
296
+    // Might change during the pointer interation
297
+    offset: { x: number; y: number } | null;
298
+  };
299
+  // We need to have these in the state so that we can unsubscribe them
300
+  eventListeners: {
301
+    // It's defined on the initial pointer down event
302
+    onMove: null | ((event: PointerEvent) => void);
303
+    // It's defined on the initial pointer down event
304
+    onUp: null | ((event: PointerEvent) => void);
305
+    // It's defined on the initial pointer down event
306
+    onKeyDown: null | ((event: KeyboardEvent) => void);
307
+    // It's defined on the initial pointer down event
308
+    onKeyUp: null | ((event: KeyboardEvent) => void);
309
+  };
310
+}>;
311
+
312
+export type ExcalidrawImperativeAPI = {
313
+  updateScene: InstanceType<typeof App>["updateScene"];
314
+  resetScene: InstanceType<typeof App>["resetScene"];
315
+  getSceneElementsIncludingDeleted: InstanceType<
316
+    typeof App
317
+  >["getSceneElementsIncludingDeleted"];
318
+  history: {
319
+    clear: InstanceType<typeof App>["resetHistory"];
320
+  };
321
+  scrollToContent: InstanceType<typeof App>["scrollToContent"];
322
+  getSceneElements: InstanceType<typeof App>["getSceneElements"];
323
+  getAppState: () => InstanceType<typeof App>["state"];
324
+  refresh: InstanceType<typeof App>["refresh"];
325
+  importLibrary: InstanceType<typeof App>["importLibraryFromUrl"];
326
+  setToastMessage: InstanceType<typeof App>["setToastMessage"];
327
+  readyPromise: ResolvablePromise<ExcalidrawImperativeAPI>;
328
+  ready: true;
329
+  id: string;
330
+};

Loading…
取消
儲存