fix: constrain claw table images
This commit is contained in:
parent
8c0d4a0930
commit
b21b4cdea0
@ -6,7 +6,7 @@ import type {
|
|||||||
PointerEvent as ReactPointerEvent,
|
PointerEvent as ReactPointerEvent,
|
||||||
ReactNode,
|
ReactNode,
|
||||||
} from "react";
|
} from "react";
|
||||||
import { useRef, useState } from "react";
|
import { createContext, useRef, useState } from "react";
|
||||||
import { Streamdown } from "streamdown";
|
import { Streamdown } from "streamdown";
|
||||||
import { ImagePreview } from "@/components/image-preview";
|
import { ImagePreview } from "@/components/image-preview";
|
||||||
import {
|
import {
|
||||||
@ -15,6 +15,7 @@ import {
|
|||||||
splitOpenClawChartBlocks,
|
splitOpenClawChartBlocks,
|
||||||
} from "@/components/elements/openclaw-chart-card";
|
} from "@/components/elements/openclaw-chart-card";
|
||||||
import { ResponsePdfDownloadButton } from "@/components/elements/response-pdf-download";
|
import { ResponsePdfDownloadButton } from "@/components/elements/response-pdf-download";
|
||||||
|
import { buildImageProxyUrl } from "@/lib/image-delivery";
|
||||||
import {
|
import {
|
||||||
Collapsible,
|
Collapsible,
|
||||||
CollapsibleContent,
|
CollapsibleContent,
|
||||||
@ -78,8 +79,11 @@ type MarkdownTableHeaderCellProps = ComponentProps<"th"> & {
|
|||||||
node?: unknown;
|
node?: unknown;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const MarkdownTableContext = createContext(false);
|
||||||
const TABLE_DRAG_INTERACTIVE_SELECTOR =
|
const TABLE_DRAG_INTERACTIVE_SELECTOR =
|
||||||
"a, button, input, textarea, select, summary, [role='button'], [contenteditable='true'], [data-no-drag='true']";
|
"a, button, input, textarea, select, summary, [role='button'], [contenteditable='true'], [data-no-drag='true']";
|
||||||
|
const IMAGE_URL_REGEX = /^https?:\/\/\S+\.(?:png|jpe?g|webp|gif|avif)(?:[?#]\S*)?$/i;
|
||||||
|
const MARKDOWN_IMAGE_REGEX = /!\[([^\]]*)\]\((https?:\/\/[^\s)]+)\)/i;
|
||||||
|
|
||||||
function isTableDragInteractiveTarget(target: EventTarget | null) {
|
function isTableDragInteractiveTarget(target: EventTarget | null) {
|
||||||
return (
|
return (
|
||||||
@ -393,6 +397,124 @@ function splitMarkdownImages(content: string) {
|
|||||||
return segments;
|
return segments;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function isLikelyImageUrl(value?: string | null) {
|
||||||
|
return IMAGE_URL_REGEX.test(String(value || "").trim());
|
||||||
|
}
|
||||||
|
|
||||||
|
function getImageUrlFromTableText(value: ReactNode) {
|
||||||
|
if (typeof value !== "string") return null;
|
||||||
|
|
||||||
|
const trimmed = value.trim();
|
||||||
|
if (!trimmed) return null;
|
||||||
|
|
||||||
|
const markdownImageMatch = trimmed.match(MARKDOWN_IMAGE_REGEX);
|
||||||
|
if (markdownImageMatch?.[2] && isLikelyImageUrl(markdownImageMatch[2])) {
|
||||||
|
return markdownImageMatch[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
return isLikelyImageUrl(trimmed) ? trimmed : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getOriginalImageUrlFromMarkdownImage(src: string, alt?: string) {
|
||||||
|
const normalizedAlt = String(alt || "");
|
||||||
|
const parseByMarker = (marker: string) => {
|
||||||
|
if (!normalizedAlt.includes(marker)) return "";
|
||||||
|
const content = normalizedAlt.slice(normalizedAlt.indexOf(marker) + marker.length);
|
||||||
|
return content.split(marker === "thumb|||" ? "|||" : marker === "thumb$$" ? "$$" : "|")[0] || "";
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
parseByMarker("thumb|||") ||
|
||||||
|
parseByMarker("thumb$$") ||
|
||||||
|
parseByMarker("thumb|") ||
|
||||||
|
src
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
function MarkdownTableImagePreview({
|
||||||
|
alt,
|
||||||
|
src,
|
||||||
|
}: {
|
||||||
|
alt?: string;
|
||||||
|
src: string;
|
||||||
|
}) {
|
||||||
|
const [isOpen, setIsOpen] = useState(false);
|
||||||
|
const originalSrc = getOriginalImageUrlFromMarkdownImage(src, alt);
|
||||||
|
const thumbnailSrc = buildImageProxyUrl(src, {
|
||||||
|
delivery: "proxy",
|
||||||
|
format: "webp",
|
||||||
|
quality: 82,
|
||||||
|
width: 180,
|
||||||
|
});
|
||||||
|
const largeSrc = buildImageProxyUrl(originalSrc, {
|
||||||
|
delivery: "proxy",
|
||||||
|
format: "webp",
|
||||||
|
quality: 90,
|
||||||
|
width: 1600,
|
||||||
|
});
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="group/table-image relative block h-24 w-24 overflow-hidden rounded-lg border border-border/70 bg-muted/40"
|
||||||
|
data-no-drag="true"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.preventDefault();
|
||||||
|
event.stopPropagation();
|
||||||
|
setIsOpen(true);
|
||||||
|
}}
|
||||||
|
title="点击查看大图"
|
||||||
|
>
|
||||||
|
<img
|
||||||
|
src={thumbnailSrc}
|
||||||
|
alt={alt || "表格图片"}
|
||||||
|
className="h-full w-full object-contain p-1 transition-transform duration-200 group-hover/table-image:scale-[1.03]"
|
||||||
|
loading="lazy"
|
||||||
|
decoding="async"
|
||||||
|
onError={(event) => {
|
||||||
|
if (event.currentTarget.src !== src) {
|
||||||
|
event.currentTarget.src = src;
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{isOpen ? (
|
||||||
|
<div
|
||||||
|
className="fixed inset-0 z-[260] flex items-center justify-center bg-black/82 p-4"
|
||||||
|
data-no-drag="true"
|
||||||
|
onClick={() => setIsOpen(false)}
|
||||||
|
>
|
||||||
|
<button
|
||||||
|
type="button"
|
||||||
|
className="absolute right-4 top-4 rounded-full bg-white/12 px-3 py-2 text-sm font-medium text-white transition-colors hover:bg-white/20"
|
||||||
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
setIsOpen(false);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
关闭
|
||||||
|
</button>
|
||||||
|
<img
|
||||||
|
src={largeSrc}
|
||||||
|
alt={alt || "表格图片大图"}
|
||||||
|
className="max-h-[90vh] max-w-[92vw] rounded-xl object-contain shadow-2xl"
|
||||||
|
loading="eager"
|
||||||
|
decoding="async"
|
||||||
|
onClick={(event) => event.stopPropagation()}
|
||||||
|
onError={(event) => {
|
||||||
|
if (event.currentTarget.src !== originalSrc) {
|
||||||
|
event.currentTarget.src = originalSrc;
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
) : null}
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
function normalizeSectionTitle(line: string) {
|
function normalizeSectionTitle(line: string) {
|
||||||
let normalized = line.trim();
|
let normalized = line.trim();
|
||||||
if (!normalized) return "";
|
if (!normalized) return "";
|
||||||
@ -568,29 +690,34 @@ function DownloadableMarkdownTable({
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
<HorizontalDragScrollArea>
|
<HorizontalDragScrollArea>
|
||||||
<table
|
<MarkdownTableContext.Provider value>
|
||||||
ref={tableRef}
|
<table
|
||||||
className={cn(
|
ref={tableRef}
|
||||||
"min-w-full table-auto border-collapse text-sm",
|
className={cn(
|
||||||
"[&_td]:min-w-[10rem] [&_td]:max-w-[24rem] [&_td]:align-top [&_td]:whitespace-normal [&_td]:break-words [&_td]:[overflow-wrap:anywhere] [&_td]:[word-break:break-word]",
|
"min-w-full table-auto border-collapse text-sm",
|
||||||
"[&_td:first-child]:min-w-[12rem] [&_td:first-child]:max-w-[18rem] [&_th:first-child]:min-w-[12rem] [&_th:first-child]:max-w-[18rem]",
|
"[&_td]:min-w-[10rem] [&_td]:max-w-[24rem] [&_td]:align-top [&_td]:whitespace-normal [&_td]:break-words [&_td]:[overflow-wrap:anywhere] [&_td]:[word-break:break-word]",
|
||||||
"[&_th]:min-w-[10rem] [&_th]:max-w-[22rem] [&_th]:whitespace-normal [&_th]:break-words [&_th]:[overflow-wrap:anywhere] [&_th]:[word-break:break-word]",
|
"[&_td:first-child]:min-w-[12rem] [&_td:first-child]:max-w-[18rem] [&_th:first-child]:min-w-[12rem] [&_th:first-child]:max-w-[18rem]",
|
||||||
className
|
"[&_th]:min-w-[10rem] [&_th]:max-w-[22rem] [&_th]:whitespace-normal [&_th]:break-words [&_th]:[overflow-wrap:anywhere] [&_th]:[word-break:break-word]",
|
||||||
)}
|
className
|
||||||
{...props}
|
)}
|
||||||
>
|
{...props}
|
||||||
{children}
|
>
|
||||||
</table>
|
{children}
|
||||||
|
</table>
|
||||||
|
</MarkdownTableContext.Provider>
|
||||||
</HorizontalDragScrollArea>
|
</HorizontalDragScrollArea>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function MarkdownTableCell({
|
function MarkdownTableCell({
|
||||||
|
children,
|
||||||
className,
|
className,
|
||||||
node: _node,
|
node: _node,
|
||||||
...props
|
...props
|
||||||
}: MarkdownTableCellProps) {
|
}: MarkdownTableCellProps) {
|
||||||
|
const imageUrl = getImageUrlFromTableText(children);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<td
|
<td
|
||||||
className={cn(
|
className={cn(
|
||||||
@ -598,7 +725,9 @@ function MarkdownTableCell({
|
|||||||
className
|
className
|
||||||
)}
|
)}
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
>
|
||||||
|
{imageUrl ? <MarkdownTableImagePreview src={imageUrl} /> : children}
|
||||||
|
</td>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -701,23 +830,31 @@ export const Response = ({
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ImagePreview
|
<MarkdownTableContext.Consumer>
|
||||||
key={`${src}-${processingKey}`}
|
{(isInsideTable) =>
|
||||||
src={src}
|
isInsideTable ? (
|
||||||
alt={alt}
|
<MarkdownTableImagePreview alt={alt} src={src} />
|
||||||
onImageEdit={onImageEdit}
|
) : (
|
||||||
onDrawingComplete={onDrawingComplete}
|
<ImagePreview
|
||||||
onImageEnhance={onImageEnhance}
|
key={`${src}-${processingKey}`}
|
||||||
onImageRmbg={onImageRmbg}
|
src={src}
|
||||||
onImageExtend={onImageExtend}
|
alt={alt}
|
||||||
onImageAngleEdit={onImageAngleEdit}
|
onImageEdit={onImageEdit}
|
||||||
onImageWatermarkRemove={onImageWatermarkRemove}
|
onDrawingComplete={onDrawingComplete}
|
||||||
isProcessingEnhance={isProcessingEnhance}
|
onImageEnhance={onImageEnhance}
|
||||||
isProcessingRmbg={isProcessingRmbg}
|
onImageRmbg={onImageRmbg}
|
||||||
isProcessingExtend={isProcessingExtend}
|
onImageExtend={onImageExtend}
|
||||||
isProcessingAngleEdit={isProcessingAngleEdit}
|
onImageAngleEdit={onImageAngleEdit}
|
||||||
isProcessingWatermarkRemove={isProcessingWatermarkRemove}
|
onImageWatermarkRemove={onImageWatermarkRemove}
|
||||||
/>
|
isProcessingEnhance={isProcessingEnhance}
|
||||||
|
isProcessingRmbg={isProcessingRmbg}
|
||||||
|
isProcessingExtend={isProcessingExtend}
|
||||||
|
isProcessingAngleEdit={isProcessingAngleEdit}
|
||||||
|
isProcessingWatermarkRemove={isProcessingWatermarkRemove}
|
||||||
|
/>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
</MarkdownTableContext.Consumer>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
video: ({ src, alt }: { src?: string; alt?: string }) => {
|
video: ({ src, alt }: { src?: string; alt?: string }) => {
|
||||||
@ -736,22 +873,33 @@ export const Response = ({
|
|||||||
onClick,
|
onClick,
|
||||||
...linkProps
|
...linkProps
|
||||||
}: ComponentProps<"a">) => (
|
}: ComponentProps<"a">) => (
|
||||||
<a
|
<MarkdownTableContext.Consumer>
|
||||||
{...linkProps}
|
{(isInsideTable) =>
|
||||||
className={cn(
|
isInsideTable && isLikelyImageUrl(href) ? (
|
||||||
"font-medium text-primary underline underline-offset-2",
|
<MarkdownTableImagePreview
|
||||||
className
|
alt={typeof children === "string" ? children : undefined}
|
||||||
)}
|
src={String(href || "")}
|
||||||
href={href}
|
/>
|
||||||
onClick={(event) => {
|
) : (
|
||||||
event.stopPropagation();
|
<a
|
||||||
onClick?.(event);
|
{...linkProps}
|
||||||
}}
|
className={cn(
|
||||||
rel="noreferrer"
|
"font-medium text-primary underline underline-offset-2",
|
||||||
target="_blank"
|
className
|
||||||
>
|
)}
|
||||||
{children}
|
href={href}
|
||||||
</a>
|
onClick={(event) => {
|
||||||
|
event.stopPropagation();
|
||||||
|
onClick?.(event);
|
||||||
|
}}
|
||||||
|
rel="noreferrer"
|
||||||
|
target="_blank"
|
||||||
|
>
|
||||||
|
{children}
|
||||||
|
</a>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
</MarkdownTableContext.Consumer>
|
||||||
),
|
),
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -778,7 +926,11 @@ export const Response = ({
|
|||||||
const renderTextSegment = (text: string, keyPrefix: string): ReactNode => {
|
const renderTextSegment = (text: string, keyPrefix: string): ReactNode => {
|
||||||
if (!text.trim()) return null;
|
if (!text.trim()) return null;
|
||||||
|
|
||||||
if (text.includes(") {
|
if (
|
||||||
|
!MARKDOWN_TABLE_REGEX.test(text) &&
|
||||||
|
text.includes("
|
||||||
|
) {
|
||||||
const segments = splitMarkdownImages(text);
|
const segments = splitMarkdownImages(text);
|
||||||
const hasImageSegments = segments.some((segment) => segment.type === "image");
|
const hasImageSegments = segments.some((segment) => segment.type === "image");
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user