interviewstreet / firepad-x

Collaborative Text Editor Powered by Firebase
Other
37 stars 20 forks source link

Cross-platform collaboration breaks on monaco due to CRLF #41

Closed Blakeinstein closed 3 years ago

Blakeinstein commented 3 years ago

Version info

Firebase: 8.9.1

Firepad: 0.2.0

Monaco: 0.20.0

Test case

Duplicate of https://github.com/FirebaseExtended/firepad/issues/315

Steps to reproduce

  1. Open session on macos
  2. Open session on windows
  3. See windows go out of sync

Expected behavior

Consistent behaviour

Actual behavior

Cannot send changes from windows

0xTheProDev commented 3 years ago

@Blakeinstein Thanks for logging this issue and this has been a long overdue. We should be able to have a consistent way to handle different line endings natively from Firepad itself. But for the time being, you could do something like this in your monaco instance in your app:

+ editor.getModel().setEOL(monaco.editor.EndOfLineSequence.LF);
- editor.setValue(stringContent);
+ editor.getModel().applyEdits([
     {
        range: editor.getModel().getFullRange(),
        text: lineContent.join("\n"),
     }
   ])

and get it resolved. We do the same at Hackerrank.

Blakeinstein commented 3 years ago

I should also do this when I update options and or language?

0xTheProDev commented 3 years ago

Only if you update Monaco Model along with them.

Blakeinstein commented 3 years ago
+ editor.getModel().setEOL(monaco.editor.EndOfLineSequence.LF);
- editor.setValue(stringContent);
+ editor.getModel().applyEdits([
     {
        range: editor.getModel().getFullRange(),
        text: lineContent.join("\n"),
     }
   ])

I assume all I need are the lines marked with +? What is line content? editor.getModel().getLinesContent()?

0xTheProDev commented 3 years ago
+ editor.getModel().setEOL(monaco.editor.EndOfLineSequence.LF);
- editor.setValue(stringContent);
+ editor.getModel().applyEdits([
     {
        range: editor.getModel().getFullRange(),
        text: lineContent.join("\n"),
     }
   ])

I assume all I need are the lines marked with +? What is line content? editor.getModel().getLinesContent()?

I am assuming you know changes that you want to update as string[] in lineContent. You can use this API as well. stringContent is the whole content in string. - marked statements must be removed.

Blakeinstein commented 3 years ago

Ended up doing the following, and so far it looks like it works.

editor.getModel().setEOL(monaco.editor.EndOfLineSequence.LF)
editor.getModel().applyEdits([
  {
    range: editor.getModel().getFullModelRange(),
    text: editor.getModel().getLinesContent().join('\n')
  }
])

You can close the issue, or keep it open as I see that you guys plan to provide a native solution for this. Thanks!

0xTheProDev commented 3 years ago

Closing this for now. Let me know if any new problem comes up in this.