summaryrefslogtreecommitdiff
path: root/source/features/show-whitespace.tsx
blob: 520c316f47386568178df381e2d6eb8af2896bcc (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
import './show-whitespace.css';
import * as pageDetect from 'github-url-detection';

import features from '../feature-manager.js';
import {codeElementsSelector} from '../github-helpers/dom-formatters.js';
import showWhiteSpacesOnLine from '../helpers/show-whitespace-on-line.js';
import onAbort from '../helpers/abort-controller.js';
import observe from '../helpers/selector-observer.js';

const viewportObserver = new IntersectionObserver(changes => {
	for (const {target: line, isIntersecting} of changes) {
		if (isIntersecting) {
			const shouldAvoidSurroundingSpaces = Boolean(line.closest('.blob-wrapper-embedded')); // #2285
			showWhiteSpacesOnLine(line, shouldAvoidSurroundingSpaces);
			viewportObserver.unobserve(line);
		}
	}
});

function showWhitespaceWhenInViewport(line: HTMLElement): void {
	viewportObserver.observe(line);
}

function init(signal: AbortSignal): void {
	observe(`:is(${codeElementsSelector.join(',')}):not(.blob-code-hunk)`, showWhitespaceWhenInViewport, {signal});
	onAbort(signal, viewportObserver);
}

void features.add(import.meta.url, {
	include: [
		pageDetect.hasCode,
	],
	init,
});

/*
TEST URL
https://github.com/refined-github/sandbox/pull/18
*/