Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'scrollable' prop to Markdown component for better control over scrolling behavior #964

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions frontend/app/element/markdown.less
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
overflow: hidden;
height: 100%;
width: 100%;

.content {
height: 100%;
width: 100%;
Expand All @@ -17,6 +18,10 @@
font-size: 14px;
overflow-wrap: break-word;

&.non-scrollable {
overflow: hidden;
}

.heading {
&:first-of-type {
margin-top: 0 !important;
Expand Down
55 changes: 52 additions & 3 deletions frontend/app/element/markdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,19 @@ type MarkdownProps = {
className?: string;
onClickExecute?: (cmd: string) => void;
resolveOpts?: MarkdownResolveOpts;
scrollable?: boolean;
};

const Markdown = ({ text, textAtom, showTocAtom, style, className, resolveOpts, onClickExecute }: MarkdownProps) => {
const Markdown = ({
text,
textAtom,
showTocAtom,
style,
className,
resolveOpts,
scrollable = true,
onClickExecute,
}: MarkdownProps) => {
const textAtomValue = useAtomValueSafe(textAtom);
const tocRef = useRef<TocItem[]>([]);
const showToc = useAtomValueSafe(showTocAtom) ?? false;
Expand Down Expand Up @@ -240,8 +250,8 @@ const Markdown = ({ text, textAtom, showTocAtom, style, className, resolveOpts,

text = textAtomValue ?? text;

return (
<div className={clsx("markdown", className)} style={style}>
const ScrollableMarkdown = () => {
return (
<OverlayScrollbarsComponent
ref={contentsOsRef}
className="content"
Expand Down Expand Up @@ -274,6 +284,45 @@ const Markdown = ({ text, textAtom, showTocAtom, style, className, resolveOpts,
{text}
</ReactMarkdown>
</OverlayScrollbarsComponent>
);
};

const NonScrollableMarkdown = () => {
return (
<div className="content non-scrollable">
<ReactMarkdown
remarkPlugins={[remarkGfm, [RemarkFlexibleToc, { tocRef: tocRef.current }]]}
rehypePlugins={[
rehypeRaw,
rehypeHighlight,
() =>
rehypeSanitize({
...defaultSchema,
attributes: {
...defaultSchema.attributes,
span: [
...(defaultSchema.attributes?.span || []),
// Allow all class names starting with `hljs-`.
["className", /^hljs-./],
// Alternatively, to allow only certain class names:
// ['className', 'hljs-number', 'hljs-title', 'hljs-variable']
],
},
tagNames: [...(defaultSchema.tagNames || []), "span"],
}),
() => rehypeSlug({ prefix: idPrefix }),
]}
components={markdownComponents}
>
{text}
</ReactMarkdown>
</div>
);
};

return (
<div className={clsx("markdown", className)} style={style}>
{scrollable ? <ScrollableMarkdown /> : <NonScrollableMarkdown />}
{toc && (
<OverlayScrollbarsComponent className="toc" options={{ scrollbars: { autoHide: "leave" } }}>
<div className="toc-inner">
Expand Down
8 changes: 2 additions & 6 deletions frontend/app/view/waveai/waveai.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import { atom, Atom, PrimitiveAtom, useAtomValue, useSetAtom, WritableAtom } fro
import type { OverlayScrollbars } from "overlayscrollbars";
import { OverlayScrollbarsComponent, OverlayScrollbarsComponentRef } from "overlayscrollbars-react";
import { forwardRef, memo, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from "react";
import tinycolor from "tinycolor2";
import "./waveai.less";

interface ChatMessageType {
Expand Down Expand Up @@ -234,11 +233,8 @@ function makeWaveAiViewModel(blockId): WaveAiModel {

const ChatItem = ({ chatItem }: ChatItemProps) => {
const { isAssistant, text, isError } = chatItem;
const senderClassName = isAssistant ? "chat-msg-assistant" : "chat-msg-user";
const msgClassName = `chat-msg ${senderClassName}`;
const cssVar = "--panel-bg-color";
const panelBgColor = getComputedStyle(document.documentElement).getPropertyValue(cssVar).trim();
const color = tinycolor(panelBgColor);

const renderError = (err: string): React.JSX.Element => <div className="chat-msg-error">{err}</div>;

Expand All @@ -255,7 +251,7 @@ const ChatItem = ({ chatItem }: ChatItemProps) => {
</div>
</div>
<div className="chat-msg chat-msg-assistant">
<Markdown text={text} />
<Markdown text={text} scrollable={false} />
</div>
</>
) : (
Expand All @@ -270,7 +266,7 @@ const ChatItem = ({ chatItem }: ChatItemProps) => {
return (
<>
<div className="chat-msg chat-msg-user">
<Markdown className="msg-text" text={text} />
<Markdown className="msg-text" text={text} scrollable={false} />
</div>
</>
);
Expand Down