summaryrefslogtreecommitdiff
path: root/src/client/directives/follow-append.ts
blob: ef3f9af43f9c259b36caf7cda9b4a7c5439b92c7 (plain)
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 { Directive } from 'vue';
import { getScrollContainer, getScrollPosition } from '@client/scripts/scroll';

export default {
	mounted(src, binding, vn) {
		if (binding.value === false) return;

		let isBottom = true;

		const container = getScrollContainer(src)!;
		container.addEventListener('scroll', () => {
			const pos = getScrollPosition(container);
			const viewHeight = container.clientHeight;
			const height = container.scrollHeight;
			isBottom = (pos + viewHeight > height - 32);
		}, { passive: true });
		container.scrollTop = container.scrollHeight;

		const ro = new ResizeObserver((entries, observer) => {
			if (isBottom) {
				const height = container.scrollHeight;
				container.scrollTop = height;
			}
		});

		ro.observe(src);

		// TODO: 新たにプロパティを作るのをやめMapを使う
		src._ro_ = ro;
	},

	unmounted(src, binding, vn) {
		if (src._ro_) src._ro_.unobserve(src);
	}
} as Directive;