|
1 | 1 | import { Node, NodeResizer } from "@xyflow/react"; |
2 | 2 | import { ImageNodeData } from "../types"; |
3 | 3 |
|
| 4 | +// Simple markdown link parser for captions |
| 5 | +function parseMarkdownLinks(text: string): React.ReactNode[] { |
| 6 | + const parts: React.ReactNode[] = []; |
| 7 | + const linkRegex = /\[([^\]]+)\]\(([^)]+)\)/g; |
| 8 | + let lastIndex = 0; |
| 9 | + let match; |
| 10 | + |
| 11 | + while ((match = linkRegex.exec(text)) !== null) { |
| 12 | + // Add text before the link |
| 13 | + if (match.index > lastIndex) { |
| 14 | + parts.push(text.substring(lastIndex, match.index)); |
| 15 | + } |
| 16 | + // Add the link |
| 17 | + parts.push( |
| 18 | + <a key={match.index} href={match[2]} target="_blank" rel="noopener noreferrer" style={{ color: "#3b82f6", textDecoration: "underline" }}> |
| 19 | + {match[1]} |
| 20 | + </a> |
| 21 | + ); |
| 22 | + lastIndex = match.index + match[0].length; |
| 23 | + } |
| 24 | + |
| 25 | + // Add remaining text |
| 26 | + if (lastIndex < text.length) { |
| 27 | + parts.push(text.substring(lastIndex)); |
| 28 | + } |
| 29 | + |
| 30 | + return parts.length > 0 ? parts : [text]; |
| 31 | +} |
| 32 | + |
4 | 33 | export const ImageNode = ({ data, selected }: Node<ImageNodeData>) => { |
5 | 34 | return ( |
6 | 35 | <> |
7 | 36 | {data.data ? ( |
8 | 37 | <> |
9 | 38 | <NodeResizer isVisible={selected} keepAspectRatio /> |
10 | | - <div style={{ display: "flex", justifyContent: "center", alignItems: "center", width: "100%", height: "100%", overflow: "hidden" }}> |
| 39 | + <div style={{ display: "flex", flexDirection: "column", justifyContent: "center", alignItems: "center", width: "100%", height: "100%", overflow: "hidden", gap: "4px" }}> |
11 | 40 | <img |
12 | 41 | src={data.data} |
13 | 42 | style={{ |
14 | 43 | maxWidth: "100%", |
15 | | - maxHeight: "100%", |
| 44 | + maxHeight: data.caption ? "calc(100% - 24px)" : "100%", |
| 45 | + objectFit: "contain", |
16 | 46 | }} |
17 | 47 | /> |
| 48 | + {data.caption && ( |
| 49 | + <div style={{ fontSize: "11px", color: "#6b7280", textAlign: "center", padding: "0 4px", lineHeight: "1.3" }}> |
| 50 | + {parseMarkdownLinks(data.caption)} |
| 51 | + </div> |
| 52 | + )} |
18 | 53 | </div> |
19 | 54 | </> |
20 | 55 | ) : ( |
|
0 commit comments