Sharkey/packages/frontend/src/directives/follow-append.ts

41 lines
1 KiB
TypeScript
Raw Normal View History

/*
* SPDX-FileCopyrightText: syuilo and other misskey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
2020-11-01 13:09:16 +00:00
import { Directive } from 'vue';
2021-11-11 17:02:25 +00:00
import { getScrollContainer, getScrollPosition } from '@/scripts/scroll';
2020-11-01 13:09:16 +00:00
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);
2020-11-01 13:09:16 +00:00
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 13:09:16 +00:00
container.scrollTop = height;
}
});
ro.observe(src);
// TODO: 新たにプロパティを作るのをやめMapを使う
src._ro_ = ro;
},
unmounted(src, binding, vn) {
if (src._ro_) src._ro_.unobserve(src);
},
2020-11-01 13:09:16 +00:00
} as Directive;