summaryrefslogtreecommitdiff
path: root/source/features/no-unnecessary-split-diff-view.tsx
blob: 4af302719f8b9a1c85a28e144864d5d16578e82e (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 '../feature-manager';
import {onDiffFileLoad} from '../github-events/on-fragment-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)) {
				diffTable.setAttribute('data-rgh-hide-empty-split-diff-side', side);
				break;
			}
		}
	}
}

void features.add(import.meta.url, {
	asLongAs: [
		// 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)'),
	],
	include: [
		pageDetect.hasFiles,
	],
	exclude: [
		isUnifiedDiff,
	],
	additionalListeners: [
		onDiffFileLoad,
	],
	deduplicate: 'has-rgh-inner',
	init,
});