浏览代码

Refactor: convert initializeApp to func component and use JSX transform in the codebase (#4056)

Aakansha Doshi 3 年之前
父节点
当前提交
ff29780760
共有 45 个文件被更改,包括 20 次插入70 次删除
  1. 0 1
      src/actions/actionAlign.tsx
  2. 0 1
      src/actions/actionCanvas.tsx
  3. 0 1
      src/actions/actionDeleteSelected.tsx
  4. 0 1
      src/actions/actionDistribute.tsx
  5. 0 1
      src/actions/actionDuplicateSelection.tsx
  6. 0 1
      src/actions/actionExport.tsx
  7. 0 1
      src/actions/actionFinalize.tsx
  8. 0 1
      src/actions/actionGroup.tsx
  9. 0 1
      src/actions/actionHistory.tsx
  10. 0 1
      src/actions/actionMenu.tsx
  11. 0 1
      src/actions/actionNavigate.tsx
  12. 0 1
      src/actions/actionProperties.tsx
  13. 0 1
      src/components/ActiveFile.tsx
  14. 0 1
      src/components/ButtonIconSelect.tsx
  15. 0 1
      src/components/ButtonSelect.tsx
  16. 0 1
      src/components/CollabButton.tsx
  17. 0 1
      src/components/ContextMenu.tsx
  18. 0 1
      src/components/DarkModeToggle.tsx
  19. 0 1
      src/components/HelpIcon.tsx
  20. 0 1
      src/components/HintViewer.tsx
  21. 15 20
      src/components/InitializeApp.tsx
  22. 1 1
      src/components/LibraryUnit.tsx
  23. 0 1
      src/components/LoadingMessage.tsx
  24. 1 1
      src/components/Toast.tsx
  25. 1 1
      src/excalidraw-app/CustomStats.tsx
  26. 1 1
      src/excalidraw-app/collab/CollabWrapper.tsx
  27. 1 7
      src/excalidraw-app/index.tsx
  28. 0 1
      src/index.tsx
  29. 0 1
      src/shapes.tsx
  30. 0 1
      src/tests/align.test.tsx
  31. 0 1
      src/tests/appState.test.tsx
  32. 0 1
      src/tests/binding.test.tsx
  33. 0 1
      src/tests/collab.test.tsx
  34. 0 1
      src/tests/dragCreate.test.tsx
  35. 0 1
      src/tests/export.test.tsx
  36. 0 1
      src/tests/flip.test.tsx
  37. 0 1
      src/tests/history.test.tsx
  38. 0 1
      src/tests/library.test.tsx
  39. 0 1
      src/tests/multiPointCreate.test.tsx
  40. 0 1
      src/tests/packages/excalidraw.test.tsx
  41. 0 1
      src/tests/resize.test.tsx
  42. 0 1
      src/tests/scroll.test.tsx
  43. 0 1
      src/tests/selection.test.tsx
  44. 0 1
      src/tests/viewMode.test.tsx
  45. 0 1
      src/tests/zindex.test.tsx

+ 0 - 1
src/actions/actionAlign.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { alignElements, Alignment } from "../align";
 import {
   AlignBottomIcon,

+ 0 - 1
src/actions/actionCanvas.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { getDefaultAppState } from "../appState";
 import { ColorPicker } from "../components/ColorPicker";
 import { trash, zoomIn, zoomOut } from "../components/icons";

+ 0 - 1
src/actions/actionDeleteSelected.tsx

@@ -1,7 +1,6 @@
 import { isSomeElementSelected } from "../scene";
 import { KEYS } from "../keys";
 import { ToolButton } from "../components/ToolButton";
-import React from "react";
 import { trash } from "../components/icons";
 import { t } from "../i18n";
 import { register } from "./register";

+ 0 - 1
src/actions/actionDistribute.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import {
   DistributeHorizontallyIcon,
   DistributeVerticallyIcon,

+ 0 - 1
src/actions/actionDuplicateSelection.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { KEYS } from "../keys";
 import { register } from "./register";
 import { ExcalidrawElement } from "../element/types";

+ 0 - 1
src/actions/actionExport.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { trackEvent } from "../analytics";
 import { load, questionCircle, saveAs } from "../components/icons";
 import { ProjectName } from "../components/ProjectName";

+ 0 - 1
src/actions/actionFinalize.tsx

@@ -1,7 +1,6 @@
 import { KEYS } from "../keys";
 import { isInvisiblySmallElement } from "../element";
 import { resetCursor } from "../utils";
-import React from "react";
 import { ToolButton } from "../components/ToolButton";
 import { done } from "../components/icons";
 import { t } from "../i18n";

+ 0 - 1
src/actions/actionGroup.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { CODES, KEYS } from "../keys";
 import { t } from "../i18n";
 import { getShortcutKey } from "../utils";

+ 0 - 1
src/actions/actionHistory.tsx

@@ -1,5 +1,4 @@
 import { Action, ActionResult } from "./types";
-import React from "react";
 import { undo, redo } from "../components/icons";
 import { ToolButton } from "../components/ToolButton";
 import { t } from "../i18n";

+ 0 - 1
src/actions/actionMenu.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { menu, palette } from "../components/icons";
 import { ToolButton } from "../components/ToolButton";
 import { t } from "../i18n";

+ 0 - 1
src/actions/actionNavigate.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { getClientColors, getClientInitials } from "../clients";
 import { Avatar } from "../components/Avatar";
 import { centerScrollOn } from "../scene/scroll";

+ 0 - 1
src/actions/actionProperties.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { AppState } from "../../src/types";
 import { ButtonIconSelect } from "../components/ButtonIconSelect";
 import { ColorPicker } from "../components/ColorPicker";

+ 0 - 1
src/components/ActiveFile.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import Stack from "../components/Stack";
 import { ToolButton } from "../components/ToolButton";
 import { save, file } from "../components/icons";

+ 0 - 1
src/components/ButtonIconSelect.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import clsx from "clsx";
 
 // TODO: It might be "clever" to add option.icon to the existing component <ButtonSelect />

+ 0 - 1
src/components/ButtonSelect.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import clsx from "clsx";
 
 export const ButtonSelect = <T extends Object>({

+ 0 - 1
src/components/CollabButton.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import clsx from "clsx";
 import { ToolButton } from "./ToolButton";
 import { t } from "../i18n";

+ 0 - 1
src/components/ContextMenu.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { render, unmountComponentAtNode } from "react-dom";
 import clsx from "clsx";
 import { Popover } from "./Popover";

+ 0 - 1
src/components/DarkModeToggle.tsx

@@ -1,6 +1,5 @@
 import "./ToolIcon.scss";
 
-import React from "react";
 import { t } from "../i18n";
 import { ToolButton } from "./ToolButton";
 import { THEME } from "../constants";

+ 0 - 1
src/components/HelpIcon.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { questionCircle } from "../components/icons";
 
 type HelpIconProps = {

+ 0 - 1
src/components/HintViewer.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { t } from "../i18n";
 import { NonDeletedExcalidrawElement } from "../element/types";
 import { getSelectedElements } from "../scene";

+ 15 - 20
src/components/InitializeApp.tsx

@@ -1,30 +1,25 @@
-import React from "react";
+import React, { useEffect, useState } from "react";
 
 import { LoadingMessage } from "./LoadingMessage";
 import { defaultLang, Language, languages, setLanguage } from "../i18n";
 
 interface Props {
   langCode: Language["code"];
+  children: React.ReactElement;
 }
-interface State {
-  isLoading: boolean;
-}
-export class InitializeApp extends React.Component<Props, State> {
-  public state: { isLoading: boolean } = {
-    isLoading: true,
-  };
 
-  async componentDidMount() {
+export const InitializeApp = (props: Props) => {
+  const [loading, setLoading] = useState(true);
+
+  useEffect(() => {
+    const updateLang = async () => {
+      await setLanguage(currentLang);
+    };
     const currentLang =
-      languages.find((lang) => lang.code === this.props.langCode) ||
-      defaultLang;
-    await setLanguage(currentLang);
-    this.setState({
-      isLoading: false,
-    });
-  }
+      languages.find((lang) => lang.code === props.langCode) || defaultLang;
+    updateLang();
+    setLoading(false);
+  }, [props.langCode]);
 
-  public render() {
-    return this.state.isLoading ? <LoadingMessage /> : this.props.children;
-  }
-}
+  return loading ? <LoadingMessage /> : props.children;
+};

+ 1 - 1
src/components/LibraryUnit.tsx

@@ -1,6 +1,6 @@
 import clsx from "clsx";
 import oc from "open-color";
-import React, { useEffect, useRef, useState } from "react";
+import { useEffect, useRef, useState } from "react";
 import { close } from "../components/icons";
 import { MIME_TYPES } from "../constants";
 import { t } from "../i18n";

+ 0 - 1
src/components/LoadingMessage.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { t } from "../i18n";
 
 export const LoadingMessage = () => {

+ 1 - 1
src/components/Toast.tsx

@@ -1,4 +1,4 @@
-import React, { useCallback, useEffect, useRef } from "react";
+import { useCallback, useEffect, useRef } from "react";
 import { TOAST_TIMEOUT } from "../constants";
 import "./Toast.scss";
 

+ 1 - 1
src/excalidraw-app/CustomStats.tsx

@@ -1,4 +1,4 @@
-import React, { useEffect, useState } from "react";
+import { useEffect, useState } from "react";
 import { debounce, getVersion, nFormatter } from "../utils";
 import {
   getElementsStorageSize,

+ 1 - 1
src/excalidraw-app/collab/CollabWrapper.tsx

@@ -1,5 +1,5 @@
 import throttle from "lodash.throttle";
-import React, { PureComponent } from "react";
+import { PureComponent } from "react";
 import { ExcalidrawImperativeAPI } from "../../types";
 import { ErrorDialog } from "../../components/ErrorDialog";
 import { APP_NAME, ENV, EVENT } from "../../constants";

+ 1 - 7
src/excalidraw-app/index.tsx

@@ -1,11 +1,5 @@
 import LanguageDetector from "i18next-browser-languagedetector";
-import React, {
-  useCallback,
-  useContext,
-  useEffect,
-  useRef,
-  useState,
-} from "react";
+import { useCallback, useContext, useEffect, useRef, useState } from "react";
 import { trackEvent } from "../analytics";
 import { getDefaultAppState } from "../appState";
 import { ErrorDialog } from "../components/ErrorDialog";

+ 0 - 1
src/index.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import ReactDOM from "react-dom";
 import ExcalidrawApp from "./excalidraw-app";
 

+ 0 - 1
src/shapes.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { KEYS } from "./keys";
 
 // We inline font-awesome icons in order to save on js size rather than including the font awesome react library

+ 0 - 1
src/tests/align.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import ReactDOM from "react-dom";
 import { render } from "./test-utils";
 import ExcalidrawApp from "../excalidraw-app";

+ 0 - 1
src/tests/appState.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { render, waitFor } from "./test-utils";
 import ExcalidrawApp from "../excalidraw-app";
 import { API } from "./helpers/api";

+ 0 - 1
src/tests/binding.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { fireEvent, render } from "./test-utils";
 import ExcalidrawApp from "../excalidraw-app";
 import { UI, Pointer, Keyboard } from "./helpers/ui";

+ 0 - 1
src/tests/collab.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { render, updateSceneData, waitFor } from "./test-utils";
 import ExcalidrawApp from "../excalidraw-app";
 import { API } from "./helpers/api";

+ 0 - 1
src/tests/dragCreate.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import ReactDOM from "react-dom";
 import ExcalidrawApp from "../excalidraw-app";
 import * as Renderer from "../renderer/renderScene";

+ 0 - 1
src/tests/export.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { render, waitFor } from "./test-utils";
 import ExcalidrawApp from "../excalidraw-app";
 import { API } from "./helpers/api";

+ 0 - 1
src/tests/flip.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import ReactDOM from "react-dom";
 import { render } from "./test-utils";
 import App from "../components/App";

+ 0 - 1
src/tests/history.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { assertSelectedElements, render } from "./test-utils";
 import ExcalidrawApp from "../excalidraw-app";
 import { Keyboard, Pointer, UI } from "./helpers/ui";

+ 0 - 1
src/tests/library.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { render, waitFor } from "./test-utils";
 import ExcalidrawApp from "../excalidraw-app";
 import { API } from "./helpers/api";

+ 0 - 1
src/tests/multiPointCreate.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import ReactDOM from "react-dom";
 import {
   render,

+ 0 - 1
src/tests/packages/excalidraw.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { fireEvent, GlobalTestState, render } from "../test-utils";
 import Excalidraw from "../../packages/excalidraw/index";
 import { queryByText, queryByTestId } from "@testing-library/react";

+ 0 - 1
src/tests/resize.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import ReactDOM from "react-dom";
 import { render } from "./test-utils";
 import App from "../components/App";

+ 0 - 1
src/tests/scroll.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import {
   mockBoundingClientRect,
   render,

+ 0 - 1
src/tests/selection.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import ReactDOM from "react-dom";
 import {
   render,

+ 0 - 1
src/tests/viewMode.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import { render, GlobalTestState } from "./test-utils";
 import ExcalidrawApp from "../excalidraw-app";
 import { KEYS } from "../keys";

+ 0 - 1
src/tests/zindex.test.tsx

@@ -1,4 +1,3 @@
-import React from "react";
 import ReactDOM from "react-dom";
 import { render } from "./test-utils";
 import ExcalidrawApp from "../excalidraw-app";