Skip to content

Commit 147f419

Browse files
committed
feat: CodeEditor
- disable browser back/forward when code editor is focused and has horizontal overflow - UAT changes
1 parent 17b7506 commit 147f419

File tree

5 files changed

+50
-11
lines changed

5 files changed

+50
-11
lines changed

src/Common/CodeEditor/CodeEditorRenderer.tsx

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import CodeMirror, { ReactCodeMirrorRef } from '@uiw/react-codemirror'
33

44
import { Progressing } from '@Common/Progressing'
55

6-
import { useEffect, useRef } from 'react'
6+
import { FocusEventHandler, useEffect, useRef, useState } from 'react'
77
import { CodeEditorRendererProps } from './types'
88
import { getCodeEditorHeight } from './utils'
99

@@ -29,6 +29,9 @@ export const CodeEditorRenderer = ({
2929
extensions,
3030
autoFocus,
3131
}: CodeEditorRendererProps) => {
32+
// STATES
33+
const [isFocused, setIsFocused] = useState(false)
34+
3235
// REFS
3336
const codeMirrorRef = useRef<ReactCodeMirrorRef>()
3437

@@ -41,6 +44,29 @@ export const CodeEditorRenderer = ({
4144
}, 100)
4245
}, [autoFocus])
4346

47+
// STOPPING OVERSCROLL BROWSER BACK/FORWARD BEHAVIOR WHEN CODE EDITOR IS FOCUSED
48+
useEffect(() => {
49+
const html = document.querySelector('html')
50+
if (html) {
51+
const { scrollWidth, clientWidth } = codeMirrorRef.current?.view?.scrollDOM ?? {}
52+
if (isFocused && scrollWidth > clientWidth) {
53+
html.classList.add('dc__overscroll-none')
54+
} else {
55+
html.classList.remove('dc__overscroll-none')
56+
}
57+
}
58+
}, [state.lhsCode, state.code, isFocused])
59+
60+
const handleOnFocus: FocusEventHandler<HTMLDivElement> = (e) => {
61+
setIsFocused(true)
62+
onFocus?.(e)
63+
}
64+
65+
const handleOnBlur: FocusEventHandler<HTMLDivElement> = (e) => {
66+
setIsFocused(false)
67+
onBlur?.(e)
68+
}
69+
4470
const { codeEditorClassName, codeEditorHeight, codeEditorParentClassName } = getCodeEditorHeight(height)
4571

4672
if (loading) {
@@ -79,7 +105,9 @@ export const CodeEditorRenderer = ({
79105
) : (
80106
<div ref={codeMirrorParentDivRef} className={`w-100 ${codeEditorParentClassName}`}>
81107
{shebang && (
82-
<div className="code-editor__shebang flexbox text__white">
108+
<div
109+
className={`flexbox text__white code-editor__shebang ${isDarkTheme ? 'code-editor__shebang__dark' : 'code-editor__shebang__light'}`}
110+
>
83111
<div className="code-editor__shebang__gutter dc__align-self-stretch" />
84112
{shebang}
85113
</div>
@@ -94,8 +122,8 @@ export const CodeEditorRenderer = ({
94122
readOnly={readOnly}
95123
height={codeEditorHeight}
96124
minHeight="250px"
97-
onFocus={onFocus}
98-
onBlur={onBlur}
125+
onFocus={handleOnFocus}
126+
onBlur={handleOnBlur}
99127
onChange={handleOnChange}
100128
extensions={extensions}
101129
/>

src/Common/CodeEditor/Extensions/yamlParseLinter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const yamlParseLinter =
2121
diagnostics.push({
2222
from,
2323
to: from + (linePos[0]?.col || 0),
24-
message,
24+
message: message.replace(/\n+/g, '\n'),
2525
severity: 'error',
2626
source: err.name,
2727
})

src/Common/CodeEditor/codeEditor.scss

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// CODE MIRROR OVERRIDES
1818
.cm-mergeView {
1919
height: 100%;
20+
background-color: var(--bg-code-editor-base);
2021
}
2122

2223
.cm-mergeViewEditor:last-child {

src/Common/CodeEditor/types.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export interface CodeEditorHeaderProps {
3636
children?: ReactNode
3737
}
3838

39-
type CodeEditorBaseProps = Pick<ReactCodeMirrorProps, 'onBlur' | 'onFocus' | 'autoFocus'> & {
39+
type CodeEditorBaseProps = Partial<Pick<ReactCodeMirrorProps, 'onBlur' | 'onFocus' | 'autoFocus'>> & {
4040
value?: ReactCodeMirrorProps['value']
4141
onChange?: (value: string) => void
4242
shebang?: string | JSX.Element
@@ -135,11 +135,21 @@ export interface FindReplaceToggleButtonProps {
135135
}
136136

137137
// CODE-EDITOR RENDERER PROPS
138-
export type CodeEditorRendererProps = Pick<
139-
CodeEditorProps,
140-
'loading' | 'customLoader' | 'height' | 'readOnly' | 'shebang' | 'placeholder' | 'onBlur' | 'onFocus' | 'autoFocus'
138+
export type CodeEditorRendererProps = Required<
139+
Pick<
140+
CodeEditorProps,
141+
| 'loading'
142+
| 'customLoader'
143+
| 'height'
144+
| 'readOnly'
145+
| 'shebang'
146+
| 'placeholder'
147+
| 'onBlur'
148+
| 'onFocus'
149+
| 'autoFocus'
150+
>
141151
> &
142-
Pick<CodeEditorDiffBaseProps, 'isOriginalModifiable'> & {
152+
Required<Pick<CodeEditorDiffBaseProps, 'isOriginalModifiable'>> & {
143153
codemirrorMergeKey: Key
144154
codeMirrorParentDivRef: MutableRefObject<HTMLDivElement>
145155
codeEditorTheme: ReactCodeMirrorProps['theme']

src/Common/Hooks/UseRegisterShortcut/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export const KEYBOARD_KEYS_MAP = {
2525
E: 'E',
2626
R: 'R',
2727
K: 'K',
28-
Escape: 'Escape',
28+
Escape: 'Esc',
2929
Enter: '↩',
3030
} as const
3131

0 commit comments

Comments
 (0)