14
14
* limitations under the License.
15
15
*/
16
16
17
- import { useEffect , useMemo , useReducer , useRef , useState } from 'react'
17
+ import { useEffect , useMemo , useRef , useState } from 'react'
18
18
import {
19
19
Extension ,
20
20
ReactCodeMirrorProps ,
@@ -31,21 +31,23 @@ import { indentationMarkers } from '@replit/codemirror-indentation-markers'
31
31
32
32
import { AppThemeType , useTheme } from '@Shared/Providers'
33
33
import { getUniqueId } from '@Shared/Helpers'
34
- import { cleanKubeManifest , useEffectAfterMount } from '@Common/Helper'
34
+ import { cleanKubeManifest } from '@Common/Helper'
35
35
import { DEFAULT_JSON_SCHEMA_URI , MODES } from '@Common/Constants'
36
36
37
37
import { codeEditorFindReplace , readOnlyTooltip , yamlHighlight } from './Extensions'
38
38
import { openSearchPanel , openSearchPanelWithReplace , replaceAll , showReplaceFieldState } from './Commands'
39
39
import { CodeEditorContextProps , CodeEditorProps } from './types'
40
- import { CodeEditorReducer , initialState , parseValueToCode } from './CodeEditor.reducer'
41
- import { getFoldGutterElement , getLanguageExtension , getValidationSchema } from './utils'
40
+ import { getFoldGutterElement , getLanguageExtension , getValidationSchema , parseValueToCode } from './utils'
42
41
import { CodeEditorContext } from './CodeEditor.context'
43
42
import { Clipboard , Container , ErrorBar , Header , Information , Warning } from './CodeEditor.components'
44
43
import { getCodeEditorTheme } from './CodeEditor.theme'
45
44
import { CodeEditorRenderer } from './CodeEditorRenderer'
46
45
47
46
import './codeEditor.scss'
48
47
48
+ // CODEMIRROR CLASSES
49
+ const foldingCompartment = new Compartment ( )
50
+
49
51
const CodeEditor = < DiffView extends boolean = false > ( {
50
52
theme,
51
53
value : propValue ,
@@ -78,16 +80,36 @@ const CodeEditor = <DiffView extends boolean = false>({
78
80
// HOOKS
79
81
const { appTheme } = useTheme ( )
80
82
83
+ // REFS
84
+ const codeMirrorParentDivRef = useRef < HTMLDivElement > ( )
85
+ const codeEditorValues = useRef ( { code : '' , lhsCode : '' } )
86
+
81
87
// MEMOISED VALUES
82
88
// Cleaning KubeManifest
83
89
const value = useMemo ( ( ) => {
84
90
const _value = diffView ? modifiedValue : propValue
85
- return cleanData ? cleanKubeManifest ( _value ) : _value
86
- } , [ propValue , modifiedValue , diffView ] )
87
- const lhsValue = useMemo ( ( ) => ( cleanData ? cleanKubeManifest ( originalValue ) : originalValue ) , [ originalValue ] )
91
+ const updatedValue = cleanData ? cleanKubeManifest ( _value ) : _value
88
92
89
- // REFS
90
- const codeMirrorParentDivRef = useRef < HTMLDivElement > ( )
93
+ if ( updatedValue !== codeEditorValues . current . code ) {
94
+ return ! [ MODES . JSON , MODES . YAML ] . includes ( mode ) || noParsing
95
+ ? updatedValue
96
+ : parseValueToCode ( updatedValue , mode , tabSize )
97
+ }
98
+
99
+ return codeEditorValues . current . code
100
+ } , [ propValue , modifiedValue , diffView , noParsing ] )
101
+
102
+ const lhsValue = useMemo ( ( ) => {
103
+ const updatedValue = cleanData ? cleanKubeManifest ( originalValue ) : originalValue
104
+
105
+ if ( updatedValue !== codeEditorValues . current . lhsCode ) {
106
+ return ! [ MODES . JSON , MODES . YAML ] . includes ( mode ) || noParsing
107
+ ? updatedValue
108
+ : parseValueToCode ( updatedValue , mode , tabSize )
109
+ }
110
+
111
+ return codeEditorValues . current . lhsCode
112
+ } , [ originalValue , noParsing ] )
91
113
92
114
// CONSTANTS
93
115
const componentTheme = theme ?? appTheme
@@ -97,24 +119,12 @@ const CodeEditor = <DiffView extends boolean = false>({
97
119
// STATES
98
120
const [ codemirrorMergeKey , setCodemirrorMergeKey ] = useState < string > ( )
99
121
const [ hasCodeEditorContainer , setHasCodeEditorContainer ] = useState ( false )
100
-
101
- // REDUCER
102
- const [ state , dispatch ] = useReducer (
103
- CodeEditorReducer ,
104
- initialState ( {
105
- mode,
106
- value,
107
- lhsValue,
108
- noParsing,
109
- tabSize,
110
- diffView,
111
- } ) ,
112
- )
122
+ const [ diffMode , setDiffMode ] = useState ( diffView )
113
123
114
124
// CONTEXT VALUE
115
125
const contextValue = useMemo < CodeEditorContextProps > (
116
- ( ) => ( { dispatch , state , height , hasCodeEditorContainer , theme : componentTheme } ) ,
117
- [ state , hasCodeEditorContainer , componentTheme ] ,
126
+ ( ) => ( { setDiffMode , diffMode , hasCodeEditorContainer , lhsValue , value , readOnly , theme : componentTheme } ) ,
127
+ [ diffMode , hasCodeEditorContainer , lhsValue , value , readOnly , componentTheme ] ,
118
128
)
119
129
120
130
// USE-EFFECTS
@@ -127,8 +137,8 @@ const CodeEditor = <DiffView extends boolean = false>({
127
137
} , [ ] )
128
138
129
139
useEffect ( ( ) => {
130
- // Toggle `state. diffMode` based on `diffView`
131
- dispatch ( { type : 'setDiff' , value : diffView } )
140
+ // Toggle `diffMode` based on `diffView`
141
+ setDiffMode ( diffView )
132
142
} , [ diffView ] )
133
143
134
144
// Re-mounting codemirror-merge is necessary because its extensions don't automatically update after being changed.
@@ -143,38 +153,20 @@ const CodeEditor = <DiffView extends boolean = false>({
143
153
144
154
// METHODS
145
155
const setCode = ( codeValue : string ) => {
146
- dispatch ( { type : 'setCode' , value : codeValue } )
147
- if ( state . diffMode ) {
156
+ codeEditorValues . current . code = codeValue
157
+ if ( diffMode ) {
148
158
onModifiedValueChange ?.( codeValue )
149
159
} else {
150
160
onChange ?.( codeValue )
151
161
}
152
162
}
153
163
154
164
const setLhsCode = ( codeValue : string ) => {
155
- dispatch ( { type : 'setLhsCode' , value : codeValue } )
165
+ codeEditorValues . current . lhsCode = codeValue
156
166
onOriginalValueChange ?.( codeValue )
157
167
}
158
168
159
- useEffectAfterMount ( ( ) => {
160
- if ( value === state . code ) {
161
- return
162
- }
163
-
164
- setCode ( noParsing ? value : parseValueToCode ( value , mode , tabSize ) )
165
- } , [ value , noParsing ] )
166
-
167
- useEffectAfterMount ( ( ) => {
168
- if ( lhsValue === state . lhsCode ) {
169
- return
170
- }
171
-
172
- setLhsCode ( noParsing ? lhsValue : parseValueToCode ( lhsValue , mode , tabSize ) )
173
- } , [ lhsValue , noParsing ] )
174
-
175
169
// CODEMIRROR PROPS
176
- const foldingCompartment = new Compartment ( )
177
-
178
170
const basicSetupOptions : BasicSetupOptions = {
179
171
defaultKeymap : false ,
180
172
searchKeymap : false ,
@@ -197,7 +189,7 @@ const CodeEditor = <DiffView extends boolean = false>({
197
189
markerDOM : getFoldGutterElement ,
198
190
} )
199
191
200
- const baseExtensions : Extension [ ] = [
192
+ const getBaseExtensions = ( ) : Extension [ ] => [
201
193
basicSetup ( basicSetupOptions ) ,
202
194
themeExtension ,
203
195
keymap . of ( [
@@ -218,14 +210,14 @@ const CodeEditor = <DiffView extends boolean = false>({
218
210
]
219
211
220
212
const extensions : Extension [ ] = [
221
- ...baseExtensions ,
222
- ...( ! state . diffMode ? getValidationSchema ( { mode, schemaURI, validatorSchema } ) : [ ] ) ,
213
+ ...getBaseExtensions ( ) ,
214
+ ...( ! diffMode ? getValidationSchema ( { mode, schemaURI, validatorSchema } ) : [ ] ) ,
223
215
readOnlyTooltip ,
224
216
]
225
217
226
- const originalViewExtensions : Extension [ ] = [ ...baseExtensions , readOnlyTooltip ]
218
+ const originalViewExtensions : Extension [ ] = [ ...getBaseExtensions ( ) , readOnlyTooltip ]
227
219
228
- const modifiedViewExtensions : Extension [ ] = [ ...baseExtensions , readOnlyTooltip ]
220
+ const modifiedViewExtensions : Extension [ ] = [ ...getBaseExtensions ( ) , readOnlyTooltip ]
229
221
230
222
const diffMinimapExtensions : Extension [ ] = [
231
223
basicSetup ( {
@@ -247,7 +239,6 @@ const CodeEditor = <DiffView extends boolean = false>({
247
239
codeMirrorParentDivRef = { codeMirrorParentDivRef }
248
240
codeEditorTheme = { codeEditorTheme }
249
241
theme = { componentTheme }
250
- state = { state }
251
242
loading = { loading }
252
243
customLoader = { customLoader }
253
244
height = { height }
0 commit comments