fix(webui): preserve native touch menus with message link actions
This commit is contained in:
1 parent
b08c9da8de
commit
4693bba81f
18 files changed
+291
-65
No files matched your search
@@ -14,6 +14,7 @@ import {
|
||||
CircleAlert,
|
||||
Clock3,
|
||||
Copy,
|
||||
Link2,
|
||||
Quote,
|
||||
Wrench,
|
||||
} from "lucide-react";
|
||||
@@ -131,6 +132,7 @@ interface MessageBlockMenuActionsProps {
|
||||
isTurnStreaming?: boolean;
|
||||
onForkFromHere?: () => void;
|
||||
activity?: MessageBlockMenuActivity;
|
||||
onViewLinks?: () => void;
|
||||
}
|
||||
|
||||
/** Actions and metadata shown after opening a visual message block's context panel. */
|
||||
@@ -139,6 +141,7 @@ export function MessageBlockMenuActions({
|
||||
isTurnStreaming = false,
|
||||
onForkFromHere,
|
||||
activity,
|
||||
onViewLinks,
|
||||
}: MessageBlockMenuActionsProps) {
|
||||
const { t } = useTranslation();
|
||||
const content = message.role === "assistant"
|
||||
@@ -274,6 +277,10 @@ export function MessageBlockMenuActions({
|
||||
) : null}
|
||||
</div>
|
||||
) : null}
|
||||
{onViewLinks ? <button type="button" data-message-links-trigger onClick={onViewLinks}
|
||||
className="flex min-h-[var(--message-block-control-size)] w-full items-center gap-2 rounded-control px-2 py-1.5 text-[13px] text-muted-foreground hover:bg-muted/70 hover:text-foreground focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring">
|
||||
<Link2 className="h-3.5 w-3.5" aria-hidden />{t("webPreview.viewLinks")}
|
||||
</button> : null}
|
||||
<FallbackResponseSources
|
||||
sources={message.role === "assistant" ? message.responseSources : undefined}
|
||||
className="min-h-[var(--message-block-control-size)] gap-1"
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
import { useContext, useLayoutEffect, useRef, useState, type RefObject } from "react";
|
||||
import { ArrowLeft, ChevronRight, Link2 } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
|
||||
import { WebPreviewContext } from "@/components/WebLink";
|
||||
import { floatingItemClassName, floatingItemFocusClassName } from "@/components/ui/floating-surface";
|
||||
import { copyTextToClipboard } from "@/lib/clipboard";
|
||||
import { parseWebLink } from "@/lib/web-preview";
|
||||
|
||||
interface MessageWebLink { href: string; title: string }
|
||||
|
||||
/** Read only links actually rendered in this message, not code samples or file references. */
|
||||
export function useMessageWebLinks(root: RefObject<HTMLElement>, open: boolean) {
|
||||
const [links, setLinks] = useState<MessageWebLink[]>([]);
|
||||
useLayoutEffect(() => {
|
||||
const element = root.current;
|
||||
if (!open || !element) return;
|
||||
const collect = () => {
|
||||
const unique = new Map<string, MessageWebLink>();
|
||||
element.querySelectorAll<HTMLAnchorElement>("[data-assistant-selectable] a[data-web-link]").forEach((anchor) => {
|
||||
const href = anchor.getAttribute("href") ?? "";
|
||||
const url = parseWebLink(href);
|
||||
if (url && !unique.has(url.href)) {
|
||||
unique.set(url.href, { href, title: anchor.textContent?.trim() || href });
|
||||
}
|
||||
});
|
||||
setLinks(Array.from(unique.values()));
|
||||
};
|
||||
collect();
|
||||
// Markdown is lazy-loaded; collect again when its real links appear/change.
|
||||
const observer = new MutationObserver(collect);
|
||||
observer.observe(element, { subtree: true, childList: true, characterData: true, attributes: true, attributeFilter: ["href"] });
|
||||
return () => observer.disconnect();
|
||||
}, [root, open]);
|
||||
return links;
|
||||
}
|
||||
|
||||
const itemClassName = `${floatingItemClassName} ${floatingItemFocusClassName} w-full min-h-11 text-start hover:bg-muted/70`;
|
||||
|
||||
/** A drill-in inside the existing message popover, not another floating layer. */
|
||||
export function MessageLinksMenu({ links, onBack, onClose }: {
|
||||
links: MessageWebLink[];
|
||||
onBack: () => void;
|
||||
onClose: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const openPreview = useContext(WebPreviewContext);
|
||||
const [selected, setSelected] = useState<string | null>(links.length === 1 ? links[0].href : null);
|
||||
const active = links.find((link) => link.href === selected);
|
||||
const backRef = useRef<HTMLButtonElement>(null);
|
||||
useLayoutEffect(() => { backRef.current?.focus({ preventScroll: true }); }, [selected]);
|
||||
return <div className="w-60 max-w-full">
|
||||
<button ref={backRef} type="button" className={itemClassName} aria-label={t("webPreview.back")}
|
||||
onClick={() => active && links.length > 1 ? setSelected(null) : onBack()}>
|
||||
<ArrowLeft aria-hidden /><span>{t("webPreview.viewLinks")}</span>
|
||||
</button>
|
||||
{active ? <MessageLinkActions key={active.href} link={active} onClose={onClose}
|
||||
onPreview={openPreview ? () => { onClose(); openPreview(active.href); } : undefined} />
|
||||
: <div className="border-t border-border/45 pt-1">
|
||||
{links.map((link) => <button type="button" key={link.href} className={itemClassName} onClick={() => setSelected(link.href)}>
|
||||
<Link2 className="text-muted-foreground" aria-hidden />
|
||||
<span className="min-w-0 flex-1 truncate">{link.title}</span><ChevronRight className="text-muted-foreground" aria-hidden />
|
||||
</button>)}
|
||||
</div>}
|
||||
</div>;
|
||||
}
|
||||
|
||||
function MessageLinkActions({ link, onClose, onPreview }: {
|
||||
link: MessageWebLink;
|
||||
onClose: () => void;
|
||||
onPreview?: () => void;
|
||||
}) {
|
||||
const { t } = useTranslation();
|
||||
const [feedback, setFeedback] = useState<"copied" | "copyFailed" | null>(null);
|
||||
const mounted = useRef(false);
|
||||
useLayoutEffect(() => {
|
||||
mounted.current = true;
|
||||
return () => { mounted.current = false; };
|
||||
}, []);
|
||||
const copy = async () => {
|
||||
const success = await copyTextToClipboard(link.href);
|
||||
if (mounted.current) setFeedback(success ? "copied" : "copyFailed");
|
||||
};
|
||||
return <div className="border-t border-border/45 pt-1">
|
||||
<p className="max-h-24 overflow-y-auto px-2.5 py-2 text-xs text-muted-foreground [overflow-wrap:anywhere]" dir="auto">{link.href}</p>
|
||||
{onPreview ? <button type="button" className={itemClassName} onClick={onPreview}>{t("webPreview.open")}</button> : null}
|
||||
<a className={itemClassName} href={link.href} target="_blank" rel="noreferrer noopener" onClick={onClose}>{t("webPreview.external")}</a>
|
||||
<button type="button" className={itemClassName} onClick={() => { void copy(); }}>{t("webPreview.copy")}</button>
|
||||
{feedback ? <p role="status" className="px-2.5 py-1.5 text-xs text-muted-foreground">{t(`webPreview.${feedback}`)}</p> : null}
|
||||
</div>;
|
||||
}
|
||||
@@ -34,20 +34,13 @@ function WebsiteLink({ href, children, className, ...props }: ComponentPropsWith
|
||||
const anchor = useRef({ getBoundingClientRect: () => new DOMRect(point.current.x, point.current.y, 0, 0) });
|
||||
const interactedOutside = useRef(false);
|
||||
const generation = useRef(0);
|
||||
const press = useRef<{ id: number; x: number; y: number; timer: number } | null>(null);
|
||||
const suppressClick = useRef(false);
|
||||
const pointerType = useRef("");
|
||||
|
||||
const cancelPress = () => {
|
||||
if (press.current) window.clearTimeout(press.current.timer);
|
||||
press.current = null;
|
||||
};
|
||||
useEffect(() => () => {
|
||||
cancelPress();
|
||||
generation.current += 1;
|
||||
}, []);
|
||||
|
||||
const changeOpen = (next: boolean) => {
|
||||
cancelPress();
|
||||
generation.current += 1;
|
||||
setOpen(next);
|
||||
};
|
||||
@@ -70,38 +63,25 @@ function WebsiteLink({ href, children, className, ...props }: ComponentPropsWith
|
||||
<Menu.Anchor virtualRef={anchor} />
|
||||
<span className="inline max-w-full"
|
||||
onContextMenu={(event) => {
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
event.stopPropagation();
|
||||
// Touch keeps the browser's native link preview/copy/share menu. Older
|
||||
// WebKit exposes contextmenu as a MouseEvent, so remember the last pointer.
|
||||
const type = (event.nativeEvent as PointerEvent).pointerType || pointerType.current;
|
||||
if (type === "touch" || (!type && window.matchMedia("(pointer: coarse)").matches)) return;
|
||||
event.preventDefault();
|
||||
openAt(event.clientX, event.clientY);
|
||||
}}
|
||||
onKeyDown={(event) => {
|
||||
suppressClick.current = false;
|
||||
if (event.key !== "ContextMenu" && !(event.shiftKey && event.key === "F10")) return;
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
const bounds = link.current!.getBoundingClientRect();
|
||||
openAt(bounds.left, bounds.bottom);
|
||||
}}
|
||||
onPointerDown={(event) => {
|
||||
suppressClick.current = false;
|
||||
cancelPress();
|
||||
if (event.pointerType !== "touch" || !event.isPrimary || event.button !== 0) return;
|
||||
const { pointerId: id, clientX: x, clientY: y } = event;
|
||||
press.current = { id, x, y, timer: window.setTimeout(() => {
|
||||
suppressClick.current = true;
|
||||
openAt(x, y);
|
||||
}, 600) };
|
||||
}}
|
||||
onPointerMove={(event) => {
|
||||
const current = press.current;
|
||||
if (current && (event.pointerId !== current.id || Math.hypot(event.clientX - current.x, event.clientY - current.y) > 10)) cancelPress();
|
||||
}}
|
||||
onPointerUp={cancelPress} onPointerCancel={cancelPress} onPointerLeave={cancelPress}
|
||||
onClickCapture={(event) => {
|
||||
if (!suppressClick.current) return;
|
||||
suppressClick.current = false;
|
||||
event.preventDefault(); event.stopPropagation();
|
||||
pointerType.current = event.pointerType;
|
||||
}}>
|
||||
<a {...props} ref={link} href={href} target="_blank" rel="noreferrer noopener"
|
||||
className={cn(className, "[-webkit-touch-callout:none]")}>{children}</a>
|
||||
<a {...props} ref={link} data-web-link href={href} target="_blank" rel="noreferrer noopener"
|
||||
className={className}>{children}</a>
|
||||
</span>
|
||||
{feedback === "copied" ? <span role="status" className="sr-only">{t("webPreview.copied")}</span> : null}
|
||||
<Menu.Portal container={portal ?? undefined}>
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
import { memo, useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState } from "react";
|
||||
import { memo, useCallback, useEffect, useId, useLayoutEffect, useMemo, useRef, useState, type RefObject } from "react";
|
||||
import { MoreHorizontal } from "lucide-react";
|
||||
import { useTranslation } from "react-i18next";
|
||||
import { MessageBlockMenuActions, MessageBubble } from "@/components/MessageBubble";
|
||||
import { MessageLinksMenu, useMessageWebLinks } from "@/components/MessageLinksMenu";
|
||||
import { FallbackResponseSources } from "@/components/ResponseSourceBadge";
|
||||
import {
|
||||
AgentActivityCluster,
|
||||
@@ -355,6 +356,7 @@ interface ThreadDisplayUnitProps {
|
||||
}
|
||||
|
||||
interface MessageBlockMenuProps {
|
||||
messageRoot: RefObject<HTMLElement>;
|
||||
message: UIMessage;
|
||||
isTurnStreaming: boolean;
|
||||
contextBlockKey: string;
|
||||
@@ -372,6 +374,7 @@ interface MessageBlockMenuProps {
|
||||
}
|
||||
|
||||
function MessageBlockMenu({
|
||||
messageRoot,
|
||||
message,
|
||||
isTurnStreaming,
|
||||
contextBlockKey,
|
||||
@@ -385,6 +388,9 @@ function MessageBlockMenu({
|
||||
const { t } = useTranslation();
|
||||
const contentRef = useRef<HTMLDivElement>(null);
|
||||
const triggerRef = useRef<HTMLButtonElement>(null);
|
||||
const links = useMessageWebLinks(messageRoot, open);
|
||||
const [linksShown, setLinksShown] = useState(false);
|
||||
useEffect(() => { if (!open) setLinksShown(false); }, [open]);
|
||||
const [triggerHeight, setTriggerHeight] = useState(28);
|
||||
useLayoutEffect(() => {
|
||||
const trigger = triggerRef.current;
|
||||
@@ -466,12 +472,15 @@ function MessageBlockMenu({
|
||||
"shadow-[0_2px_10px_rgba(0,0,0,0.08)] dark:shadow-[0_2px_10px_rgba(0,0,0,0.24)]",
|
||||
)}
|
||||
>
|
||||
<MessageBlockMenuActions
|
||||
{linksShown && links.length > 0 ? <MessageLinksMenu links={links}
|
||||
onBack={() => { setLinksShown(false); contentRef.current?.focus({ preventScroll: true }); }}
|
||||
onClose={() => onOpenChange(false)} /> : <MessageBlockMenuActions
|
||||
message={message}
|
||||
isTurnStreaming={isTurnStreaming}
|
||||
onForkFromHere={onForkFromHere}
|
||||
activity={activity}
|
||||
/>
|
||||
onViewLinks={links.length > 0 ? () => setLinksShown(true) : undefined}
|
||||
/>}
|
||||
</PopoverContent>
|
||||
</Popover>
|
||||
);
|
||||
@@ -570,6 +579,7 @@ const ThreadDisplayUnit = memo(function ThreadDisplayUnit({
|
||||
]);
|
||||
const blockMenu = unit.type === "message" && contextBlockKey !== undefined ? (
|
||||
<MessageBlockMenu
|
||||
messageRoot={elementRef}
|
||||
message={unit.message}
|
||||
isTurnStreaming={isTurnStreaming}
|
||||
contextBlockKey={contextBlockKey}
|
||||
|
||||
Reference in new issue
Block a user