({
content,
onChange,
onSave,
hasUnsavedChanges,
disabled = false,
}: MarkdownEditorProps)
| 12 | } |
| 13 | |
| 14 | export function MarkdownEditor({ |
| 15 | content, |
| 16 | onChange, |
| 17 | onSave, |
| 18 | hasUnsavedChanges, |
| 19 | disabled = false, |
| 20 | }: MarkdownEditorProps) { |
| 21 | const [isSaving, setIsSaving] = useState(false); |
| 22 | const textareaRef = useRef<HTMLTextAreaElement>(null); |
| 23 | |
| 24 | const handleKeyDown = (e: KeyboardEvent<HTMLTextAreaElement>) => { |
| 25 | if (e.key === "Tab") { |
| 26 | e.preventDefault(); |
| 27 | const target = e.currentTarget; |
| 28 | const start = target.selectionStart; |
| 29 | const end = target.selectionEnd; |
| 30 | |
| 31 | const newValue = |
| 32 | content.substring(0, start) + " " + content.substring(end); |
| 33 | onChange(newValue); |
| 34 | |
| 35 | // Restore cursor position after state update |
| 36 | setTimeout(() => { |
| 37 | target.selectionStart = target.selectionEnd = start + 2; |
| 38 | }, 0); |
| 39 | } |
| 40 | |
| 41 | // Ctrl/Cmd + S to save |
| 42 | if ((e.ctrlKey || e.metaKey) && e.key === "s") { |
| 43 | e.preventDefault(); |
| 44 | handleSave(); |
| 45 | } |
| 46 | }; |
| 47 | |
| 48 | const handleSave = async () => { |
| 49 | if (!hasUnsavedChanges || isSaving) return; |
| 50 | setIsSaving(true); |
| 51 | try { |
| 52 | await onSave(); |
| 53 | } finally { |
| 54 | setIsSaving(false); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | return ( |
| 59 | <div className="h-full flex flex-col"> |
| 60 | {/* Toolbar */} |
| 61 | <div |
| 62 | className="flex items-center justify-between px-4 py-2" |
| 63 | style={{ |
| 64 | backgroundColor: "var(--card)", |
| 65 | borderBottom: "1px solid var(--border)", |
| 66 | }} |
| 67 | > |
| 68 | <div className="flex items-center gap-2"> |
| 69 | {hasUnsavedChanges && ( |
| 70 | <span |
| 71 | className="flex items-center gap-1.5 text-sm" |
nothing calls this directly
no outgoing calls
no test coverage detected