Skip to content

Commit caf9c05

Browse files
committed
Fix CodeSandbox Live in VSCode
1 parent f885a18 commit caf9c05

File tree

1 file changed

+55
-46
lines changed
  • packages/app/src/app/components/CodeEditor/VSCode

1 file changed

+55
-46
lines changed

packages/app/src/app/components/CodeEditor/VSCode/index.js

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -428,10 +428,6 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
428428
});
429429
};
430430

431-
setReceivingCode = (receiving: boolean) => {
432-
this.receivingCode = receiving;
433-
};
434-
435431
setTSConfig = (config: Object) => {
436432
this.tsconfig = config;
437433

@@ -444,28 +440,27 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
444440
corrections?: Array<ModuleCorrection>
445441
) => {
446442
const oldModule = this.currentModule;
443+
this.swapDocuments(oldModule, newModule);
447444

448-
this.swapDocuments(oldModule, newModule).then(() => {
449-
this.currentModule = newModule;
450-
this.currentTitle = newModule.title;
451-
this.currentDirectoryShortid = newModule.directoryShortid;
445+
this.currentModule = newModule;
446+
this.currentTitle = newModule.title;
447+
this.currentDirectoryShortid = newModule.directoryShortid;
452448

453-
if (errors) {
454-
this.setErrors(errors);
455-
}
449+
if (errors) {
450+
this.setErrors(errors);
451+
}
456452

457-
if (corrections) {
458-
this.setCorrections(corrections);
459-
}
453+
if (corrections) {
454+
this.setCorrections(corrections);
455+
}
460456

461-
if (this.props.onCodeReceived) {
462-
// Whenever the user changes a module we set up a state that defines
463-
// that the changes of code are not sent to live users. We need to reset
464-
// this state when we're doing changing modules
465-
this.props.onCodeReceived();
466-
this.liveOperationCode = '';
467-
}
468-
});
457+
if (this.props.onCodeReceived) {
458+
// Whenever the user changes a module we set up a state that defines
459+
// that the changes of code are not sent to live users. We need to reset
460+
// this state when we're doing changing modules
461+
this.props.onCodeReceived();
462+
this.liveOperationCode = '';
463+
}
469464
};
470465

471466
onSelectionChangedDebounced = data => {
@@ -737,7 +732,6 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
737732
code !== this.getCode() &&
738733
(!moduleId || this.currentModule.id === moduleId)
739734
) {
740-
// this.updateCode(code);
741735
this.lint(
742736
code,
743737
this.currentModule.title,
@@ -749,9 +743,11 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
749743
}
750744
};
751745

752-
applyOperationToModel = (operation, pushStack) => {
753-
const model = this.editor.getActiveCodeEditor().getModel();
754-
746+
applyOperationToModel = (
747+
operation,
748+
pushStack = false,
749+
model = this.editor.getActiveCodeEditor().getModel()
750+
) => {
755751
const results = [];
756752
let index = 0;
757753
for (let i = 0; i < operation.ops.length; i++) {
@@ -791,11 +787,13 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
791787
}
792788
}
793789

790+
this.receivingCode = true;
794791
if (pushStack) {
795792
model.pushEditOperations([], results);
796793
} else {
797794
model.applyEdits(results);
798795
}
796+
this.receivingCode = false;
799797
};
800798

801799
applyOperations = (operations: { [moduleShortid: string]: any }) => {
@@ -804,25 +802,30 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
804802
Object.keys(operationsJSON).forEach(moduleShortid => {
805803
const operation = TextOperation.fromJSON(operationsJSON[moduleShortid]);
806804

807-
if (moduleShortid !== this.currentModule.shortid) {
808-
// Apply the code to the current module code itself
809-
const module = this.sandbox.modules.find(
810-
m => m.shortid === moduleShortid
811-
);
805+
const module = this.sandbox.modules.find(
806+
m => m.shortid === moduleShortid
807+
);
808+
if (!module) {
809+
return;
810+
}
812811

813-
if (!module) {
814-
return;
815-
}
812+
const modulePath = getModulePath(
813+
this.sandbox.modules,
814+
this.sandbox.directories,
815+
module.id
816+
);
817+
const uri = this.monaco.Uri.file('/sandbox' + modulePath);
818+
const model = this.editor.textFileService.modelService.getModel(uri);
816819

820+
if (model) {
821+
this.applyOperationToModel(operation, false, model);
822+
this.liveOperationCode = '';
823+
} else {
817824
const code = operation.apply(module.code || '');
818825
if (this.props.onChange) {
819826
this.props.onChange(code, module.shortid);
820827
}
821-
return;
822828
}
823-
824-
this.liveOperationCode = '';
825-
this.applyOperationToModel(operation);
826829
});
827830
};
828831

@@ -1108,6 +1111,17 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
11081111
.deltaDecorations(modelInfo.decorations || [], decorations);
11091112
};
11101113

1114+
getModelById = (id: string) => {
1115+
const modulePath = getModulePath(
1116+
this.sandbox.modules,
1117+
this.sandbox.directories,
1118+
id
1119+
);
1120+
1121+
const uri = this.monaco.Uri.file('/sandbox' + modulePath);
1122+
return this.editor.textFileService.modelService.getModel(uri);
1123+
};
1124+
11111125
updateLintWarnings = async (markers: Array<Object>) => {
11121126
const currentModule = this.currentModule;
11131127

@@ -1133,14 +1147,9 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
11331147
}
11341148
};
11351149

1136-
swapDocuments = (currentModule: Module, nextModule: Module) =>
1137-
new Promise(resolve => {
1138-
this.openModule(nextModule);
1139-
1140-
// Reset changes
1141-
this.changes = { code: '', changes: [] };
1142-
resolve();
1143-
});
1150+
swapDocuments = (currentModule: Module, nextModule: Module) => {
1151+
this.openModule(nextModule);
1152+
};
11441153

11451154
updateCode(code: string = '') {
11461155
const operation = getTextOperation(this.getCode(), code);

0 commit comments

Comments
 (0)