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

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

export const createHeightObserver = (details = true) => {
	document.querySelectorAll('.list').forEach((element) => {
		if (
			!(
				element as unknown as {
					dataset: { observed: string };
				}
			).dataset.observed
		) {
			new ResizeObserver((entries) => {
				entries.forEach((entry) => {
					const element = entry.target as HTMLElement;

					if (get(settings).displayLimitListHeight) {
						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`;
					}
				});
			}).observe(element);

			if (details)
				new MutationObserver((mutations) => {
					mutations.forEach((mutation) => {
						const element = mutation.target as HTMLDetailsElement;

						if (element.tagName === 'DETAILS' && !element.open) element.style.height = 'auto';
					});
				}).observe(element, { attributes: true });

			element.setAttribute('data-observed', 'true');
		}
	});
};