11// MarkdownEditor.tsx
2- import React , { useEffect , useState } from 'react' ;
2+ import React , { useEffect , useState , useRef } from 'react' ;
33import FileUpload from './FileUpload' ;
44import CopyButton from './CopyButton' ;
55import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter' ;
@@ -11,6 +11,7 @@ import './styles.css';
1111
1212interface FileInfo {
1313 name : string ;
14+ path : string ;
1415 size : number ;
1516 extension : string ;
1617 content : string ;
@@ -29,6 +30,7 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({ ruleContent, roleConten
2930 const [ files , setFiles ] = useState < FileInfo [ ] > ( [ ] ) ;
3031 const [ previewFile , setPreviewFile ] = useState < FileInfo | null > ( null ) ;
3132 const [ isDialogOpen , setIsDialogOpen ] = useState ( false ) ;
33+ const previewContentRef = useRef < HTMLDivElement > ( null ) ;
3234
3335 useEffect ( ( ) => {
3436 const timer = setTimeout ( ( ) => {
@@ -37,6 +39,16 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({ ruleContent, roleConten
3739 return ( ) => clearTimeout ( timer ) ;
3840 } , [ markdownContent ] ) ;
3941
42+ useEffect ( ( ) => {
43+ if ( previewFile && previewContentRef . current ) {
44+ const savedScrollTop = parseInt (
45+ localStorage . getItem ( `scrollPosition:${ previewFile . name } ` ) || '0' ,
46+ 10
47+ ) ;
48+ previewContentRef . current . scrollTop = savedScrollTop ;
49+ }
50+ } , [ previewFile ] ) ;
51+
4052 const handleFilesUploaded = ( newFiles : FileInfo [ ] ) => {
4153 setFiles ( prev => [ ...prev , ...newFiles ] ) ;
4254 } ;
@@ -72,7 +84,7 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({ ruleContent, roleConten
7284 </ SyntaxHighlighter >
7385 ) ;
7486
75- const combinedContent = `# 角色\n${ roleContent } \n\n# 任务 \n${ markdownContent } \n\n# 规则 \n${ ruleContent } ` ;
87+ const combinedContent = `# 角色\n${ roleContent } \n\n# 规则 \n${ ruleContent } \n\n# 任务 \n${ markdownContent } ` ;
7688
7789 return (
7890 < div className = "editor-container" >
@@ -93,7 +105,7 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({ ruleContent, roleConten
93105 } }
94106 >
95107 < Typography variant = "subtitle1" >
96- { file . name }
108+ { file . path }
97109 </ Typography >
98110 < Typography variant = "caption" >
99111 { formatFileSize ( file . size ) } - { file . extension }
@@ -121,15 +133,25 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({ ruleContent, roleConten
121133 fullWidth
122134 >
123135 < DialogTitle >
124- { previewFile ?. name }
136+ { previewFile ?. path }
125137 < IconButton
126138 onClick = { ( ) => setIsDialogOpen ( false ) }
127139 sx = { { position : 'absolute' , right : 8 , top : 8 } }
128140 >
129141 < CloseIcon />
130142 </ IconButton >
131143 </ DialogTitle >
132- < DialogContent dividers >
144+ < DialogContent
145+ dividers
146+ ref = { previewContentRef }
147+ onScroll = { ( e ) => {
148+ if ( previewFile ) {
149+ const scrollTop = e . currentTarget . scrollTop ;
150+ localStorage . setItem ( `scrollPosition:${ previewFile . name } ` , scrollTop . toString ( ) ) ;
151+ }
152+ } }
153+ sx = { { overflow : 'auto' } }
154+ >
133155 { previewFile && (
134156 < CodePreview
135157 content = { previewFile . content }
@@ -143,12 +165,19 @@ const MarkdownEditor: React.FC<MarkdownEditorProps> = ({ ruleContent, roleConten
143165 < TextField
144166 fullWidth
145167 multiline
146- rows = { 10 }
168+ minRows = { 10 }
169+ maxRows = { 20 }
147170 value = { markdownContent }
148171 onChange = { ( e ) => setMarkdownContent ( e . target . value ) }
149172 label = "执行任务(Markdown格式)"
150173 variant = "outlined"
151174 className = "markdown-editor"
175+ sx = { {
176+ '& .MuiInputBase-root' : {
177+ overflow : 'hidden' ,
178+ transition : 'height 0.2s ease-out'
179+ }
180+ } }
152181 />
153182 </ div >
154183
0 commit comments