react-json-view icon indicating copy to clipboard operation
react-json-view copied to clipboard

display new line character

Open 1mike12 opened this issue 5 years ago • 5 comments

If I have an object like {key: "line1\nline2"} when viewing the json, the new line characters dissapear

1mike12 avatar May 09 '20 00:05 1mike12

+1 having same issue

tinaszheng avatar Jun 09 '21 17:06 tinaszheng

It appears as the new lines are preserved in the HTML but as any whitespace in html gets rendered as a single space. With some browser inspection I changed the span to a <pre>...text...</pre> and it rendered with line breaks.

What I plan to do is see if that can be done via CSS/themes or custom rendering of a type in this library. If not, maybe a fork and a PR.

Screen Shot 2021-10-06 at 4 20 08 PM

klappy avatar Oct 06 '21 20:10 klappy

With some more googling, we can do that via CSS: .string-value { white-space: pre; }

https://www.w3.org/TR/CSS21/text.html#propdef-white-space via https://stackoverflow.com/questions/219219/how-to-change-a-span-to-look-like-a-pre-with-css

klappy avatar Oct 06 '21 20:10 klappy

I have been testing this and so far this has worked swimmingly for all the use cases I've tested it on:

    <ReactJson
      style={{ whiteSpace: 'pre' }}
      src={json}
      theme="monokai"
    />
Screen Shot 2021-10-07 at 6 33 27 PM

The only downside I see is that the long lines overflow and need scrolling as they no longer wrap.

There doesn't need to be any modifications to the codebase or a custom theme supplied as it is just as quick and simple as adding an option if there was one added. We may ask the dev team to edit the themes to do this by default but not sure how big the need is outside of the 3 of us on this issue.

klappy avatar Oct 07 '21 22:10 klappy

Good solution w/ custom Css, but still... it should either render the new line, or show the \n character. The current behavior is too weird

What do you think @mac-s-g

1mike12 avatar Oct 19 '21 12:10 1mike12