Skip to content

Commit c3a5936

Browse files
committed
feat: Add basicSetup & indentWithTab props. #163
1 parent 1029353 commit c3a5936

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

src/index.tsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ export interface ReactCodeMirrorProps
2222
/** focus on the editor. */
2323
autoFocus?: boolean;
2424
theme?: 'light' | 'dark';
25+
/**
26+
* Whether to optional basicSetup by default
27+
* @default true
28+
*/
29+
basicSetup?: boolean;
30+
/**
31+
* Whether to optional basicSetup by default
32+
* @default true
33+
*/
34+
indentWithTab?: boolean;
2535
/** Fired whenever a change occurs to the document. */
2636
onChange?(value: string, viewUpdate: ViewUpdate): void;
2737
/** Fired whenever a change occurs to the document. There is a certain difference with `onChange`. */
@@ -57,6 +67,8 @@ const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProp
5767
width,
5868
minWidth,
5969
maxWidth,
70+
basicSetup,
71+
indentWithTab,
6072
...other
6173
} = props;
6274
const editor = useRef<HTMLDivElement>(null);
@@ -71,6 +83,8 @@ const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProp
7183
width,
7284
minWidth,
7385
maxWidth,
86+
basicSetup,
87+
indentWithTab,
7488
selection,
7589
onChange,
7690
onUpdate,

src/useCodeMirror.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useEffect, useState } from 'react';
2-
import { basicSetup } from '@codemirror/basic-setup';
2+
import { basicSetup as defaultBasicSetup } from '@codemirror/basic-setup';
33
import { EditorState, StateEffect } from '@codemirror/state';
4-
import { indentWithTab } from '@codemirror/commands';
4+
import { indentWithTab as defaultIndentWithTab } from '@codemirror/commands';
55
import { EditorView, keymap, ViewUpdate } from '@codemirror/view';
66
import { ReactCodeMirrorProps } from './';
77
import { oneDarkTheme } from '@codemirror/theme-one-dark';
@@ -26,6 +26,8 @@ export function useCodeMirror(props: UseCodeMirror) {
2626
width = '',
2727
minWidth = '',
2828
maxWidth = '',
29+
indentWithTab = true,
30+
basicSetup = true,
2931
} = props;
3032
const [container, setContainer] = useState(props.container);
3133
const [view, setView] = useState<EditorView>();
@@ -47,7 +49,13 @@ export function useCodeMirror(props: UseCodeMirror) {
4749
onChange(value, vu);
4850
}
4951
});
50-
let getExtensions = [basicSetup, keymap.of([indentWithTab]), updateListener, defaultThemeOption];
52+
let getExtensions = [updateListener, defaultThemeOption];
53+
if (indentWithTab) {
54+
getExtensions.unshift(keymap.of([defaultIndentWithTab]));
55+
}
56+
if (basicSetup) {
57+
getExtensions.unshift(defaultBasicSetup);
58+
}
5159
theme === 'light' ? getExtensions.push(defaultLightThemeOption) : getExtensions.push(oneDarkTheme);
5260
if (onUpdate && typeof onUpdate === 'function') {
5361
getExtensions.push(EditorView.updateListener.of(onUpdate));

0 commit comments

Comments
 (0)