|
| 1 | +import React, { useContext, useEffect, useMemo, useRef, MutableRefObject } 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, IDiffEditor, 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 | +import { useMemorizeFn } from '../hooks' |
| 10 | +import { parseUri } from './util' |
| 11 | + |
| 12 | +const noop = () => {} |
| 13 | + |
| 14 | +export interface ICodeEditorProps extends React.HTMLAttributes<HTMLDivElement> { |
| 15 | + originalUri: URI | string; |
| 16 | + |
| 17 | + modifiedUri: URI | string; |
| 18 | + |
| 19 | + editorOptions?: any; |
| 20 | + |
| 21 | + onEditorCreate?: (editor: IDiffEditor) => void; |
| 22 | +} |
| 23 | + |
| 24 | +export const DiffEditorComponent = ({ originalUri, modifiedUri, editorOptions, onEditorCreate, ...props }: ICodeEditorProps) => { |
| 25 | + const editorCollectionService: EditorCollectionService = useInjectable(EditorCollectionService); |
| 26 | + const documentService: IEditorDocumentModelService = useInjectable(IEditorDocumentModelService); |
| 27 | + const appConfig: AppConfig = useInjectable(AppConfig); |
| 28 | + |
| 29 | + const containerRef = React.useRef<HTMLDivElement | null>(null); |
| 30 | + const editorRef = useRef<IDiffEditor>() |
| 31 | + const unmountRef = useRef(false); |
| 32 | + const onEditorCreateMemorizeFn = useMemorizeFn(onEditorCreate || noop) |
| 33 | + |
| 34 | + const originalUriStr = useMemo(() => parseUri(originalUri, appConfig.workspaceDir), [originalUri]) |
| 35 | + const originalFetchingUriRef = useRef<string>(''); |
| 36 | + const originalDocumentModelRef = useRef<IEditorDocumentModelRef>(); |
| 37 | + |
| 38 | + const modifiedUriStr = useMemo(() => parseUri(modifiedUri, appConfig.workspaceDir), [modifiedUri]) |
| 39 | + const modifiedFetchingUriRef = useRef<string>(''); |
| 40 | + const modifiedDocumentModelRef = useRef<IEditorDocumentModelRef>(); |
| 41 | + |
| 42 | + const openDocumentModel = () => { |
| 43 | + if (editorRef.current && originalDocumentModelRef.current && modifiedDocumentModelRef.current) { |
| 44 | + editorRef.current.compare(originalDocumentModelRef.current, modifiedDocumentModelRef.current) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + React.useEffect(() => { |
| 49 | + if (containerRef.current) { |
| 50 | + editorRef.current?.dispose(); |
| 51 | + editorRef.current = editorCollectionService.createDiffEditor(containerRef.current, { |
| 52 | + automaticLayout: true, |
| 53 | + ...editorOptions, |
| 54 | + }); |
| 55 | + onEditorCreateMemorizeFn(editorRef.current) |
| 56 | + openDocumentModel() |
| 57 | + } |
| 58 | + return () => { |
| 59 | + unmountRef.current = true; |
| 60 | + editorRef.current?.dispose() |
| 61 | + originalDocumentModelRef.current?.dispose() |
| 62 | + modifiedDocumentModelRef.current?.dispose() |
| 63 | + }; |
| 64 | + }, []); |
| 65 | + |
| 66 | + useEffect(() => { |
| 67 | + const createModelReference = ( |
| 68 | + fetchingUriRef: MutableRefObject<string>, |
| 69 | + uriStr: string, |
| 70 | + documentModelRef: MutableRefObject<IEditorDocumentModelRef | undefined> |
| 71 | + ) => { |
| 72 | + if (fetchingUriRef.current !== uriStr) { |
| 73 | + fetchingUriRef.current = uriStr |
| 74 | + documentService.createModelReference(new URI(uriStr), 'diff-editor-react-component').then((ref) => { |
| 75 | + if (documentModelRef.current) { |
| 76 | + documentModelRef.current.dispose(); |
| 77 | + } |
| 78 | + if (!unmountRef.current && ref.instance.uri.toString() === uriStr) { |
| 79 | + documentModelRef.current = ref; |
| 80 | + openDocumentModel() |
| 81 | + } else { |
| 82 | + ref.dispose(); |
| 83 | + } |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + createModelReference(originalFetchingUriRef, originalUriStr, originalDocumentModelRef) |
| 88 | + createModelReference(modifiedFetchingUriRef, modifiedUriStr, modifiedDocumentModelRef) |
| 89 | + }, [originalUriStr, modifiedUriStr]) |
| 90 | + |
| 91 | + return <div ref={containerRef} {...props}></div>; |
| 92 | +}; |
| 93 | + |
| 94 | +export const DiffEditor = (props: ICodeEditorProps) => { |
| 95 | + const appContext = useContext(AppContext) |
| 96 | + if (!appContext.app) return null |
| 97 | + return ( |
| 98 | + <ConfigProvider value={appContext.app.config}> |
| 99 | + <DiffEditorComponent {...props} /> |
| 100 | + </ConfigProvider> |
| 101 | + ) |
| 102 | +}; |
0 commit comments