|
| 1 | +import React, { useContext, useEffect, useMemo, useRef, useCallback } from 'react'; |
| 2 | + |
| 3 | +import * as path from 'path' |
| 4 | +import { URI, useInjectable } from '@opensumi/ide-core-browser'; |
| 5 | +import { ConfigProvider, AppConfig } from '@opensumi/ide-core-browser/lib/react-providers/config-provider' |
| 6 | +import { EditorCollectionService, ICodeEditor, IEditorDocumentModelRef } from '@opensumi/ide-editor/lib/common' |
| 7 | +import { IEditorDocumentModelService } from '@opensumi/ide-editor/lib/browser/doc-model/types' |
| 8 | +import { AppContext } from './context' |
| 9 | + |
| 10 | +const noop = () => {} |
| 11 | + |
| 12 | +export function useMemorizeFn<T extends (...args: any[]) => any>(fn: T) { |
| 13 | + const fnRef = useRef<T>(fn); |
| 14 | + fnRef.current = useMemo(() => fn, [fn]); |
| 15 | + return useCallback((...args: any) => fnRef.current(...args), []) as T; |
| 16 | +} |
| 17 | + |
| 18 | +export interface ICodeEditorProps extends React.HTMLAttributes<HTMLDivElement> { |
| 19 | + uri: URI | string; |
| 20 | + |
| 21 | + editorOptions?: any; |
| 22 | + |
| 23 | + onEditorCreate?: (editor: ICodeEditor) => void; |
| 24 | +} |
| 25 | + |
| 26 | +export const CodeEditorComponent = ({ uri, editorOptions, onEditorCreate, ...props }: ICodeEditorProps) => { |
| 27 | + const editorCollectionService: EditorCollectionService = useInjectable(EditorCollectionService); |
| 28 | + const documentService: IEditorDocumentModelService = useInjectable(IEditorDocumentModelService); |
| 29 | + const appConfig: AppConfig = useInjectable(AppConfig); |
| 30 | + |
| 31 | + const containerRef = React.useRef<HTMLDivElement | null>(null); |
| 32 | + const uriStr = useMemo(() => { |
| 33 | + if (typeof uri === 'string') { |
| 34 | + return URI.file(path.join(appConfig.workspaceDir, uri)).toString() |
| 35 | + } |
| 36 | + return uri.toString() |
| 37 | + }, [uri]) |
| 38 | + const fetchingUriRef = useRef<string>(''); |
| 39 | + const documentModelRef = useRef<IEditorDocumentModelRef>(); |
| 40 | + const editorRef = useRef<ICodeEditor>() |
| 41 | + const unmountRef = useRef(false); |
| 42 | + const onEditorCreateMemorizeFn = useMemorizeFn(onEditorCreate || noop) |
| 43 | + |
| 44 | + const openDocumentModel = () => { |
| 45 | + if (editorRef.current && documentModelRef.current) { |
| 46 | + editorRef.current.open(documentModelRef.current) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + React.useEffect(() => { |
| 51 | + if (containerRef.current) { |
| 52 | + editorRef.current?.dispose(); |
| 53 | + editorRef.current = editorCollectionService.createCodeEditor(containerRef.current, { |
| 54 | + automaticLayout: true, |
| 55 | + ...editorOptions, |
| 56 | + }); |
| 57 | + onEditorCreateMemorizeFn(editorRef.current) |
| 58 | + openDocumentModel() |
| 59 | + } |
| 60 | + return () => { |
| 61 | + unmountRef.current = true; |
| 62 | + editorRef.current?.dispose() |
| 63 | + documentModelRef.current?.dispose() |
| 64 | + }; |
| 65 | + }, []); |
| 66 | + |
| 67 | + useEffect(() => { |
| 68 | + if (fetchingUriRef.current !== uriStr) { |
| 69 | + fetchingUriRef.current = uriStr |
| 70 | + documentService.createModelReference(new URI(uriStr), 'editor-react-component').then((ref) => { |
| 71 | + if (documentModelRef.current) { |
| 72 | + documentModelRef.current.dispose(); |
| 73 | + } |
| 74 | + if (!unmountRef.current && ref.instance.uri.toString() === uriStr) { |
| 75 | + documentModelRef.current = ref; |
| 76 | + openDocumentModel() |
| 77 | + } else { |
| 78 | + ref.dispose(); |
| 79 | + } |
| 80 | + }); |
| 81 | + } |
| 82 | + }, [uriStr]) |
| 83 | + |
| 84 | + return <div ref={containerRef} {...props}></div>; |
| 85 | +}; |
| 86 | + |
| 87 | +export const CodeEditor = (props: ICodeEditorProps) => { |
| 88 | + const appContext = useContext(AppContext) |
| 89 | + if (!appContext.app) return null |
| 90 | + return ( |
| 91 | + <ConfigProvider value={appContext.app.config}> |
| 92 | + <CodeEditorComponent {...props} /> |
| 93 | + </ConfigProvider> |
| 94 | + ) |
| 95 | +}; |
0 commit comments