|
|
@@ -1,10 +1,8 @@
|
|
|
import {
|
|
|
- forwardRef, useMemo, useRef, useEffect,
|
|
|
+ forwardRef, useMemo, useRef,
|
|
|
} from 'react';
|
|
|
|
|
|
|
|
|
-import { defaultKeymap } from '@codemirror/commands';
|
|
|
-import { keymap } from '@codemirror/view';
|
|
|
import type { ReactCodeMirrorProps } from '@uiw/react-codemirror';
|
|
|
|
|
|
import { GlobalCodeMirrorEditorKey } from '../../consts';
|
|
|
@@ -24,14 +22,12 @@ const CodeMirrorEditorContainer = forwardRef<HTMLDivElement>((props, ref) => {
|
|
|
type Props = {
|
|
|
editorKey: string | GlobalCodeMirrorEditorKey,
|
|
|
onChange?: (value: string) => void,
|
|
|
- onSave?: () => void,
|
|
|
}
|
|
|
|
|
|
export const CodeMirrorEditor = (props: Props): JSX.Element => {
|
|
|
const {
|
|
|
editorKey,
|
|
|
onChange,
|
|
|
- onSave,
|
|
|
} = props;
|
|
|
|
|
|
const containerRef = useRef(null);
|
|
|
@@ -42,33 +38,7 @@ export const CodeMirrorEditor = (props: Props): JSX.Element => {
|
|
|
};
|
|
|
}, [onChange]);
|
|
|
|
|
|
- const { data: codeMirrorEditor } = useCodeMirrorEditorIsolated(editorKey, containerRef.current, cmProps);
|
|
|
-
|
|
|
- // set handler to save with ctrl/cmd + Enter key
|
|
|
- useEffect(() => {
|
|
|
- if (onSave == null) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
- const extension = keymap.of([
|
|
|
- {
|
|
|
- key: 'Mod-Enter',
|
|
|
- preventDefault: true,
|
|
|
- run: () => {
|
|
|
- const doc = codeMirrorEditor?.getDoc();
|
|
|
- if (doc != null) {
|
|
|
- onSave();
|
|
|
- }
|
|
|
- return true;
|
|
|
- },
|
|
|
- },
|
|
|
- ...defaultKeymap,
|
|
|
- ]);
|
|
|
-
|
|
|
- const cleanupFunction = codeMirrorEditor?.appendExtensions?.(extension);
|
|
|
-
|
|
|
- return cleanupFunction;
|
|
|
- }, [codeMirrorEditor, onSave]);
|
|
|
+ useCodeMirrorEditorIsolated(editorKey, containerRef.current, cmProps);
|
|
|
|
|
|
return (
|
|
|
<div className="flex-expand-vert">
|