Skip to content

Instantly share code, notes, and snippets.

@SimeonC
Last active March 25, 2022 03:02
Show Gist options
  • Save SimeonC/911c94166d0078c7c0903f317fb00aa0 to your computer and use it in GitHub Desktop.
Save SimeonC/911c94166d0078c7c0903f317fb00aa0 to your computer and use it in GitHub Desktop.
Mark as viewed and jump to next unviewed file in Github Pull Requests review
// change the following two key combos as you like
// this key combo will mark the current focused file as viewed and jump to the next unviewed file
const viewedAndNextKey = "ctrl-alt-meta-shift-v";
// this key combo will just jump to the next unviewed file
const nextKey = "ctrl-alt-meta-shift-w";
const modifiers = ["ctrl", "alt", "meta", "shift"];
const viewedAndNextKeyParts = viewedAndNextKey.split("-");
const nextKeyParts = nextKey.split("-");
function testEventSequence(event, sequence) {
for (const command of sequence) {
switch (command) {
case "ctrl": {
if (!event.ctrlKey) return false;
break;
}
case "alt": {
if (!event.altKey) return false;
break;
}
case "meta": {
if (!event.metaKey) return false;
break;
}
case "shift": {
if (!event.shiftKey) return false;
break;
}
default: {
if (command !== event.key.toLowerCase()) return false;
}
}
}
return true;
}
function testEvent(event) {
if (testEventSequence(event, viewedAndNextKeyParts)) return "view";
if (testEventSequence(event, nextKeyParts)) return "next";
return false;
}
window.addEventListener("keydown", (event) => {
const commandType = testEvent(event);
if (commandType) {
let hasFound = false;
const elements = document.querySelectorAll(".js-reviewed-toggle input");
for (let element of elements) {
if (hasFound && !element.checked) {
element.closest(".file").scrollIntoView();
element.focus();
return;
}
if (element === document.activeElement) {
hasFound = true;
if (commandType === "view") element.click();
}
}
if (!hasFound) {
const uncheckedElements = document.querySelectorAll(
".js-reviewed-toggle input:not(:checked)"
);
if (uncheckedElements.length) {
uncheckedElements[0].closest(".file").scrollIntoView();
uncheckedElements[0].focus();
}
}
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment