Skip to content

Commit 2f163c1

Browse files
authored
Scroll to view code panel as needed (#1120)
* scroll to view code panel as needed * update function name to be clearer
1 parent d43d7f9 commit 2f163c1

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

packages/host/app/components/matrix/room-message.gts

+29-10
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,12 @@ import { tracked } from '@glimmer/tracking';
88

99
import { task } from 'ember-concurrency';
1010
import perform from 'ember-concurrency/helpers/perform';
11-
11+
import { modifier } from 'ember-modifier';
1212
import { marked } from 'marked';
1313

1414
import { Button } from '@cardstack/boxel-ui/components';
1515
import { eq } from '@cardstack/boxel-ui/helpers';
1616
import { Copy as CopyIcon } from '@cardstack/boxel-ui/icons';
17-
import { setCssVar } from '@cardstack/boxel-ui/modifiers';
1817

1918
import { sanitizeHtml } from '@cardstack/runtime-common';
2019

@@ -47,6 +46,7 @@ interface Signature {
4746
export default class Room extends Component<Signature> {
4847
<template>
4948
<AiAssistantMessage
49+
id='message-container-{{@message.index}}'
5050
class='room-message'
5151
@formattedMessage={{htmlSafe this.formattedMessage}}
5252
@datetime={{@message.created}}
@@ -110,7 +110,7 @@ export default class Room extends Component<Signature> {
110110
</Button>
111111
<div
112112
class='monaco-container'
113-
{{setCssVar monaco-container-height=this.monacoContainerHeight}}
113+
{{this.scrollBottomIntoView}}
114114
{{monacoModifier
115115
content=this.previewPatchCode
116116
contentChanged=undefined
@@ -251,13 +251,32 @@ export default class Room extends Component<Signature> {
251251
}
252252
}
253253

254-
private get monacoContainerHeight() {
255-
if (this.args.currentEditor === this.args.message.index) {
256-
let height = this.monacoService.getContentHeight();
257-
if (height && height > 0) {
258-
return `${height}px`;
259-
}
254+
private scrollBottomIntoView = modifier((element: HTMLElement) => {
255+
if (this.args.currentEditor !== this.args.message.index) {
256+
return;
257+
}
258+
259+
let height = this.monacoService.getContentHeight();
260+
if (!height || height < 0) {
261+
return;
262+
}
263+
element.style.height = `${height}px`;
264+
265+
let outerContainer = document.getElementById(
266+
`message-container-${this.args.message.index}`,
267+
);
268+
if (!outerContainer) {
269+
return;
270+
}
271+
this.scrollIntoView(outerContainer);
272+
});
273+
274+
private scrollIntoView(element: HTMLElement) {
275+
let { top, bottom } = element.getBoundingClientRect();
276+
let isVerticallyInView = top >= 0 && bottom <= window.innerHeight;
277+
278+
if (!isVerticallyInView) {
279+
element.scrollIntoView({ block: 'end' });
260280
}
261-
return undefined;
262281
}
263282
}

0 commit comments

Comments
 (0)