Przeglądaj źródła

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 lat temu
rodzic
commit
15f02ba191
No account linked to committer's email address

+ 2
- 79
src/components/App.tsx Wyświetl plik

@@ -111,7 +111,6 @@ import {
111 111
 import { LinearElementEditor } from "../element/linearElementEditor";
112 112
 import { mutateElement } from "../element/mutateElement";
113 113
 import { deepCopyElement, newFreeDrawElement } from "../element/newElement";
114
-import { MaybeTransformHandleType } from "../element/transformHandles";
115 114
 import {
116 115
   isBindingElement,
117 116
   isBindingElementType,
@@ -167,9 +166,11 @@ import { findShapeByKey } from "../shapes";
167 166
 import {
168 167
   AppProps,
169 168
   AppState,
169
+  ExcalidrawImperativeAPI,
170 170
   Gesture,
171 171
   GestureEvent,
172 172
   LibraryItems,
173
+  PointerDownState,
173 174
   SceneData,
174 175
 } from "../types";
175 176
 import {
@@ -180,7 +181,6 @@ import {
180 181
   isToolIcon,
181 182
   isWritableElement,
182 183
   resetCursor,
183
-  ResolvablePromise,
184 184
   resolvablePromise,
185 185
   sceneCoordsToViewportCoords,
186 186
   setCursor,
@@ -222,83 +222,6 @@ const gesture: Gesture = {
222 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 225
 class App extends React.Component<AppProps, AppState> {
303 226
   canvas: HTMLCanvasElement | null = null;
304 227
   rc: RoughCanvas | null = null;

+ 1
- 1
src/data/library.ts Wyświetl plik

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

+ 1
- 1
src/element/dragElements.ts Wyświetl plik

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

+ 1
- 2
src/element/resizeElements.ts Wyświetl plik

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

+ 1
- 1
src/excalidraw-app/collab/CollabWrapper.tsx Wyświetl plik

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

+ 1
- 2
src/excalidraw-app/index.tsx Wyświetl plik

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

src/tests/__snapshots__/excalidrawPackage.test.tsx.snap → src/tests/packages/__snapshots__/excalidraw.test.tsx.snap Wyświetl plik


src/tests/excalidrawPackage.test.tsx → src/tests/packages/excalidraw.test.tsx Wyświetl plik

@@ -1,9 +1,9 @@
1 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 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 8
 const { h } = window;
9 9
 

+ 80
- 1
src/types.ts Wyświetl plik

@@ -16,11 +16,13 @@ import { Point as RoughPoint } from "roughjs/bin/geometry";
16 16
 import { LinearElementEditor } from "./element/linearElementEditor";
17 17
 import { SuggestedBinding } from "./element/binding";
18 18
 import { ImportedDataState } from "./data/types";
19
-import { ExcalidrawImperativeAPI } from "./components/App";
19
+import type App from "./components/App";
20 20
 import type { ResolvablePromise } from "./utils";
21 21
 import { Spreadsheet } from "./charts";
22 22
 import { Language } from "./i18n";
23 23
 import { ClipboardData } from "./clipboard";
24
+import { isOverScrollBars } from "./scene";
25
+import { MaybeTransformHandleType } from "./element/transformHandles";
24 26
 
25 27
 export type Point = Readonly<RoughPoint>;
26 28
 
@@ -249,3 +251,80 @@ export type AppProps = ExcalidrawProps & {
249 251
   detectScroll: boolean;
250 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
+};

Ładowanie…
Anuluj
Zapisz