Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Add event callback when click line #44

Open
Zmnortal opened this issue Dec 24, 2024 · 4 comments
Open

Feature Request: Add event callback when click line #44

Zmnortal opened this issue Dec 24, 2024 · 4 comments

Comments

@Zmnortal
Copy link

Hello, this library is really convenient, and I am very grateful to the author!

I am currently using this library at work for some batch update checks. In this process, I found that if I could directly click on the code diff to remove unnecessary diffs, it would be very convenient (I also use the just-diff package to patch the diffs). Currently, there is no way to achieve this requirement, so it's a pity. If possible, I hope to add event callbacks, which would really be helpful! If possible, I am willing to contribute code.

Once again, thank you very much to the author.

@RexSkz
Copy link
Owner

RexSkz commented Dec 24, 2024

PR is always welcome!

Just be curious: I don't know how you map the diff results to the original key or value; if I implement this feature, I can only provide the DiffResult object as the callback parameter, which seems not easy to achieve your original goal.


Update: adding the key prefix (e.g. a.b[0].c) to DiffResult seems to work in most cases, but I can still find an edge case that you set arrayDiffMethod to "unorder-normal" or "unorder-lcs", which will sort the original array before diffing so you won't be able to find the correct array index.

@Zmnortal
Copy link
Author

Zmnortal commented Dec 26, 2024

Yes, this is indeed a problem that I need to find a way to solve.

My intuitive thought is that the callback will carry information such as the content of the current line and the line number, and then compare it with the original information to determine the specific part of the diff. Since my current requirement is to replace keywords, the diff will only occur within a single line and not span multiple lines. Moreover, the possibility of the diff content being repetitive is quite low, and I can specifically handle this part of the bad case. The line numbers might not align due to different formatters, but in most scenarios, it should be feasible through content. I want to give it a try.

@RexSkz
Copy link
Owner

RexSkz commented Dec 26, 2024

I see, you just want a feature like this:

interface RowClickInfo {
  // must have
  diff: [DiffResult, DiffResult];

  // nice to have
  side: 'left' | 'right';
  isLineNumberColumn: boolean;
}

const viewerProps: ViewerProps = {
  onRowClick: (info: RowClickInfo) => {
    const [left, right] = info.diff;
    console.log(left.lineNumber, left.text, left.comma);
  },
};

Feel free to submit a PR :)

@Zmnortal
Copy link
Author

Yes, this is good enough. I will submit this PR I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants