scroll v1
This commit is contained in:
parent
b28c50fa6c
commit
ce62603886
3 changed files with 13 additions and 36 deletions
|
|
@ -25,74 +25,52 @@ function deleteOldest() {
|
|||
messages.value.delete(lastKey);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
// Is the message div scrolled to the bottom?
|
||||
let isScrolledToBottom: boolean;
|
||||
|
||||
// Update isScrolledToBottom
|
||||
const updateScrollRef = () => {
|
||||
const e = document.getElementById("messenger")
|
||||
|
||||
if (e) {
|
||||
isScrolledToBottom = e.scrollHeight - e.clientHeight <= e.scrollTop + 1
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Adjust scroll after we add content to the messenger.
|
||||
const adjustScroll = () => {
|
||||
const e = document.getElementById("messenger")
|
||||
|
||||
if (e) {
|
||||
|
||||
console.log(`Bottom? : ${isScrolledToBottom}`)
|
||||
console.log(e.scrollHeight)
|
||||
console.log(e.clientHeight)
|
||||
console.log(e.scrollTop)
|
||||
|
||||
// // Scroll to bottom if isScrolledToBottom is true
|
||||
if (isScrolledToBottom) {
|
||||
e.scrollTop = e.scrollHeight - e.clientHeight
|
||||
e.scrollTo({
|
||||
top: e.scrollHeight - e.clientHeight,
|
||||
behavior: 'smooth'
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export default function useMessages() {
|
||||
|
||||
const addMessage = (m: RenderMessage) => {
|
||||
updateScrollRef() // must update before we add new elements.
|
||||
|
||||
console.log(`Adding message: ${m.content.text}`)
|
||||
|
||||
// check for message limit before setting a new message.
|
||||
if (messages.value.size >= sizeLimit) deleteOldest();
|
||||
|
||||
|
||||
messages.value.set(m.uid, m);
|
||||
|
||||
adjustScroll()
|
||||
setTimeout(adjustScroll, 10);
|
||||
}
|
||||
|
||||
const updateMessage = (a: Annotation) => {
|
||||
updateScrollRef()
|
||||
|
||||
console.log(`Updating message...`)
|
||||
|
||||
// update message if in the map.
|
||||
const m = messages.value.get(a.uid)
|
||||
m.context = a.context
|
||||
m.text = a.text
|
||||
|
||||
adjustScroll()
|
||||
setTimeout(adjustScroll, 10);
|
||||
|
||||
return m
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue