2023-07-27 07:31:52 +02:00
|
|
|
/*
|
2024-02-13 16:59:27 +01:00
|
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
2023-07-27 07:31:52 +02:00
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
2021-10-09 05:33:08 +02:00
|
|
|
type ScrollBehavior = 'auto' | 'smooth' | 'instant';
|
|
|
|
|
2022-06-20 06:20:28 +02:00
|
|
|
export function getScrollContainer(el: HTMLElement | null): HTMLElement | null {
|
|
|
|
if (el == null || el.tagName === 'HTML') return null;
|
2022-07-06 19:02:39 +02:00
|
|
|
const overflow = window.getComputedStyle(el).getPropertyValue('overflow-y');
|
|
|
|
if (overflow === 'scroll' || overflow === 'auto') {
|
2020-02-16 12:58:41 +01:00
|
|
|
return el;
|
|
|
|
} else {
|
|
|
|
return getScrollContainer(el.parentElement);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-22 07:28:17 +01:00
|
|
|
export function getStickyTop(el: HTMLElement, container: HTMLElement | null = null, top = 0) {
|
2023-01-13 10:25:40 +01:00
|
|
|
if (!el.parentElement) return top;
|
|
|
|
const data = el.dataset.stickyContainerHeaderHeight;
|
|
|
|
const newTop = data ? Number(data) + top : top;
|
|
|
|
if (el === container) return newTop;
|
|
|
|
return getStickyTop(el.parentElement, container, newTop);
|
2020-02-16 12:58:41 +01:00
|
|
|
}
|
|
|
|
|
2024-07-12 09:25:44 +02:00
|
|
|
export function getStickyBottom(el: HTMLElement, container: HTMLElement | null = null, bottom = 0) {
|
|
|
|
if (!el.parentElement) return bottom;
|
|
|
|
const data = el.dataset.stickyContainerFooterHeight;
|
|
|
|
const newBottom = data ? Number(data) + bottom : bottom;
|
|
|
|
if (el === container) return newBottom;
|
|
|
|
return getStickyBottom(el.parentElement, container, newBottom);
|
|
|
|
}
|
|
|
|
|
2023-01-13 10:25:40 +01:00
|
|
|
export function getScrollPosition(el: HTMLElement | null): number {
|
|
|
|
const container = getScrollContainer(el);
|
|
|
|
return container == null ? window.scrollY : container.scrollTop;
|
2020-08-19 10:52:11 +02:00
|
|
|
}
|
|
|
|
|
2024-09-24 03:09:55 +02:00
|
|
|
export function onScrollTop(el: HTMLElement, cb: (topVisible: boolean) => unknown, tolerance = 1, once = false) {
|
2023-01-13 10:25:40 +01:00
|
|
|
// とりあえず評価してみる
|
2024-09-24 03:09:55 +02:00
|
|
|
const firstTopVisible = isTopVisible(el);
|
|
|
|
if (el.isConnected && firstTopVisible) {
|
|
|
|
cb(firstTopVisible);
|
2023-01-13 10:25:40 +01:00
|
|
|
if (once) return null;
|
|
|
|
}
|
2022-06-20 06:20:28 +02:00
|
|
|
|
2023-02-22 07:28:17 +01:00
|
|
|
const container = getScrollContainer(el) ?? window;
|
2023-01-13 10:25:40 +01:00
|
|
|
|
2024-09-24 03:09:55 +02:00
|
|
|
// 以下のケースにおいて、cbが何度も呼び出されてしまって具合が悪いので1回呼んだら以降は無視するようにする
|
|
|
|
// - スクロールイベントは1回のスクロールで複数回発生することがある
|
|
|
|
// - toleranceの範囲内に収まる程度の微量なスクロールが発生した
|
|
|
|
let prevTopVisible = firstTopVisible;
|
2024-09-09 13:57:36 +02:00
|
|
|
const onScroll = () => {
|
2020-02-16 12:58:41 +01:00
|
|
|
if (!document.body.contains(el)) return;
|
2024-09-24 03:09:55 +02:00
|
|
|
|
|
|
|
const topVisible = isTopVisible(el, tolerance);
|
|
|
|
if (topVisible !== prevTopVisible) {
|
|
|
|
prevTopVisible = topVisible;
|
|
|
|
cb(topVisible);
|
2023-01-13 10:25:40 +01:00
|
|
|
if (once) removeListener();
|
2020-02-16 12:58:41 +01:00
|
|
|
}
|
|
|
|
};
|
2023-01-13 10:25:40 +01:00
|
|
|
|
|
|
|
function removeListener() { container.removeEventListener('scroll', onScroll); }
|
2023-10-29 08:09:20 +01:00
|
|
|
|
2020-02-16 12:58:41 +01:00
|
|
|
container.addEventListener('scroll', onScroll, { passive: true });
|
2023-01-13 10:25:40 +01:00
|
|
|
return removeListener;
|
2020-02-16 12:58:41 +01:00
|
|
|
}
|
2020-07-12 09:14:49 +02:00
|
|
|
|
2023-02-22 07:28:17 +01:00
|
|
|
export function onScrollBottom(el: HTMLElement, cb: () => unknown, tolerance = 1, once = false) {
|
2023-01-13 10:25:40 +01:00
|
|
|
const container = getScrollContainer(el);
|
|
|
|
|
|
|
|
// とりあえず評価してみる
|
2023-08-13 14:22:57 +02:00
|
|
|
if (el.isConnected && isBottomVisible(el, tolerance, container)) {
|
2023-01-13 10:25:40 +01:00
|
|
|
cb();
|
|
|
|
if (once) return null;
|
|
|
|
}
|
|
|
|
|
2023-02-22 07:28:17 +01:00
|
|
|
const containerOrWindow = container ?? window;
|
2024-09-09 13:57:36 +02:00
|
|
|
const onScroll = () => {
|
2020-07-19 05:26:05 +02:00
|
|
|
if (!document.body.contains(el)) return;
|
2023-01-13 10:25:40 +01:00
|
|
|
if (isBottomVisible(el, 1, container)) {
|
2020-07-19 05:26:05 +02:00
|
|
|
cb();
|
2023-01-13 10:25:40 +01:00
|
|
|
if (once) removeListener();
|
2020-07-19 05:26:05 +02:00
|
|
|
}
|
|
|
|
};
|
2023-01-13 10:25:40 +01:00
|
|
|
|
|
|
|
function removeListener() {
|
|
|
|
containerOrWindow.removeEventListener('scroll', onScroll);
|
|
|
|
}
|
2023-10-29 08:09:20 +01:00
|
|
|
|
2023-01-13 10:25:40 +01:00
|
|
|
containerOrWindow.addEventListener('scroll', onScroll, { passive: true });
|
|
|
|
return removeListener;
|
2020-07-19 05:26:05 +02:00
|
|
|
}
|
|
|
|
|
2023-01-13 10:25:40 +01:00
|
|
|
export function scroll(el: HTMLElement, options: ScrollToOptions | undefined) {
|
2020-07-12 09:14:49 +02:00
|
|
|
const container = getScrollContainer(el);
|
|
|
|
if (container == null) {
|
2021-10-09 05:33:08 +02:00
|
|
|
window.scroll(options);
|
2020-07-12 09:14:49 +02:00
|
|
|
} else {
|
2021-10-09 05:33:08 +02:00
|
|
|
container.scroll(options);
|
2020-07-12 09:14:49 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-19 05:26:05 +02:00
|
|
|
|
2023-01-13 10:25:40 +01:00
|
|
|
/**
|
|
|
|
* Scroll to Top
|
|
|
|
* @param el Scroll container element
|
|
|
|
* @param options Scroll options
|
|
|
|
*/
|
|
|
|
export function scrollToTop(el: HTMLElement, options: { behavior?: ScrollBehavior; } = {}) {
|
2021-10-09 05:33:08 +02:00
|
|
|
scroll(el, { top: 0, ...options });
|
2021-02-14 14:26:07 +01:00
|
|
|
}
|
|
|
|
|
2023-01-13 10:25:40 +01:00
|
|
|
/**
|
|
|
|
* Scroll to Bottom
|
|
|
|
* @param el Content element
|
|
|
|
* @param options Scroll options
|
|
|
|
* @param container Scroll container element
|
|
|
|
*/
|
|
|
|
export function scrollToBottom(
|
|
|
|
el: HTMLElement,
|
|
|
|
options: ScrollToOptions = {},
|
|
|
|
container = getScrollContainer(el),
|
|
|
|
) {
|
|
|
|
if (container) {
|
|
|
|
container.scroll({ top: el.scrollHeight - container.clientHeight + getStickyTop(el, container) || 0, ...options });
|
|
|
|
} else {
|
|
|
|
window.scroll({
|
|
|
|
top: (el.scrollHeight - window.innerHeight + getStickyTop(el, container) + (window.innerWidth <= 500 ? 96 : 0)) || 0,
|
2023-02-22 07:28:17 +01:00
|
|
|
...options,
|
2023-01-13 10:25:40 +01:00
|
|
|
});
|
|
|
|
}
|
2021-02-14 14:26:07 +01:00
|
|
|
}
|
|
|
|
|
2023-02-22 07:28:17 +01:00
|
|
|
export function isTopVisible(el: HTMLElement, tolerance = 1): boolean {
|
2023-01-13 10:25:40 +01:00
|
|
|
const scrollTop = getScrollPosition(el);
|
2024-09-24 03:09:55 +02:00
|
|
|
if (_DEV_) console.log(scrollTop, tolerance, scrollTop <= tolerance);
|
2023-01-13 10:25:40 +01:00
|
|
|
return scrollTop <= tolerance;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function isBottomVisible(el: HTMLElement, tolerance = 1, container = getScrollContainer(el)) {
|
|
|
|
if (container) return el.scrollHeight <= container.clientHeight + Math.abs(container.scrollTop) + tolerance;
|
|
|
|
return el.scrollHeight <= window.innerHeight + window.scrollY + tolerance;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://ja.javascript.info/size-and-scroll-window#ref-932
|
|
|
|
export function getBodyScrollHeight() {
|
|
|
|
return Math.max(
|
|
|
|
document.body.scrollHeight, document.documentElement.scrollHeight,
|
|
|
|
document.body.offsetHeight, document.documentElement.offsetHeight,
|
2023-02-22 07:28:17 +01:00
|
|
|
document.body.clientHeight, document.documentElement.clientHeight,
|
2023-01-13 10:25:40 +01:00
|
|
|
);
|
2020-07-19 05:26:05 +02:00
|
|
|
}
|