Probably also breaks stuff but yknow
This commit is contained in:
unfunny
2026-10-22 08:12:24 -04:00

View File

@@ -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
* </summary>
* </description>
*
* @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 (
<div
className="katex-display-block"
dangerouslySetInnerHTML={{ __html: html }}
/>
);
} catch (err) {
return (
<div className="katex-error">
<code>Failed to render KaTeX: {err.message}</code>
</div
);
}
}
// Render SVG live diagram
if (language === 'svg') {
return (
<div className="svg-rendered-container">
<div className="svg-rendered-header">
<span>SVG Preview</span>
</div
<div
className="svg-rendered-content"
dangerouslySetInnerHTML={{ __html: codeString }}
/>
</div
);
}
return (
<code className={className} {...props}>
{children}
</code>
);
};
/**
* Custom Image component that safely renders SVGs and normal images
* </summary>
* </description>
*
* @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 (
<span className={`markdown-img-wrapper ${isSvg ? 'is-svg' : ''}`}>
<img
src={src}
alt={alt || ''}
loading="lazy"
className="markdown-img"
onError={(e) => {
e.target.style.opacity = '0.5';
e.target.title = `Failed to load image: ${src}`;
}}
{...props}
/>
{alt && <span className="img-caption">{alt}</span>}
</span
);
};
export default function Editor({
value,
onChange,
colorMode = 'dark',
theme = 'solarized-dark',
height = '100%',
onFileDrop
}) {
const previewOptions = useMemo(() => ({
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 (
<div
className="editor-wrapper"
data-color-mode={colorMode}
data-theme={theme}
onDragOver={handleDragOver}
onDrop={handleDrop}
>
<MDEditor
value={value}
onChange={onChange}
height={height}
preview="live"
previewOptions={previewOptions}
textareaProps={{
placeholder: 'Type your GitHub Flavored Markdown here...\n\n- Math: $E = mc^2$ or $$\\int_0^1 x^2 dx$$\n- SVG: write ```svg ... ``` or standard image tags\n- Tables, tasks, bold, italics, code blocks supported.',
spellCheck: true
}}
/>
{__IWA__ === 'true' && (
<div className="mcp-config-panel" style={{ padding: '10px', borderTop: '1px solid #444', marginTop: '10px' }}>
<h3>MCP Connection (IWA Only)</h3>
<input
type="text"
placeholder="Remote Address"
value={mcpConfig.address}
onChange={(e) => setMcpConfig({ ...mcpConfig, address: e.target.value })}
style={{ marginRight: '10px' }}
/>
<input
type="number"
placeholder="Port"
value={mcpConfig.port}
onChange={(e) => setMcpConfig({ ...mcpConfig, port: e.target.value })}
style={{ marginRight: '10px' }}
/>
<button onClick={connectMCP}>Connect</button>
<p>Status: {mcpConnected ? 'Connected' : mcpStatus || 'Disconnected'}</p>
</div>
)}
</div
);
}
/**
* Custom Code component that handles:
* - Language "katex" or "math": live KaTeX math display