Skip to content

Alt+click to expand/collapse all outdated pull request comments - #821

Merged
fregante merged 14 commits into
masterfrom
unknown repository
Dec 1, 2017
Merged

fregante merged 14 commits into
masterfrom
unknown repository

Conversation

@ghost

@ghost ghost commented Nov 17, 2017 •

Copy link
Copy Markdown

Closes #781

I tested this on Ubuntu/Linux in Chrome and Firefox.

(edit: updated gif to reflect the latest changes)
peek 2017-11-26 13-38

How does it work?

  • When hovering over a "Show outdated" or "Hide outdated" button, a tooltip explains that you can Alt+click on it to collapse/expand all comments
  • When Alt+clicking on the "show outdated" button, all other buttons with the caption will be clicked and thus expanded
  • The same happens when clicking on the "hide outdated" button

Suggestions very welcome. I am not a JS developer, but I tried my best to write clean code.

Edit: Replaced "Ctrl" with "Alt" in the original description

@branimir-natue

Copy link
Copy Markdown

really useful! Thanks

@ghost

ghost commented Nov 17, 2017

Copy link
Copy Markdown
Author

I guess one thing that could be improved is avoiding this jumping behavior. Maybe scroll/jump back to the clicked on comment, so users don't get confused?

@hkdobrev

Copy link
Copy Markdown
Contributor

@sgr-amboss 👍 We've already done something similar to keep the scrolled position sane in #748 when collapsing long files in diffs

@hkdobrev

Copy link
Copy Markdown
Contributor

Could you also add it to the Readme (probably with a link to this animation) and mention the shortcut keys?

@ghost

ghost commented Nov 18, 2017 •

Copy link
Copy Markdown
Author

@hkdobrev, thanks for that tip. I might actually need a little help here, as I am a bit new to Promises, async, etc. Basically, I added:

		// how can I make this work without a hardcoded timeout?
		setTimeout(() => {
			const offsetTop = $(e.target).parent().offset().top;
			window.scrollTo(0, offsetTop);
		}, 500);

after triggering the .click() on all the buttons. The timeout is needed, because all those outdated comments need to be expanded/collapsed and the rendering takes some time. I just made the timeout a bit higher here to be more noticeable during development.

How can I make this work with async or await? I tried a lot of different things (e.g. safeElementReady), but I think I'd need a tip to be pointed in the right direction. Do you have a suggestion?

@ghost

ghost commented Nov 19, 2017

Copy link
Copy Markdown
Author

I tried to make it work for a few hours with await, but I couldn't figure it out. The problem is that there doesn't seem to be any event that is triggered after all click() event chains have finished.

I committed my changes with a hacky setTimeout. Does anyone have a suggestion on how to improve this?

I updated the gif at the top to reflect the current state.

@hkdobrev

hkdobrev commented Nov 19, 2017 •

Copy link
Copy Markdown
Contributor

await works with resolved/rejected promises, you could use if you return a promise which you would resolve using a setTimeout. However, you would still use setTimeout underneath to resolve the promise.

Removing the setTimeout would be possible only if we find another way to detect when things have collapsed. I'm not really familiar with the new observer APIs in the DOM, but I don't think there's something for us to use in this scenario.

@ramlmn

ramlmn commented Nov 19, 2017

Copy link
Copy Markdown
requestAnimationFrame(() => {
    const offsetTop = $(parentElement).offset().top - viewportOffset;
    window.scroll(0, offsetTop);
});

should work the same as the one with setTimeout, and rAF is even clean to look at.

I've tested with single rAF, and it works as expected. If it doesn't work for some reason, we can even try double rAF.

@ghost

ghost commented Nov 19, 2017

Copy link
Copy Markdown
Author

@ramlmn, thanks! That works great and was exactly what I was looking for!

@ramlmn ramlmn left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is able to merge unless there is any more cleaning to do!

@ghost

ghost commented Nov 19, 2017

Copy link
Copy Markdown
Author

Added that feature to the readme, linking to the latest gif. Not sure if that was the correct place, so feel free to change that.

You might want to test this in both browsers on a Mac to be sure, as I don't have one nearby at the moment. Can anything else be improved here, @hkdobrev @bfred-it @sindresorhus ?

@fregante fregante left a comment •

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.

There was also an old feature request for the same feature but for files in PRs, it’d be great if this is modified to cover both. If its too hard, it’s alright, we’ll just add a link to this file on that issue.

Edit: I was referring to #212 (comment), but perhaps the implementation can be simpler there (just delegate click triggers the i keyboard shortcut)


export default () => {
const showOutdatedButtons = select.all('.show-outdated-button, .hide-outdated-button');
showOutdatedButtons.forEach(button => {

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.

Replace all forEach with for-of

button.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.

Reduce this duplicate loop to one, e.g.
if current is open
items = ...
else
Items = ...

And then loop and click items

requestAnimationFrame(() => {
const offsetTop = $(parentElement).offset().top - viewportOffset;
window.scroll(0, offsetTop);
});

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.

Is this wait required or does it work without?

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.

I ask because even if “rendering takes some time” as you mentioned in a comment, the offset value should be updated in the same loop (relayout is synchronous)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey, I made a small demo here without rAF, and here with rAF. Both of them work the same as you described (relayout as synchronous).

But in case of GitHub (both in Chrome and Firefox), the scroll has to be done as a microtask, because GitHub has its own methods to run in the mean time (which are run as microtasks)


Without rAF:
image


With rAF:
image


GH's code that runs async (promise on line 8057):
image

import select from 'select-dom';
import {isMac, metaKey} from '../libs/utils';

const keyName = isMac ? 'Alt' : 'Ctrl';

@fregante fregante Nov 20, 2017 •

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.

On Mac metaKey is actually CMD, not alt

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 would actually expect to be Alt on both Windows and Mac. Ctrl/Cmd+Click imply open in a new tab. Alt + Click on MacOS is alternative behaviour which makes sense here. What do you guys think?


function addTooltip(button) {
button.setAttribute('aria-label', keyName + ' + click to expand/collapse all outdated comments');
button.classList.add('tooltipped', 'tooltipped-n');

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.

I think you’ll also need the rgh-tooltip here otherwise they’re hidden

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.

I saw you added more selectors to the exclusion CSS. This class here would remove the need to make that selector longer.

const showOutdatedButtons = select.all('.show-outdated-button, .hide-outdated-button');
showOutdatedButtons.forEach(button => {
addTooltip(button);
button.addEventListener('click', e => {

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.

I’d like this listener to be applied via jQuery delegates, so it works on all AJAX-loaded comments too.

Comment thread src/content.css Outdated
.show-outdated-button:before,
.show-outdated-button:after,
.hide-outdated-button:before,
.hide-outdated-button:after,

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 should be added as a separate block and not inside here.

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.

Wait, why a separate block? Anyway I already suggested to add rgh-tooltip on the elements which is already picked up.

const keyName = isMac ? 'Alt' : 'Ctrl';

function addTooltip(button) {
button.setAttribute('aria-label', keyName + ' + click to expand/collapse all outdated comments');

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.

Use template literal for the second argument.

@ghost

ghost commented Nov 20, 2017

Copy link
Copy Markdown
Author

Thanks for the great suggestions! Implemented all of them, except the following:

There was also an old feature request for the same feature but for files in PRs, it’d be great if this is modified to cover both. If its too hard, it’s alright, we’ll just add a link to this file on that issue.

I didn't have time to do this today, but either we treat this as a separate issue, or I'll take a look in a couple of days. In the interest of finishing this, I'd vote for the first option 😉

Is this wait required or does it work without?

Didn't change anything here, as I am not sure if I should or not. Whenever I tried to make it work without waiting, collapsing the elements again didn't jump to the correct position. So I'd vote for leaving it as it is right now. But maybe I'm misunderstanding?

Once this is ready to merge, I'd like to finalize the GIF and link it to the README. Right now it's slightly outdated (the tooltip still says "Ctrl" instead of "Alt")

@ghost ghost changed the title Ctrl+click (or Alt+click on Mac) expands/collapses all outdated comments Alt+click expands/collapses all outdated comments Nov 21, 2017

export default () => {
const showOutdatedButtons = select.all('.show-outdated-button, .hide-outdated-button');
$('.js-discussion').on('click', '.show-outdated-button, .hide-outdated-button', e => {

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.

e => event

@sindresorhus sindresorhus changed the title Alt+click expands/collapses all outdated comments Alt/Ctrl+click to expand/collapse all outdated pull request comments Nov 26, 2017
@ghost

ghost commented Nov 26, 2017

Copy link
Copy Markdown
Author

@sindresorhus, @bfred-it please review again. I recorded another GIF of the feature with the latest changes and linked to it in the readme.

Comment thread readme.md

### New Features

- [Collapse/expand all outdated pull request comments by pressing <kbd>Alt</kbd> while clicking](https://user-images.githubusercontent.com/25818354/33240033-3e271588-d2af-11e7-93af-13b6e325f65d.gif)

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.

You reset my change from d849b05

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@sindresorhus, I saw that commit and it made me realize that I need to update the readme:

@ghost ghost changed the title Alt/Ctrl+click to expand/collapse all outdated pull request comments Alt+click to expand/collapse all outdated pull request comments Nov 26, 2017
@sindresorhus

Copy link
Copy Markdown
Member

@bfred-it :shipit: ?

`this` in jQuery events is the element with the event listener, while event.target might be a descendent of that, like the a>svg in this case
It was necessary for .forEach but not in for-of
@fregante
fregante merged commit 34f713a into refined-github:master Dec 1, 2017
@leventov

leventov commented Jun 2, 2018

Copy link
Copy Markdown

This feature doesn't expand "hidden" conversations.

@fregante

fregante commented Jun 2, 2018

Copy link
Copy Markdown
Member

Can you link to such a conversation?

@leventov

leventov commented Jun 2, 2018

Copy link
Copy Markdown

This issue has plenty (if you are lucky to not get a Unicorn opening it, hehe), search "hidden" on page

@fregante

fregante commented Jun 2, 2018

Copy link
Copy Markdown
Member

What's the expected behavior? "Show outdated" works correctly on the un-hidden items as well.

gif

If this is not what you mean, open a feature request with more info about what you expect to happen.

@leventov

leventov commented Jun 3, 2018

Copy link
Copy Markdown

It would be ideal if Alt+Click automatically loaded those hidden conversations.

@shanimal

Copy link
Copy Markdown

Someone obliterated automatic hiding of outdated comments. Now I'd have to write a JS snippet to remove them automatically. So dumb.

@busches

busches commented May 15, 2021

Copy link
Copy Markdown
Member

Someone obliterated automatic hiding of outdated comments. Now I'd have to write a JS snippet to remove them automatically. So dumb.

Please open a new issue if you're having problems, resurrecting a three year old PR isn't the best way for us to help.

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.

8 participants