From d98784b6e191e439f544fc5019bff6717958b166 Mon Sep 17 00:00:00 2001 From: Geoffrey Allott Date: Sat, 27 May 2023 21:35:26 +0100 Subject: [PATCH] fix scroll-to-bottom by suppressing scroll events for 1/2 a second --- site/modules/chatroom.js | 18 ++++++++++++++---- site/modules/poker.js | 1 + site/style/chatroom.css | 1 + 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/site/modules/chatroom.js b/site/modules/chatroom.js index b2b17a1..ddd669a 100644 --- a/site/modules/chatroom.js +++ b/site/modules/chatroom.js @@ -22,6 +22,14 @@ export class Chatroom { this.chatroom.append(this.chatroom_input); this.container.append(this.chatroom); + this.is_at_end = true; + this.suppress_scroll_events = null; + this.chat.onscroll = () => { + if (!this.suppress_scroll_events) { + this.is_at_end = this.chat.scrollTop + this.chat.clientHeight > this.chat.scrollHeight - 10; + } + }; + if (close) { const close_button = document.createElement("button"); close_button.classList.add("game-close"); @@ -40,18 +48,18 @@ export class Chatroom { } set_error_text(text) { - const is_at_end = this.chat.scrollTop + this.chat.clientHeight == this.chat.scrollHeight; this.chat.append(this.chat_element("chatroom-error", "«error»", text)); - if (is_at_end) { + if (this.is_at_end) { this.chat.scrollTo({ top: this.chat.scrollHeight - this.chat.clientHeight, behavior: "smooth" }); + window.clearTimeout(this.suppress_scroll_events); + this.suppress_scroll_events = window.setTimeout(() => this.suppress_scroll_events = null, 500); } } take_action(user_action, initialising) { - const is_at_end = this.chat.scrollTop + this.chat.clientHeight == this.chat.scrollHeight; switch (user_action.action.action) { case "Join": this.seats.set(user_action.username, user_action.action.seat); @@ -140,11 +148,13 @@ export class Chatroom { console.error("Unknown action for chatroom", user_action); break; } - if (is_at_end) { + if (this.is_at_end) { this.chat.scrollTo({ top: this.chat.scrollHeight - this.chat.clientHeight, behavior: "smooth" }); + window.clearTimeout(this.suppress_scroll_events); + this.suppress_scroll_events = window.setTimeout(() => this.suppress_scroll_events = null, 500); } } diff --git a/site/modules/poker.js b/site/modules/poker.js index b2e5761..c5c75f1 100644 --- a/site/modules/poker.js +++ b/site/modules/poker.js @@ -563,6 +563,7 @@ export class TexasHoldEm { } } this.pot = 0; + this.dealer = null; this.hands.clear(); this.redraw_players(); new CongratulateWinner(this.svg, user_action.username); diff --git a/site/style/chatroom.css b/site/style/chatroom.css index 3b36722..0c032d0 100644 --- a/site/style/chatroom.css +++ b/site/style/chatroom.css @@ -66,6 +66,7 @@ .chatroom-text { justify-self: right; text-align: left; + overflow-wrap: break-word; height: 100%; margin: 0.5vmin; width: calc(100% - 36vmin); -- 2.34.1