Skip to content

Keep PR file in view after collapsing it - #748

Merged
fregante merged 8 commits into
refined-github:masterfrom
bradzacher:655-scroll-to-top-on-pr-file-close
Oct 25, 2017
Merged

fregante merged 8 commits into
refined-github:masterfrom
bradzacher:655-scroll-to-top-on-pr-file-close

Conversation

@bradzacher

Copy link
Copy Markdown
Contributor

as per #655

Comment thread src/libs/scroll-to-top-on-collapse.js Outdated
// get the diff container for this button
const parent = b.parentElement.parentElement.parentElement

b.addEventListener('click', () => {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid the loop and use a delegate listener instead? Big PRs are loaded via ajax. Something like this instead of approximately the first 6 lines of this file:

$('.js-diff-progressive-container').on('click', '.js-details-target', fn);

Comment thread src/libs/scroll-to-top-on-collapse.js Outdated
console.log('hihi')
const btns = document.querySelectorAll('.file-actions > .js-details-target')
const prToolbar = document.querySelector('.pr-toolbar')
const toolbarHeight = prToolbar.getBoundingClientRect().height

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prToolbar might not exist on the PR's Conversation tab, you'll need a condition or just move it inside the event handler.

Comment thread src/libs/scroll-to-top-on-collapse.js Outdated

btns.forEach((b) => {
// get the diff container for this button
const parent = b.parentElement.parentElement.parentElement

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is best done via b.closest('.file')

Comment thread src/libs/scroll-to-top-on-collapse.js Outdated
const parentOffset = parent.getBoundingClientRect().top
const parentY = parentOffset - bodyOffset - toolbarHeight

if (window.scrollY > parentY) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bodyOffset is the same as window.scrollY, so you're cancelling it out.

When the element is inside the viewport, parentOffset will be positive, so the condition can just be parentOffset < toolbarHeight.

@bradzacher

Copy link
Copy Markdown
Contributor Author

sorry, I didn't realise there was full jQuery available.
Reworked as requested.

also removed the silly console log i left in.

@fregante fregante changed the title added function to auto scroll when pr file is closed Keep PR file in view after collapsing it Oct 20, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

3 participants