Skip to content

Show current filename in sticky pull request header - #99

Merged
sindresorhus merged 1 commit into
refined-github:masterfrom
DrewML:issue98
Mar 30, 2016
Merged

sindresorhus merged 1 commit into
refined-github:masterfrom
DrewML:issue98

Conversation

@DrewML

@DrewML DrewML commented Mar 24, 2016

Copy link
Copy Markdown
Contributor

Ref: Issue #98

Todo:

  • Unbind scroll/resize listeners on partial page nav to non-PR page
  • Only update file name label in DOM when it's changed from previous val
  • Hide filename in header when not sticky (top of page)
  • Enable on Commit pages (currently only PR pages)
  • Determine desired behavior when sticky header will never reach last commit

@hkdobrev

Copy link
Copy Markdown
Contributor

@DrewML You could use GFM todo lists in the PR description https://github.com/blog/1375-task-lists-in-gfm-issues-pulls-comments 😉

@DrewML

DrewML commented Mar 24, 2016

Copy link
Copy Markdown
Contributor Author

@hkdobrev Good call! Should be knocking out the top 3 in the list when I'm home from work tonight.

Still need some help determining what is the most appropriate way to handle the 4th item in that list.

@hkdobrev

Copy link
Copy Markdown
Contributor

Determine desired behavior when sticky header will never reach last commit

According to other libraries and websites that do this kind of things, you should only change the file name in the header when the contents of the previous one has gone above the viewport. I think the eyeline library for Discourse.org used to do it like that, but that was years ago.

Comment thread extension/content.js Outdated
addPatchDiffLinks();
}

if (isPRFiles()) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@DrewML Ultimately you'd use a check which will catch either /pull/<id>/files or <pull/<id>/commits/<sha>.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You could break the isPR() and isCommit() checks above into smaller pieces using a new one like isPRCommit() and then you could combine checks.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's funny - I originally had the POC checking for both, but I had thought late last night that the sticky bar wasn't enabled on commit pages. I just double-checked, and I was wrong :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's enabled on:

  • PR files listing
  • PR commit pages (new)

It's not enabled on:

  • commit pages (default)
  • compare views

@DrewML
DrewML force-pushed the issue98 branch 2 times, most recently from 20f994f to 412e020 Compare March 28, 2016 00:53
@DrewML

DrewML commented Mar 28, 2016

Copy link
Copy Markdown
Contributor Author

All todo items have been completed/pushed. Let me know if anything needs to change, or if I should squash my commits down.

@paulmolluzzo

Copy link
Copy Markdown
Contributor

This is related to lines 343-350, 373, and 401-405:

Returning the remove event listener in the function bindDiffHeaderListeners() and then going through the let unbindDiffHeaderListeners; and if {} else if {} logic seems kind of roundabout. It could just be personal preference, but would something like this be a little more to the point?

const headerEvents = ['scroll', 'resize'];
.
.
.
function bindDiffHeaderListeners() {
    headerEvents.forEach(event => window.addEventListener(event, diffHeaderFilename));
}

function unbindDiffHeaderListeners() {
    headerEvents.forEach(event => window.removeEventListener(event, diffHeaderFilename));
};
.
.
.
unbindDiffHeaderListeners();

if (isPRFiles() || isPRCommit()) {
    bindDiffHeaderListeners();
}

@DrewML

DrewML commented Mar 28, 2016

Copy link
Copy Markdown
Contributor Author

@paulmolluzzo yeah, that's much simpler, and avoids the awkward state in the DOMContentLoaded callback. Will update today

@hkdobrev

Copy link
Copy Markdown
Contributor

I've noticed a buggy behaviour. The first file name is not shown in the header initially, but only after the second file name is shown and then you scroll back up to the first file. If you scroll to the top of the page then and scroll down the first file name is correctly shown from the initial stuck of the header.

Here's a GIF demonstrating the buggy behaviour (sorry for the broken colours, it's from the recording):

scroll-filename-wait

@DrewML

DrewML commented Mar 28, 2016

Copy link
Copy Markdown
Contributor Author

@hkdobrev Nice catch - I noticed that earlier today when surfing around. I'm pretty sure I know what's causing it - will work on cleaning that up either tonight or tomorrow.

@DrewML

DrewML commented Mar 29, 2016

Copy link
Copy Markdown
Contributor Author

@hkdobrev Fix has been pushed for your reported bug. Let me know if you see any other weirdness.
@paulmolluzzo Switched to bind/unbind functions as suggested.

I don't feel super great about the diffFile IIFE, but the only immediate alternatives I could think of were:

  1. Checking the DOM each time for the last value (less than ideal)
  2. Keeping the state in a let assignment in the outermost scope in content.js, which feels more hacky than my current solution.

Comment thread extension/content.js Outdated
return false;
};

const reset = function reset() {

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.

No need to name the function. It's inferred from the variable name.

@sindresorhus

Copy link
Copy Markdown
Member

This introduces a lot of global functions. Maybe we should move all the functions to a separate file inside an IIFE and only expose the two "publicly" used functions under a namespace?

diffFileHeader.setup();
diffFileHeader.destroy();

Comment thread extension/content.js
const isRepoRoot = () => location.pathname.replace(/\/$/, '') === `/${ownerName}/${repoName}` || /(\/tree\/)(\w|\d|\.)+(\/$|$)/.test(location.href);
const isPR = () => /^\/[^/]+\/[^/]+\/pull\/\d+/.test(location.pathname) || /^\/[^/]+\/[^/]+\/pull\/\d+\/commits\/[0-9a-f]{5,40}/.test(location.pathname);
const isPRCommit = () => /^\/[^/]+\/[^/]+\/pull\/\d+\/commits\/[0-9a-f]{5,40}/.test(location.pathname);
const isCommit = () => {

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.

@hkdobrev @paulmolluzzo Unrelated to this PR, but I think our path checking are getting out of control. How about we make a util.js file that exposes these utilities?

util.isRepo();
util.isRepoRoot();
...

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'm OK with this as long as we don't use the name utils, but something more explicit and with one purpose only like urlHelper, urlUtils or just url.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 it'll clean up the code a lot.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We can do that after this PR is merged :)

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.

We can do that after this PR is merged :)

👍 Yes, meant after.

@sindresorhus sindresorhus changed the title [WIP] Current filename in sticky PR header Show current filename in sticky pull request header Mar 29, 2016
@sindresorhus

Copy link
Copy Markdown
Member

This looks very good and seems to be working perfectly for me. 👌

@DrewML

DrewML commented Mar 29, 2016

Copy link
Copy Markdown
Contributor Author

This introduces a lot of global functions. Maybe we should move all the functions to a separate file inside an IIFE and only expose the two "publicly" used functions under a namespace?

Sounds good to me - was starting to feel crowded. Will push an update today.

@DrewML

DrewML commented Mar 29, 2016

Copy link
Copy Markdown
Contributor Author

Updates pushed to branch - moved all functionality relating to this PR into a new content script.

@sindresorhus

Copy link
Copy Markdown
Member

@DrewML Can you use arrow functions instead of functions and fix the merge conflict? Looks good to me after that :)

@DrewML

DrewML commented Mar 30, 2016

Copy link
Copy Markdown
Contributor Author

@sindresorhus Done! Also squashed the mile of commits down to 1 for the feature.

@sindresorhus
sindresorhus merged commit bbb69ea into refined-github:master Mar 30, 2016
@sindresorhus

Copy link
Copy Markdown
Member

@DrewML This is amazing. Superb work!

celebration

@sindresorhus

Copy link
Copy Markdown
Member

@DrewML Would you be interested in joining this project as a maintainer? No worries if not though :)

sindresorhus added a commit that referenced this pull request Mar 30, 2016
@DrewML

DrewML commented Mar 30, 2016

Copy link
Copy Markdown
Contributor Author

@sindresorhus For sure! I'm in.

@sindresorhus

Copy link
Copy Markdown
Member

Yay! :)

// @hkdobrev @paulmolluzzo

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.

4 participants