From d802844516c3c9da8c0d25adf032d23de6c3150f Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Tue, 9 Jan 2024 16:03:22 +0800 Subject: [PATCH] Optimize CodeEditor --- .../Playground/CodeEditor/Editor/index.tsx | 1 + .../Playground/CodeEditor/Editor/loader.ts | 7 +++++ .../Playground/CodeEditor/index.tsx | 30 ++++++++++++------- src/pages/OnlineEditor.tsx | 1 - 4 files changed, 28 insertions(+), 11 deletions(-) create mode 100644 src/components/Playground/CodeEditor/Editor/loader.ts diff --git a/src/components/Playground/CodeEditor/Editor/index.tsx b/src/components/Playground/CodeEditor/Editor/index.tsx index aef15e8..7186f81 100644 --- a/src/components/Playground/CodeEditor/Editor/index.tsx +++ b/src/components/Playground/CodeEditor/Editor/index.tsx @@ -2,6 +2,7 @@ import React from 'react' import { editor, Selection } from 'monaco-editor' import MonacoEditor, { Monaco } from '@monaco-editor/react' import '@/components/Playground/CodeEditor/Editor/editor.scss' +import '@/components/Playground/CodeEditor/Editor/loader' import { IEditorOptions, IFiles, ITheme } from '@/components/Playground/shared' import { fileNameToLanguage } from '@/components/Playground/files' import { useEditor, useTypesProgress } from '@/components/Playground/CodeEditor/Editor/hooks' diff --git a/src/components/Playground/CodeEditor/Editor/loader.ts b/src/components/Playground/CodeEditor/Editor/loader.ts new file mode 100644 index 0000000..c568b2c --- /dev/null +++ b/src/components/Playground/CodeEditor/Editor/loader.ts @@ -0,0 +1,7 @@ +import { loader } from '@monaco-editor/react' + +loader.config({ + paths: { + vs: 'https://unpkg.com/monaco-editor@0.45.0/min/vs' + } +}) diff --git a/src/components/Playground/CodeEditor/index.tsx b/src/components/Playground/CodeEditor/index.tsx index 97991a5..6014a5a 100644 --- a/src/components/Playground/CodeEditor/index.tsx +++ b/src/components/Playground/CodeEditor/index.tsx @@ -2,7 +2,11 @@ import React from 'react' import _ from 'lodash' import '@/components/Playground/CodeEditor/code-editor.scss' import { IEditorOptions, IFiles, ITheme } from '@/components/Playground/shared' -import { fileNameToLanguage } from '@/components/Playground/files' +import { + ENTRY_FILE_NAME, + fileNameToLanguage, + IMPORT_MAP_FILE_NAME +} from '@/components/Playground/files' import FileSelector from '@/components/Playground/CodeEditor/FileSelector' import Editor from '@/components/Playground/CodeEditor/Editor' import FlexBox from '@/components/common/FlexBox' @@ -13,7 +17,7 @@ interface CodeEditorProps { readonly?: boolean readonlyFiles?: string[] notRemovable?: string[] - selectedFileName: string + selectedFileName?: string options?: IEditorOptions onSelectedFileChange?: (fileName: string) => void onAddFile?: (fileName: string, files: IFiles) => void @@ -38,7 +42,12 @@ const CodeEditor: React.FC = ({ onError, ...props }) => { - const [selectedFileName, setSelectedFileName] = useState(props.selectedFileName) + const filteredFilesName = Object.keys(files).filter( + (item) => ![IMPORT_MAP_FILE_NAME, ENTRY_FILE_NAME].includes(item) && !files[item].hidden + ) + const propsSelectedFileName = + props.selectedFileName ?? (filteredFilesName.length ? filteredFilesName[0] : '') + const [selectedFileName, setSelectedFileName] = useState(propsSelectedFileName) const [errorMsg, setErrorMsg] = useState('') const handleOnChangeSelectedFile = (fileName: string) => { @@ -93,14 +102,14 @@ const CodeEditor: React.FC = ({ } const handleOnChangeFileContent = _.debounce((code = '') => { - if (!files[onSelectedFileChange ? props.selectedFileName : selectedFileName]) { + if (!files[onSelectedFileChange ? propsSelectedFileName : selectedFileName]) { return } const clone = _.cloneDeep(files) - clone[onSelectedFileChange ? props.selectedFileName : selectedFileName].value = code ?? '' + clone[onSelectedFileChange ? propsSelectedFileName : selectedFileName].value = code ?? '' onChangeFileContent?.( code, - onSelectedFileChange ? props.selectedFileName : selectedFileName, + onSelectedFileChange ? propsSelectedFileName : selectedFileName, clone ) }, 250) @@ -113,7 +122,7 @@ const CodeEditor: React.FC = ({ readonly={readonly} notRemovableFiles={notRemovable} selectedFileName={ - onSelectedFileChange ? props.selectedFileName : selectedFileName + onSelectedFileChange ? propsSelectedFileName : selectedFileName } onChange={handleOnChangeSelectedFile} onRemoveFile={handleOnRemoveFile} @@ -124,18 +133,19 @@ const CodeEditor: React.FC = ({ {errorMsg &&
{errorMsg}
} diff --git a/src/pages/OnlineEditor.tsx b/src/pages/OnlineEditor.tsx index e4c44d4..1f45a3c 100644 --- a/src/pages/OnlineEditor.tsx +++ b/src/pages/OnlineEditor.tsx @@ -10,7 +10,6 @@ const OnlineEditor: React.FC = () => { <> setFiles(files)} onRemoveFile={(_, files) => setFiles(files)} onRenameFile={(_, __, files) => setFiles(files)}