-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: migrate to typescript project reference
- Loading branch information
Showing
60 changed files
with
1,302 additions
and
857 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,3 +178,8 @@ pip-log.txt | |
|
||
# Mac crap | ||
.DS_Store | ||
|
||
################# | ||
## MISC | ||
################# | ||
.ts-build |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
**/.ts-build | ||
**/dist | ||
**/release | ||
**/coverage | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,26 @@ | ||
import './style/global.less'; | ||
import React from 'react'; | ||
import { createRoot, Root } from 'react-dom/client'; | ||
import { createRoot } from 'react-dom/client'; | ||
import { RootStore } from './store/root.store'; | ||
import { JetbrainsLogo } from '@apex/svg-project'; | ||
import { RootContext } from './context'; | ||
import { TimerView } from './component/mobx/Timer.View'; | ||
import { ThemeSwitchView, ThemeSwitchView2 } from './component/mobx/ThemeSwitch.View'; | ||
import { MobxThemeSwitch } from './component/mobx/MobxThemeSwitch'; | ||
import { MobxThemeSwitch2 } from './component/mobx/MobxThemeSwitch2'; | ||
import { Section } from './component/mobx/Section'; | ||
|
||
export class App { | ||
root: Root; | ||
rootStore: RootStore; | ||
timer: number; | ||
export function createReactApp(container: HTMLElement, node?: HTMLElement) { | ||
const rootStore = new RootStore(); | ||
const root = createRoot(container); | ||
|
||
constructor( | ||
readonly container: HTMLElement, | ||
readonly rate?: number, | ||
readonly node?: HTMLElement, | ||
) { | ||
this.rootStore = new RootStore(); | ||
this.root = createRoot(container); | ||
this.render(); | ||
this.startCount(); | ||
} | ||
root.render( | ||
<RootContext.Provider value={rootStore}> | ||
<Section /> | ||
<Section /> | ||
<p style={{ width: 128 }} dangerouslySetInnerHTML={{ __html: JetbrainsLogo }} /> | ||
<MobxThemeSwitch /> | ||
<MobxThemeSwitch2 /> | ||
</RootContext.Provider>, | ||
); | ||
|
||
render() { | ||
this.root.render( | ||
<RootContext.Provider value={this.rootStore}> | ||
<TimerView node={this.node} tip={this.rate === 1 || this.rate == null ? null : `${this.rate}x`} /> | ||
<p style={{ width: 128 }} dangerouslySetInnerHTML={{ __html: JetbrainsLogo }} /> | ||
<ThemeSwitchView /> | ||
<ThemeSwitchView2 /> | ||
</RootContext.Provider>, | ||
); | ||
} | ||
|
||
startCount() { | ||
this.timer = window.setInterval(() => this.rootStore.timerStore.increaseTimer(this.rate), 1000); | ||
} | ||
|
||
dispose() { | ||
clearInterval(this.timer); | ||
this.root.unmount(); | ||
this.root = null; | ||
this.rootStore = null; | ||
this.timer = null; | ||
} | ||
return () => root.unmount(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
interface ISubmitProps { | ||
submitted: boolean; | ||
onClick: () => void; | ||
} | ||
|
||
/** | ||
* @desc Pure React Component (Can reuse in other project) | ||
*/ | ||
export function Submit({ submitted, onClick }: ISubmitProps) { | ||
return ( | ||
<p> | ||
<button onClick={onClick} disabled={submitted}> | ||
Submit Button (App Level) | ||
</button> | ||
</p> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { useStore } from '../../context'; | ||
import { observer } from 'mobx-react-lite'; | ||
import { Submit } from '../atom/Submit'; | ||
import { useCallback } from 'react'; | ||
|
||
/** | ||
* @desc View with Store (Cannot reuse without same store) | ||
*/ | ||
export const MobxSubmit = observer(() => { | ||
// App store (Mobx) | ||
const { submitStore } = useStore(); | ||
const onClick = useCallback(() => { | ||
submitStore.toggle(); | ||
setTimeout(() => submitStore.toggle(), 3000); | ||
}, []); | ||
|
||
return <Submit submitted={submitStore.submitted} onClick={onClick} />; | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { observer } from 'mobx-react-lite'; | ||
import { useEffect } from 'react'; | ||
import { themeStore } from '../../store/global/theme.store'; | ||
|
||
export const MobxThemeSwitch = observer(() => { | ||
useEffect(() => { | ||
document.body.dataset.theme = themeStore.theme; | ||
}, [themeStore.theme]); | ||
|
||
return ( | ||
<p> | ||
<button onClick={() => themeStore.toggle()}>Switch Theme (Global Level: import global store)</button> | ||
</p> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { observer } from 'mobx-react-lite'; | ||
import { useStore } from '../../context'; | ||
import { useEffect } from 'react'; | ||
|
||
/** | ||
* @desc Only demo | ||
*/ | ||
export const MobxThemeSwitch2 = observer(() => { | ||
/** | ||
* Use ReactContext instead of import variable. | ||
*/ | ||
const { themeStore } = useStore(); | ||
|
||
useEffect(() => { | ||
document.body.dataset.theme = themeStore.theme; | ||
}, [themeStore.theme]); | ||
|
||
return ( | ||
<p> | ||
<button onClick={() => themeStore.toggle()}>Switch Theme2 (Global Level: useContext)</button> | ||
</p> | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Counter } from '../atom/Counter'; | ||
import { Heading } from '../atom/Heading'; | ||
import { MobxSubmit } from './MobxSubmit'; | ||
|
||
/** | ||
* @desc Should put it to page or section directory. Here is a demo. | ||
*/ | ||
export const Section = () => { | ||
return ( | ||
<> | ||
{/* It's a memo component */} | ||
<Heading /> | ||
|
||
{/* It's a pure react component */} | ||
{/* DON'T USE THIS in Mobx, it's only a demo */} | ||
{/* Wrap it with Mobx.observer if you want to use it in mobx environment */} | ||
<Counter /> | ||
|
||
{/* Share application store */} | ||
<MobxSubmit /> | ||
</> | ||
); | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,21 @@ | ||
import { App } from './app'; | ||
import { createReactApp } from './app'; | ||
|
||
function getCustomNode(text: string) { | ||
const span = document.createElement('span'); | ||
span.textContent = text; | ||
return span; | ||
function getElementById(id: string) { | ||
return document.getElementById(id) as HTMLElement; | ||
} | ||
|
||
const app1Container = document.getElementById('app1'); | ||
const app2Container = document.getElementById('app2'); | ||
const app3Container = document.getElementById('app3'); | ||
const app1Container = getElementById('app1'); | ||
const app2Container = getElementById('app2'); | ||
|
||
const app1 = new App(app1Container); | ||
const app2 = new App(app2Container, 2); | ||
const app3 = new App(app3Container, 3, getCustomNode('(app3)')); | ||
const disposeApp1 = createReactApp(app1Container); | ||
const disposeApp2 = createReactApp(app2Container); | ||
|
||
setTimeout(() => { | ||
app3.dispose(); | ||
console.log('[debug] app3 has been disposed'); | ||
disposeApp2(); | ||
console.log('[debug] app2 has been disposed'); | ||
|
||
setTimeout(() => { | ||
new App(app3Container, 4, getCustomNode('(app3 with rate=4)')); | ||
console.log('[debug] re-render app3 with rate=4'); | ||
createReactApp(app2Container); | ||
console.log('[debug] re-render app2'); | ||
}, 2000); | ||
}, 4000); |
Oops, something went wrong.