diff --git a/src/renderer/src/components/Playground/CodeEditor/Editor/index.tsx b/src/renderer/src/components/Playground/CodeEditor/Editor/index.tsx index d783590..d06954b 100644 --- a/src/renderer/src/components/Playground/CodeEditor/Editor/index.tsx +++ b/src/renderer/src/components/Playground/CodeEditor/Editor/index.tsx @@ -6,7 +6,11 @@ import { IEditorOptions, IFiles, ITheme, ITsconfig } from '@/components/Playgrou import { fileNameToLanguage, tsconfigJsonDiagnosticsOptions } from '@/components/Playground/files' import { useEditor, useTypesProgress } from '@/components/Playground/CodeEditor/Editor/hooks' import { MonacoEditorConfig } from '@/components/Playground/CodeEditor/Editor/monacoConfig' -import nativeApiDTS from '@/components/Playground/CodeEditor/Editor/_NativeApi.d.ts?raw' + +export interface ExtraLib { + path: string + content: string +} interface EditorProps { tsconfig?: ITsconfig @@ -17,6 +21,7 @@ interface EditorProps { options?: IEditorOptions theme?: ITheme onJumpFile?: (fileName: string) => void + extraLibs?: ExtraLib[] } const Editor = ({ @@ -27,7 +32,8 @@ const Editor = ({ theme, onChange, options, - onJumpFile + onJumpFile, + extraLibs = [] }: EditorProps) => { const editorRef = useRef() const monacoRef = useRef() @@ -85,9 +91,8 @@ const Editor = ({ } jsxSyntaxHighlightRef.current = loadJsxSyntaxHighlight(editor, monaco) - monaco.languages.typescript.typescriptDefaults.addExtraLib( - nativeApiDTS, - 'file:///node_modules/_NativeApi.d.ts' + extraLibs.forEach((item) => + monaco.languages.typescript.typescriptDefaults.addExtraLib(item.content, item.path) ) void autoLoadExtraLib(editor, monaco, file.value, onWatch) diff --git a/src/renderer/src/components/Playground/CodeEditor/index.tsx b/src/renderer/src/components/Playground/CodeEditor/index.tsx index b2d70d6..9116633 100644 --- a/src/renderer/src/components/Playground/CodeEditor/index.tsx +++ b/src/renderer/src/components/Playground/CodeEditor/index.tsx @@ -9,7 +9,7 @@ import { TS_CONFIG_FILE_NAME } from '@/components/Playground/files' import FileSelector from '@/components/Playground/CodeEditor/FileSelector' -import Editor from '@/components/Playground/CodeEditor/Editor' +import Editor, { ExtraLib } from '@/components/Playground/CodeEditor/Editor' interface CodeEditorProps { theme?: ITheme @@ -21,6 +21,7 @@ interface CodeEditorProps { notRemovable?: string[] selectedFileName?: string options?: IEditorOptions + extraLibs?: ExtraLib[] onSelectedFileChange?: (fileName: string) => void onAddFile?: (fileName: string, files: IFiles) => void onRemoveFile?: (fileName: string, files: IFiles) => void @@ -44,6 +45,7 @@ const CodeEditor = ({ onRenameFile, onChangeFileContent, onError, + extraLibs, ...props }: CodeEditorProps) => { const filteredFilesName = getFileNameList(files).filter( @@ -153,6 +155,7 @@ const CodeEditor = ({ } onChange={handleOnChangeFileContent} onJumpFile={handleOnChangeSelectedFile} + extraLibs={extraLibs} /> {errorMsg &&
{errorMsg}
} diff --git a/src/renderer/src/pages/System/Tools/Base.tsx b/src/renderer/src/pages/System/Tools/Base.tsx index 9e3f5f3..7a7177d 100644 --- a/src/renderer/src/pages/System/Tools/Base.tsx +++ b/src/renderer/src/pages/System/Tools/Base.tsx @@ -10,6 +10,7 @@ import { } from '@/constants/common.constants' import { utcToLocalTime } from '@/util/datetime' import { hasPermission } from '@/util/auth' +import editorExtraLibs from '@/util/editorExtraLibs' import { r_sys_tool_base_add, r_sys_tool_base_delete, @@ -1107,6 +1108,7 @@ const Base = () => { baseDetailLoading[editingBaseId] || !hasPermission('system:tool:modify:base') } + extraLibs={editorExtraLibs} />
diff --git a/src/renderer/src/pages/System/Tools/Code.tsx b/src/renderer/src/pages/System/Tools/Code.tsx index e9e9f1a..d81a12c 100644 --- a/src/renderer/src/pages/System/Tools/Code.tsx +++ b/src/renderer/src/pages/System/Tools/Code.tsx @@ -4,6 +4,7 @@ import '@/assets/css/pages/system/tools/code.scss' import { DATABASE_NO_RECORD_FOUND, DATABASE_SELECT_SUCCESS } from '@/constants/common.constants' import { checkDesktop } from '@/util/common' import { navigateToExecute, navigateToRepository } from '@/util/navigation' +import editorExtraLibs from '@/util/editorExtraLibs' import { r_sys_tool_get_one } from '@/services/system' import { IFiles } from '@/components/Playground/shared' import { base64ToFiles } from '@/components/Playground/files' @@ -87,6 +88,7 @@ const Code = () => { files={files} selectedFileName={selectedFileName} onSelectedFileChange={setSelectedFileName} + extraLibs={editorExtraLibs} /> diff --git a/src/renderer/src/pages/System/Tools/Template.tsx b/src/renderer/src/pages/System/Tools/Template.tsx index 61c5565..bfba80e 100644 --- a/src/renderer/src/pages/System/Tools/Template.tsx +++ b/src/renderer/src/pages/System/Tools/Template.tsx @@ -10,6 +10,7 @@ import { } from '@/constants/common.constants' import { utcToLocalTime } from '@/util/datetime' import { hasPermission } from '@/util/auth' +import editorExtraLibs from '@/util/editorExtraLibs' import { r_sys_tool_template_update, r_sys_tool_template_delete, @@ -1051,6 +1052,7 @@ const Template = () => { templateDetailLoading[editingTemplateId] || !hasPermission('system:tool:modify:template') } + extraLibs={editorExtraLibs} />
diff --git a/src/renderer/src/pages/Tools/Edit.tsx b/src/renderer/src/pages/Tools/Edit.tsx index 8655f52..ded6c7a 100644 --- a/src/renderer/src/pages/Tools/Edit.tsx +++ b/src/renderer/src/pages/Tools/Edit.tsx @@ -9,6 +9,7 @@ import { TOOL_UNDER_REVIEW } from '@/constants/common.constants' import { navigateToRepository } from '@/util/navigation' +import editorExtraLibs from '@/util/editorExtraLibs' import { r_tool_category_get, r_tool_detail, r_tool_update } from '@/services/tool' import { IFiles, IImportMap, ITsconfig } from '@/components/Playground/shared' import { @@ -443,6 +444,7 @@ const Edit = () => { onRenameFile={(_, __, files) => setFiles(files)} onChangeFileContent={handleOnChangeFileContent} onSelectedFileChange={setSelectedFileName} + extraLibs={editorExtraLibs} /> { files={files} selectedFileName={selectedFileName} onSelectedFileChange={setSelectedFileName} + extraLibs={editorExtraLibs} /> diff --git a/src/renderer/src/components/Playground/CodeEditor/Editor/_NativeApi.d.ts b/src/renderer/src/util/editorExtraLibs/_NativeApi.d.ts similarity index 100% rename from src/renderer/src/components/Playground/CodeEditor/Editor/_NativeApi.d.ts rename to src/renderer/src/util/editorExtraLibs/_NativeApi.d.ts diff --git a/src/renderer/src/util/editorExtraLibs/_index.d.ts b/src/renderer/src/util/editorExtraLibs/_index.d.ts new file mode 100644 index 0000000..a0f0afe --- /dev/null +++ b/src/renderer/src/util/editorExtraLibs/_index.d.ts @@ -0,0 +1,7 @@ +interface Window { + /** + * Initialization tool.\ + * This method must be implemented to run the tool. + */ + initOxygenTool: (id: string) => void +} diff --git a/src/renderer/src/util/editorExtraLibs/index.ts b/src/renderer/src/util/editorExtraLibs/index.ts new file mode 100644 index 0000000..3cf795b --- /dev/null +++ b/src/renderer/src/util/editorExtraLibs/index.ts @@ -0,0 +1,16 @@ +import { ExtraLib } from '@/components/Playground/CodeEditor/Editor' +import indexDTS from '@/util/editorExtraLibs/_index.d.ts?raw' +import nativeApiDTS from '@/util/editorExtraLibs/_NativeApi.d.ts?raw' + +const editorExtraLibs: ExtraLib[] = [ + { + path: 'file:///node_modules/_index.d.ts', + content: indexDTS + }, + { + path: 'file:///node_modules/_NativeApi.d.ts', + content: nativeApiDTS + } +] + +export default editorExtraLibs