@@ -6,14 +6,15 @@ import React, {
6
6
useRef ,
7
7
useState ,
8
8
} from 'react' ;
9
- import { type EditorView } from '@codemirror/view' ;
9
+ import { type EditorView , type ViewUpdate } from '@codemirror/view' ;
10
10
11
11
import { useDarkMode } from '@leafygreen-ui/leafygreen-provider' ;
12
12
import { Body } from '@leafygreen-ui/typography' ;
13
13
14
14
import { CodeEditorCopyButton } from '../CodeEditorCopyButton' ;
15
15
import { CopyButtonVariant } from '../CodeEditorCopyButton/CodeEditorCopyButton.types' ;
16
16
17
+ import { useFormattingModuleLoaders } from './hooks/formatting/useFormattingModuleLoaders' ;
17
18
import {
18
19
getCopyButtonStyles ,
19
20
getEditorStyles ,
@@ -75,12 +76,21 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
75
76
const editorViewRef = useRef < EditorView | null > ( null ) ;
76
77
77
78
// Load core modules
78
- const moduleLoaders = useModuleLoaders ( props ) ;
79
- const { isLoading, modules } = useLazyModules ( moduleLoaders ) ;
79
+ const coreModuleLoaders = useModuleLoaders ( props ) ;
80
+ const { isLoading : isLoadingCoreModules , modules : coreModules } =
81
+ useLazyModules ( coreModuleLoaders ) ;
82
+
83
+ // Load formatting modules
84
+ const formattingModuleLoaders = useFormattingModuleLoaders ( language ) ;
85
+ const {
86
+ isLoading : isLoadingFormattingModules ,
87
+ modules : formattingModules ,
88
+ } = useLazyModules ( formattingModuleLoaders ) ;
80
89
81
90
// Get formatting functionality
82
91
const { formatCode, isFormattingAvailable } = useCodeFormatter ( {
83
92
props : { language, indentSize, indentUnit } ,
93
+ modules : formattingModules ,
84
94
} ) ;
85
95
86
96
// Get custom extensions
@@ -95,7 +105,7 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
95
105
darkMode : darkModeProp ,
96
106
baseFontSize : baseFontSizeProp ,
97
107
} ,
98
- modules,
108
+ modules : coreModules ,
99
109
} ) ;
100
110
101
111
// Get the current contents of the editor
@@ -127,12 +137,14 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
127
137
insert : formattedContent ,
128
138
} ,
129
139
} ) ;
140
+
130
141
editorViewRef . current . dispatch ( transaction ) ;
131
142
143
+ onChangeProp ?.( formattedContent ) ;
144
+
132
145
// Update controlled value if in controlled mode
133
146
if ( isControlled ) {
134
147
setControlledValue ( formattedContent ) ;
135
- onChangeProp ?.( formattedContent ) ;
136
148
}
137
149
}
138
150
@@ -180,9 +192,9 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
180
192
} , [ modules ] ) ;
181
193
182
194
useLayoutEffect ( ( ) => {
183
- const EditorView = modules ?. [ '@codemirror/view' ] ;
184
- const commands = modules ?. [ '@codemirror/commands' ] ;
185
- const Prec = modules ?. [ '@codemirror/state' ] ?. Prec ;
195
+ const EditorView = coreModules ?. [ '@codemirror/view' ] ;
196
+ const commands = coreModules ?. [ '@codemirror/commands' ] ;
197
+ const Prec = coreModules ?. [ '@codemirror/state' ] ?. Prec ;
186
198
187
199
if ( ! editorContainerRef ?. current || ! EditorView || ! Prec || ! commands ) {
188
200
return ;
@@ -198,7 +210,7 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
198
210
199
211
commands . history ( ) ,
200
212
201
- EditorView . EditorView . updateListener . of ( update => {
213
+ EditorView . EditorView . updateListener . of ( ( update : ViewUpdate ) => {
202
214
if ( isControlled && update . docChanged ) {
203
215
const editorText = getContents ( ) ;
204
216
onChangeProp ?.( editorText ) ;
@@ -216,7 +228,7 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
216
228
} ) ;
217
229
218
230
if ( forceParsingProp ) {
219
- const Language = modules ?. [ '@codemirror/language' ] ;
231
+ const Language = coreModules ?. [ '@codemirror/language' ] ;
220
232
const docLength = editorViewRef . current ?. state . doc . length ;
221
233
222
234
if ( Language && Language . forceParsing && docLength > 0 ) {
@@ -231,7 +243,7 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
231
243
} ;
232
244
} , [
233
245
value ,
234
- modules ,
246
+ coreModules ,
235
247
controlledValue ,
236
248
defaultValue ,
237
249
isControlled ,
@@ -285,11 +297,13 @@ export const CodeEditor = forwardRef<CodeEditorHandle, CodeEditorProps>(
285
297
getContentsToCopy = { getContents }
286
298
className = { getCopyButtonStyles ( copyButtonAppearance ) }
287
299
variant = { CopyButtonVariant . Button }
288
- disabled = { isLoadingProp || isLoading }
300
+ disabled = { isLoadingProp || isLoadingCoreModules }
289
301
data-lgid = { CopyButtonLgId }
290
302
/>
291
303
) }
292
- { ( isLoadingProp || isLoading ) && (
304
+ { ( isLoadingProp ||
305
+ isLoadingCoreModules ||
306
+ isLoadingFormattingModules ) && (
293
307
< div
294
308
className = { getLoaderStyles ( {
295
309
theme,
0 commit comments