@@ -184,7 +184,7 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
184184 ) ;
185185
186186 React . useEffect ( ( ) => {
187- if ( ! wrapper . current || ! shouldInitEditor ) return ;
187+ if ( ! wrapper . current || ! shouldInitEditor || readOnly ) return ;
188188
189189 const parentDiv = wrapper . current ;
190190 const existingPlaceholder = parentDiv . querySelector (
@@ -198,16 +198,6 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
198198 doc : code ,
199199 extensions : [ ] ,
200200 parent : parentDiv ,
201- dispatch : ( tr ) : void => {
202- view . update ( [ tr ] ) ;
203-
204- if ( tr . docChanged ) {
205- const newCode = tr . newDoc . sliceString ( 0 , tr . newDoc . length ) ;
206-
207- setInternalCode ( newCode ) ;
208- onCodeUpdate ?.( newCode ) ;
209- }
210- } ,
211201 } ) ;
212202
213203 view . contentDOM . setAttribute ( "data-gramm" , "false" ) ;
@@ -216,23 +206,18 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
216206 "aria-label" ,
217207 filePath ? `Code Editor for ${ getFileName ( filePath ) } ` : `Code Editor`
218208 ) ;
219-
220- if ( readOnly ) {
221- view . contentDOM . classList . add ( "cm-readonly" ) ;
222- } else {
223- view . contentDOM . setAttribute ( "tabIndex" , "-1" ) ;
224- }
209+ view . contentDOM . setAttribute ( "tabIndex" , "-1" ) ;
225210
226211 cmView . current = view ;
227212
228213 return ( ) : void => {
229214 cmView . current ?. destroy ( ) ;
230215 } ;
231216 // eslint-disable-next-line react-hooks/exhaustive-deps
232- } , [ shouldInitEditor ] ) ;
217+ } , [ shouldInitEditor , readOnly ] ) ;
233218
234219 React . useEffect ( ( ) => {
235- if ( cmView . current ) {
220+ if ( cmView . current && ! readOnly ) {
236221 const customCommandsKeymap : KeyBinding [ ] = [
237222 {
238223 key : "Tab" ,
@@ -294,6 +279,15 @@ export const CodeMirror = React.forwardRef<CodeMirrorRef, CodeMirrorProps>(
294279
295280 getEditorTheme ( ) ,
296281 syntaxHighlighting ( highlightTheme ) ,
282+
283+ EditorView . updateListener . of ( ( update ) => {
284+ if ( update . docChanged ) {
285+ const newCode = update . state . doc . toString ( ) ;
286+
287+ setInternalCode ( newCode ) ;
288+ onCodeUpdate ?.( newCode ) ;
289+ }
290+ } ) ,
297291 ] ;
298292
299293 if ( readOnly ) {
0 commit comments