diff options
Diffstat (limited to 'packages/client/src/directives/follow-append.ts')
| -rw-r--r-- | packages/client/src/directives/follow-append.ts | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/packages/client/src/directives/follow-append.ts b/packages/client/src/directives/follow-append.ts new file mode 100644 index 0000000000..b0e99628b0 --- /dev/null +++ b/packages/client/src/directives/follow-append.ts @@ -0,0 +1,35 @@ +import { Directive } from 'vue'; +import { getScrollContainer, getScrollPosition } from '@/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; |