Skip to content

Commit 52cc15b

Browse files
committed
feat: Codemirror - enhance diffMinimap styles and functionality
1 parent 3d6bcc9 commit 52cc15b

File tree

5 files changed

+58
-33
lines changed

5 files changed

+58
-33
lines changed

src/Common/CodeMirror/CodeEditor.constants.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ export const REPLACE_ALL_SHORTCUT_KEYS: SupportedKeyboardKeysType[] = [
2828
export const CLOSE_SEARCH_SHORTCUT_KEYS: SupportedKeyboardKeysType[] = ['Escape']
2929

3030
export const READ_ONLY_TOOLTIP_TIMEOUT = 2000
31+
32+
export const CODE_EDITOR_FONT_SIZE = 15
33+
34+
export const CODE_EDITOR_LINE_HEIGHT = 15

src/Common/CodeMirror/CodeEditor.theme.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import { EditorView } from '@uiw/react-codemirror'
1818
import { githubDarkInit, githubLightInit } from '@uiw/codemirror-theme-github'
1919
import { tags } from '@lezer/highlight'
2020

21+
import { CODE_EDITOR_FONT_SIZE } from './CodeEditor.constants'
22+
2123
export const getCodeEditorTheme = (isDark: boolean) => {
2224
const themeInit = isDark ? githubDarkInit : githubLightInit
2325
const styles = isDark
@@ -71,7 +73,7 @@ export const getCodeEditorTheme = (isDark: boolean) => {
7173
}),
7274
codeEditorTheme: themeInit({
7375
settings: {
74-
fontSize: '15px',
76+
fontSize: `${CODE_EDITOR_FONT_SIZE}px`,
7577
fontFamily: 'Inconsolata, monospace',
7678
background: 'var(--bg-code-editor-base)',
7779
foreground: 'var(--fg-code-editor)',

src/Common/CodeMirror/CodeEditorRenderer.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export const CodeEditorRenderer = ({
3232
height,
3333
state,
3434
codeEditorTheme,
35-
codemirrorMergeKey,
3635
readOnly,
3736
isOriginalModifiable,
3837
handleLhsOnChange,
@@ -122,7 +121,6 @@ export const CodeEditorRenderer = ({
122121
<CodeMirrorMerge
123122
ref={codeMirrorMergeRef}
124123
theme={codeEditorTheme}
125-
key={codemirrorMergeKey}
126124
className={`flex-grow-1 h-100 dc__overflow-hidden ${componentSpecificThemeClass} ${readOnly ? 'code-editor__read-only' : ''}`}
127125
gutter
128126
destroyRerender={false}

src/Common/CodeMirror/Extensions/diffMinimap.tsx

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,35 @@
11
import { useEffect, useMemo, useRef, useState } from 'react'
2-
import CodeMirrorMerge from 'react-codemirror-merge'
2+
import { EditorView } from '@uiw/react-codemirror'
3+
import CodeMirrorMerge, { CodeMirrorMergeRef } from 'react-codemirror-merge'
34

45
import { getComponentSpecificThemeClass } from '@Shared/Providers'
56

67
import { DiffMinimapProps } from '../types'
8+
import { CODE_EDITOR_FONT_SIZE, CODE_EDITOR_LINE_HEIGHT } from '../CodeEditor.constants'
79

810
export const DiffMinimap = ({ view, state, diffMinimapExtensions, codeEditorTheme, theme }: DiffMinimapProps) => {
9-
const minimapContainerRef = useRef<HTMLDivElement>(null)
10-
const overlayRef = useRef<HTMLDivElement>(null)
11-
const [overlayHeight, setOverlayHeight] = useState<number>(50)
11+
// STATES
1212
const [overlayTop, setOverlayTop] = useState<number>(0)
13+
const [overlayHeight, setOverlayHeight] = useState<number>(50)
1314
const [isDragging, setIsDragging] = useState<boolean>(false)
15+
16+
// REFS
17+
const minimapContainerRef = useRef<HTMLDivElement>(null)
18+
const minimapEditorRef = useRef<CodeMirrorMergeRef>(null)
19+
const overlayRef = useRef<HTMLDivElement>(null)
1420
const dragStartY = useRef<number>(0)
1521
const startScrollTop = useRef<number>(0)
1622

23+
// CONSTANTS
1724
const componentSpecificThemeClass = getComponentSpecificThemeClass(theme)
1825

19-
const { scaleFactor, diffMinimapHeight } = useMemo(() => {
26+
const scalingFactor = useMemo(() => {
2027
if (view?.dom) {
21-
return {
22-
scaleFactor: view.dom.parentElement.parentElement.clientHeight / view.dom.scrollHeight,
23-
diffMinimapHeight: view.dom.scrollHeight,
24-
}
28+
return view.dom.clientHeight / view.dom.scrollHeight
2529
}
2630

27-
return { scaleFactor: 0, diffMinimapHeight: 0 }
28-
}, [view])
31+
return 1
32+
}, [view?.a.state.doc.length, view?.b.state.doc.length])
2933

3034
// Update the overlay position and size
3135
const updateOverlay = () => {
@@ -45,7 +49,7 @@ export const DiffMinimap = ({ view, state, diffMinimapExtensions, codeEditorThem
4549

4650
useEffect(() => {
4751
updateOverlay()
48-
}, [scaleFactor])
52+
}, [scalingFactor])
4953

5054
// Sync overlay scrolling with the diff view
5155
const handleDiffScroll = () => {
@@ -128,48 +132,48 @@ export const DiffMinimap = ({ view, state, diffMinimapExtensions, codeEditorThem
128132
view.dom.scrollTo({ top: scrollRatio * (scrollHeight - clientHeight) })
129133
}
130134

135+
const minimapTheme = EditorView.theme({
136+
'&.cm-editor': {
137+
fontSize: `${scalingFactor * CODE_EDITOR_FONT_SIZE}px`,
138+
lineHeight: `${scalingFactor * CODE_EDITOR_LINE_HEIGHT}`,
139+
},
140+
})
141+
131142
return (
132-
<div ref={minimapContainerRef} className="dc__position-rel">
143+
<div
144+
ref={minimapContainerRef}
145+
className={`code-editor__minimap-container dc__position-rel dc__no-shrink cursor ${componentSpecificThemeClass}`}
146+
onClick={handleMinimapClick}
147+
>
133148
<CodeMirrorMerge
134-
key={scaleFactor}
149+
key={scalingFactor}
150+
ref={minimapEditorRef}
135151
theme={codeEditorTheme}
136-
className={`code-editor__mini-map dc__overflow-hidden ${componentSpecificThemeClass}`}
152+
className="code-editor__mini-map dc__overflow-hidden w-100"
137153
gutter={false}
138154
destroyRerender={false}
139-
style={{
140-
maxWidth: '30px',
141-
transform: `scale(1, ${scaleFactor})`,
142-
height: `${diffMinimapHeight}px`,
143-
transformOrigin: 'top left',
144-
}}
145155
>
146156
<CodeMirrorMerge.Original
147157
basicSetup={false}
148158
value={state.lhsCode}
149159
readOnly
150160
editable={false}
151-
extensions={diffMinimapExtensions}
161+
extensions={[...diffMinimapExtensions, minimapTheme]}
152162
/>
153163
<CodeMirrorMerge.Modified
154164
basicSetup={false}
155165
value={state.code}
156166
readOnly
157167
editable={false}
158-
extensions={diffMinimapExtensions}
168+
extensions={[...diffMinimapExtensions, minimapTheme]}
159169
/>
160170
</CodeMirrorMerge>
161-
<div
162-
className="dc__position-abs dc__top-0 dc__left-0 dc__right-0 dc__bottom-0 dc__zi-1 cursor"
163-
onClick={handleMinimapClick}
164-
/>
165171
<div
166172
ref={overlayRef}
167-
className="dc__position-abs dc__left-0 w-100 br-4 dc__zi-2"
173+
className="code-editor__minimap-overlay dc__position-abs dc__left-0 w-100 dc__zi-1"
168174
style={{
169175
top: `${overlayTop}px`,
170176
height: `${overlayHeight}px`,
171-
backgroundColor: 'rgba(0, 0, 0, 0.3)',
172-
cursor: 'grab',
173177
}}
174178
onMouseDown={handleOverlayMouseDown}
175179
/>

src/Common/CodeMirror/codeEditor.scss

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@
1818
.cm-mergeView {
1919
height: 100%;
2020
background-color: var(--bg-code-editor-base);
21+
scrollbar-width: none;
22+
&::-webkit-scrollbar {
23+
display: none;
24+
}
2125
}
2226

2327
.cm-merge-theme:not(.code-editor__read-only):not(.code-editor__mini-map) {
@@ -221,6 +225,9 @@
221225

222226
// MINIMAP STYLES
223227
.code-editor__mini-map {
228+
user-select: none;
229+
pointer-events: none;
230+
224231
.cm-editor {
225232
&.cm-merge-a .cm-changedLine,
226233
.cm-deletedChunk,
@@ -376,4 +383,14 @@
376383
&__read-only-tooltip {
377384
background-color: var(--bg-tooltip-black);
378385
}
386+
387+
&__minimap-container {
388+
width: 30px;
389+
background-color: var(--bg-code-editor-base);
390+
}
391+
392+
&__minimap-overlay {
393+
background-color: var(--bg-code-editor-diffmap-scrollbar);
394+
cursor: grab;
395+
}
379396
}

0 commit comments

Comments
 (0)