diff --git a/src/components/Editor.jsx b/src/components/Editor.jsx index 2a0913b..770eab6 100644 --- a/src/components/Editor.jsx +++ b/src/components/Editor.jsx @@ -6,6 +6,204 @@ import katex from 'katex'; import 'katex/dist/katex.css'; import { MCPClient } from '../mcp/client'; +/** + * Custom Code component that handles: + * - Language "katex" or "math": live KaTeX math display + * - Language "svg": live SVG rendering + * - Standard code blocks with syntax highlighting + * + * + * + * @param {Object} props + * @param {Object} props.node + * @param {string} props.className + * @param {React.ReactNode} props.children + * @param {Object} props.props + */ +const CodeRenderer = ({ node, className, children, ...props }) => { + const codeString = String(children || '').replace(/\n$/, ''); + const match = /language-(\w+)/.exec(className || ''); + const language = match ? match[1].toLowerCase() : ''; + + // react-markdown v9+ removed the `inline` prop. Language-tagged fenced + // blocks always carry a `language-*` className, which is sufficient to + // distinguish the special KaTeX / SVG blocks from inline code. + if (language === 'katex' || language === 'math') { + try { + const html = katex.renderToString(codeString, { + displayMode: true, + throwOnError: false, + strict: false + }); + return ( +
+ ); + } catch (err) { + return ( +
+ Failed to render KaTeX: {err.message} +
+
+ SVG Preview +
+
+ {children} + + ); +}; + +/** + * Custom Image component that safely renders SVGs and normal images + * + * + * + * @param {Object} props + * @param {Object} props.node + * @param {string} props.src + * @param {string} props.alt + * @param {Object} props.props + */ +const ImageRenderer = ({ node, src, alt, ...props }) => { + const isSvg = src && (src.endsWith('.svg') || src.includes('.svg?') || src.startsWith('data:image/svg+xml')); + + return ( + + {alt { + e.target.style.opacity = '0.5'; + e.target.title = `Failed to load image: ${src}`; + }} + {...props} + /> + {alt && {alt}} + ({ + remarkPlugins: [remarkMath], + rehypePlugins: [[rehypeKatex, { throwOnError: false, strict: false }]], + components: { + code: CodeRenderer, + img: ImageRenderer, + } + }), []); + + // Handle drag and drop over the editor + const handleDragOver = (e) => { + e.preventDefault(); + e.stopPropagation(); + }; + + const handleDrop = (e) => { + e.preventDefault(); + e.stopPropagation(); + + const file = e.dataTransfer?.files?.[0]; + if (file && onFileDrop) { + onFileDrop(file); + } + }; + + const [mcpConfig, setMcpConfig] = useState({ address: '', port: '' }); + const [mcpConnected, setMcpConnected] = useState(false); + const [mcpStatus, setMcpStatus] = useState(''); + const mcpClient = useMemo(() => (typeof __IWA__ !== 'undefined' && __IWA__ ? new MCPClient() : null), []); + + useEffect(() => { + if (mcpClient && __IWA__ === 'true') { + // Optionally auto-connect or wait for user action + } + }, [mcpClient]); + + const connectMCP = async () => { + if (mcpClient && mcpConfig.address && mcpConfig.port) { + const success = await mcpClient.connect(mcpConfig.address, parseInt(mcpConfig.port)); + if (success) { + setMcpConnected(true); + setMcpStatus('Connected'); + } else { + setMcpStatus('Failed to connect'); + } + } + }; + + return ( +
+ + {__IWA__ === 'true' && ( +
+

MCP Connection (IWA Only)

+ setMcpConfig({ ...mcpConfig, address: e.target.value })} + style={{ marginRight: '10px' }} + /> + setMcpConfig({ ...mcpConfig, port: e.target.value })} + style={{ marginRight: '10px' }} + /> + +

Status: {mcpConnected ? 'Connected' : mcpStatus || 'Disconnected'}

+
+ )} +