11import type * as monaco from "monaco-editor" ;
2- import { Component , createRef , type RefObject } from "react" ;
2+ import { Component , createRef , lazy , Suspense } from "react" ;
33import { ErrorBoundary } from "react-error-boundary" ;
4+ import { BarLoader } from "react-spinners" ;
45import SplitPane from "react-split-pane" ;
56
67import { getSplitConfig , saveSplitConfig } from "../config.js" ;
78import type { SupportedEngine , SupportedFormat } from "../rendering.js" ;
8- import EditorPane from "./EditorPane.js" ;
9- import GraphPane from "./GraphPane.js" ;
9+ import type EditorPane from "./EditorPane.js" ;
10+
11+ const EditorLazy = lazy ( ( ) => import ( "./EditorPane.js" ) ) ;
12+ const GraphPaneLazy = lazy ( ( ) => import ( "./GraphPane.js" ) ) ;
1013
1114type ErrorList = monaco . editor . IMarkerData [ ] ;
1215
13- interface Props {
16+ export type SplitEditorProps = {
1417 initialSource : string ;
1518 format : SupportedFormat ;
1619 engine : SupportedEngine ;
1720 onSourceChange ?( source : string ) : void ;
18- }
21+ } ;
1922
20- type State = SourceState | ErroredState ;
21- interface SourceState {
23+ type SplitEditorState = SourceState | ErroredState ;
24+ type SourceState = {
2225 dotSrc : string ;
23- errors : undefined ;
24- lastKnownGoodSrc : undefined ;
25- }
26- interface ErroredState {
27- dotSrc : undefined ;
26+ errors ? : undefined ;
27+ lastKnownGoodSrc ? : undefined ;
28+ } ;
29+ type ErroredState = {
30+ dotSrc ? : undefined ;
2831 errors : ErrorList ;
2932 lastKnownGoodSrc ?: string ;
30- }
33+ } ;
3134
32- const createSourceState = ( dotSrc : string ) : SourceState => ( {
33- dotSrc,
34- errors : undefined ,
35- lastKnownGoodSrc : undefined ,
36- } ) ;
37- const createErroredState = (
38- errors : ErrorList ,
39- lastKnownGoodSrc ?: string ,
40- ) : ErroredState => ( { dotSrc : undefined , errors, lastKnownGoodSrc } ) ;
35+ const loadingStyle = {
36+ position : "absolute" ,
37+ display : "flex" ,
38+ inset : "0" ,
39+ justifyContent : "center" ,
40+ alignItems : "center" ,
41+ } as const ;
4142
42- export default class SplitEditor extends Component < Props , State > {
43- #editorPaneRef: RefObject < EditorPane | null > = createRef < EditorPane > ( ) ;
43+ export default class SplitEditor extends Component <
44+ SplitEditorProps ,
45+ SplitEditorState
46+ > {
47+ #editorPaneRef = createRef < EditorPane > ( ) ;
4448
45- constructor ( props : Props ) {
49+ constructor ( props : SplitEditorProps ) {
4650 super ( props ) ;
4751 const p = this . props ;
4852
49- this . state = createSourceState ( p . initialSource ) ;
53+ this . state = { dotSrc : p . initialSource } ;
5054 if ( p . onSourceChange ) p . onSourceChange ( this . state . dotSrc ) ;
5155 }
5256
@@ -64,14 +68,14 @@ export default class SplitEditor extends Component<Props, State> {
6468 const p = this . props ;
6569 if ( p . onSourceChange ) p . onSourceChange ( dotSrc ) ;
6670
67- this . setState ( createSourceState ( dotSrc ) ) ;
71+ this . setState ( { dotSrc } ) ;
6872 } ;
6973
7074 dotSourceErrored = ( errors : ErrorList ) : void => {
71- this . setState ( prevState => {
72- const lastKnownGoodSrc = prevState . dotSrc || prevState . lastKnownGoodSrc ;
73- return createErroredState ( errors , lastKnownGoodSrc ) ;
74- } ) ;
75+ this . setState ( prev => ( {
76+ errors ,
77+ lastKnownGoodSrc : prev . dotSrc || prev . lastKnownGoodSrc ,
78+ } ) ) ;
7579 } ;
7680
7781 render ( ) {
@@ -88,20 +92,36 @@ export default class SplitEditor extends Component<Props, State> {
8892 { ...( { } as any ) }
8993 >
9094 < ErrorBoundary fallback = "Could not load editor" >
91- < EditorPane
92- ref = { this . #editorPaneRef}
93- defaultValue = { s . dotSrc }
94- onChangeValue = { this . dotSourceChanged }
95- onValueError = { this . dotSourceErrored }
96- />
95+ < Suspense
96+ fallback = {
97+ < div style = { loadingStyle } >
98+ < BarLoader />
99+ </ div >
100+ }
101+ >
102+ < EditorLazy
103+ ref = { this . #editorPaneRef}
104+ defaultValue = { s . dotSrc }
105+ onChangeValue = { this . dotSourceChanged }
106+ onValueError = { this . dotSourceErrored }
107+ />
108+ </ Suspense >
97109 </ ErrorBoundary >
98110 < ErrorBoundary fallback = "Could not load graph preview" >
99- < GraphPane
100- hasErrors = { ! ! ( s . errors && s . errors . length > 0 ) }
101- dotSrc = { s . dotSrc || s . lastKnownGoodSrc || "" }
102- engine = { p . engine }
103- format = { p . format }
104- />
111+ < Suspense
112+ fallback = {
113+ < div style = { loadingStyle } >
114+ < BarLoader />
115+ </ div >
116+ }
117+ >
118+ < GraphPaneLazy
119+ hasErrors = { ! ! ( s . errors && s . errors . length > 0 ) }
120+ dotSrc = { s . dotSrc || s . lastKnownGoodSrc || "" }
121+ engine = { p . engine }
122+ format = { p . format }
123+ />
124+ </ Suspense >
105125 </ ErrorBoundary >
106126 </ SplitPane >
107127 ) ;
0 commit comments