Feat(CodeEditor): Support add extra libs

This commit is contained in:
2024-10-27 00:50:37 +08:00
parent ba3082c88c
commit a48187bb4e
10 changed files with 47 additions and 6 deletions

View File

@@ -6,7 +6,11 @@ import { IEditorOptions, IFiles, ITheme, ITsconfig } from '@/components/Playgrou
import { fileNameToLanguage, tsconfigJsonDiagnosticsOptions } from '@/components/Playground/files' import { fileNameToLanguage, tsconfigJsonDiagnosticsOptions } from '@/components/Playground/files'
import { useEditor, useTypesProgress } from '@/components/Playground/CodeEditor/Editor/hooks' import { useEditor, useTypesProgress } from '@/components/Playground/CodeEditor/Editor/hooks'
import { MonacoEditorConfig } from '@/components/Playground/CodeEditor/Editor/monacoConfig' 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 { interface EditorProps {
tsconfig?: ITsconfig tsconfig?: ITsconfig
@@ -17,6 +21,7 @@ interface EditorProps {
options?: IEditorOptions options?: IEditorOptions
theme?: ITheme theme?: ITheme
onJumpFile?: (fileName: string) => void onJumpFile?: (fileName: string) => void
extraLibs?: ExtraLib[]
} }
const Editor = ({ const Editor = ({
@@ -27,7 +32,8 @@ const Editor = ({
theme, theme,
onChange, onChange,
options, options,
onJumpFile onJumpFile,
extraLibs = []
}: EditorProps) => { }: EditorProps) => {
const editorRef = useRef<editor.IStandaloneCodeEditor>() const editorRef = useRef<editor.IStandaloneCodeEditor>()
const monacoRef = useRef<Monaco>() const monacoRef = useRef<Monaco>()
@@ -85,9 +91,8 @@ const Editor = ({
} }
jsxSyntaxHighlightRef.current = loadJsxSyntaxHighlight(editor, monaco) jsxSyntaxHighlightRef.current = loadJsxSyntaxHighlight(editor, monaco)
monaco.languages.typescript.typescriptDefaults.addExtraLib( extraLibs.forEach((item) =>
nativeApiDTS, monaco.languages.typescript.typescriptDefaults.addExtraLib(item.content, item.path)
'file:///node_modules/_NativeApi.d.ts'
) )
void autoLoadExtraLib(editor, monaco, file.value, onWatch) void autoLoadExtraLib(editor, monaco, file.value, onWatch)

View File

@@ -9,7 +9,7 @@ import {
TS_CONFIG_FILE_NAME TS_CONFIG_FILE_NAME
} from '@/components/Playground/files' } from '@/components/Playground/files'
import FileSelector from '@/components/Playground/CodeEditor/FileSelector' import FileSelector from '@/components/Playground/CodeEditor/FileSelector'
import Editor from '@/components/Playground/CodeEditor/Editor' import Editor, { ExtraLib } from '@/components/Playground/CodeEditor/Editor'
interface CodeEditorProps { interface CodeEditorProps {
theme?: ITheme theme?: ITheme
@@ -21,6 +21,7 @@ interface CodeEditorProps {
notRemovable?: string[] notRemovable?: string[]
selectedFileName?: string selectedFileName?: string
options?: IEditorOptions options?: IEditorOptions
extraLibs?: ExtraLib[]
onSelectedFileChange?: (fileName: string) => void onSelectedFileChange?: (fileName: string) => void
onAddFile?: (fileName: string, files: IFiles) => void onAddFile?: (fileName: string, files: IFiles) => void
onRemoveFile?: (fileName: string, files: IFiles) => void onRemoveFile?: (fileName: string, files: IFiles) => void
@@ -44,6 +45,7 @@ const CodeEditor = ({
onRenameFile, onRenameFile,
onChangeFileContent, onChangeFileContent,
onError, onError,
extraLibs,
...props ...props
}: CodeEditorProps) => { }: CodeEditorProps) => {
const filteredFilesName = getFileNameList(files).filter( const filteredFilesName = getFileNameList(files).filter(
@@ -153,6 +155,7 @@ const CodeEditor = ({
} }
onChange={handleOnChangeFileContent} onChange={handleOnChangeFileContent}
onJumpFile={handleOnChangeSelectedFile} onJumpFile={handleOnChangeSelectedFile}
extraLibs={extraLibs}
/> />
{errorMsg && <div className={'playground-error-message'}>{errorMsg}</div>} {errorMsg && <div className={'playground-error-message'}>{errorMsg}</div>}
</FlexBox> </FlexBox>

View File

@@ -10,6 +10,7 @@ import {
} from '@/constants/common.constants' } from '@/constants/common.constants'
import { utcToLocalTime } from '@/util/datetime' import { utcToLocalTime } from '@/util/datetime'
import { hasPermission } from '@/util/auth' import { hasPermission } from '@/util/auth'
import editorExtraLibs from '@/util/editorExtraLibs'
import { import {
r_sys_tool_base_add, r_sys_tool_base_add,
r_sys_tool_base_delete, r_sys_tool_base_delete,
@@ -1107,6 +1108,7 @@ const Base = () => {
baseDetailLoading[editingBaseId] || baseDetailLoading[editingBaseId] ||
!hasPermission('system:tool:modify:base') !hasPermission('system:tool:modify:base')
} }
extraLibs={editorExtraLibs}
/> />
<div className={'close-editor-btn'} onClick={handleOnCloseBtnClick}> <div className={'close-editor-btn'} onClick={handleOnCloseBtnClick}>
<Icon component={IconOxygenClose} /> <Icon component={IconOxygenClose} />

View File

@@ -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 { DATABASE_NO_RECORD_FOUND, DATABASE_SELECT_SUCCESS } from '@/constants/common.constants'
import { checkDesktop } from '@/util/common' import { checkDesktop } from '@/util/common'
import { navigateToExecute, navigateToRepository } from '@/util/navigation' import { navigateToExecute, navigateToRepository } from '@/util/navigation'
import editorExtraLibs from '@/util/editorExtraLibs'
import { r_sys_tool_get_one } from '@/services/system' import { r_sys_tool_get_one } from '@/services/system'
import { IFiles } from '@/components/Playground/shared' import { IFiles } from '@/components/Playground/shared'
import { base64ToFiles } from '@/components/Playground/files' import { base64ToFiles } from '@/components/Playground/files'
@@ -87,6 +88,7 @@ const Code = () => {
files={files} files={files}
selectedFileName={selectedFileName} selectedFileName={selectedFileName}
onSelectedFileChange={setSelectedFileName} onSelectedFileChange={setSelectedFileName}
extraLibs={editorExtraLibs}
/> />
</Card> </Card>

View File

@@ -10,6 +10,7 @@ import {
} from '@/constants/common.constants' } from '@/constants/common.constants'
import { utcToLocalTime } from '@/util/datetime' import { utcToLocalTime } from '@/util/datetime'
import { hasPermission } from '@/util/auth' import { hasPermission } from '@/util/auth'
import editorExtraLibs from '@/util/editorExtraLibs'
import { import {
r_sys_tool_template_update, r_sys_tool_template_update,
r_sys_tool_template_delete, r_sys_tool_template_delete,
@@ -1051,6 +1052,7 @@ const Template = () => {
templateDetailLoading[editingTemplateId] || templateDetailLoading[editingTemplateId] ||
!hasPermission('system:tool:modify:template') !hasPermission('system:tool:modify:template')
} }
extraLibs={editorExtraLibs}
/> />
<div className={'close-editor-btn'} onClick={handleOnCloseBtnClick}> <div className={'close-editor-btn'} onClick={handleOnCloseBtnClick}>
<Icon component={IconOxygenClose} /> <Icon component={IconOxygenClose} />

View File

@@ -9,6 +9,7 @@ import {
TOOL_UNDER_REVIEW TOOL_UNDER_REVIEW
} from '@/constants/common.constants' } from '@/constants/common.constants'
import { navigateToRepository } from '@/util/navigation' 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 { r_tool_category_get, r_tool_detail, r_tool_update } from '@/services/tool'
import { IFiles, IImportMap, ITsconfig } from '@/components/Playground/shared' import { IFiles, IImportMap, ITsconfig } from '@/components/Playground/shared'
import { import {
@@ -443,6 +444,7 @@ const Edit = () => {
onRenameFile={(_, __, files) => setFiles(files)} onRenameFile={(_, __, files) => setFiles(files)}
onChangeFileContent={handleOnChangeFileContent} onChangeFileContent={handleOnChangeFileContent}
onSelectedFileChange={setSelectedFileName} onSelectedFileChange={setSelectedFileName}
extraLibs={editorExtraLibs}
/> />
<Playground.Output <Playground.Output
files={files} files={files}

View File

@@ -2,6 +2,7 @@ import '@/assets/css/pages/tools/source.scss'
import { DATABASE_NO_RECORD_FOUND, DATABASE_SELECT_SUCCESS } from '@/constants/common.constants' import { DATABASE_NO_RECORD_FOUND, DATABASE_SELECT_SUCCESS } from '@/constants/common.constants'
import { getLoginStatus } from '@/util/auth' import { getLoginStatus } from '@/util/auth'
import { navigateToRepository, navigateToSource } from '@/util/navigation' import { navigateToRepository, navigateToSource } from '@/util/navigation'
import editorExtraLibs from '@/util/editorExtraLibs'
import { r_tool_detail } from '@/services/tool' import { r_tool_detail } from '@/services/tool'
import { IFiles } from '@/components/Playground/shared' import { IFiles } from '@/components/Playground/shared'
import { base64ToFiles } from '@/components/Playground/files' import { base64ToFiles } from '@/components/Playground/files'
@@ -90,6 +91,7 @@ const Source = () => {
files={files} files={files}
selectedFileName={selectedFileName} selectedFileName={selectedFileName}
onSelectedFileChange={setSelectedFileName} onSelectedFileChange={setSelectedFileName}
extraLibs={editorExtraLibs}
/> />
</Card> </Card>
</FitFullscreen> </FitFullscreen>

View File

@@ -0,0 +1,7 @@
interface Window {
/**
* Initialization tool.\
* This method must be implemented to run the tool.
*/
initOxygenTool: (id: string) => void
}

View File

@@ -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