1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-24 10:27:10 +02:00

Desktop: Beta editor: Fix crash when switching between notes that use CRLF line endings (#10531)

This commit is contained in:
Henry Heino 2024-06-04 01:52:09 -07:00 committed by GitHub
parent c9fb06fd0c
commit e049698012
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -103,4 +103,14 @@ describe('CodeMirrorControl', () => {
control.execCommand('deleteLine');
expect(control.getValue()).toBe('Hello\n');
});
it('should replace the editor body in a document with CRLF', () => {
const initialContent = 'Hello\r\nWorld\r\na';
const control = createEditorControl(initialContent);
control.setCursor(1, initialContent.length);
control.updateBody('Hello\r\nWorld\r\n');
control.updateBody('Hello\r\nWorld\r\ntest');
control.updateBody('Hello\r\n');
});
});

View File

@ -102,7 +102,11 @@ export default class CodeMirrorControl extends CodeMirror5Emulation implements E
// to ensure that the selection stays within the document
// (and thus avoids an exception).
const mainCursorPosition = this.editor.state.selection.main.anchor;
const newCursorPosition = Math.min(mainCursorPosition, newBody.length);
// The maximum cursor position needs to be calculated using the EditorState,
// to correctly account for line endings.
const maxCursorPosition = this.editor.state.toText(newBody).length;
const newCursorPosition = Math.min(mainCursorPosition, maxCursorPosition);
this.editor.dispatch(this.editor.state.update({
changes: {