summaryrefslogtreecommitdiff
path: root/source/features/no-unnecessary-split-diff-view.tsx
blob: ea9ca151f58032fa55c5b3ec6e5f7d0c75dfd83a (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
import './no-unnecessary-split-diff-view.css';
import select from 'select-dom';
import * as pageDetect from 'github-url-detection';

import features from '.';
import onDiffFileLoad from '../github-events/on-diff-file-load';

function isUnifiedDiff(): boolean {
	return select.exists([
		'[value="unified"][checked]', // Form in PR
		'.table-of-contents .selected[href*="diff=unified"]', // Link in single commit
	]);
}

function init(): void {
	for (const diffTable of select.all('.js-diff-table:not(.rgh-no-unnecessary-split-diff-view-visited)')) {
		diffTable.classList.add('rgh-no-unnecessary-split-diff-view-visited');
		for (const side of ['left', 'right']) {
			if (!select.exists(`[data-split-side="${side}"]:is(.blob-code-addition, .blob-code-deletion)`, diffTable)) {
				// eslint-disable-next-line unicorn/prefer-dom-node-dataset -- CSS file has the same selector, this can be grepped
				diffTable.setAttribute('data-rgh-hide-empty-split-diff-side', side);
				break;
			}
		}
	}
}

void features.add(__filebasename, {
	include: [
		pageDetect.isCommit,
		pageDetect.isCompare,
		pageDetect.isPRFiles,
	],
	exclude: [
		isUnifiedDiff,
		// Make sure the class names we need exist on the page #4483
		() => !select.exists('.js-diff-table :is([data-split-side="left"], [data-split-side="right"]):is(.blob-code-addition, .blob-code-deletion)'),
	],
	additionalListeners: [
		onDiffFileLoad,
	],
	init,
});