Preserve line terminator upon edits

The adaptation from CodeMirror 5 to 6 has inadvertently lost the correct
management of the line termination, with the side-effect of converting
any DOS-style terminated text file into a Unix-style.

Remember the original line termination and concat the text lines using
the correct terminator once the content is updated using CodeMirror.

Bug: Issue 393149327
Change-Id: I67ab5ce3c0227c963d818247e412eb569b331a58
diff --git a/web/element/codemirror-element.ts b/web/element/codemirror-element.ts
index 27c6ef2..386aa67 100644
--- a/web/element/codemirror-element.ts
+++ b/web/element/codemirror-element.ts
@@ -60,6 +60,8 @@
   @query('#result')
   result!: HTMLElement;
 
+  private lineTerminator?: string;
+
   private initialized = false;
 
   private onResize: (() => void) | null = null;
@@ -122,6 +124,8 @@
     if (this.initialized) return;
     this.initialized = true;
 
+    this.lineTerminator = this.fileContent?.includes('\r\n') ? '\r\n' : '\n';
+
     const editor = new EditorView({
       state: EditorState.create({
         doc: this.fileContent ?? '',
@@ -151,7 +155,7 @@
             if (update.docChanged) {
               this.dispatchEvent(
                 new CustomEvent('content-change', {
-                  detail: {value: update.state.doc.toString()},
+                  detail: {value: update.state.doc.toJSON().join(this.lineTerminator)},
                   bubbles: true,
                   composed: true,
                 })