Fix journal scrolling on refresh and match chat height

This commit is contained in:
coyotte508 2026-09-17 14:22:08 +02:00
commit ed21bfe073
3 changed files with 73 additions and 5 deletions

View file

@ -9,6 +9,7 @@ const game = 'container';
const require = createRequire(import.meta.url);
const repo = new URL('../../', import.meta.url);
const engine = require(fileURLToPath(new URL('engine/dist/index.js', repo)));
const journalEntry = (text) => ({ type: 'move', move: { name: 'pass' }, pretty: text, simple: text });
const html =
'<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="/bundle.css"><div id="app"></div><script src="/vue.js"></script>' +
'<script src="/bundle.js"></script>';
@ -47,9 +48,13 @@ try {
assert.deepEqual(errors, [], 'bundle loads without browser errors');
let state = engine.setup(3, {}, '17');
state = JSON.parse(JSON.stringify(state));
state.newTurn = true;
state.players.forEach((player, index) => {
player.name = ['You', 'Ada Lovelace', 'Bob'][index];
});
state.log = Array.from({ length: 50 }, (_, index) =>
journalEntry(`Journal entry ${index + 1}: Ada buys containers from Bob's warehouse.`)
);
const id = (value) => value.toString(16).padStart(24, '0');
const messages = Array.from({ length: 35 }, (_, i) => ({
_id: id(i + 1),
@ -59,6 +64,8 @@ try {
text: `Earlier message ${i + 1}`,
createdAt: '2026-09-13T12:00:00Z',
}));
// BGS can load the viewer before revealing its iframe.
await page.locator('#app').evaluate((el) => (el.style.display = 'none'));
await page.evaluate(
({ game, state, messages }) => {
window.globalName = game;
@ -89,9 +96,42 @@ try {
{ game, state, messages }
);
await page.waitForFunction(() => readyCount === 1);
await page.locator('#app').evaluate((el) => (el.style.display = ''));
const panel = page.locator('.bgs-game-chat');
const input = panel.locator('input');
const list = panel.locator('.chat-messages');
const journal = page.locator('.journal-feed');
const assertJournalAtEnd = () =>
page.waitForFunction(
() => {
const el = document.querySelector('.journal-feed');
return el && el.clientHeight > 0 && Math.abs(el.scrollHeight - el.scrollTop - el.clientHeight) < 3;
},
null,
{ timeout: 3000 }
);
await assertJournalAtEnd();
const journalHeight = await page
.locator('.inline-game-log')
.evaluate((el) => el.getBoundingClientRect().height);
const chatHeight = await panel.evaluate((el) => el.getBoundingClientRect().height);
assert.ok(Math.abs(journalHeight - chatHeight) < 2, 'journal and chat have the same height');
await journal.evaluate((el) => {
el.scrollTop = 0;
el.dispatchEvent(new Event('scroll'));
});
state.log.push(journalEntry('New entry while reading older moves.'));
await page.evaluate((state) => host.emit('state', state), state);
await journal.locator('.journal-entry').last().getByText('New entry while reading older moves.').waitFor();
assert.equal(await journal.evaluate((el) => el.scrollTop), 0, 'new moves preserve the reading position');
await journal.evaluate((el) => {
el.scrollTop = el.scrollHeight;
el.dispatchEvent(new Event('scroll'));
});
state.log.push(journalEntry('New entry while following the journal.'));
await page.evaluate((state) => host.emit('state', state), state);
await journal.locator('.journal-entry').last().getByText('New entry while following the journal.').waitFor();
await assertJournalAtEnd();
await panel.scrollIntoViewIfNeeded();
await page.waitForFunction(() => {
const el = document.querySelector('.chat-messages');
@ -212,6 +252,14 @@ try {
);
assert.equal(await page.locator('.bgs-game-chat').count(), 1);
assert.equal(await page.locator('.chat-messages article').count(), 0, 'relaunch detaches old chat');
await assertJournalAtEnd();
await page.reload();
await page.evaluate((state) => {
const host = window.container.launch('#app');
host.emit('preferences', { sound: false });
host.emit('state', state);
}, state);
await assertJournalAtEnd();
assert.deepEqual(errors, [], 'no browser errors');
await page.close();
console.log(`${game} ${width}px: protocol/chat smoke passed`);

View file

@ -13,8 +13,18 @@ import { Component, Prop, Vue, Watch } from 'vue-property-decorator';
export default class InlineLog extends Vue {
@Prop({ default: () => [] }) entries!: string[];
follow = true;
resizeObserver?: ResizeObserver;
mounted() {
this.scrollToLatest();
this.$nextTick(this.scrollToLatest);
// A viewer can mount while its iframe is still hidden. Follow the end
// once the feed is visible and its scroll dimensions are available.
this.resizeObserver = new ResizeObserver(() => {
if (this.follow) this.scrollToLatest();
});
this.resizeObserver.observe(this.$refs.feed as HTMLElement);
}
beforeDestroy() {
this.resizeObserver?.disconnect();
}
@Watch('entries') changed() {
if (this.follow) {
@ -29,6 +39,7 @@ export default class InlineLog extends Vue {
}
onScroll() {
const feed = this.$refs.feed as HTMLElement;
if (!feed.clientHeight) return;
this.follow = feed.scrollHeight - feed.scrollTop - feed.clientHeight < 32;
}
}
@ -37,20 +48,21 @@ export default class InlineLog extends Vue {
.inline-game-log {
box-sizing: border-box;
width: 100%;
padding: 10px 14px;
padding: 8px 12px;
border: 1px solid #75818d66;
border-radius: 6px;
background: #e5eeea;
color: #203a45;
font: 14px system-ui;
font: 14px/1.4 system-ui;
}
summary {
cursor: pointer;
font-weight: 600;
}
.journal-feed {
max-height: 220px;
height: var(--game-panel-content-height, 300px);
overflow: auto;
overflow-anchor: none;
overscroll-behavior: contain;
margin-top: 8px;
}

View file

@ -17,7 +17,9 @@ export function mountGameChat(emitter: ViewerEmitter<any, any>, host: Element):
.bgs-game-chat .chat-day{display:flex;align-items:center;gap:8px;margin:12px 0 6px;font-size:.85em;opacity:.8}
.bgs-game-chat .chat-day::before,.bgs-game-chat .chat-day::after{content:"";flex:1;border-top:1px solid currentColor;opacity:.3}
.bgs-game-chat .chat-day time{margin:0;color:inherit;font-size:inherit}
.bgs-game-chat .chat-messages{max-height:250px;overflow:auto;overscroll-behavior:contain;margin:6px 0 8px}
.bgs-game-chat .chat-body{height:var(--game-panel-content-height,300px);display:flex;flex-direction:column;margin-top:8px}
.bgs-game-chat .chat-body > :not(.chat-messages){flex-shrink:0}
.bgs-game-chat .chat-messages{flex:1;min-height:0;overflow:auto;overscroll-behavior:contain;margin:0 0 8px}
.bgs-game-chat article{padding:5px 0;border-bottom:1px solid #75818d26;white-space:pre-wrap;overflow-wrap:anywhere}
.bgs-game-chat article strong{padding:0 3px;font-weight:bold}
.bgs-game-chat article:last-child{border-bottom:0}
@ -61,6 +63,12 @@ export function mountGameChat(emitter: ViewerEmitter<any, any>, host: Element):
updateShortcut();
},
});
// Keep the message list flexible so the composer, suggestions and status
// fit inside the same panel height as the journal.
const body = document.createElement('div');
body.className = 'chat-body';
body.append(...Array.from(view.element.children).filter((child) => child.tagName !== 'SUMMARY'));
view.element.append(body);
function renderAuthor(message: ChatMessage): Node {
const author = document.createElement('strong');
author.textContent = message.author || 'Game';