react-diff-viewer icon indicating copy to clipboard operation
react-diff-viewer copied to clipboard

Add option to disable line text-wrap

Open circAssimilate opened this issue 5 years ago • 7 comments

In some cases, like long strings or very nested JSON, it may be useful to allow the user or implementer to enable / disable text wrap. If it's not too difficult it would be neat to allow for a prop or CSS option to easily enable / disable line text wrapping.

circAssimilate avatar Nov 26 '19 17:11 circAssimilate

Hi @circAssimilate, can you provide me an example with screenshot?

Currently, all the text inside <pre> has a style white-space: pre-wrap. You can override by adding it under diffContainer style.

praneshr avatar Dec 19 '19 05:12 praneshr

Thanks, @praneshr. The white-space: pre-wrap suggestion is a good tip, but upon switching it to no-wrap I'd additionally like to control the max-width of each column and allow for before / after diff horizontal scrolling as an option after 50% of the parent container. So here's how it looks without wrap:

image

And I envision an opportunity to allow overflow-x: auto on the content column when it's longer than 50% of the parent container.

circAssimilate avatar Dec 21 '19 00:12 circAssimilate

I think I narrowed in on the core issue here. When a long string has no spaces, automatic breaking doesn't happen as expected and the container gets really big (while still wrapping). It would still be nice to allow for a non wrap option or style with a set width, but also figuring out how to ensure breaking happens at 50% for long strings like this:

Before

"[\"and\",[\"or\",[\"or\",{\"name\":\"user_id\",\"value\":123,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":456,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":789,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":321,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":654,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":987,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":111,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":333,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":777,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":999,\"match_type\":\"exact\",\"type\":\"custom_attribute\"}]]]"

After

"[\"and\",[\"or\",[\"or\",{\"name\":\"user_id\",\"value\":123,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":456,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":789,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":321,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":654,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":987,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":111,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":333,\"match_type\":\"exact\",\"type\":\"custom_attribute\"},{\"name\":\"user_id\",\"value\":777,\"match_type\":\"exact\",\"type\":\"custom_attribute\"}]]]"

circAssimilate avatar Dec 27 '19 00:12 circAssimilate

Hi, i am having this exact issue when my payload contains a JWT. JWTs are fairly lengthy and have no good break point.

EricCatlin avatar Jan 02 '20 07:01 EricCatlin

@praneshr, @EricCatlin, the simplest workaround I just found was to add the following style override to ensure a wrap happens (probably should be default):

line: {
    wordBreak: 'break-word',
},

image

Then, something like this helps with preventing wrap but allowing horizontal scroll:

diffRemoved: {
    overflowX: 'auto',
    maxWidth: 300,
},
diffAdded: {
    overflowX: 'auto',
    maxWidth: 300,
},
Screen Shot 2020-01-02 at 10 20 17 PM

For that, it would be best if the maxWidth could be computed within to be half the width of the table container minus gutters and whatnot.

The above works for now but I could imagine these could be powered by an shouldWrapText option that defaults to true.

circAssimilate avatar Jan 03 '20 04:01 circAssimilate

The solution above ends up for me like this: image

Shows multiple horizontal scrolling bars. Optimally there should only be one.


To avoid this, here's what I'm using:

styles={{
  diffContainer: {
    overflowX: "auto",
    display: "block",
    "& pre": { whiteSpace: "pre" },
  },
}}

Then just set the max-width on the container or the table it self and it should result in something like this: image

fev4 avatar Mar 02 '22 22:03 fev4

image I have the same problem. The style is ...

YeeJone avatar Nov 18 '22 07:11 YeeJone