141 lines
3.6 KiB
JavaScript
141 lines
3.6 KiB
JavaScript
import React, { useMemo, useState, useEffect } from 'react';
|
|
import MDEditor from '@uiw/react-md-editor';
|
|
import remarkMath from 'remark-math';
|
|
import rehypeKatex from 'rehype-katex';
|
|
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
|
|
*/
|
|
const CodeRenderer = ({ node, className, children, ...props }) => {
|
|
const codeString = String(children || '').replace(/\n$/, '');
|
|
const match = /language-(\w+)/.exec(className || '');
|
|
const language = match ? match[1].toLowerCase() : '';
|
|
|
|
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>
|
|
);
|
|
}
|
|
}
|
|
|
|
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
|
|
*/
|
|
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,
|
|
}
|
|
}), []);
|
|
|
|
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);
|
|
}
|
|
};
|
|
|
|
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
|
|
}}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|