scroll v1

This commit is contained in:
Andrew Gundersen 2021-03-14 13:21:38 -05:00
commit ce62603886
3 changed files with 13 additions and 36 deletions

View file

@ -161,6 +161,10 @@
.messageBox { .messageBox {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
// Animate on render.
animation-name: appear;
animation-duration: 0.25s;
} }
#aiMessageBox { #aiMessageBox {
@ -190,17 +194,13 @@
padding: 10px; padding: 10px;
border-radius: 18px; border-radius: 18px;
// Animate on render.
animation-name: appear;
animation-duration: 0.25s;
} }
@keyframes appear { @keyframes appear {
from { 10% {
transform: scale(0.3); transform: scale(0.3);
} }
to { 100% {
transform: scale(1); transform: scale(1);
} }
} }

View file

@ -25,74 +25,52 @@ function deleteOldest() {
messages.value.delete(lastKey); messages.value.delete(lastKey);
} }
// Is the message div scrolled to the bottom? // Is the message div scrolled to the bottom?
let isScrolledToBottom: boolean; let isScrolledToBottom: boolean;
// Update isScrolledToBottom // Update isScrolledToBottom
const updateScrollRef = () => { const updateScrollRef = () => {
const e = document.getElementById("messenger") const e = document.getElementById("messenger")
if (e) { if (e) {
isScrolledToBottom = e.scrollHeight - e.clientHeight <= e.scrollTop + 1 isScrolledToBottom = e.scrollHeight - e.clientHeight <= e.scrollTop + 1
} }
} }
// Adjust scroll after we add content to the messenger. // Adjust scroll after we add content to the messenger.
const adjustScroll = () => { const adjustScroll = () => {
const e = document.getElementById("messenger") const e = document.getElementById("messenger")
if (e) { 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) { if (isScrolledToBottom) {
e.scrollTop = e.scrollHeight - e.clientHeight e.scrollTo({
top: e.scrollHeight - e.clientHeight,
behavior: 'smooth'
});
} }
} }
} }
export default function useMessages() { export default function useMessages() {
const addMessage = (m: RenderMessage) => { const addMessage = (m: RenderMessage) => {
updateScrollRef() // must update before we add new elements. updateScrollRef() // must update before we add new elements.
console.log(`Adding message: ${m.content.text}`)
// check for message limit before setting a new message. // check for message limit before setting a new message.
if (messages.value.size >= sizeLimit) deleteOldest(); if (messages.value.size >= sizeLimit) deleteOldest();
messages.value.set(m.uid, m); messages.value.set(m.uid, m);
adjustScroll() setTimeout(adjustScroll, 10);
} }
const updateMessage = (a: Annotation) => { const updateMessage = (a: Annotation) => {
updateScrollRef() updateScrollRef()
console.log(`Updating message...`)
// update message if in the map. // update message if in the map.
const m = messages.value.get(a.uid) const m = messages.value.get(a.uid)
m.context = a.context m.context = a.context
m.text = a.text m.text = a.text
adjustScroll() setTimeout(adjustScroll, 10);
return m return m
} }

View file

@ -103,7 +103,6 @@ export default defineComponent({
#messenger { #messenger {
width: 100vw; width: 100vw;
height: 100vh; height: 100vh;
overflow: auto; overflow: auto;
} }