Skip to content

Commit db0e608

Browse files
authored
Fix carriage returns with live (codesandbox#2384)
1 parent 7c166dd commit db0e608

File tree

2 files changed

+28
-10
lines changed

2 files changed

+28
-10
lines changed

packages/app/src/app/components/CodeEditor/Monaco/event-to-transform.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function convertChangeEventToOperation(
1313
for (const change of [...changeEvent.changes]) {
1414
const newOt = new TextOperation();
1515
const cursorStartOffset = lineAndColumnToIndex(
16-
composedCode.split(/\r?\n/),
16+
composedCode.split(/\n/),
1717
change.range.startLineNumber,
1818
change.range.startColumn
1919
);
@@ -29,8 +29,7 @@ export default function convertChangeEventToOperation(
2929
}
3030

3131
if (change.text) {
32-
const normalizedChangeText = change.text.split(/\r?\n/).join('\n');
33-
newOt.insert(normalizedChangeText);
32+
newOt.insert(change.text);
3433
}
3534

3635
const remaining = composedCode.length - newOt.baseLength;

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

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export class VSCode extends React.Component<Props> implements Editor {
225225
provideDocumentFormattingEdits = (model, options, token) =>
226226
prettify(
227227
model.uri.fsPath,
228-
() => model.getValue(1),
228+
() => model.getValue(),
229229
this.getPrettierConfig(),
230230
() => false,
231231
token
@@ -298,7 +298,7 @@ export class VSCode extends React.Component<Props> implements Editor {
298298
this.sendChangeOperations(e);
299299
}
300300

301-
this.handleChange(module.shortid, module.title, model.getValue(1));
301+
this.handleChange(module.shortid, module.title, model.getValue());
302302
} catch (err) {
303303
if (process.env.NODE_ENV === 'development') {
304304
console.error('caught', err);
@@ -416,7 +416,7 @@ export class VSCode extends React.Component<Props> implements Editor {
416416
if (
417417
modulePath === this.getCurrentModuleVSCodePath() &&
418418
this.currentModule.code !== undefined &&
419-
activeEditor.getValue(1) !== this.currentModule.code
419+
activeEditor.getValue() !== this.currentModule.code
420420
) {
421421
// Don't send these changes over live, since these changes can also be made by someone else and
422422
// we don't want to keep singing these changes
@@ -652,13 +652,15 @@ export class VSCode extends React.Component<Props> implements Editor {
652652
) => {
653653
const results = [];
654654
let index = 0;
655+
const currentEOLLength = model.getEOL().length;
656+
let eolChanged = false;
655657
for (let i = 0; i < operation.ops.length; i++) {
656658
const op = operation.ops[i];
657659
if (TextOperation.isRetain(op)) {
658660
index += op;
659661
} else if (TextOperation.isInsert(op)) {
660662
const { lineNumber, column } = indexToLineAndColumn(
661-
model.getLinesContent() || [],
663+
model.getValue().split(/\n/) || [],
662664
index
663665
);
664666
const range = new this.monaco.Range(
@@ -667,13 +669,24 @@ export class VSCode extends React.Component<Props> implements Editor {
667669
lineNumber,
668670
column
669671
);
672+
673+
// if there's a new line
674+
if (/\n/.test(op)) {
675+
const eol = /\r\n/.test(op) ? 2 : 1;
676+
if (eol !== currentEOLLength) {
677+
// With this insert the EOL of the document changed on the other side. We need
678+
// to accomodate our EOL to it.
679+
eolChanged = true;
680+
}
681+
}
682+
670683
results.push({
671684
range,
672685
text: op,
673686
forceMoveMarkers: true,
674687
});
675688
} else if (TextOperation.isDelete(op)) {
676-
const lines = model.getLinesContent() || [];
689+
const lines = model.getValue().split(/\n/) || [];
677690
const from = indexToLineAndColumn(lines, index);
678691
const to = indexToLineAndColumn(lines, index - op);
679692
results.push({
@@ -690,6 +703,12 @@ export class VSCode extends React.Component<Props> implements Editor {
690703
}
691704

692705
this.receivingCode = true;
706+
707+
if (eolChanged) {
708+
const newEolMode = currentEOLLength === 2 ? 0 : 1;
709+
model.setEOL(newEolMode);
710+
}
711+
693712
if (pushStack) {
694713
model.pushEditOperations([], results);
695714
} else {
@@ -749,7 +768,7 @@ export class VSCode extends React.Component<Props> implements Editor {
749768

750769
if (this.props.onChange) {
751770
this.props.onChange(
752-
model.object.textEditorModel.getValue(1),
771+
model.object.textEditorModel.getValue(),
753772
module.shortid
754773
);
755774
}
@@ -1087,7 +1106,7 @@ export class VSCode extends React.Component<Props> implements Editor {
10871106
const activeEditor = this.editor.getActiveCodeEditor();
10881107
if (!activeEditor) return '';
10891108

1090-
return activeEditor.getValue(1);
1109+
return activeEditor.getValue();
10911110
};
10921111

10931112
getEditorOptions = () => {

0 commit comments

Comments
 (0)