From ade2565f497243a5e428f4906d8ed80c872fd981 Mon Sep 17 00:00:00 2001 From: Aakansha Doshi Date: Mon, 4 Jan 2021 02:21:52 +0530 Subject: [PATCH] feat: add langCode and renderFooter props (#2644) Co-authored-by: Aakansha Doshi Co-authored-by: dwelle --- src/components/App.tsx | 19 ++- src/components/InitializeApp.tsx | 23 ++- src/components/LayerUI.tsx | 20 +-- src/components/MobileMenu.tsx | 15 +- .../components/LanguageList.tsx | 18 +-- src/excalidraw-app/index.tsx | 60 +++++++- src/i18n.ts | 127 +++++++---------- src/packages/excalidraw/CHANGELOG.md | 3 + src/packages/excalidraw/README.md | 20 +++ src/packages/excalidraw/index.tsx | 8 +- .../regressionTests.test.tsx.snap | 134 +++++++++--------- src/tests/align.test.tsx | 4 +- src/tests/regressionTests.test.tsx | 9 +- src/types.ts | 3 + 14 files changed, 271 insertions(+), 192 deletions(-) rename src/{ => excalidraw-app}/components/LanguageList.tsx (58%) diff --git a/src/components/App.tsx b/src/components/App.tsx index ecce0fd2..30c069f4 100644 --- a/src/components/App.tsx +++ b/src/components/App.tsx @@ -111,7 +111,7 @@ import { selectGroupsForSelectedElements, } from "../groups"; import { createHistory, SceneHistory } from "../history"; -import { getLanguage, t } from "../i18n"; +import { t, getLanguage, setLanguage, languages, defaultLang } from "../i18n"; import { CODES, getResizeCenterPointKey, @@ -332,7 +332,7 @@ class App extends React.Component { offsetLeft, } = this.state; - const { onCollabButtonClick, onExportToBackend } = this.props; + const { onCollabButtonClick, onExportToBackend, renderFooter } = this.props; const canvasScale = window.devicePixelRatio; const canvasWidth = canvasDOMWidth * canvasScale; @@ -369,9 +369,10 @@ class App extends React.Component { } zenModeEnabled={zenModeEnabled} toggleZenMode={this.toggleZenMode} - lng={getLanguage().lng} + langCode={getLanguage().code} isCollaborating={this.props.isCollaborating || false} onExportToBackend={onExportToBackend} + renderCustomFooter={renderFooter} /> {this.state.showStats && ( { } componentDidUpdate(prevProps: ExcalidrawProps, prevState: AppState) { + if (prevProps.langCode !== this.props.langCode) { + this.updateLanguage(); + } + if ( prevProps.width !== this.props.width || prevProps.height !== this.props.height || @@ -3849,6 +3854,14 @@ class App extends React.Component { offsetTop: typeof offsets?.offsetTop === "number" ? offsets.offsetTop : 0, }; } + + private async updateLanguage() { + const currentLang = + languages.find((lang) => lang.code === this.props.langCode) || + defaultLang; + await setLanguage(currentLang); + this.setAppState({}); + } } // ----------------------------------------------------------------------------- diff --git a/src/components/InitializeApp.tsx b/src/components/InitializeApp.tsx index 091f726d..ae0bea03 100644 --- a/src/components/InitializeApp.tsx +++ b/src/components/InitializeApp.tsx @@ -1,18 +1,29 @@ import React from "react"; import { LoadingMessage } from "./LoadingMessage"; -import { setLanguageFirstTime } from "../i18n"; +import { + defaultLang, + Language, + languages, + setLanguageFirstTime, +} from "../i18n"; -export class InitializeApp extends React.Component< - any, - { isLoading: boolean } -> { +interface Props { + langCode: Language["code"]; +} +interface State { + isLoading: boolean; +} +export class InitializeApp extends React.Component { public state: { isLoading: boolean } = { isLoading: true, }; async componentDidMount() { - await setLanguageFirstTime(); + const currentLang = + languages.find((lang) => lang.code === this.props.langCode) || + defaultLang; + await setLanguageFirstTime(currentLang); this.setState({ isLoading: false, }); diff --git a/src/components/LayerUI.tsx b/src/components/LayerUI.tsx index 5a806e82..dd09ffef 100644 --- a/src/components/LayerUI.tsx +++ b/src/components/LayerUI.tsx @@ -19,8 +19,7 @@ import { FixedSideContainer } from "./FixedSideContainer"; import { UserList } from "./UserList"; import { LockIcon } from "./LockIcon"; import { ExportDialog, ExportCB } from "./ExportDialog"; -import { LanguageList } from "./LanguageList"; -import { t, languages, setLanguage } from "../i18n"; +import { Language, t } from "../i18n"; import { HintViewer } from "./HintViewer"; import useIsMobile from "../is-mobile"; @@ -64,13 +63,14 @@ interface LayerUIProps { onInsertElements: (elements: readonly NonDeletedExcalidrawElement[]) => void; zenModeEnabled: boolean; toggleZenMode: () => void; - lng: string; + langCode: Language["code"]; isCollaborating: boolean; onExportToBackend?: ( exportedElements: readonly NonDeletedExcalidrawElement[], appState: AppState, canvas: HTMLCanvasElement | null, ) => void; + renderCustomFooter?: (isMobile: boolean) => JSX.Element; } const useOnClickOutside = ( @@ -316,6 +316,7 @@ const LayerUI = ({ toggleZenMode, isCollaborating, onExportToBackend, + renderCustomFooter, }: LayerUIProps) => { const isMobile = useIsMobile(); @@ -551,14 +552,7 @@ const LayerUI = ({ "transition-right disable-pointerEvents": zenModeEnabled, })} > - { - await setLanguage(lng); - setAppState({}); - }} - languages={languages} - floating - /> + {renderCustomFooter?.(false)} {actionManager.renderAction("toggleShortcuts")}