1
0

Scroll lock now working!

Signed-off-by: James Ketrenos <james_eikona@ketrenos.com>
This commit is contained in:
James Ketrenos 2022-01-30 15:55:55 -08:00
parent d4993dbc62
commit 042a582363
2 changed files with 50 additions and 27 deletions

View File

@ -247,18 +247,19 @@ class Resource extends React.Component {
}; };
const Chat = ({ board, promoteGameState }) => { const Chat = ({ board, promoteGameState }) => {
const [lastHeight, setLastHeight] = useState(0); const [lastTop, setLastTop] = useState(0),
[autoScroll, setAutoscroll] = useState(true),
[scrollTime, setScrollTime] = useState(0);
const chatInput = (event) => { const chatInput = (event) => {
}; };
const chatKeyPress = (event) => { const chatKeyPress = (event) => {
if (event.key === "Enter") { if (event.key === "Enter") {
console.log(`Send: ${event.target.value} ${lastHeight}`); if (!autoScroll) {
const chatList = document.getElementById("ChatList"); setAutoscroll(true);
setLastHeight(0); /* Reset sticky scroll */ }
chatList.scrollTop = chatList.scrollHeight - chatList.offsetHeight;
promoteGameState({ promoteGameState({
chat: { chat: {
player: board.game.color ? board.game.color : undefined, player: board.game.color ? board.game.color : undefined,
@ -269,22 +270,49 @@ const Chat = ({ board, promoteGameState }) => {
} }
}; };
const classes = useStyles(); const chatScroll = (event) => {
useEffect(() => { const chatList = event.target,
const chatList = document.getElementById("ChatList"); fromBottom = Math.round(Math.abs((chatList.scrollHeight - chatList.offsetHeight) - chatList.scrollTop));
const scrolled = Math.abs((lastHeight ? lastHeight : chatList.scrollHeight) - chatList.offsetHeight - chatList.scrollTop); /* If scroll is within 20 pixels of the bottom, turn on auto-scroll */
const shouldAutoscroll = (fromBottom < 20);
if (lastHeight !== Math.round(chatList.scrollHeight)) {
if (lastHeight === 0 || scrolled < 20) { if (shouldAutoscroll !== autoScroll) {
console.log("Auto scroll", scrolled, chatList.scrollHeight, lastHeight); setAutoscroll(shouldAutoscroll);
chatList.scrollTop = chatList.scrollHeight - chatList.offsetHeight; }
setLastHeight(Math.round(chatList.scrollHeight));
} else { /* If the list should not auto scroll, then cache the current
console.log('Sticking scroll position', scrolled, chatList.scrollHeight, lastHeight); * top of the list and record when we did this so we honor
setLastHeight(Math.round(chatList.scrollHeight)); * the auto-scroll for at least 500ms */
if (!shouldAutoscroll) {
const target = Math.round(chatList.scrollTop);
if (target !== lastTop) {
setLastTop(target);
setScrollTime(Date.now());
} }
} }
};
const classes = useStyles();
useEffect(() => {
const chatList = document.getElementById("ChatList"),
currentTop = Math.round(chatList.scrollTop);
if (autoScroll) {
/* Auto-scroll to the bottom of the chat window */
const target = Math.round(chatList.scrollHeight - chatList.offsetHeight);
if (currentTop !== target) {
chatList.scrollTop = target;
}
return;
}
/* Maintain current position in scrolled view if the user hasn't
* been scrolling in the past 0.5s */
if ((Date.now() - scrollTime) > 500 && currentTop !== lastTop) {
chatList.scrollTop = lastTop;
}
}); });
//const timeDelta = game.timestamp - Date.now(); //const timeDelta = game.timestamp - Date.now();
@ -303,7 +331,7 @@ const Chat = ({ board, promoteGameState }) => {
return ( return (
<Paper className="Chat"> <Paper className="Chat">
<List id="ChatList"> <List id="ChatList" onScroll={chatScroll}>
{ messages } { messages }
</List> </List>
<TextField className="chatInput" <TextField className="chatInput"
@ -467,7 +495,6 @@ class Board extends React.Component {
this.drawBorders = this.drawBorders.bind(this); this.drawBorders = this.drawBorders.bind(this);
this.drawPips = this.drawPips.bind(this); this.drawPips = this.drawPips.bind(this);
this.drawDie = this.drawDie.bind(this); this.drawDie = this.drawDie.bind(this);
this.keyUp = this.keyUp.bind(this);
this.mouseMove = this.mouseMove.bind(this); this.mouseMove = this.mouseMove.bind(this);
this.throwDice = this.throwDice.bind(this); this.throwDice = this.throwDice.bind(this);
this.promoteGameState = this.promoteGameState.bind(this); this.promoteGameState = this.promoteGameState.bind(this);
@ -739,9 +766,6 @@ class Board extends React.Component {
}); });
} }
keyUp(event) {
}
setGameState(state) { setGameState(state) {
if (this.loadTimer) { if (this.loadTimer) {
window.clearTimeout(this.loadTimer); window.clearTimeout(this.loadTimer);
@ -832,7 +856,6 @@ class Board extends React.Component {
}); });
} }
mouseMove(event) { mouseMove(event) {
const rect = this.canvas.parentElement.getBoundingClientRect(); const rect = this.canvas.parentElement.getBoundingClientRect();
let x, y; let x, y;
@ -1375,7 +1398,6 @@ class Board extends React.Component {
componentDidMount() { componentDidMount() {
this.start = new Date(); this.start = new Date();
document.addEventListener("keyup", this.keyUp);
window.addEventListener("touchmove", this.mouseMove); window.addEventListener("touchmove", this.mouseMove);
window.addEventListener("mousemove", this.mouseMove); window.addEventListener("mousemove", this.mouseMove);
window.addEventListener("resize", this.updateDimensions); window.addEventListener("resize", this.updateDimensions);
@ -1456,7 +1478,6 @@ class Board extends React.Component {
this.updateSizeTimer = 0; this.updateSizeTimer = 0;
} }
document.removeEventListener("keyup", this.keyUp);
window.removeEventListener("mousemove", this.mouseMove); window.removeEventListener("mousemove", this.mouseMove);
window.removeEventListener("touchmove", this.mouseMove); window.removeEventListener("touchmove", this.mouseMove);
window.removeEventListener("resize", this.updateDimensions); window.removeEventListener("resize", this.updateDimensions);

View File

@ -459,9 +459,11 @@ router.put("/:id", (req, res/*, next*/) => {
date: Date.now(), date: Date.now(),
message: changes.chat.message message: changes.chat.message
}); });
/*
if (game.chat.length > 10) { if (game.chat.length > 10) {
game.chat.splice(0, game.chat.length - 10); game.chat.splice(0, game.chat.length - 10);
} }
*/
break; break;
} }
} }