Skip to content

Commit 1c70347

Browse files
committed
feat: CodeEditor
- Header - redesign header to have consistent height - DiffMiniMap - color tokens update, fixed issue of not showing until editor is modified - DiffView - Sync horizontal scroll of the editors
1 parent 52cc15b commit 1c70347

File tree

6 files changed

+73
-31
lines changed

6 files changed

+73
-31
lines changed

src/Common/CodeEditor/CodeEditor.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -391,16 +391,18 @@ const Header: React.FC<CodeEditorHeaderInterface> & CodeEditorHeaderComposition
391391
children,
392392
className,
393393
hideDefaultSplitHeader,
394-
diffViewWidth,
395394
}) => {
396-
const { defaultValue } = useCodeEditorContext()
395+
const { defaultValue, state } = useCodeEditorContext()
397396
return (
398-
<div
399-
className={className || 'code-editor__header flex right'}
400-
style={{ ...(diffViewWidth ? { width: 'calc(100% - 30px)' } : {}) }}
401-
>
402-
{children}
403-
{!hideDefaultSplitHeader && defaultValue && <SplitPane />}
397+
<div className="flexbox w-100 dc__border-bottom ">
398+
<div
399+
data-code-editor-header
400+
className={`code-editor__header flex-grow-1 bg__secondary ${className || ''} ${state.diffMode ? 'dc__grid-half vertical-divider' : ''}`}
401+
>
402+
{children}
403+
{!hideDefaultSplitHeader && defaultValue && <SplitPane />}
404+
</div>
405+
{state.diffMode ? <div className="bg__secondary px-15 dc__align-self-stretch" /> : null}
404406
</div>
405407
)
406408
}

src/Common/CodeEditor/codeEditor.scss

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,6 @@
1515
*/
1616

1717
.code-editor__header {
18-
height: 40px;
19-
background: var(--N100);
20-
padding: 0 16px;
21-
border-bottom: 1px solid var(--N200);
22-
2318
.radio-group {
2419
height: 24px;
2520
overflow: hidden;

src/Common/CodeEditor/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ export interface CodeEditorHeaderInterface {
8282
children?: any
8383
className?: string
8484
hideDefaultSplitHeader?: boolean
85-
diffViewWidth?: boolean
8685
}
8786
export interface CodeEditorComposition {
8887
Header?: React.FC<any>

src/Common/CodeMirror/CodeEditor.components.tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,15 @@ export const Header = ({ children, className, hideDefaultSplitHeader }: CodeEdit
4747
const { state, hasCodeEditorContainer } = useCodeEditorContext()
4848

4949
return (
50-
<div
51-
data-code-editor-header
52-
className={`${hasCodeEditorContainer ? 'dc__top-radius-4' : ''} ${className || 'code-editor__header flex right bcn-1 pt-10 pb-9 px-16 dc__border-bottom'}`}
53-
>
54-
{children}
55-
{!hideDefaultSplitHeader && state.lhsCode && <SplitPane />}
50+
<div className="flexbox w-100 dc__border-bottom ">
51+
<div
52+
data-code-editor-header
53+
className={`${hasCodeEditorContainer ? 'dc__top-radius-4' : ''} code-editor__header flex-grow-1 bg__secondary ${className || ''} ${state.diffMode ? 'dc__grid-half vertical-divider' : ''}`}
54+
>
55+
{children}
56+
{!hideDefaultSplitHeader && state.lhsCode && <SplitPane />}
57+
</div>
58+
{state.diffMode ? <div className="bg__secondary px-5 dc__align-self-stretch" /> : null}
5659
</div>
5760
)
5861
}

src/Common/CodeMirror/CodeEditorRenderer.tsx

Lines changed: 48 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import CodeMirror, { ReactCodeMirrorRef } from '@uiw/react-codemirror'
1919
import CodeMirrorMerge, { CodeMirrorMergeRef } from 'react-codemirror-merge'
2020

2121
import { getComponentSpecificThemeClass } from '@Shared/Providers'
22+
import { getUniqueId } from '@Shared/Helpers'
2223
import { Progressing } from '@Common/Progressing'
2324

2425
import { CodeEditorRendererProps } from './types'
@@ -50,6 +51,7 @@ export const CodeEditorRenderer = ({
5051
// STATES
5152
const [isFocused, setIsFocused] = useState(false)
5253
const [gutterWidth, setGutterWidth] = useState(0)
54+
const [codeEditorDiffViewKey, setCodeEditorDiffViewKey] = useState<string>('')
5355

5456
// REFS
5557
const codeMirrorRef = useRef<ReactCodeMirrorRef>()
@@ -90,6 +92,44 @@ export const CodeEditorRenderer = ({
9092
updateGutterWith()
9193
}, [state.lhsCode, state.code, isFocused])
9294

95+
// SYNCING LEFT RIGHT EDITOR HORIZONTAL SCROLLS
96+
const handleLHSScroll = () => {
97+
codeMirrorMergeRef.current.view.a.scrollDOM.scrollTo({
98+
left: codeMirrorMergeRef.current.view.b.scrollDOM.scrollLeft,
99+
})
100+
}
101+
102+
const handleRHSScroll = () => {
103+
codeMirrorMergeRef.current.view.b.scrollDOM.scrollTo({
104+
left: codeMirrorMergeRef.current.view.a.scrollDOM.scrollLeft,
105+
})
106+
}
107+
useEffect(() => {
108+
if (!loading) {
109+
// This timeout is added to ensure ref of diff editor is properly initialized and \
110+
// key state is set to trigger re-render of diffMinimap with diff editor latest ref.
111+
setTimeout(() => {
112+
setCodeEditorDiffViewKey(getUniqueId())
113+
114+
// SYNCING LEFT RIGHT EDITOR HORIZONTAL SCROLLS
115+
if (codeMirrorMergeRef.current?.view) {
116+
codeMirrorMergeRef.current.view.a.scrollDOM.addEventListener('scroll', handleRHSScroll)
117+
codeMirrorMergeRef.current.view.b.scrollDOM.addEventListener('scroll', handleLHSScroll)
118+
}
119+
}, 0)
120+
}
121+
122+
return () => {
123+
setCodeEditorDiffViewKey('')
124+
125+
// SYNCING LEFT RIGHT EDITOR HORIZONTAL SCROLLS
126+
if (codeMirrorMergeRef.current?.view) {
127+
codeMirrorMergeRef.current.view.b.scrollDOM.removeEventListener('scroll', handleLHSScroll)
128+
codeMirrorMergeRef.current.view.a.scrollDOM.removeEventListener('scroll', handleRHSScroll)
129+
}
130+
}
131+
}, [loading])
132+
93133
const onCreateEditor = () => {
94134
updateGutterWith()
95135
}
@@ -141,15 +181,14 @@ export const CodeEditorRenderer = ({
141181
extensions={modifiedViewExtensions}
142182
/>
143183
</CodeMirrorMerge>
144-
{codeMirrorMergeRef.current && (
145-
<DiffMinimap
146-
theme={theme}
147-
codeEditorTheme={codeEditorTheme}
148-
view={codeMirrorMergeRef.current.view}
149-
state={state}
150-
diffMinimapExtensions={diffMinimapExtensions}
151-
/>
152-
)}
184+
<DiffMinimap
185+
key={codeEditorDiffViewKey}
186+
theme={theme}
187+
codeEditorTheme={codeEditorTheme}
188+
view={codeMirrorMergeRef.current?.view}
189+
state={state}
190+
diffMinimapExtensions={diffMinimapExtensions}
191+
/>
153192
</div>
154193
) : (
155194
<div ref={codeMirrorParentDivRef} className={`w-100 ${codeEditorParentClassName}`}>

src/Common/CodeMirror/codeEditor.scss

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,16 +234,20 @@
234234
&.cm-merge-a .cm-changedText,
235235
.cm-deletedChunk .cm-deletedText,
236236
&.cm-merge-a .cm-changedLineGutter {
237-
background-color: var(--R200);
237+
background-color: var(--R300);
238238
}
239239

240240
&.cm-merge-b .cm-changedLine,
241241
&.cm-merge-b .cm-changedText,
242242
&.cm-merge-b .cm-changedLineGutter {
243-
background: var(--G200);
243+
background: var(--G300);
244244
}
245245
}
246246

247+
.cm-content {
248+
padding: 0;
249+
}
250+
247251
& .cm-line,
248252
& .cm-line span {
249253
color: transparent !important;

0 commit comments

Comments
 (0)