Skip to content

Commit abe5ae3

Browse files
committed
feat: CodeMirror - add revert controls in diff view
1 parent a43f74d commit abe5ae3

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

src/Assets/IconV2/ic-arrow-right.svg

Lines changed: 3 additions & 0 deletions
Loading

src/Common/CodeMirror/CodeEditorRenderer.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { Progressing } from '@Common/Progressing'
2121

2222
import { FocusEventHandler, useEffect, useRef, useState } from 'react'
2323
import { CodeEditorRendererProps } from './types'
24-
import { getCodeEditorHeight } from './utils'
24+
import { getCodeEditorHeight, getRevertControlButton } from './utils'
2525

2626
export const CodeEditorRenderer = ({
2727
loading,
@@ -114,9 +114,11 @@ export const CodeEditorRenderer = ({
114114
<CodeMirrorMerge
115115
theme={codeEditorTheme}
116116
key={codemirrorMergeKey}
117-
className={`w-100 vertical-divider ${isDarkTheme ? 'cm-merge-theme__dark' : 'cm-merge-theme__light'} ${codeEditorParentClassName}`}
117+
className={`w-100 ${isDarkTheme ? 'cm-merge-theme__dark' : 'cm-merge-theme__light'} ${codeEditorParentClassName}`}
118118
gutter
119119
destroyRerender={false}
120+
revertControls="a-to-b"
121+
renderRevertControl={getRevertControlButton}
120122
>
121123
<CodeMirrorMerge.Original
122124
basicSetup={false}

src/Common/CodeMirror/codeEditor.scss

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@
2020
background-color: var(--bg-code-editor-base);
2121
}
2222

23+
.cm-mergeViewEditor:first-child {
24+
border-right: 1px solid var(--N200);
25+
}
26+
2327
.cm-mergeViewEditor:last-child {
2428
border-left: 1px solid var(--N200);
2529
}
@@ -28,6 +32,15 @@
2832
min-height: inherit;
2933
}
3034

35+
.cm-merge-revert {
36+
background-color: var(--bg-primary);
37+
38+
button {
39+
margin-top: 4px;
40+
padding: 0;
41+
}
42+
}
43+
3144
.cm-editor {
3245
&.cm-focused {
3346
outline: none;
@@ -45,10 +58,6 @@
4558
}
4659

4760
&.cm-merge-a .cm-changedLineGutter {
48-
position: absolute;
49-
width: 100%;
50-
left: 0;
51-
z-index: -1;
5261
background: var(--bg-code-editor-red-gutter);
5362
}
5463

@@ -62,10 +71,6 @@
6271
}
6372

6473
&.cm-merge-b .cm-changedLineGutter {
65-
position: absolute;
66-
width: 100%;
67-
left: 0;
68-
z-index: -1;
6974
background: var(--bg-code-editor-green-gutter);
7075
}
7176

@@ -147,6 +152,12 @@
147152
}
148153
}
149154

155+
.cm-changeGutter {
156+
position: absolute;
157+
width: 100%;
158+
z-index: -1;
159+
}
160+
150161
.cm-foldGutter .cm-gutterElement:not(:first-child) span:not(.is-closed) {
151162
visibility: hidden;
152163
}

src/Common/CodeMirror/utils.tsx

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { render } from 'react-dom'
1718
import { renderToString } from 'react-dom/server'
1819
import DOMPurify from 'dompurify'
1920
import { linter } from '@codemirror/lint'
@@ -36,6 +37,10 @@ import {
3637
stateExtensions,
3738
} from 'codemirror-json-schema'
3839
import { yamlCompletion, yamlSchemaHover, yamlSchemaLinter } from 'codemirror-json-schema/yaml'
40+
41+
import { Icon } from '@Shared/Components'
42+
import { Tooltip } from '@Common/Tooltip'
43+
3944
import { yamlParseLinter } from './Extensions'
4045
import { CodeEditorProps, FindReplaceToggleButtonProps, GetCodeEditorHeightReturnType, HoverTexts } from './types'
4146

@@ -156,6 +161,21 @@ export const getReadOnlyElement = () => {
156161
return dom
157162
}
158163

164+
export const getRevertControlButton = () => {
165+
const dom = document.createElement('button')
166+
167+
render(
168+
<Tooltip content="Revert this chunk" alwaysShowTippyOnHover>
169+
<div className="flex">
170+
<Icon name="ic-arrow-right" color="N600" size={20} />
171+
</div>
172+
</Tooltip>,
173+
dom,
174+
)
175+
176+
return dom
177+
}
178+
159179
// EXTENSION UTILS
160180
export const getLanguageExtension = (mode: CodeEditorProps['mode']): Extension => {
161181
switch (mode) {

src/Shared/Components/Icon/Icon.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// NOTE: This file is auto-generated. Do not edit directly. Run the script `npm run generate-icon` to update.
22

33
import { ReactComponent as ICAborted } from '@IconsV2/ic-aborted.svg'
4+
import { ReactComponent as ICArrowRight } from '@IconsV2/ic-arrow-right.svg'
45
import { ReactComponent as ICAzure } from '@IconsV2/ic-azure.svg'
56
import { ReactComponent as ICBitbucket } from '@IconsV2/ic-bitbucket.svg'
67
import { ReactComponent as ICBuildColor } from '@IconsV2/ic-build-color.svg'
@@ -49,6 +50,7 @@ import { IconBaseProps } from './types'
4950

5051
export const iconMap = {
5152
'ic-aborted': ICAborted,
53+
'ic-arrow-right': ICArrowRight,
5254
'ic-azure': ICAzure,
5355
'ic-bitbucket': ICBitbucket,
5456
'ic-build-color': ICBuildColor,

0 commit comments

Comments
 (0)