@@ -17,6 +17,9 @@ export type EditorAPI = {
1717
1818const fontPromise = new FontFaceObserver ( 'dm' ) . load ( ) . catch ( ( ) => { } ) ;
1919
20+ let serviceCache ;
21+ let editorPart ;
22+
2023class MonacoEditor extends React . PureComponent {
2124 constructor ( props ) {
2225 super ( props ) ;
@@ -49,8 +52,35 @@ class MonacoEditor extends React.PureComponent {
4952 } ) ;
5053 } ;
5154
55+ initializeEditor ( container , cb ) {
56+ if ( serviceCache ) {
57+ cb ( serviceCache ) ;
58+ return ;
59+ }
60+
61+ const context = this . props . context || window ;
62+ const [ { CodeSandboxService } ] = [
63+ window . require (
64+ 'vs/codesandbox/services/codesandbox/browser/codesandboxService'
65+ ) ,
66+ ] ;
67+
68+ context . monaco . editor . create (
69+ container ,
70+ { } ,
71+ {
72+ codesandboxService : i =>
73+ i . createInstance ( CodeSandboxService , controller ) ,
74+ } ,
75+ returnedServices => {
76+ serviceCache = returnedServices ;
77+ cb ( serviceCache ) ;
78+ }
79+ ) ;
80+ }
81+
5282 initMonaco = ( ) => {
53- const { theme, diffEditor = false } = this . props ;
83+ const { theme } = this . props ;
5484 const context = this . props . context || window ;
5585 if ( this . containerElement && typeof context . monaco !== 'undefined' ) {
5686 // Before initializing monaco editor
@@ -63,14 +93,12 @@ class MonacoEditor extends React.PureComponent {
6393 const r = context . require ;
6494
6595 const [
66- { CodeSandboxService } ,
6796 { IEditorService } ,
6897 { ICodeEditorService } ,
6998 { ITextFileService } ,
7099 { ILifecycleService } ,
71100 { IEditorGroupsService } ,
72101 ] = [
73- r ( 'vs/codesandbox/services/codesandbox/browser/codesandboxService' ) ,
74102 r ( 'vs/workbench/services/editor/common/editorService' ) ,
75103 r ( 'vs/editor/browser/services/codeEditorService' ) ,
76104 r ( 'vs/workbench/services/textfile/common/textfiles' ) ,
@@ -87,61 +115,61 @@ class MonacoEditor extends React.PureComponent {
87115 const rootEl = document . getElementById ( 'vscode-container' ) ;
88116 rootEl . appendChild ( container ) ;
89117
90- context . monaco . editor [ diffEditor ? 'createDiffEditor' : 'create' ] (
91- container ,
92- { } ,
93- {
94- codesandboxService : i =>
95- i . createInstance ( CodeSandboxService , controller ) ,
96- } ,
97- services => {
98- const editorElement = document . getElementById (
99- 'workbench.main.container'
100- ) ;
101-
102- container . className = 'monaco-workbench' ;
103- part . className = 'part editor has-watermark' ;
104- editorElement . className += ' monaco-workbench mac nopanel' ;
105-
106- const EditorPart = services . get ( IEditorGroupsService ) ;
118+ this . initializeEditor ( container , services => {
119+ const editorElement = document . getElementById (
120+ 'workbench.main.container'
121+ ) ;
122+
123+ container . className = 'monaco-workbench' ;
124+ part . className = 'part editor has-watermark' ;
125+ editorElement . className += ' monaco-workbench mac nopanel' ;
126+
127+ const EditorPart = services . get ( IEditorGroupsService ) ;
128+
129+ if ( editorPart ) {
130+ editorPart . parent = part ;
131+ editorPart = EditorPart ;
132+ } else {
107133 EditorPart . create ( part ) ;
134+ }
108135
109- EditorPart . layout ( {
110- width : this . props . width ,
111- height : this . props . height ,
112- } ) ;
136+ EditorPart . layout ( {
137+ width : this . props . width ,
138+ height : this . props . height ,
139+ } ) ;
113140
114- const codeEditorService = services . get ( ICodeEditorService ) ;
115- const textFileService = services . get ( ITextFileService ) ;
116- const editorService = services . get ( IEditorService ) ;
117- const lifecycleService = services . get ( ILifecycleService ) ;
141+ const codeEditorService = services . get ( ICodeEditorService ) ;
142+ const textFileService = services . get ( ITextFileService ) ;
143+ const editorService = services . get ( IEditorService ) ;
144+ const lifecycleService = services . get ( ILifecycleService ) ;
118145
119- lifecycleService . phase = 3 ; // Running
146+ lifecycleService . phase = 3 ; // Running
120147
121- const editorApi = {
122- openFile ( path : string ) {
123- fontPromise . then ( ( ) => {
124- codeEditorService . openCodeEditor ( {
125- resource : context . monaco . Uri . file ( '/sandbox' + path ) ,
126- } ) ;
148+ const editorApi = {
149+ openFile ( path : string ) {
150+ fontPromise . then ( ( ) => {
151+ codeEditorService . openCodeEditor ( {
152+ resource : context . monaco . Uri . file ( '/sandbox' + path ) ,
127153 } ) ;
128- } ,
129- getActiveCodeEditor ( ) {
130- return codeEditorService . getActiveCodeEditor ( ) ;
131- } ,
132- textFileService,
133- editorPart : EditorPart ,
134- editorService,
135- } ;
136- if ( process . env . NODE_ENV === 'development' ) {
137- // eslint-disable-next-line
138- console . log ( services ) ;
139- }
140-
141- // After initializing monaco editor
142- this . editorDidMount ( editorApi , context . monaco ) ;
154+ } ) ;
155+ } ,
156+ getActiveCodeEditor ( ) {
157+ return codeEditorService . getActiveCodeEditor ( ) ;
158+ } ,
159+ textFileService,
160+ editorPart : EditorPart ,
161+ editorService,
162+ } ;
163+ if ( process . env . NODE_ENV === 'development' ) {
164+ // eslint-disable-next-line
165+ console . log ( services ) ;
143166 }
144- ) ;
167+
168+ this . editor = editorApi ;
169+
170+ // After initializing monaco editor
171+ this . editorDidMount ( editorApi , context . monaco ) ;
172+ } ) ;
145173
146174 // TODO: move this to a better place
147175 if ( theme ) {
@@ -151,9 +179,17 @@ class MonacoEditor extends React.PureComponent {
151179 } ;
152180
153181 destroyMonaco = ( ) => {
154- if ( typeof this . editor !== 'undefined' ) {
155- this . editor . dispose ( ) ;
156- }
182+ const groupsToClose = this . editor . editorService . editorGroupService . getGroups ( ) ;
183+
184+ Promise . all ( groupsToClose . map ( g => g . closeAllEditors ( ) ) )
185+ . then ( ( ) => {
186+ groupsToClose . forEach ( group =>
187+ this . editor . editorService . editorGroupService . removeGroup ( group )
188+ ) ;
189+ } )
190+ . then ( ( ) => {
191+ this . editor . editorPart . shutdown ( ) ;
192+ } ) ;
157193 } ;
158194
159195 assignRef = component => {
0 commit comments