@@ -57,23 +57,35 @@ const Component: FC<EditorProps> = ({
5757 const containerRef = useRef < HTMLDivElement > ( null ) ;
5858 const editorInstanceRef = useRef < StacksEditor | null > ( null ) ;
5959 const isInitializedRef = useRef ( false ) ;
60+ const onChangeRef = useRef ( onChange ) ;
61+ const onFocusRef = useRef ( onFocus ) ;
62+ const onBlurRef = useRef ( onBlur ) ;
63+ const autoFocusTimeoutRef = useRef < ReturnType < typeof setTimeout > | null > (
64+ null
65+ ) ;
6066
6167 // Version compatibility temporarily disabled
6268
69+ useEffect ( ( ) => {
70+ onChangeRef . current = onChange ;
71+ onFocusRef . current = onFocus ;
72+ onBlurRef . current = onBlur ;
73+ } ) ;
74+
6375 const syncTheme = useCallback ( ( ) => {
6476 if ( ! containerRef . current ) return ;
6577
66- containerRef . current . parentElement ?. classList . remove (
78+ containerRef . current ?. classList . remove (
6779 "theme-light" ,
6880 "theme-dark" ,
69- "theme-system" ,
81+ "theme-system"
7082 ) ;
7183 const themeAttr =
7284 document . documentElement . getAttribute ( "data-bs-theme" ) ||
7385 document . body . getAttribute ( "data-bs-theme" ) ;
7486
7587 if ( themeAttr ) {
76- containerRef . current . parentElement ?. classList . add ( `theme-system ` ) ;
88+ containerRef . current ?. classList . add ( `theme-${ themeAttr } ` ) ;
7789 }
7890 } , [ ] ) ;
7991
@@ -101,15 +113,10 @@ const Component: FC<EditorProps> = ({
101113 return ;
102114 }
103115
104- const container = document . createElement ( "div" ) ;
105- container . className = "stacks-editor-container" ;
106- container . style . minHeight = "320px" ;
107- containerRef . current . appendChild ( container ) ;
108-
109116 let editorInstance : StacksEditor | null = null ;
110117
111118 try {
112- editorInstance = new StacksEditor ( container , value || "" , {
119+ editorInstance = new StacksEditor ( containerRef . current , value || "" , {
113120 placeholderText : placeholder || t ( "placeholder" , "" ) ,
114121 parserFeatures : {
115122 tables : true ,
@@ -139,30 +146,41 @@ const Component: FC<EditorProps> = ({
139146 editor . editorView . updateState ( newState ) ;
140147 }
141148
142- if ( tr . docChanged && onChange ) {
149+ if ( tr . docChanged && onChangeRef . current ) {
143150 const newContent = editor . content ;
144- onChange ( newContent ) ;
151+ onChangeRef . current ( newContent ) ;
145152 }
146153 } ,
147154 } ) ;
148155
149156 const editorElement = editor . dom as HTMLElement ;
157+ const handleFocus = ( ) => onFocusRef . current ?.( ) ;
158+ const handleBlur = ( ) => onBlurRef . current ?.( ) ;
159+
150160 if ( editorElement ) {
151- const handleFocus = ( ) => onFocus ?.( ) ;
152- const handleBlur = ( ) => onBlur ?.( ) ;
153161 editorElement . addEventListener ( "focus" , handleFocus , true ) ;
154162 editorElement . addEventListener ( "blur" , handleBlur , true ) ;
155163 }
156164
157165 if ( autoFocus ) {
158- setTimeout ( ( ) => {
166+ autoFocusTimeoutRef . current = setTimeout ( ( ) => {
159167 if ( editor ) {
160168 editor . focus ( ) ;
161169 }
162170 } , 100 ) ;
163171 }
164172
165173 return ( ) => {
174+ if ( autoFocusTimeoutRef . current ) {
175+ clearTimeout ( autoFocusTimeoutRef . current ) ;
176+ autoFocusTimeoutRef . current = null ;
177+ }
178+
179+ if ( editorElement ) {
180+ editorElement . removeEventListener ( "focus" , handleFocus , true ) ;
181+ editorElement . removeEventListener ( "blur" , handleBlur , true ) ;
182+ }
183+
166184 if ( editorInstance ) {
167185 try {
168186 editorInstance . destroy ( ) ;
@@ -174,9 +192,7 @@ const Component: FC<EditorProps> = ({
174192 editorInstanceRef . current = null ;
175193 isInitializedRef . current = false ;
176194
177- try {
178- container . remove ( ) ;
179- } catch { }
195+ containerRef . current ! . innerHTML = "" ;
180196 } ;
181197 } catch ( error ) {
182198 console . error ( "Failed to initialize Stacks Editor:" , error ) ;
@@ -200,9 +216,10 @@ const Component: FC<EditorProps> = ({
200216 } , [ value ] ) ;
201217
202218 return (
203- < div className = "editor-stacks-wrapper editor-stacks-scope" >
204- < div ref = { containerRef } style = { { minHeight : 320 } } />
205- </ div >
219+ < div
220+ className = "editor-stacks-wrapper editor-stacks-scope"
221+ ref = { containerRef }
222+ />
206223 ) ;
207224} ;
208225
0 commit comments