-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support merge editor minimap (#2859)
* feat: support merge editor minimap * fix: improve code
- Loading branch information
Showing
5 changed files
with
151 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/monaco/src/browser/contrib/merge-editor/view/mini-map.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import cls from 'classnames'; | ||
import React, { useCallback, useEffect } from 'react'; | ||
|
||
import { Disposable, Event, useInjectable } from '@opensumi/ide-core-browser'; | ||
import { EditorOption } from '@opensumi/monaco-editor-core/esm/vs/editor/common/config/editorOptions'; | ||
|
||
import { MergeEditorService } from '../merge-editor.service'; | ||
import { LineRange } from '../model/line-range'; | ||
import { EditorViewType } from '../types'; | ||
|
||
import { BaseCodeEditor } from './editors/baseCodeEditor'; | ||
import styles from './merge-editor.module.less'; | ||
|
||
interface BlockPiece { | ||
top: string; | ||
height: string; | ||
className: string; | ||
} | ||
|
||
export const MiniMap: React.FC<{ contrastType: EditorViewType }> = ({ contrastType }) => { | ||
const mergeEditorService = useInjectable<MergeEditorService>(MergeEditorService); | ||
const [blocks, setBlocks] = React.useState<BlockPiece[]>([]); | ||
|
||
useEffect(() => { | ||
const disposables = new Disposable(); | ||
|
||
disposables.addDispose( | ||
mergeEditorService.onDidMount((data) => { | ||
const { currentView, incomingView } = data; | ||
|
||
if (contrastType === EditorViewType.CURRENT) { | ||
disposables.addDispose( | ||
Event.debounce( | ||
currentView.onDidChangeDecorations, | ||
() => {}, | ||
1, | ||
)(() => { | ||
computePiece(currentView, currentView.documentMapping.getOriginalRange()); | ||
}), | ||
); | ||
} else if (contrastType === EditorViewType.INCOMING) { | ||
disposables.addDispose( | ||
Event.debounce( | ||
incomingView.onDidChangeDecorations, | ||
() => {}, | ||
1, | ||
)(() => { | ||
computePiece(incomingView, incomingView.documentMapping.getModifiedRange()); | ||
}), | ||
); | ||
} | ||
}), | ||
); | ||
|
||
return () => disposables.dispose(); | ||
}, [mergeEditorService, contrastType]); | ||
|
||
const computePiece = useCallback((viewEditor: BaseCodeEditor, ranges: LineRange[]) => { | ||
const editor = viewEditor.getEditor(); | ||
|
||
const lineHeight = editor.getOption(EditorOption.lineHeight); | ||
const contentHeight = editor.getContentHeight(); | ||
const blocks: BlockPiece[] = []; | ||
|
||
ranges.forEach((range) => { | ||
if (range.isComplete) { | ||
return; | ||
} | ||
|
||
blocks.push({ | ||
top: (((range.startLineNumber - 1) * lineHeight) / contentHeight) * 100 + '%', | ||
height: ((range.length * lineHeight) / contentHeight) * 100 + '%', | ||
className: styles[range.type], | ||
}); | ||
}); | ||
|
||
setBlocks(blocks); | ||
}, []); | ||
|
||
return ( | ||
<div className={styles.minimap_content}> | ||
{blocks.map((block) => ( | ||
<span className={cls(styles.block, block.className)} style={{ top: block.top, height: block.height }}></span> | ||
))} | ||
</div> | ||
); | ||
}; |