Skip to content

Commit

Permalink
history: implement mobile view
Browse files Browse the repository at this point in the history
  • Loading branch information
turtleDev committed Jun 19, 2023
1 parent 63ab388 commit 48bcd0d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 23 deletions.
55 changes: 33 additions & 22 deletions src/Game.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,29 +36,39 @@ class Game extends React.Component {
}
adjustHistoryScroll() {
const history = document.querySelector(".history");
const move = history.querySelector(".current-move")
const move = history.querySelector(".current-move-row")
if (!move) {
history.scroll(0,0);
history.scroll(0, 0);
return;
}

const viewBox = {
bottom: history.offsetTop + history.offsetHeight,
top: history.offsetTop + history.scrollTop
bottom: history.offsetHeight + history.scrollTop,
top: history.scrollTop,
right: history.offsetWidth + history.scrollLeft,
left: history.scrollLeft
}
const moveBox = {
bottom: move.offsetTop + move.offsetHeight,
top: move.offsetTop
top: move.offsetTop,
right: move.offsetLeft + move.offsetWidth,
left: move.offsetLeft
}


if (moveBox.bottom > viewBox.bottom) {
history.scrollBy(0, moveBox.bottom-viewBox.bottom);
history.scrollBy(0, moveBox.bottom - viewBox.bottom);
}
if (moveBox.top < viewBox.top) {
history.scrollBy(0, moveBox.top-viewBox.top)
history.scrollBy(0, moveBox.top - viewBox.top);
}

if (moveBox.right > viewBox.right) {
history.scrollBy(moveBox.right - viewBox.right, 0);
}
if (moveBox.left < viewBox.left) {
history.scrollBy(moveBox.left - viewBox.left, 0);
}

}
init(pgn) {
if (!pgn) {
Expand Down Expand Up @@ -131,21 +141,26 @@ class Game extends React.Component {
if (!this.state.history) {
return null;
}

const getMoveClass = (idx, current) => idx === current ? "current-move" : "";
const getRowClass = (idx, current) =>
(idx === current || idx + 1 === current) ? "current-move-row" : "";

const hist = this.state.history;
let rows = [];
for (let i = 0; i < hist.moves.length; i += 2) {
rows.push(
<tr key={i}>
<tr className={getRowClass(i, hist.current)} key={i}>
<td>{(i / 2) + 1}</td>
<td className={getRowClass(i, hist.current)}>{hist.moves[i]}</td>
<td className={getRowClass(i + 1, hist.current)}>{hist.moves[i + 1]}</td>
<td className={getMoveClass(i, hist.current)}>{hist.moves[i]}</td>
<td className={getMoveClass(i + 1, hist.current)}>{hist.moves[i + 1]}</td>
</tr>
)
}
return (
<div className="history hidden lg:block absolute top-0 -right-[11rem] max-h-full overflow-y-auto overflow-x-hidden max-h-[97%]">
<div className="history select-none lg:block lg:absolute my-[1rem] lg:top-2rem lg:-right-[11rem] overflow-y-auto w-[calc(100vw-2rem)] lg:w-auto overflow-x-auto lg:overflow-x-hidden lg:h-[32rem]">
<table>
<tbody>
<tbody className="flex lg:table-row-group">
{rows}
</tbody>
</table>
Expand Down Expand Up @@ -174,7 +189,10 @@ class Game extends React.Component {
<div className="game-container relative">
{this.gameTitle()}
<div className="board-container">
<Board state={this.state.board} move={this.state.move} />
<div className="flex flex-col lg:flex-row">
<Board state={this.state.board} move={this.state.move} />
{this.history()}
</div>
{gameDataAvailable &&
<div className="controls">
<FontAwesomeIcon className="hover:cursor-pointer" icon={faBackwardFast} onClick={this.handleGotoFirst} size="2x" />
Expand All @@ -191,7 +209,7 @@ class Game extends React.Component {
}

<button
className={"btn my-4 w-full " + (showSource ? "bg-slate-400" : "")}
className={"btn my-4 w-full select-none " + (showSource ? "bg-slate-400" : "")}
onClick={() => this.setState({ showSource: !showSource })}
>
view source
Expand All @@ -206,20 +224,13 @@ class Game extends React.Component {
</div>

</div>
{this.history()}
</div>
</div>
);
}
}


function getRowClass(idx, current) {
if (idx === current) {
return "current-move";
}
return "";
}

function GameContainer(props) {
const loaderData = useLoaderData();
Expand Down
1 change: 0 additions & 1 deletion src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ code {
}

.history {

td {
padding: 2px 4px;
}
Expand Down

0 comments on commit 48bcd0d

Please sign in to comment.