|
| 1 | +import React, { useState, useRef } from 'react'; |
| 2 | +import { Eye, Edit2 } from 'lucide-react'; |
| 3 | +import { MarkdownRenderer } from './MarkdownRenderer'; |
| 4 | +import '../styles/markdown.css'; |
| 5 | + |
| 6 | +interface MarkdownEditorProps { |
| 7 | + value: string; |
| 8 | + onChange: (value: string) => void; |
| 9 | + placeholder?: string; |
| 10 | +} |
| 11 | + |
| 12 | +type EditorLayout = 'split' | 'edit' | 'preview'; |
| 13 | + |
| 14 | +export const MarkdownEditor: React.FC<MarkdownEditorProps> = ({ |
| 15 | + value, |
| 16 | + onChange, |
| 17 | + placeholder = 'Enter your content here...', |
| 18 | +}) => { |
| 19 | + const [layout, setLayout] = useState<EditorLayout>('split'); |
| 20 | + const textareaRef = useRef<HTMLTextAreaElement>(null); |
| 21 | + const previewRef = useRef<HTMLDivElement>(null); |
| 22 | + const isSyncingRef = useRef(false); |
| 23 | + |
| 24 | + // Handle synchronized scrolling |
| 25 | + const handleEditScroll = () => { |
| 26 | + if (!textareaRef.current || !previewRef.current || layout !== 'split') return; |
| 27 | + |
| 28 | + isSyncingRef.current = true; |
| 29 | + const scrollPercentage = |
| 30 | + textareaRef.current.scrollTop / |
| 31 | + (textareaRef.current.scrollHeight - textareaRef.current.clientHeight); |
| 32 | + |
| 33 | + previewRef.current.scrollTop = |
| 34 | + scrollPercentage * (previewRef.current.scrollHeight - previewRef.current.clientHeight); |
| 35 | + |
| 36 | + setTimeout(() => { |
| 37 | + isSyncingRef.current = false; |
| 38 | + }, 0); |
| 39 | + }; |
| 40 | + |
| 41 | + const handlePreviewScroll = () => { |
| 42 | + if (!textareaRef.current || !previewRef.current || layout !== 'split') return; |
| 43 | + |
| 44 | + if (isSyncingRef.current) return; |
| 45 | + |
| 46 | + isSyncingRef.current = true; |
| 47 | + const scrollPercentage = |
| 48 | + previewRef.current.scrollHeight > previewRef.current.clientHeight |
| 49 | + ? previewRef.current.scrollTop / |
| 50 | + (previewRef.current.scrollHeight - previewRef.current.clientHeight) |
| 51 | + : 0; |
| 52 | + |
| 53 | + textareaRef.current.scrollTop = |
| 54 | + scrollPercentage * (textareaRef.current.scrollHeight - textareaRef.current.clientHeight); |
| 55 | + |
| 56 | + setTimeout(() => { |
| 57 | + isSyncingRef.current = false; |
| 58 | + }, 0); |
| 59 | + }; |
| 60 | + |
| 61 | + const layoutButtons = [ |
| 62 | + { value: 'edit' as const, label: 'Edit', icon: Edit2 }, |
| 63 | + { value: 'split' as const, label: 'Split', icon: null }, |
| 64 | + { value: 'preview' as const, label: 'Preview', icon: Eye }, |
| 65 | + ]; |
| 66 | + |
| 67 | + return ( |
| 68 | + <div className="space-y-2"> |
| 69 | + <div className="flex items-center justify-between"> |
| 70 | + <label className="text-sm font-medium text-zinc-300">Prompt Content</label> |
| 71 | + <div className="flex items-center gap-1 p-0.5 bg-black-200 border border-black-300 rounded-lg"> |
| 72 | + {layoutButtons.map((btn) => ( |
| 73 | + <button |
| 74 | + key={btn.value} |
| 75 | + type="button" |
| 76 | + onClick={() => setLayout(btn.value)} |
| 77 | + className={`flex items-center gap-1 px-3 py-1.5 rounded transition-all text-xs font-medium ${ |
| 78 | + layout === btn.value |
| 79 | + ? 'bg-accent/20 text-accent border border-accent/50' |
| 80 | + : 'text-zinc-400 hover:text-zinc-300' |
| 81 | + }`} |
| 82 | + title={btn.label} |
| 83 | + > |
| 84 | + {btn.icon && <btn.icon className="w-3.5 h-3.5" />} |
| 85 | + {btn.label} |
| 86 | + </button> |
| 87 | + ))} |
| 88 | + </div> |
| 89 | + </div> |
| 90 | + |
| 91 | + <div className="relative bg-black-200 border border-black-300 rounded-lg overflow-hidden"> |
| 92 | + {layout === 'edit' && ( |
| 93 | + <textarea |
| 94 | + ref={textareaRef} |
| 95 | + value={value} |
| 96 | + onChange={(e) => onChange(e.target.value)} |
| 97 | + placeholder={placeholder} |
| 98 | + className="w-full h-64 bg-black-200 p-4 text-white font-mono text-sm focus:outline-none focus:ring-2 focus:ring-accent focus:ring-inset transition-all resize-none leading-relaxed" |
| 99 | + /> |
| 100 | + )} |
| 101 | + |
| 102 | + {layout === 'preview' && ( |
| 103 | + <div className="w-full min-h-64 bg-black-200/50 p-4 overflow-y-auto"> |
| 104 | + <MarkdownRenderer content={value} className="text-sm" /> |
| 105 | + </div> |
| 106 | + )} |
| 107 | + |
| 108 | + {layout === 'split' && ( |
| 109 | + <div className="flex h-96 divide-x divide-black-300"> |
| 110 | + {/* Edit pane */} |
| 111 | + <div className="flex-1 flex flex-col min-w-0"> |
| 112 | + <textarea |
| 113 | + ref={textareaRef} |
| 114 | + value={value} |
| 115 | + onChange={(e) => onChange(e.target.value)} |
| 116 | + onScroll={handleEditScroll} |
| 117 | + placeholder={placeholder} |
| 118 | + className="flex-1 bg-black-200 p-4 text-white font-mono text-sm focus:outline-none focus:ring-2 focus:ring-accent focus:ring-inset transition-all resize-none leading-relaxed overflow-y-auto" |
| 119 | + /> |
| 120 | + </div> |
| 121 | + |
| 122 | + {/* Preview pane */} |
| 123 | + <div |
| 124 | + ref={previewRef} |
| 125 | + onScroll={handlePreviewScroll} |
| 126 | + className="flex-1 overflow-y-auto bg-black-300/30 p-4" |
| 127 | + > |
| 128 | + <MarkdownRenderer content={value} className="text-sm" /> |
| 129 | + </div> |
| 130 | + </div> |
| 131 | + )} |
| 132 | + </div> |
| 133 | + |
| 134 | + <p className="text-xs text-zinc-500 px-1"> |
| 135 | + {layout === 'edit' && 'Edit mode: Markdown formatting is supported.'} |
| 136 | + {layout === 'preview' && 'Preview mode: See how your markdown will look.'} |
| 137 | + {layout === 'split' && 'Split mode: Edit on left, preview on right. Scrolling is synchronized.'} |
| 138 | + </p> |
| 139 | + </div> |
| 140 | + ); |
| 141 | +}; |
0 commit comments