Skip to content

Commit 3c41574

Browse files
more refactoring, solid stuff now
1 parent 476dfac commit 3c41574

File tree

8 files changed

+189
-242
lines changed

8 files changed

+189
-242
lines changed

packages/app/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,9 @@
150150
"normalizr": "^3.2.3",
151151
"onigasm": "^2.2.1",
152152
"ot": "^0.0.15",
153-
"overmind": "^20.0.0-1570104917847",
153+
"overmind": "^20.1.0-1571936446466",
154154
"overmind-devtools": "^19.0.0",
155-
"overmind-react": "^21.0.0-1570104917847",
155+
"overmind-react": "^21.1.0-1571936446466",
156156
"phoenix": "^1.3.0",
157157
"postcss": "^6.0.9",
158158
"postcss-selector-parser": "^2.2.3",

packages/app/src/app/overmind/effects/vscode/Linter.ts

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
import { actions, dispatch } from 'codesandbox-api';
2+
import debounce from 'lodash-es/debounce';
13
// @ts-ignore
24
import LinterWorker from 'worker-loader?publicPath=/&name=monaco-linter.[hash:8].worker.js!./LinterWorker';
3-
import { dispatch, actions } from 'codesandbox-api';
4-
import debounce from 'lodash-es/debounce';
5+
56
import { getCurrentModelPath } from './utils';
67

78
const requireAMDModule = paths =>
@@ -11,14 +12,15 @@ export class Linter {
1112
private worker: LinterWorker;
1213
private editor;
1314
private monaco;
14-
private template;
1515
private isDisposed: boolean = false;
1616
// Can the template change during session of sandbox?
17-
constructor(template, editor, monaco) {
18-
this.template = template;
17+
constructor(editor, monaco) {
1918
this.editor = editor;
2019
this.monaco = monaco;
21-
this.initialize();
20+
this.worker = new LinterWorker();
21+
22+
// This should be disposed?
23+
this.worker.addEventListener('message', this.onMessage);
2224
}
2325

2426
dispose(): null {
@@ -29,13 +31,6 @@ export class Linter {
2931
return null;
3032
}
3133

32-
private initialize() {
33-
this.worker = new LinterWorker();
34-
35-
// This should be disposed?
36-
this.worker.addEventListener('message', this.onMessage);
37-
}
38-
3934
private onMessage = event => {
4035
const { markers, version } = event.data;
4136
const activeEditor = this.editor.getActiveCodeEditor();
@@ -63,22 +58,27 @@ export class Linter {
6358
}
6459
};
6560

66-
lint = debounce(async (code: string, title: string, version: number) => {
67-
if (!title || this.isDisposed) {
68-
return;
69-
}
61+
lint = debounce(
62+
async (code: string, title: string, version: number, template: string) => {
63+
if (!title || this.isDisposed) {
64+
return;
65+
}
7066

71-
const mode = (await this.getMonacoMode(title)) || '';
67+
const mode = (await this.getMonacoMode(title)) || '';
7268

73-
if (['javascript', 'typescript', 'typescriptreact', 'vue'].includes(mode)) {
74-
this.worker.postMessage({
75-
code,
76-
title,
77-
version,
78-
template: this.template,
79-
});
80-
}
81-
}, 100);
69+
if (
70+
['javascript', 'typescript', 'typescriptreact', 'vue'].includes(mode)
71+
) {
72+
this.worker.postMessage({
73+
code,
74+
title,
75+
version,
76+
template,
77+
});
78+
}
79+
},
80+
100
81+
);
8282

8383
private async getMonacoMode(title: string) {
8484
if (title == null) return 'javascript';

packages/app/src/app/overmind/effects/vscode/ModelHandler.ts renamed to packages/app/src/app/overmind/effects/vscode/ModelsHandler.ts

Lines changed: 66 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { actions, dispatch } from 'codesandbox-api';
1313
import { css } from 'glamor';
1414
import { TextOperation } from 'ot';
1515

16-
import { getModel } from './utils';
16+
import { getCurrentModelPath, getModel, getVSCodePath } from './utils';
1717

1818
// @ts-ignore
1919
const fadeIn = css.keyframes('fadeIn', {
@@ -38,12 +38,12 @@ export type OnFileChangeData = {
3838

3939
export 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

Comments
 (0)