@@ -11,24 +11,36 @@ import { history, defaultKeymap, historyKeymap, indentWithTab } from '@codemirro
1111import { highlightSelectionMatches , searchKeymap } from '@codemirror/search'
1212import { closeBrackets , autocompletion , closeBracketsKeymap , completionKeymap } from '@codemirror/autocomplete'
1313import { lintKeymap } from '@codemirror/lint'
14- import { Compartment , EditorState } from '@codemirror/state'
14+ import { Compartment , EditorState , Extension } from '@codemirror/state'
1515import { useEditorTheme } from '@ui-schema/material-code/useEditorTheme'
1616import { useHighlightStyle } from '@ui-schema/material-code/useHighlightStyle'
1717import { CodeMirrorComponentProps , CodeMirror , CodeMirrorProps } from '@ui-schema/kit-codemirror/CodeMirror'
18+ import { useExtension } from '@ui-schema/kit-codemirror/useExtension'
19+ import { MuiCodeMirrorStyleProps } from '@ui-schema/material-code'
1820
19- export const CustomCodeMirror : React . FC < CodeMirrorComponentProps > = (
21+ export const CustomCodeMirror : React . FC < CodeMirrorComponentProps & MuiCodeMirrorStyleProps > = (
2022 {
2123 // values we want to override in this component
22- value, extensions,
24+ value, extensions, effects,
25+ dense, variant,
2326 // everything else is just passed down
2427 ...props
2528 } ,
2629) => {
2730 const { onChange} = props
28- const theme = useEditorTheme ( typeof onChange === 'undefined' )
31+ const theme = useEditorTheme ( typeof onChange === 'undefined' , dense , variant )
2932 const highlightStyle = useHighlightStyle ( )
33+ const { init : initHighlightExt , effects : effectsHighlightExt } = useExtension (
34+ ( ) => syntaxHighlighting ( highlightStyle || defaultHighlightStyle , { fallback : true } ) ,
35+ [ highlightStyle ] ,
36+ )
37+ const { init : initThemeExt , effects : effectsThemeExt } = useExtension (
38+ ( ) => theme ,
39+ [ theme ] ,
40+ )
41+ const effectsRef = React . useRef < ( ( editor : EditorView ) => void ) [ ] > ( effects || [ ] )
3042
31- const extensionsAll = React . useMemo ( ( ) => [
43+ const extensionsAll : Extension [ ] = React . useMemo ( ( ) => [
3244 lineNumbers ( ) ,
3345 EditorView . lineWrapping ,
3446 highlightActiveLineGutter ( ) ,
@@ -38,16 +50,15 @@ export const CustomCodeMirror: React.FC<CodeMirrorComponentProps> = (
3850 drawSelection ( ) ,
3951 dropCursor ( ) ,
4052 EditorState . allowMultipleSelections . of ( true ) ,
53+ new Compartment ( ) . of ( EditorState . tabSize . of ( 4 ) ) ,
4154 indentOnInput ( ) ,
42- syntaxHighlighting ( highlightStyle || defaultHighlightStyle , { fallback : true } ) ,
4355 bracketMatching ( ) ,
4456 closeBrackets ( ) ,
4557 autocompletion ( ) ,
4658 rectangularSelection ( ) ,
4759 // crosshairCursor(),
4860 highlightActiveLine ( ) ,
4961 highlightSelectionMatches ( ) ,
50- new Compartment ( ) . of ( EditorState . tabSize . of ( 4 ) ) ,
5162 keymap . of ( [
5263 ...closeBracketsKeymap ,
5364 ...defaultKeymap ,
@@ -58,9 +69,24 @@ export const CustomCodeMirror: React.FC<CodeMirrorComponentProps> = (
5869 ...lintKeymap ,
5970 indentWithTab ,
6071 ] ) ,
61- theme ,
72+ initHighlightExt ( ) ,
73+ initThemeExt ( ) ,
6274 ...( extensions || [ ] ) ,
63- ] , [ highlightStyle , extensions , theme ] )
75+ ] , [ extensions , initHighlightExt , initThemeExt ] )
76+
77+ // attach parent plugin effects first
78+ React . useMemo ( ( ) => {
79+ if ( ! effects ) return
80+ effectsRef . current . push ( ...effects )
81+ } , [ effects ] )
82+
83+ // attach each plugin effect separately (thus only the one which changes get reconfigured)
84+ React . useMemo ( ( ) => {
85+ effectsRef . current . push ( ...effectsHighlightExt )
86+ } , [ effectsHighlightExt ] )
87+ React . useMemo ( ( ) => {
88+ effectsRef . current . push ( ...effectsThemeExt )
89+ } , [ effectsThemeExt ] )
6490
6591 const onViewLifecycle : CodeMirrorProps [ 'onViewLifecycle' ] = React . useCallback ( ( view ) => {
6692 console . log ( 'on-view-lifecycle' , view )
@@ -70,6 +96,7 @@ export const CustomCodeMirror: React.FC<CodeMirrorComponentProps> = (
7096 value = { value || '' }
7197 extensions = { extensionsAll }
7298 onViewLifecycle = { onViewLifecycle }
99+ effects = { effectsRef . current . splice ( 0 , effectsRef . current . length ) }
73100 { ...props }
74101 // className={className}
75102 />
0 commit comments