@@ -113,7 +113,7 @@ export class EditorController extends Controller implements IEditorController {
113
113
const { current } = this . editorService . getState ( ) ;
114
114
if ( current ) {
115
115
const model = current ?. editorInstance ?. getModel ( ) ;
116
- const newValue = model . getValue ( ) ;
116
+ const newValue = current . tab ?. data . value || '' ;
117
117
current ?. editorInstance ?. executeEdits ( 'update-value' , [
118
118
{
119
119
range : model . getFullModelRange ( ) ,
@@ -164,6 +164,9 @@ export class EditorController extends Controller implements IEditorController {
164
164
this . emit ( EditorEvent . OnSelectTab , tabId , groupId ) ;
165
165
} ;
166
166
167
+ /**
168
+ * Called when open a new group
169
+ */
167
170
public onUpdateEditorIns = (
168
171
editorInstance : IStandaloneCodeEditor ,
169
172
groupId : number
@@ -178,7 +181,7 @@ export class EditorController extends Controller implements IEditorController {
178
181
179
182
const { current } = this . editorService . getState ( ) ;
180
183
const tab = current ?. tab ;
181
- this . openFile (
184
+ this . openTab (
182
185
editorInstance ,
183
186
tab ?. id ! ,
184
187
tab ?. data ?. value ! ,
@@ -241,6 +244,9 @@ export class EditorController extends Controller implements IEditorController {
241
244
} ) ;
242
245
}
243
246
247
+ /**
248
+ * Called when Editor props changed
249
+ */
244
250
public onChangeEditorProps = (
245
251
prevProps : IMonacoEditorProps ,
246
252
props : IMonacoEditorProps
@@ -253,7 +259,7 @@ export class EditorController extends Controller implements IEditorController {
253
259
prevProps . path ,
254
260
editorInstance ?. saveViewState ( )
255
261
) ;
256
- this . openFile (
262
+ this . openTab (
257
263
editorInstance ,
258
264
path ! ,
259
265
options ?. value ! ,
@@ -262,32 +268,33 @@ export class EditorController extends Controller implements IEditorController {
262
268
}
263
269
} ;
264
270
265
- private openFile (
271
+ /**
272
+ * Open a tab via instance.
273
+ * Actually, one tab to one Model, so that
274
+ * - the action to open a exist tab equals to switch the model in instance
275
+ * - the action to open a new tab equals to create a new model in instance
276
+ */
277
+ private openTab (
266
278
editorInstance : IStandaloneCodeEditor ,
267
279
path : string ,
268
280
value : string ,
269
281
language : string
270
282
) {
271
- this . initializeFile ( path , value , language ) ;
272
- const model = monacoEditor . getModel ( Uri . parse ( path ) ) ;
273
- editorInstance . setModel ( model ! ) ;
274
- // Restore the editor state for the file
283
+ let model = monacoEditor . getModel ( Uri . parse ( path ) ) ;
284
+ if ( ! model ) {
285
+ model = monacoEditor . createModel ( value , language , Uri . parse ( path ) ) ;
286
+ }
287
+ // 1. switch model
288
+ editorInstance . setModel ( model ) ;
289
+ // 2. Restore view state
275
290
const editorState = this . editorStates . get ( path ) ;
276
291
if ( editorState ) {
292
+ // viewState contains: scroller info, cursor info, contributions info
277
293
editorInstance . restoreViewState ( editorState ) ;
278
294
}
279
295
editorInstance ?. focus ( ) ;
280
296
}
281
297
282
- private initializeFile ( path : string , value : string , language : string ) {
283
- let model = monacoEditor . getModel ( Uri . parse ( path ) ) ;
284
- if ( model ) {
285
- model . setValue ( value ) ;
286
- } else {
287
- model = monacoEditor . createModel ( value , language , Uri . parse ( path ) ) ;
288
- }
289
- }
290
-
291
298
private updateStatusBar ( editorInstance : IStandaloneCodeEditor ) {
292
299
if ( editorInstance ) {
293
300
const model :
0 commit comments