11import { useEffect , useState } from 'react' ;
22import { Annotation , EditorState , StateEffect } from '@codemirror/state' ;
3- import { indentWithTab } from '@codemirror/commands' ;
4- import { EditorView , keymap , ViewUpdate , placeholder } from '@codemirror/view' ;
5- import { basicSetup } from '@uiw/codemirror-extensions-basic-setup' ;
6- import { oneDark } from '@codemirror/theme-one-dark' ;
3+ import { EditorView , ViewUpdate } from '@codemirror/view' ;
4+ import { getDefaultExtensions } from './getDefaultExtensions' ;
75import { getStatistics } from './utils' ;
86import { ReactCodeMirrorProps } from '.' ;
97
@@ -41,16 +39,6 @@ export function useCodeMirror(props: UseCodeMirror) {
4139 const [ container , setContainer ] = useState < HTMLDivElement > ( ) ;
4240 const [ view , setView ] = useState < EditorView > ( ) ;
4341 const [ state , setState ] = useState < EditorState > ( ) ;
44- const defaultLightThemeOption = EditorView . theme (
45- {
46- '&' : {
47- backgroundColor : '#fff' ,
48- } ,
49- } ,
50- {
51- dark : false ,
52- } ,
53- ) ;
5442 const defaultThemeOption = EditorView . theme ( {
5543 '&' : {
5644 height,
@@ -76,42 +64,16 @@ export function useCodeMirror(props: UseCodeMirror) {
7664 onStatistics && onStatistics ( getStatistics ( vu ) ) ;
7765 } ) ;
7866
79- let getExtensions = [ updateListener , defaultThemeOption ] ;
80- if ( defaultIndentWithTab ) {
81- getExtensions . unshift ( keymap . of ( [ indentWithTab ] ) ) ;
82- }
83- if ( defaultBasicSetup ) {
84- if ( typeof defaultBasicSetup === 'boolean' ) {
85- getExtensions . unshift ( basicSetup ( ) ) ;
86- } else {
87- getExtensions . unshift ( basicSetup ( defaultBasicSetup ) ) ;
88- }
89- }
90-
91- if ( placeholderStr ) {
92- getExtensions . unshift ( placeholder ( placeholderStr ) ) ;
93- }
94-
95- switch ( theme ) {
96- case 'light' :
97- getExtensions . push ( defaultLightThemeOption ) ;
98- break ;
99- case 'dark' :
100- getExtensions . push ( oneDark ) ;
101- break ;
102- case 'none' :
103- break ;
104- default :
105- getExtensions . push ( theme ) ;
106- break ;
107- }
67+ const defaultExtensions = getDefaultExtensions ( {
68+ theme,
69+ editable : true ,
70+ readOnly : false ,
71+ placeholder : placeholderStr ,
72+ indentWithTab : defaultIndentWithTab ,
73+ basicSetup : defaultBasicSetup ,
74+ } ) ;
10875
109- if ( editable === false ) {
110- getExtensions . push ( EditorView . editable . of ( false ) ) ;
111- }
112- if ( readOnly ) {
113- getExtensions . push ( EditorState . readOnly . of ( true ) ) ;
114- }
76+ let getExtensions = [ updateListener , defaultThemeOption , ...defaultExtensions ] ;
11577
11678 if ( onUpdate && typeof onUpdate === 'function' ) {
11779 getExtensions . push ( EditorView . updateListener . of ( onUpdate ) ) ;
0 commit comments