Skip to content

Commit 7e48152

Browse files
committed
feat: add onUpdate props.
1 parent ea6ac0e commit 7e48152

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/index.tsx

+12-10
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@ export * from "@codemirror/basic-setup";
99
export * from "@codemirror/state";
1010

1111
export interface ReactCodeMirrorProps extends Omit<EditorStateConfig, 'doc' | 'extensions'>, Omit<React.HTMLAttributes<HTMLDivElement>, 'onChange'> {
12-
/**
13-
* value of the auto created model in the editor.
14-
*/
12+
/** value of the auto created model in the editor. */
1513
value?: string;
1614
height?: string;
1715
minHeight?: string;
@@ -22,10 +20,10 @@ export interface ReactCodeMirrorProps extends Omit<EditorStateConfig, 'doc' | 'e
2220
/** focus on the editor. */
2321
autoFocus?: boolean;
2422
theme?: 'light' | 'dark';
25-
/**
26-
* Fired whenever a change occurs to the document.
27-
*/
28-
onChange?(value: string, viewUpdate: ViewUpdate): void;
23+
/** Fired whenever a change occurs to the document. */
24+
onChange? (value: string, viewUpdate: ViewUpdate): void;
25+
/** Fired whenever a change occurs to the document. There is a certain difference with `onChange`. */
26+
onUpdate? (viewUpdate: ViewUpdate): void
2927
/**
3028
* Extension values can be [provided](https://codemirror.net/6/docs/ref/#state.EditorStateConfig.extensions) when creating a state to attach various kinds of configuration and behavior information.
3129
* They can either be built-in extension-providing objects,
@@ -41,14 +39,14 @@ export interface ReactCodeMirrorRef {
4139
view?: EditorView;
4240
}
4341

44-
export default React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props, ref) => {
45-
const { className, value, selection, extensions = [], onChange, autoFocus, theme, height, minHeight, maxHeight, width, minWidth, maxWidth, ...other } = props;
42+
const ReactCodeMirror = React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props, ref) => {
43+
const { className, value, selection, extensions = [], onChange, onUpdate, autoFocus, theme, height, minHeight, maxHeight, width, minWidth, maxWidth, ...other } = props;
4644
const editor = useRef<HTMLDivElement>(null);
4745
const { state, view, container, setContainer } = useCodeMirror({
4846
container: editor.current,
4947
value, autoFocus, theme, height, minHeight, maxHeight, width, minWidth, maxWidth,
5048
selection,
51-
onChange,
49+
onChange, onUpdate,
5250
extensions,
5351
});
5452
useImperativeHandle(ref, () => ({ editor: container, state, view }));
@@ -67,3 +65,7 @@ export default React.forwardRef<ReactCodeMirrorRef, ReactCodeMirrorProps>((props
6765
<div ref={editor} className={`cm-theme-${theme} ${className || ''}`} {...other}></div>
6866
);
6967
});
68+
69+
ReactCodeMirror.displayName = 'CodeMirror';
70+
71+
export default ReactCodeMirror;

src/useCodeMirror.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export interface UseCodeMirror extends ReactCodeMirrorProps {
1212
}
1313

1414
export function useCodeMirror(props: UseCodeMirror) {
15-
const { value, selection, onChange, extensions = [], autoFocus, theme = 'light', height = '', minHeight = '', maxHeight ='', width = '', minWidth = '', maxWidth = '' } = props;
15+
const { value, selection, onChange, onUpdate, extensions = [], autoFocus, theme = 'light', height = '', minHeight = '', maxHeight ='', width = '', minWidth = '', maxWidth = '' } = props;
1616
const [container, setContainer] = useState(props.container);
1717
const [view, setView] = useState<EditorView>();
1818
const [state, setState] = useState<EditorState>();
@@ -32,6 +32,9 @@ export function useCodeMirror(props: UseCodeMirror) {
3232
});
3333
let getExtensions = [basicSetup, keymap.of([indentWithTab]), updateListener, defaultThemeOption];
3434
theme === 'light' ? getExtensions.push(defaultLightThemeOption) : getExtensions.push(oneDarkTheme);
35+
if (onUpdate && typeof onUpdate === 'function') {
36+
getExtensions.push(EditorView.updateListener.of(onUpdate));
37+
}
3538
getExtensions = getExtensions.concat(extensions);
3639

3740
useEffect(() => {

0 commit comments

Comments
 (0)