Files
fusion/packages/dashboard/app/components/FileEditor.tsx
gsxdsm cd8a1e9779 feat(KB-226): add markdown preview to FileEditor
- Add markdown preview toggle to FileEditor with live preview pane
- Implement syntax-highlighted code blocks and responsive layout
- Add comprehensive tests for preview toggle and content rendering
- Update dashboard README with FileEditor usage documentation
- Add changeset for markdown preview feature
- Clean up obsolete changeset files
2026-03-30 18:41:12 -07:00

82 lines
2.4 KiB
TypeScript

import { useState, useCallback } from "react";
import ReactMarkdown from "react-markdown";
import remarkGfm from "remark-gfm";
import { FileEdit, Eye } from "lucide-react";
interface FileEditorProps {
content: string;
onChange: (content: string) => void;
readOnly?: boolean;
filePath?: string;
}
function isMarkdownFile(filePath?: string): boolean {
if (!filePath) return false;
const lowerPath = filePath.toLowerCase();
return lowerPath.endsWith(".md") || lowerPath.endsWith(".markdown") || lowerPath.endsWith(".mdx");
}
export function FileEditor({ content, onChange, readOnly, filePath }: FileEditorProps) {
const [showPreview, setShowPreview] = useState(false);
const isMarkdown = isMarkdownFile(filePath);
// For markdown files in readOnly mode, default to preview
const effectiveShowPreview = isMarkdown && (readOnly ? true : showPreview);
const handleEditClick = useCallback(() => {
setShowPreview(false);
}, []);
const handlePreviewClick = useCallback(() => {
setShowPreview(true);
}, []);
return (
<div className="file-editor-container">
{isMarkdown && (
<div className="file-editor-toolbar">
<div className="file-editor-mode-toggle">
{!readOnly && (
<button
className={`btn btn-sm ${!effectiveShowPreview ? "btn-primary" : ""}`}
onClick={handleEditClick}
disabled={!effectiveShowPreview}
aria-label="Edit mode"
>
<FileEdit size={14} />
Edit
</button>
)}
<button
className={`btn btn-sm ${effectiveShowPreview ? "btn-primary" : ""}`}
onClick={handlePreviewClick}
disabled={effectiveShowPreview}
aria-label="Preview mode"
>
<Eye size={14} />
Preview
</button>
</div>
</div>
)}
{effectiveShowPreview ? (
<div className="file-editor-preview markdown-body">
<ReactMarkdown remarkPlugins={[remarkGfm]}>
{content}
</ReactMarkdown>
</div>
) : (
<textarea
className="file-editor-textarea"
value={content}
onChange={(e) => onChange(e.target.value)}
readOnly={readOnly}
spellCheck={false}
aria-label={filePath ? `Editor for ${filePath}` : "File editor"}
/>
)}
</div>
);
}