Skip to content

Sync scrolling in side-by-side diff #1073

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

Conversation

Mr0grog
Copy link
Member

@Mr0grog Mr0grog commented Apr 21, 2025

A long overdue issue in the side-by-side diff view is that scrolling isn’t sync'd, so you need to manually scroll each view to be able to compare what’s happening an you move down the page. This finally adds support.

This is a little complicated for a few reasons:

First, the two side-by-side views have a different origin than the main app, posing security issues. To solve this, we use the postMessage API:

  1. We inject a script into each view that listens for scroll and message events.
  2. When one page receives a scroll event, it packages it up and posts a message about its new scroll position to the parent window (the main app).
  3. Then the app forwards that to the other page (using window.postMessage()).
  4. The other page receives that message event and updates its scroll position to match.

Second, large insertions or deletions often cause matching parts of the page to be in different places on either side of the diff. For sync'd scrolling to be useful, we need to keep relevant parts of the page together. To handle this, we have a concept of “landmarks” — parts of the page that are present on both sides and should line up visually when scrolling. Then we keep track of scroll positions in terms of a landmark and an offset (actually a % distance between the landmark and the next landmark) instead of an absolute pixel position. Each side of the diff keeps track of where its landmarks are on screen so when it updates its scroll position, it keeps landmarks visually side-by-side.

Ideally, the differ would output placeholders for insertions on the deletion side, and vice-versa, so the changes themselves would be landmarks. It doesn’t do that right now, so instead we look for unchanged headings and other major elements to serve as landmarks.

This first pass is pretty messy, but seems to work alright. I need to clean up all the logging, and then I think this OK to land for analysts to get some use out of it.

Mr0grog added 3 commits April 21, 2025 14:09
This is a super messy and crazy first implementation of synced scrolling for side-by-side diff views.
@Mr0grog Mr0grog merged commit 5a00111 into main Apr 21, 2025
4 checks passed
@Mr0grog Mr0grog deleted the side-by-side-views-are-bffs-that-do-everything-together-including-scrolling branch April 21, 2025 23:58
@github-project-automation github-project-automation bot moved this from Inbox to Done in Web Monitoring Apr 21, 2025
Mr0grog added a commit that referenced this pull request Apr 22, 2025
Mr0grog added a commit to edgi-govdata-archiving/web-monitoring-ops that referenced this pull request Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

1 participant