aboutsummaryrefslogtreecommitdiff
path: root/src/lib/Utility/html.ts
blob: 8a0b7888317eb54b6001096d5ea973ea6c9ca8f1 (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
import settings from '$stores/settings';
import { get } from 'svelte/store';

export const nbsp = (str: string) => str.replace(/ /g, ' ');

export const createHeightObserver = (details = true) => {
	const heightObserver = new ResizeObserver(() => {
		if (get(settings).displayLimitListHeight) {
			document.querySelectorAll('.list').forEach((list) => {
				const element = list as HTMLElement;

				element.style.height = 'auto';

				const elementBound = element.getBoundingClientRect();
				const height = window.innerHeight - elementBound.top - 2.5 * 16;

				if (elementBound.height > height) element.style.height = `${height}px`;
			});
		}
	});
	const collapseObserver = new MutationObserver(() => {
		document.querySelectorAll('.list').forEach((list) => {
			const element = list as HTMLDetailsElement;

			if (element.tagName === 'DETAILS' && !element.open) element.style.height = 'auto';
		});
	});

	document.querySelectorAll('.list').forEach((element) => heightObserver.observe(element));

	if (details)
		document
			.querySelectorAll('.list')
			.forEach((element) => collapseObserver.observe(element, { attributes: true }));
};