diff --git a/src/components/ChatMarkdown.test.ts b/src/components/ChatMarkdown.test.ts index 79ad1b6a4..ed3083eb4 100644 --- a/src/components/ChatMarkdown.test.ts +++ b/src/components/ChatMarkdown.test.ts @@ -77,8 +77,11 @@ describe("mention highlighting", () => { text: "@Atlas @Juniper @everyone", everyone: true, mentionPeers: [{ name: "Atlas", color: "blue" }, { name: "Juniper", color: "red" }], })); - expect(html).toContain('style="--mention-color:#286ef9">@Atlas'); - expect(html).toContain('style="--mention-color:#ef3a3f">@Juniper'); + expect(html).toContain(''); + expect(html).toContain('@Atlas'); + expect(html).toContain('data-colour="#286ef9"'); + expect(html).toContain('data-bot-color="red"'); + expect(html).toContain('data-colour="#ef3a3f"'); expect(html).toContain('@everyone'); }); it("highlights known mentions in prose, lists and tables", () => { diff --git a/src/components/ChatMarkdown.tsx b/src/components/ChatMarkdown.tsx index aa218bffd..ad915628d 100644 --- a/src/components/ChatMarkdown.tsx +++ b/src/components/ChatMarkdown.tsx @@ -26,6 +26,7 @@ import { knownImageSize, rememberRenderedSize, renderedSize } from "@/lib/image- import { MarkdownImagePreview, MessageFolderFiles, OutsideWorkspaceFile, saveFailureText, useLocalFileSave, type MessageAttachmentContext } from "./AttachmentPreview"; import { ThreadLink, ThreadRefsContext, threadLinkFromProps, type ThreadRefsValue } from "./ThreadRefs"; import { MarkdownTable } from "./MarkdownTable"; +import { MentionChip } from "./MentionText"; import { CodeBlock, CodePlate, codeHash } from "./CodeBlock"; import { Icon } from "./Icon"; import { appLogo } from "./connect-apps/logos"; @@ -563,6 +564,7 @@ interface MessageScope { threads: ThreadRefsValue["threads"]; currentBotId?: string; delivered?: DeliveredFiles; + mentionPeers?: readonly MentionPeer[]; } export type DeliveredFiles = Readonly>; @@ -692,6 +694,12 @@ function MarkdownLink({ href, children, ...rest }: { href?: string; children?: R ); } +function MarkdownMention({ name, children }: { name: string; children: ReactNode }) { + const { mentionPeers = NO_MENTION_PEERS } = useContext(MessageScopeContext); + const bot = mentionPeers.find((peer) => !peer.person && peer.color && peer.name === name); + return bot?.color ? : children; +} + const MARKDOWN_COMPONENTS: Components = { pre: MarkdownCode, img: MarkdownImage, @@ -705,6 +713,8 @@ const MARKDOWN_COMPONENTS: Components = { const link = threadLinkFromProps(props as Record); if (link) return {props.children}; const { node: _node, children, ...rest } = props; + const mentioned = (props as Record)["data-mention-bot"]; + if (typeof mentioned === "string") return {children}; return {children}; }, a: MarkdownLink, @@ -845,7 +855,7 @@ function ChatMarkdownComponent({ text, message, mentionPeers = NO_MENTION_PEERS, }; const markdown = reveal ? draw() : markdownCache.render(text, variant, draw); return ( - +
{markdown}
); diff --git a/src/components/MentionColors.test.ts b/src/components/MentionColors.test.ts index f08735486..a6650fcc1 100644 --- a/src/components/MentionColors.test.ts +++ b/src/components/MentionColors.test.ts @@ -1,7 +1,7 @@ import { createElement } from "react"; import { renderToStaticMarkup } from "react-dom/server"; import { expect, it } from "vitest"; -import { MAUS_COLORS, MAUS_COLOR_NAMES } from "@/lib/mascot"; +import { MAUS_COLOR_NAMES } from "@/lib/mascot"; import { MentionText } from "./MentionText"; import { ChatMarkdown } from "./ChatMarkdown"; @@ -13,10 +13,12 @@ it.each(["plain text", "Markdown"] as const)("keeps all ten bot colors independe const html = renderToStaticMarkup(renderer === "plain text" ? createElement(MentionText, { text, peers: roster, everyone: true }) : createElement(ChatMarkdown, { text, mentionPeers: roster, everyone: true })); - const highlights = [...html.matchAll(/([^<]+)<\/span>/g)] + const faceless = html.replace(/.*?@<\/span>([^<]+)<\/span>/g, + '@$2'); + const highlights = [...faceless.matchAll(/([^<]+)<\/span>/g)] .map(([, color, name]) => ({ name, color })); expect(highlights).toEqual(ordered.flatMap(({ name, color }) => [ - { name: `@${name}`, color: MAUS_COLORS[color] }, + { name: `@${name}`, color }, { name: "@everyone", color: undefined }, ])); expect(html.match(/<\/span> ordinary /g)).toHaveLength(ordered.length); diff --git a/src/components/MentionText.test.ts b/src/components/MentionText.test.ts index 870c7a13e..59eea9687 100644 --- a/src/components/MentionText.test.ts +++ b/src/components/MentionText.test.ts @@ -10,8 +10,11 @@ it("uses each bot's avatar palette and updates when its color changes", () => { text: "@Atlas @Juniper @everyone", everyone: true, peers: [{ name: "Atlas", color }, { name: "Juniper", color: "purple" }], })); - expect(html).toContain(`style="--mention-color:${MAUS_COLORS[color]}">@Atlas`); - expect(html).toContain(`style="--mention-color:${MAUS_COLORS.purple}">@Juniper`); + expect(html).toContain(``); + expect(html).toContain('@Atlas'); + expect(html).toContain(`data-colour="${MAUS_COLORS[color]}"`); + expect(html).toContain(`data-bot-color="purple"`); + expect(html).toContain('@Juniper'); expect(html).toContain('@everyone'); } }); diff --git a/src/components/MentionText.tsx b/src/components/MentionText.tsx index 2aa7b9049..267be5148 100644 --- a/src/components/MentionText.tsx +++ b/src/components/MentionText.tsx @@ -1,17 +1,48 @@ -import { Fragment, type CSSProperties, type ReactNode } from "react"; -import { mentionClassName, mentionRanges, type MentionPeer, type MentionPerson } from "@/lib/mentions"; +import { Fragment, useContext, type CSSProperties, type ReactNode } from "react"; +import { mentionClassName, mentionRanges, type MentionBot, type MentionPeer, type MentionPerson } from "@/lib/mentions"; import { linkRanges } from "@/lib/link-ranges"; +import { openBot } from "@/state/thread-navigation"; +import { StoreRefContext } from "@/state/store-ref-context"; +import { BotAvatar } from "./Avatar"; + +/** A bot mention as a chip: the bot's face and name in its colour. With `opens`, a click opens that bot's chat. */ +export function MentionChip({ bot, opens = true }: { bot: MentionBot; opens?: boolean }) { + const store = useContext(StoreRefContext); + const className = "mention-highlight bot-identity mention-chip"; + const body = <> + + @{bot.name} + ; + if (!opens || !store || !bot.id) return {body}; + const open = () => { + const found = store.state().bots.find((one) => one.id === bot.id); + if (found) openBot(store.dispatch, found); + }; + return ; +} + +/** The "@" of a bot mention in the composer's mirror, drawn as the bot's face. The glyph stays in the text, unseen, so the + * field's caret keeps its place. The face is wider than the "@", so it can meet a caret placed just before the mention. */ +function MentionAt({ bot }: { bot: MentionBot }) { + return ( + + @ + + ); +} /** * `links` marks web addresses as link pills: "highlight" paints them only (the composer's mirror, which sits under the * real field and must never change the text's layout), "open" makes each one a link that opens outside the app. * `hoverStart` is where the address under the pointer starts, so the composer can show its box. */ -export function MentionText({ text, peers, everyone = false, person, links, hoverStart, command }: { +export function MentionText({ text, peers, everyone = false, person, links, hoverStart, command, chips = true }: { text: string; peers: readonly MentionPeer[]; everyone?: boolean; person?: MentionPerson; links?: "highlight" | "open"; hoverStart?: number | null; /** The composer's slash command at the start of the text: painted in `color`, the text itself unchanged. */ command?: { end: number; color: string }; + /** Bot mentions as chips. The composer's mirror draws each one's face over its "@" instead, keeping every glyph under the field's caret. */ + chips?: boolean; }) { const addresses = links ? linkRanges(text) : []; // a name inside an address is part of the address, not a mention @@ -35,8 +66,9 @@ export function MentionText({ text, peers, everyone = false, person, links, hove : {value}; } else { - const style = range.color ? { "--mention-color": range.color } as CSSProperties : undefined; - node = {value}; + node = !range.bot ? {value} + : chips ? + : {value.slice(1)}; } return {prefix}{node}; }); diff --git a/src/components/MentionTextarea.dom.test.ts b/src/components/MentionTextarea.dom.test.ts index 3638d94d3..3a1e5d3af 100644 --- a/src/components/MentionTextarea.dom.test.ts +++ b/src/components/MentionTextarea.dom.test.ts @@ -14,7 +14,7 @@ afterEach(() => { vi.unstubAllGlobals(); }); -it("draws its words in the field's own type and padding, so the caret stays at the end of the text", async () => { +it("draws its words in the field's own type and padding, 4px wider each side for a mention's face, so the caret stays at the end of the text", async () => { vi.stubGlobal("ResizeObserver", class { observe() {} disconnect() {} }); document.head.appendChild(document.createElement("style")).textContent = ".dock { font-size: 14px; line-height: 20px; padding: 4px 2px } .wide { font-size: 15px; line-height: 24px; padding: 4px }"; @@ -25,9 +25,9 @@ it("draws its words in the field's own type and padding, so the caret stays at t }); const mirror = () => document.querySelector(".mention-editor-mirror")!.style; await show("dock"); - expect([mirror().fontSize, mirror().lineHeight, mirror().paddingLeft]).toEqual(["14px", "20px", "2px"]); + expect([mirror().fontSize, mirror().lineHeight, mirror().paddingLeft]).toEqual(["14px", "20px", "6px"]); await show("wide"); - expect([mirror().fontSize, mirror().lineHeight, mirror().paddingLeft], "and follows the field when its classes change").toEqual(["15px", "24px", "4px"]); + expect([mirror().fontSize, mirror().lineHeight, mirror().paddingLeft], "and follows the field when its classes change").toEqual(["15px", "24px", "8px"]); act(() => root.unmount()); }); @@ -40,7 +40,7 @@ it("sits over the field where the wrapper pads it, so the typed words start wher root.render(createElement(MentionTextarea, { inputRef, peers: [], wrapperClassName: "px-1.5", value: "hey", onChange: () => {} })); }); const mirror = document.querySelector(".mention-editor-mirror")!.style; - expect([mirror.left, mirror.right]).toEqual(["6px", "auto"]); + expect([mirror.left, mirror.right, mirror.paddingLeft]).toEqual(["2px", "auto", "4px"]); offsetLeft.mockRestore(); act(() => root.unmount()); }); diff --git a/src/components/MentionTextarea.test.ts b/src/components/MentionTextarea.test.ts index cadf7d474..25115a987 100644 --- a/src/components/MentionTextarea.test.ts +++ b/src/components/MentionTextarea.test.ts @@ -11,5 +11,6 @@ it.each(["auto", "rtl", "ltr"])("shares the %s direction between the native inpu })); expect(html).toContain(`