1- import React , { useEffect , useRef } from 'react' ;
1+ import React , { useLayoutEffect , useRef } from 'react' ;
22import { AnycodeEditor } from 'anycode-base' ;
33
44interface AnycodeEditorProps {
@@ -10,35 +10,38 @@ export default function AnycodeEditorReact({ id, editorState, }: AnycodeEditorP
1010
1111 const containerRef = useRef < HTMLDivElement > ( null ) ;
1212
13- useEffect ( ( ) => {
13+ useLayoutEffect ( ( ) => {
1414 if ( ! editorState || ! containerRef . current ) return ;
1515
16- containerRef . current . innerHTML = '' ;
16+ const host = containerRef . current ;
17+ const editorContainer = editorState . getContainer ( ) ;
18+ host . replaceChildren ( editorContainer ) ;
19+
1720 if ( editorState . hasScroll ( ) ) {
18- containerRef . current . appendChild ( editorState . getContainer ( ) ) ;
1921 let focus = editorState . requestedFocus ( ) ;
2022
21- if ( focus ) {
23+ if ( focus ) {
2224 let { line, column } = editorState . getCursor ( ) ;
2325 if ( line !== undefined && column !== undefined ) {
2426 editorState . requestFocus ( line , column ) ;
2527 editorState . renderCursorOrSelection ( ) ;
2628 }
2729 } else {
28- editorState . restoreScroll ( ) ;
29- editorState . renderCursorOrSelection ( ) ;
30+ editorState . onAttach ( ) ;
3031 }
3132 } else {
3233 editorState . render ( ) ;
33- containerRef . current . appendChild ( editorState . getContainer ( ) ) ;
34-
3534 let { line, column } = editorState . getCursor ( ) ;
3635 if ( line !== undefined && column !== undefined ) {
3736 editorState . requestFocus ( line , column ) ;
3837 editorState . renderCursorOrSelection ( ) ;
3938 }
4039 }
41-
40+
41+ return ( ) => {
42+ // The editor node is moved between hosts on the next mount.
43+ // Avoid clearing here to prevent a brief blank frame during switches.
44+ } ;
4245 } , [ id , editorState ] ) ;
4346
4447 return < div ref = { containerRef } style = { { width : '100%' , height : '100%' } } /> ;
0 commit comments