diff --git a/app/src/components/channels/chat-transcript.tsx b/app/src/components/channels/chat-transcript.tsx index 4253e33f09..fe5e959fff 100644 --- a/app/src/components/channels/chat-transcript.tsx +++ b/app/src/components/channels/chat-transcript.tsx @@ -187,6 +187,7 @@ import { splitChatOrigin } from "./chat-origin"; import type { QueuedMessage } from "./composer"; import { transcriptColumn } from "./conversation-columns"; import { FileTypeIcon } from "./file-icon"; +import { FoldedMessage } from "./folded-message"; import { FollowScroll, RESUME_PX, useFollowIntent } from "./follow-intent"; import { LateRenderersContext, LateRenderersWatch } from "./late-renderers"; import { usePacedReveal } from "./paced-reveal"; @@ -1311,30 +1312,33 @@ const TranscriptMessage = memo(function TranscriptMessage({ // emphasis. The chip is the one exception, and it is not reformatting: it is drawing // the thing that was already a chip in the composer as a chip here too, so the // transcript shows a skill was used rather than a slash that was typed. - - {origin && ( - - via {origin.via} - - )} - {invoked ? ( - <> - {/* - * The same icon the sidebar uses for Skills, so the badge says WHAT KIND of - * thing was invoked before it says which one. `inline-flex` with - * `align-middle` rather than a block: this sits mid-sentence, and a badge that - * breaks the line it is in reads as a separate message. - */} - - - {invoked.chip} + // A long one folds to its first lines (see `FoldedMessage`). + + + {origin && ( + + via {origin.via} - {invoked.rest} - - ) : ( - said - )} - + )} + {invoked ? ( + <> + {/* + * The same icon the sidebar uses for Skills, so the badge says WHAT KIND of + * thing was invoked before it says which one. `inline-flex` with + * `align-middle` rather than a block: this sits mid-sentence, and a badge that + * breaks the line it is in reads as a separate message. + */} + + + {invoked.chip} + + {invoked.rest} + + ) : ( + said + )} + + ) : ( /* * A Bot's prose is markdown, and it arrives in pieces. diff --git a/app/src/components/channels/folded-message.tsx b/app/src/components/channels/folded-message.tsx new file mode 100644 index 0000000000..98c11cb5f9 --- /dev/null +++ b/app/src/components/channels/folded-message.tsx @@ -0,0 +1,105 @@ +import { + type ReactNode, + useId, + useLayoutEffect, + useRef, + useState, +} from "react"; +import { Button } from "@/components/ui/button"; +import { cn } from "@/lib/utils"; + +/** How many lines of a long message show while it is folded. */ +export const FOLD_LINES = 8; + +/** + * How many lines more than `FOLD_LINES` a message needs before it folds. A message one line too long + * would fold to hide that one line, and "Show more" would cost more room than it saves. + */ +export const FOLD_SLACK_LINES = 2; + +/** Used when the browser gives no line height to measure with, as in a test's DOM. */ +const FALLBACK_LINE_HEIGHT = 24; + +/** + * A person's message, folded to its first lines when it is long, with Show more and Show less. + * + * A pasted prompt of forty lines pushed the Bot's answer below the screen, and the person scrolled + * past their own words to read it every time. Claude and ChatGPT fold a long message the same way. + * + * WHAT IS LONG is measured, not counted: lines are what the screen draws, so a message is long by its + * height at the width it has, and is measured again when that width changes. The text is all there + * while folded, only clipped (`max-h-[8lh]`, eight of its own lines), so a screen reader reads it + * all, Copy copies it all, and the browser's find reaches it: find scrolls the clipped box to show a + * match, and a scroll of a folded box unfolds it. The button is a real button and says, with + * `aria-expanded`, which way it went. + */ +export function FoldedMessage({ children }: { children: ReactNode }) { + const id = useId(); + const body = useRef(null); + const toggle = useRef(null); + const [long, setLong] = useState(false); + const [open, setOpen] = useState(false); + + // Before paint, so a long message never shows whole for a frame and then jumps. + useLayoutEffect(() => { + const element = body.current; + if (!element) return; + const measure = () => { + const line = Number.parseFloat(getComputedStyle(element).lineHeight); + const lineHeight = line > 0 ? line : FALLBACK_LINE_HEIGHT; + // The whole text's height, folded or not: clipping leaves `scrollHeight` alone. + setLong( + element.scrollHeight > lineHeight * (FOLD_LINES + FOLD_SLACK_LINES), + ); + }; + measure(); + if (typeof ResizeObserver === "undefined") return; + const observer = new ResizeObserver(measure); + observer.observe(element); + return () => observer.disconnect(); + }, []); + + const folded = long && !open; + + return ( + <> +
{ + // The browser's find scrolled the clipped text to a match: show the whole message. + if (!folded) return; + event.currentTarget.scrollTop = 0; + setOpen(true); + }} + ref={body} + > + {children} +
+ {long ? ( + + ) : null} + + ); +} diff --git a/app/src/lib/markdown-table.tsx b/app/src/lib/markdown-table.tsx index 64f98d00d5..f974b6d146 100644 --- a/app/src/lib/markdown-table.tsx +++ b/app/src/lib/markdown-table.tsx @@ -241,6 +241,20 @@ const MENU_ITEM = */ const CELLS_AT_TOP = "[&_td]:align-top"; +/* + * The second change: Copy, Download and Full screen show while the pointer is on the table or focus + * is in it, as a reply's own Copy shows on hover, so a reply with three tables isn't nine icons at + * rest. Where there is no hover (a touch screen), they always show. They fade rather than leave, so + * their row keeps its room and the table doesn't move when they appear, and they stay in the tab + * order: focus on one shows them all. + * + * An open Copy or Download menu keeps them shown (`has-[[data-slot=markdown-table-menu]]`), with the + * pointer anywhere. Focus alone doesn't: Safari doesn't focus a button on a click, so there the menu + * faded away with its buttons as soon as the pointer left the table. + */ +const ACTIONS_ON_HOVER = + "transition-opacity focus-within:opacity-100 motion-reduce:transition-none [@media(hover:hover)]:opacity-0 [@media(hover:hover)]:group-hover/table:opacity-100 has-[[data-slot=markdown-table-menu]]:opacity-100"; + function Icon({ d, ...props }: SVGProps & { d: string }) { return ( {open ? ( -
+
{open ? ( -
+