@@ -45,6 +45,8 @@ const hideScrollbar = EditorView.theme({
4545 '.cm-content' : {
4646 padding : '0' , // Adjust padding to fit your needs
4747 overflow : 'auto' ,
48+ lineHeight : '1.75rem' ,
49+ fontSize : '1.125rem' ,
4850 } ,
4951 '.cm-scroller::-webkit-scrollbar' : {
5052 display : 'none' /* For Chrome, Safari, and Opera */ ,
@@ -54,10 +56,12 @@ const hideScrollbar = EditorView.theme({
5456 } ,
5557 '&' : {
5658 height : 'auto' , // Adjust height to auto for single-line input
59+ cursor : 'text' ,
5760 } ,
5861 '.cm-placeholder' : {
5962 color : '#aaa' , // Placeholder text color
6063 } ,
64+ '&.cm-focused' : { outline : 'none' } ,
6165} ) ;
6266
6367// Rebind the Enter key to do nothing
@@ -98,27 +102,13 @@ const highlightStyle = EditorView.baseTheme({
98102} ) ;
99103
100104export const TextEditor = ( { placeHolder, onChangeHandler, value, disableState, completionOptions, styles } ) => {
101- const editor1 = useRef ( ) ;
102- const [ view , setView ] = useState ( null ) ;
105+ const editorRef = useRef ( null ) ;
106+ const viewRef = useRef ( null ) ;
103107 const [ dynamicOptions , setDynamicOptions ] = useState ( completionOptions ) ;
104108
105- if ( view ) {
106- if ( ! isEqual ( dynamicOptions , completionOptions ) ) {
107- updateAutocompleteOptions ( view , completionOptions ) ;
108- setDynamicOptions ( completionOptions ) ;
109- }
110- if ( value != view . state . doc . toString ( ) ) {
111- view . dispatch ( { changes : { from : 0 , to : view . state . doc . length , insert : value } } ) ;
112- }
113- }
114-
115- const onUpdate = EditorView . updateListener . of ( ( v ) => {
116- if ( onChangeHandler ) {
117- onChangeHandler ( v . state . doc . toString ( ) ) ;
118- }
119- } ) ;
120-
121109 useEffect ( ( ) => {
110+ if ( ! editorRef . current ) return ;
111+
122112 const state = EditorState . create ( {
123113 doc : value ,
124114 extensions : [
@@ -127,7 +117,11 @@ export const TextEditor = ({ placeHolder, onChangeHandler, value, disableState,
127117 //myAutocomplete,
128118 //basicSetup,
129119 keymap . of ( [ defaultKeymap , indentWithTab ] ) ,
130- onUpdate ,
120+ EditorView . updateListener . of ( ( v ) => {
121+ if ( onChangeHandler && v . docChanged ) {
122+ onChangeHandler ( v . state . doc . toString ( ) ) ;
123+ }
124+ } ) ,
131125 //EditorState.readOnly.of(props.readOnly || false),
132126 history ( ) ,
133127 hideScrollbar ,
@@ -138,18 +132,36 @@ export const TextEditor = ({ placeHolder, onChangeHandler, value, disableState,
138132 ] ,
139133 } ) ;
140134
141- const view = new EditorView ( { state, parent : editor1 . current } ) ;
142- setView ( view ) ;
135+ const view = new EditorView ( { state, parent : editorRef . current } ) ;
136+ viewRef . current = view ;
143137
144138 return ( ) => {
145139 view . destroy ( ) ;
146- setView ( null ) ;
147140 } ;
148141 } , [ ] ) ;
149142
150- const mainStyles =
151- 'nodrag nowheel block rounded border border-slate-700 bg-background-light p-2.5 text-sm outline-none' ;
143+ useEffect ( ( ) => {
144+ const view = viewRef . current ;
145+ if ( ! view ) return ;
146+
147+ // Update completion options if they've changed
148+ if ( ! isEqual ( dynamicOptions , completionOptions ) ) {
149+ updateAutocompleteOptions ( view , completionOptions ) ;
150+ setDynamicOptions ( completionOptions ) ;
151+ }
152+
153+ // Update content if it's different from current editor content
154+ const currentContent = view . state . doc . toString ( ) ;
155+ if ( value !== currentContent ) {
156+ view . dispatch ( {
157+ changes : { from : 0 , to : currentContent . length , insert : value } ,
158+ } ) ;
159+ }
160+ } , [ value , completionOptions ] ) ;
161+
162+ //const mainStyles =
163+ // 'nodrag nowheel block border border-slate-700 bg-background-light p-2.5 text-base outline-none';
152164 const intentStyles = disableState ? 'cursor-not-allowed text-slate-400' : 'text-slate-900' ;
153165
154- return < div ref = { editor1 } className = { `${ mainStyles } ${ intentStyles } ${ styles } ` } > </ div > ;
166+ return < div ref = { editorRef } className = { `${ styles } ` } > </ div > ;
155167} ;
0 commit comments