Skip to content

Commit

Permalink
Feat/1.2.1/UI (#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
shuashuai authored Nov 24, 2023
1 parent 6a0432b commit f770445
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions ui/src/components/Editor/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { useEffect, useState } from 'react';
import { useEffect, useState, useRef } from 'react';

import type { Editor, Position } from 'codemirror';
import type CodeMirror from 'codemirror';
Expand Down Expand Up @@ -148,6 +148,7 @@ export const useEditor = ({
}) => {
const [editor, setEditor] = useState<CodeMirror.Editor | null>(null);
const [value, setValue] = useState<string>('');
const isMountedRef = useRef(false);

const onEnter = (cm) => {
const cursor = cm.getCursor();
Expand Down Expand Up @@ -178,6 +179,11 @@ export const useEditor = ({
};

const init = async () => {
if (isMountedRef.current) {
return false;
}
isMountedRef.current = true;

const { default: codeMirror } = await import('codemirror');
await import('codemirror/mode/markdown/markdown');
await import('codemirror/addon/display/placeholder');
Expand All @@ -186,11 +192,13 @@ export const useEditor = ({
mode: 'markdown',
lineWrapping: true,
placeholder,
focus: autoFocus,
});

setEditor(cm);
createEditorUtils(codeMirror, cm);
if (autoFocus) {
setTimeout(() => {
cm.focus();
}, 10);
}

cm.on('change', (e) => {
const newValue = e.getValue();
Expand All @@ -203,6 +211,10 @@ export const useEditor = ({
cm.on('blur', () => {
onBlur?.();
});

setEditor(cm);
createEditorUtils(codeMirror, cm);

cm.setSize('100%', '100%');
cm.addKeyMap({
Enter: onEnter,
Expand Down

0 comments on commit f770445

Please sign in to comment.