Skip to content

Commit

Permalink
fix: 代码缓存修复
Browse files Browse the repository at this point in the history
  • Loading branch information
xjq committed Nov 17, 2022
1 parent 0ceca1c commit e3b6d2d
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 22 deletions.
6 changes: 3 additions & 3 deletions client/src/pages/editor/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react';
import { useCallback, useEffect, useRef, useState } from 'react';
import Editor, { Expose, languageMap } from '~components/CodeEditorMonaco';
import styles from './index.module.less';
import Header from './components/Header';
Expand All @@ -16,9 +16,9 @@ const Component = () => {
const [editorConfig] = useState(() => EditorConfig);
const { editorThemeType, codeType } = editorConfig;

const getEditor = () => {
const getEditor = useCallback(() => {
return editorRef.current?.getEditor();
};
}, [editorRef.current]);

useEffect(() => {
const codeCache =
Expand Down
32 changes: 13 additions & 19 deletions client/src/pages/question/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef, useState } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import Editor, { Expose, languageMap } from '~components/CodeEditorMonaco';
import { marked } from 'marked';
import styles from './index.module.less';
Expand Down Expand Up @@ -37,28 +37,22 @@ function Question() {
};

const saveCode = () => {
if (detail?.name) {
storage.set(
CodeStorageKey[CodeType.nodejs] + '_' + detail?.name,
editorRef.current?.getEditor()?.getValue()
);
}
storage.set(
CodeStorageKey[CodeType.nodejs] + '_' + detail?.name,
editorRef.current?.getEditor()?.getValue()
);
};

useEffect(() => {
const editor = editorRef.current?.getEditor();
if (editor && detail) {
editor.setValue(
storage.get(CodeStorageKey[CodeType.nodejs] + '_' + detail?.name) ||
detail?.index
);
}
const defaultCode = useMemo(() => {
return (
storage.get(CodeStorageKey[CodeType.nodejs] + '_' + detail?.name) ||
detail?.index
);
}, [detail]);

useEffect(() => {
const editor = editorRef.current?.getEditor();

if (editor) {
if (editor && detail) {
const debounceSaveCode = debounce(saveCode, autoSaveDelay * 1000);
const saveCodeListen = editor
?.getModel()
Expand All @@ -70,7 +64,7 @@ function Question() {
debounceSaveCode.cancel();
};
}
}, [autoSaveDelay, editorRef]);
}, [autoSaveDelay, editorRef.current, detail]);

useEffect(() => {
const fetchQuestion = async () => {
Expand Down Expand Up @@ -116,7 +110,7 @@ function Question() {
className={styles.editor}
theme={ThemeType['Visual Studio Dark']}
language={languageMap[CodeType.nodejs]}
code={detail?.index || ''}
code={defaultCode}
/>
<div className={styles.operator}>
<div className={styles.left}>
Expand Down

0 comments on commit e3b6d2d

Please sign in to comment.