rudeshark.net/packages/client/src/directives/follow-append.ts

40 lines
951 B
TypeScript
Raw Normal View History

2023-01-13 05:40:33 +01:00
import { Directive } from "vue";
import { getScrollContainer, getScrollPosition } from "@/scripts/scroll";
2020-11-01 14:09:16 +01:00
export default {
mounted(src, binding, vn) {
if (binding.value === false) return;
let isBottom = true;
const container = getScrollContainer(src)!;
2023-01-13 05:40:33 +01:00
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;
2020-11-01 14:09:16 +01:00
container.scrollTop = height;
}
});
ro.observe(src);
// TODO: 新たにプロパティを作るのをやめMapを使う
src._ro_ = ro;
},
unmounted(src, binding, vn) {
if (src._ro_) src._ro_.unobserve(src);
2023-01-13 05:40:33 +01:00
},
2020-11-01 14:09:16 +01:00
} as Directive;