-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Navigating Issues/PRs with keyboard shortcuts #1270
Comments
It doesn't look like any of those are taken already, which is a great start. We can probably reuse the yellow I think this would work this way:
|
Accepted. PR welcome. For anyone that wants this, please also send this feature request to GitHub: support@github.com |
I had fun trying this out. The import select from 'select-dom';
import * as pageDetect from 'github-url-detection';
import features from '.';
const shortcutKeys = new Set(['e', 'd', 'h']);
const shortcutClass: Record<string, string> = {
e: '.rgh-edit-comment, [aria-label="Edit comment"]',
d: '[aria-label="Delete comment"]',
h: '[aria-label="Hide comment"]'
};
function triggerShortcut(shortcut: string) {
const selectedComment = select(location.hash)!;
select<HTMLButtonElement>(shortcutClass[shortcut], selectedComment)?.click();
}
function init(): void {
document.addEventListener('keypress', event => {
if (!shortcutKeys.has(event.key) || event.target instanceof HTMLTextAreaElement || event.target instanceof HTMLInputElement) {
return;
}
event.preventDefault();
if (location.hash) {
triggerShortcut(event.key);
}
});
}
void features.add({
id: __filebasename,
description: '',
screenshot: false
}, {
include: [
pageDetect.hasComments
],
repeatOnAjax: false,
init
}); |
Why do we need cycling in this. The major problem I could think of is figuring out where to start, and "what" can be considered a comment and focused (eg: load more comments button, review comments, etc). |
Any thing that has a
The cycle code does exactly that |
That feature is specifically for "popover lists". We probably do not need cycling over comments. |
We need to find the index of the one we are on and go back and forth. Where do we start? Get the position of all the elements if (['j', 'k'].includes(event.key)) {
if (!location.hash) {
const closestComment = select.all<HTMLAnchorElement>('div.timeline-comment-header a.js-timestamp')
.find(element => element.getBoundingClientRect().top > 0);
if (!closestComment) {
return;
}
location.hash = closestComment.hash;
}
} |
One disadvantage (I hope) would be querying the DOM on each keypress (for only |
But that is beyond my scope of javascript. |
Could work; just ensure those items have
We can skip this (for now?) and the rest is easier: just start from current This way, the navigation part is essentially: const items = select.all('navigable items')
// Find current
const currentIndex = items.indexOf(select(':target'));
const verifiedCurrentIndex = currentIndex < 0 ? 0 : currentIndex; // :target might not be navigable, therefore `-1`
// Find chosen
const chosen = items[verifiedCurrentIndex + direction]; // TODO: clamp it so -1 means "the last item"
// Focus comment
history.replace({}, document.title, chosen.hash);
chosen.scrollIntoView(); |
No need. GitHub already has style for |
Thanks I figured it out!!! |
@fregante If you give the ok I will send a pr. (If not its ok I had loads of fun doing it) https://github.com/yakov116/refined-github/tree/comment-shortcuts |
Looks reasonable. Let's see a PR |
+
(=)-
The text was updated successfully, but these errors were encountered: