import React, { useCallback, useRef } from 'react' import { useTabStore } from '../../stores/tabStore' interface SourceEditorProps { darkMode: boolean } /** * 源码编辑模式 — 使用原生 textarea 编辑原始 Markdown 文本。 * 内容实时同步到 tabStore,与 WYSIWYG 编辑器共享同一数据源。 */ export const SourceEditor = React.memo(function SourceEditor({ darkMode }: SourceEditorProps) { const activeTab = useTabStore(s => s.getActiveTab()) const activeTabId = useTabStore(s => s.activeTabId) const updateTabContent = useTabStore(s => s.updateTabContent) const setModified = useTabStore(s => s.setModified) const textareaRef = useRef(null) const handleChange = useCallback((e: React.ChangeEvent) => { if (!activeTabId) return updateTabContent(activeTabId, e.target.value) setModified(activeTabId, true) }, [activeTabId, updateTabContent, setModified]) const handleKeyDown = useCallback((e: React.KeyboardEvent) => { // Tab 插入两个空格而非跳转焦点 if (e.key === 'Tab') { e.preventDefault() const textarea = textareaRef.current if (!textarea) return const start = textarea.selectionStart const end = textarea.selectionEnd const value = textarea.value const newValue = value.substring(0, start) + ' ' + value.substring(end) textarea.value = newValue textarea.selectionStart = textarea.selectionEnd = start + 2 if (activeTabId) { updateTabContent(activeTabId, newValue) setModified(activeTabId, true) } } }, [activeTabId, updateTabContent, setModified]) return (