Skip to content

Commit 63ab388

Browse files
committed
history: scroll active move into view
1 parent 73d1f7d commit 63ab388

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/Game.jsx

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,35 @@ class Game extends React.Component {
3131
componentDidMount() {
3232
this.init(this.state.src);
3333
}
34+
componentDidUpdate() {
35+
this.adjustHistoryScroll()
36+
}
37+
adjustHistoryScroll() {
38+
const history = document.querySelector(".history");
39+
const move = history.querySelector(".current-move")
40+
if (!move) {
41+
history.scroll(0,0);
42+
return;
43+
}
44+
45+
const viewBox = {
46+
bottom: history.offsetTop + history.offsetHeight,
47+
top: history.offsetTop + history.scrollTop
48+
}
49+
const moveBox = {
50+
bottom: move.offsetTop + move.offsetHeight,
51+
top: move.offsetTop
52+
}
53+
54+
55+
if (moveBox.bottom > viewBox.bottom) {
56+
history.scrollBy(0, moveBox.bottom-viewBox.bottom);
57+
}
58+
if (moveBox.top < viewBox.top) {
59+
history.scrollBy(0, moveBox.top-viewBox.top)
60+
}
61+
62+
}
3463
init(pgn) {
3564
if (!pgn) {
3665
return

0 commit comments

Comments
 (0)