react-codemirror icon indicating copy to clipboard operation
react-codemirror copied to clipboard

Settings for line break

Open Tatalya opened this issue 3 years ago • 4 comments

Hello. I noticed that Codemirror forcibly replaces my line breaks (\r\n) with (\n). It does not depend on setting 'lineSeparator'. It replaces \r\n to \n always. Can I work with \r\n\ inside Codemirror?

Tatalya avatar May 26 '22 16:05 Tatalya

Can you get the updated string for replacement? @Tatalya

'Hello \nWorld'.replace(/\n/g, '\r\n')

I think you can customize an Extension to handle your problem, but it is extremely difficult.

jaywcjlove avatar May 26 '22 16:05 jaywcjlove

Hi, is there any better solution to set 'lineSeparator'? I tried set initialState.json prop, but it did not work.

Currently I have to use \r\n separator, but CodeEditor replaces it with \n and calls onChange handler.

Could you add 'lineSeparator' to react-codemirror props?

jadran90 avatar Sep 07 '22 09:09 jadran90

@jaywcjlove I have the same issue, isn't is possible to configure the line separator in react-codemirror as documented here?

guillermoamaral avatar Jun 16 '23 00:06 guillermoamaral

@guillermoamaral https://codesandbox.io/embed/react-codemirror-example-codemirror-6-forked-mscmx4?fontsize=14&hidenavigation=1&theme=dark

I tested this api and it doesn't work.

import React from "react";
import CodeMirror from "@uiw/react-codemirror";
import { EditorState } from "@codemirror/state";
import { javascript } from "@codemirror/lang-javascript";

export default function App() {
  const onChange = React.useCallback((value, viewUpdate) => {
    console.log(JSON.stringify(value));
  }, []);
  const txt = "console.log('hello \r\nworld!');";
  return (
    <div>
      <CodeMirror
        value={txt}
        height="200px"
        theme="dark"
        extensions={[
          EditorState.lineSeparator.of("\r\n"),
          javascript({ jsx: true })
        ]}
        onChange={onChange}
      />
    </div>
  );
}

So I think value does the processing and may be the current solution

 'Hello \nWor \r\nld! \rtest'.replace(/(?<!\r)\n|\r(?!\n)/g, '\r\n')

jaywcjlove avatar Jun 16 '23 01:06 jaywcjlove