@@ -13,7 +13,7 @@ import { actions, dispatch } from 'codesandbox-api';
1313import { css } from 'glamor' ;
1414import { TextOperation } from 'ot' ;
1515
16- import { getModel } from './utils' ;
16+ import { getCurrentModelPath , getModel , getVSCodePath } from './utils' ;
1717
1818// @ts -ignore
1919const fadeIn = css . keyframes ( 'fadeIn' , {
@@ -38,12 +38,12 @@ export type OnFileChangeData = {
3838
3939export type OnFileChangeCallback = ( data : OnFileChangeData ) => void ;
4040
41- export class ModelHandler {
41+ export class ModelsHandler {
4242 private modelAddedListener : { dispose : Function } ;
4343 private modelRemovedListener : { dispose : Function } ;
4444 private onChangeCallback : OnFileChangeCallback ;
4545 private sandbox : Sandbox ;
46- private editor ;
46+ private editorApi ;
4747 private monaco ;
4848 private userClassesGenerated = { } ;
4949 private userSelectionDecorations = { } ;
@@ -57,15 +57,66 @@ export class ModelHandler {
5757 } ;
5858 } = { } ;
5959
60- constructor ( editor , monaco , sandbox : Sandbox , cb : OnFileChangeCallback ) {
61- this . editor = editor ;
60+ constructor ( editorApi , monaco , sandbox : Sandbox , cb : OnFileChangeCallback ) {
61+ this . editorApi = editorApi ;
6262 this . monaco = monaco ;
6363 this . sandbox = sandbox ;
6464 this . onChangeCallback = cb ;
6565 this . listenForChanges ( ) ;
6666 }
6767
68- applyOperations ( operations : { [ moduleShortid : string ] : any } ) {
68+ public dispose ( ) : null {
69+ this . modelAddedListener . dispose ( ) ;
70+ this . modelRemovedListener . dispose ( ) ;
71+ Object . keys ( this . modelListeners ) . forEach ( p => {
72+ this . modelListeners [ p ] . listener . dispose ( ) ;
73+ } ) ;
74+ this . modelListeners = { } ;
75+
76+ return null ;
77+ }
78+
79+ public changeModule = ( module : Module ) => {
80+ const { sandbox } = this ;
81+
82+ const path = getModulePath ( sandbox . modules , sandbox . directories , module . id ) ;
83+
84+ if ( path && getCurrentModelPath ( this . editorApi ) !== path ) {
85+ return this . editorApi . openFile ( path ) ;
86+ }
87+
88+ return Promise . resolve ( ) ;
89+ } ;
90+
91+ public updateModules = ( ) => {
92+ Object . keys ( this . modelListeners ) . forEach ( path => {
93+ const shortid = this . modelListeners [ path ] . moduleShortid ;
94+ const { model } = this . modelListeners [ path ] ;
95+ const module = this . sandbox . modules . find ( m => m . shortid === shortid ) ;
96+ if ( ! module ) {
97+ // Deleted
98+ return ;
99+ }
100+
101+ const modulePath = getVSCodePath ( this . sandbox , module . id ) ;
102+
103+ if ( modulePath !== model . uri . path ) {
104+ this . editorApi . textFileService
105+ . move ( model . uri , this . monaco . Uri . file ( modulePath ) )
106+ . then ( ( ) => {
107+ const editor = this . editorApi . getActiveCodeEditor ( ) ;
108+ const currentModel = editor && editor . getModel ( ) ;
109+ const isCurrentFile =
110+ currentModel && currentModel . uri . path === path ;
111+ if ( isCurrentFile ) {
112+ this . editorApi . openFile ( modulePath . replace ( '/sandbox' , '' ) ) ;
113+ }
114+ } ) ;
115+ }
116+ } ) ;
117+ } ;
118+
119+ public applyOperations ( operations : { [ moduleShortid : string ] : any } ) {
69120 const operationsJSON = operations . toJSON ? operations . toJSON ( ) : operations ;
70121
71122 return Promise . all (
@@ -90,11 +141,9 @@ export class ModelHandler {
90141 moduleId
91142 ) ;
92143
93- const modelEditor =
94- this . editor &&
95- this . editor . editorService . editors . find (
96- editor => editor . resource && editor . resource . path === modulePath
97- ) ;
144+ const modelEditor = this . editorApi . editorService . editors . find (
145+ editor => editor . resource && editor . resource . path === modulePath
146+ ) ;
98147
99148 // Apply the code to the current module code itself
100149 const module = this . sandbox . modules . find (
@@ -143,18 +192,18 @@ export class ModelHandler {
143192 ) ;
144193 }
145194
146- updateUserSelections (
195+ public updateUserSelections (
147196 module ,
148197 userSelections : Array < UserSelection | EditorSelection >
149198 ) {
150- const model = getModel ( this . editor ) ;
199+ const model = getModel ( this . editorApi ) ;
151200
152201 if ( ! model ) {
153202 return ;
154203 }
155204
156205 const lines = model . getLinesContent ( ) || [ ] ;
157- const activeEditor = this . editor . getActiveEditor ( ) ;
206+ const activeEditor = this . editorApi . getActiveEditor ( ) ;
158207
159208 userSelections . forEach ( ( data : EditorSelection & UserSelection ) => {
160209 const { userId } = data ;
@@ -320,21 +369,10 @@ export class ModelHandler {
320369 } ) ;
321370 }
322371
323- dispose ( ) : null {
324- this . modelAddedListener . dispose ( ) ;
325- this . modelRemovedListener . dispose ( ) ;
326- Object . keys ( this . modelListeners ) . forEach ( p => {
327- this . modelListeners [ p ] . listener . dispose ( ) ;
328- } ) ;
329- this . modelListeners = { } ;
330-
331- return null ;
332- }
333-
334372 private applyOperationToModel (
335373 operation ,
336374 pushStack = false ,
337- model = this . editor . getActiveCodeEditor ( ) . getModel ( )
375+ model = this . editorApi . getActiveCodeEditor ( ) . getModel ( )
338376 ) {
339377 const results : Array < {
340378 range : unknown ;
@@ -405,7 +443,7 @@ export class ModelHandler {
405443 }
406444
407445 private listenForChanges ( ) {
408- this . modelAddedListener = this . editor . textFileService . modelService . onModelAdded (
446+ this . modelAddedListener = this . editorApi . textFileService . modelService . onModelAdded (
409447 model => {
410448 if ( this . modelListeners [ model . uri . path ] === undefined ) {
411449 let module : Module ;
@@ -433,7 +471,7 @@ export class ModelHandler {
433471 }
434472 ) ;
435473
436- this . modelRemovedListener = this . editor . textFileService . modelService . onModelRemoved (
474+ this . modelRemovedListener = this . editorApi . textFileService . modelService . onModelRemoved (
437475 model => {
438476 if ( this . modelListeners [ model . uri . path ] ) {
439477 this . modelListeners [ model . uri . path ] . listener . dispose ( ) ;
0 commit comments