summaryrefslogtreecommitdiff
path: root/source/features/keyboard-navigation.tsx
blob: 365075ebfc1b78ab538dcb17f2c36983ba3bd4d0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '.';
import {isEditable} from '../helpers/dom-utils';

const isCommentGroupMinimized = (comment: HTMLElement): boolean =>
	select.exists('.minimized-comment:not(.d-none)', comment) ||
	Boolean(comment.closest([
		'.js-resolvable-thread-contents.d-none', // Regular comments
		'.js-resolvable-timeline-thread-container:not([open])' // Review comments
	].join()));

function runShortcuts(event: KeyboardEvent): void {
	if (isEditable(event.target)) {
		return;
	}

	const focusedComment = select(':target')!;

	if (['j', 'k'].includes(event.key)) {
		event.preventDefault();

		const items = select
			.all([
				'.js-targetable-element[id^="diff-"]', // Files in diffs
				'.js-minimizable-comment-group'// Comments (to be `.filter()`ed)
			])
			.filter(element =>
				element.classList.contains('js-minimizable-comment-group') ?
					!isCommentGroupMinimized(element) :
					true
			);

		// `j` goes to the next comment, `k` goes back a comment
		const direction = event.key === 'j' ? 1 : -1;

		const currentIndex = items.indexOf(focusedComment);

		// Start at 0 if nothing is; clamp index
		const chosenCommentIndex = Math.min(
			Math.max(0, currentIndex + direction),
			items.length - 1
		);

		if (currentIndex !== chosenCommentIndex) {
			// Focus comment without pushing to history
			location.replace('#' + items[chosenCommentIndex].id);
		}
	}
}

function init(): void {
	document.addEventListener('keypress', runShortcuts);
}

void features.add({
	id: __filebasename,
	description: 'Adds shortcuts to conversations and PR file lists: `j` focuses the comment/file below; `k` focuses the comment/file above.',
	screenshot: 'https://user-images.githubusercontent.com/1402241/86573176-48665900-bf74-11ea-8996-a5c46cb7bdfd.gif',
	shortcuts: {
		j: 'Focus the comment/file below',
		k: 'Focus the comment/file above'
	}
}, {
	include: [
		pageDetect.hasComments
	],
	init
});