diff --git a/src/index.tsx b/src/index.tsx index 22992edd0..d19952d04 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -9,9 +9,7 @@ export * from "@codemirror/basic-setup"; export * from "@codemirror/state"; export interface ReactCodeMirrorProps extends Omit, Omit, 'onChange'> { - /** - * value of the auto created model in the editor. - */ + /** value of the auto created model in the editor. */ value?: string; height?: string; minHeight?: string; @@ -22,10 +20,10 @@ export interface ReactCodeMirrorProps extends Omit((props, ref) => { - const { className, value, selection, extensions = [], onChange, autoFocus, theme, height, minHeight, maxHeight, width, minWidth, maxWidth, ...other } = props; +const ReactCodeMirror = React.forwardRef((props, ref) => { + const { className, value, selection, extensions = [], onChange, onUpdate, autoFocus, theme, height, minHeight, maxHeight, width, minWidth, maxWidth, ...other } = props; const editor = useRef(null); const { state, view, container, setContainer } = useCodeMirror({ container: editor.current, value, autoFocus, theme, height, minHeight, maxHeight, width, minWidth, maxWidth, selection, - onChange, + onChange, onUpdate, extensions, }); useImperativeHandle(ref, () => ({ editor: container, state, view })); @@ -67,3 +65,7 @@ export default React.forwardRef((props
); }); + +ReactCodeMirror.displayName = 'CodeMirror'; + +export default ReactCodeMirror; \ No newline at end of file diff --git a/src/useCodeMirror.ts b/src/useCodeMirror.ts index 5150cfdb5..0dc72bb47 100644 --- a/src/useCodeMirror.ts +++ b/src/useCodeMirror.ts @@ -12,7 +12,7 @@ export interface UseCodeMirror extends ReactCodeMirrorProps { } export function useCodeMirror(props: UseCodeMirror) { - const { value, selection, onChange, extensions = [], autoFocus, theme = 'light', height = '', minHeight = '', maxHeight ='', width = '', minWidth = '', maxWidth = '' } = props; + const { value, selection, onChange, onUpdate, extensions = [], autoFocus, theme = 'light', height = '', minHeight = '', maxHeight ='', width = '', minWidth = '', maxWidth = '' } = props; const [container, setContainer] = useState(props.container); const [view, setView] = useState(); const [state, setState] = useState(); @@ -32,6 +32,9 @@ export function useCodeMirror(props: UseCodeMirror) { }); let getExtensions = [basicSetup, keymap.of([indentWithTab]), updateListener, defaultThemeOption]; theme === 'light' ? getExtensions.push(defaultLightThemeOption) : getExtensions.push(oneDarkTheme); + if (onUpdate && typeof onUpdate === 'function') { + getExtensions.push(EditorView.updateListener.of(onUpdate)); + } getExtensions = getExtensions.concat(extensions); useEffect(() => {