1
1
import PropTypes from 'prop-types' ;
2
2
import React , { useCallback , useEffect , useRef , useState } from 'react' ;
3
- import CodeMirror from 'codemirror' ;
4
3
import { withTranslation } from 'react-i18next' ;
5
4
import StackTrace from 'stacktrace-js' ;
6
5
@@ -36,7 +35,6 @@ import { FolderIcon } from '../../../../common/icons';
36
35
import IconButton from '../../../../common/IconButton' ;
37
36
38
37
import { hideHinter } from './hinter' ;
39
- import getFileMode from './utils' ;
40
38
import tidyCode from './tidier' ;
41
39
import useCodeMirror from './codemirror' ;
42
40
import { useEffectWithComparison } from '../../hooks/custom-hooks' ;
@@ -50,7 +48,6 @@ function Editor({
50
48
lineNumbers,
51
49
closeProjectOptions,
52
50
setSelectedFile,
53
- unsavedChanges,
54
51
setUnsavedChanges,
55
52
lintMessages,
56
53
lintWarning,
@@ -75,7 +72,6 @@ function Editor({
75
72
} ) {
76
73
const [ currentLine , setCurrentLine ] = useState ( 1 ) ;
77
74
const beep = useRef ( ) ;
78
- const docs = useRef ( ) ;
79
75
80
76
const updateLintingMessageAccessibility = debounce ( ( annotations ) => {
81
77
clearLintMessage ( ) ;
@@ -89,6 +85,8 @@ function Editor({
89
85
}
90
86
} , 2000 ) ;
91
87
88
+ // The useCodeMirror hook manages CodeMirror state and returns
89
+ // a reference to the actual CM instance.
92
90
const {
93
91
setupCodeMirrorOnContainerMounted,
94
92
teardownCodeMirror,
@@ -105,6 +103,7 @@ function Editor({
105
103
hideRuntimeErrorWarning,
106
104
updateFileContent,
107
105
file,
106
+ files,
108
107
autorefresh,
109
108
isPlaying,
110
109
clearConsole,
@@ -124,22 +123,11 @@ function Editor({
124
123
getContent
125
124
} ) ;
126
125
127
- const initializeDocuments = ( ) => {
128
- docs . current = { } ;
129
- files . forEach ( ( currentFile ) => {
130
- if ( currentFile . name !== 'root' ) {
131
- docs . current [ currentFile . id ] = CodeMirror . Doc (
132
- currentFile . content ,
133
- getFileMode ( currentFile . name )
134
- ) ;
135
- }
136
- } ) ;
137
- } ;
138
-
139
- // Component did mount
126
+ // When the CM container div mounts, we set up CodeMirror.
140
127
const onContainerMounted = useCallback ( setupCodeMirrorOnContainerMounted , [ ] ) ;
141
128
142
- // Component did mount
129
+ // This is acting as a "componentDidMount" call where it runs once
130
+ // at the start and never again. It also provides a cleanup function.
143
131
useEffect ( ( ) => {
144
132
beep . current = new Audio ( beepUrl ) ;
145
133
@@ -156,43 +144,6 @@ function Editor({
156
144
} ;
157
145
} , [ ] ) ;
158
146
159
- useEffect ( ( ) => {
160
- initializeDocuments ( ) ;
161
- } , [ files ] ) ;
162
-
163
- useEffectWithComparison (
164
- ( _ , prevProps ) => {
165
- const fileMode = getFileMode ( file . name ) ;
166
- if ( fileMode === 'javascript' ) {
167
- // Define the new Emmet configuration based on the file mode
168
- const emmetConfig = {
169
- preview : [ 'html' ] ,
170
- markTagPairs : false ,
171
- autoRenameTags : true
172
- } ;
173
- cmInstance . current . setOption ( 'emmet' , emmetConfig ) ;
174
- }
175
- const oldDoc = cmInstance . current . swapDoc ( docs . current [ file . id ] ) ;
176
- if ( prevProps ?. file ) {
177
- docs . current [ prevProps . file . id ] = oldDoc ;
178
- }
179
- cmInstance . current . focus ( ) ;
180
-
181
- if ( ! prevProps ?. unsavedChanges ) {
182
- setTimeout ( ( ) => setUnsavedChanges ( false ) , 400 ) ;
183
- }
184
-
185
- for ( let i = 0 ; i < cmInstance . current . lineCount ( ) ; i += 1 ) {
186
- cmInstance . current . removeLineClass (
187
- i ,
188
- 'background' ,
189
- 'line-runtime-error'
190
- ) ;
191
- }
192
- } ,
193
- [ file . id ]
194
- ) ;
195
-
196
147
useEffect ( ( ) => {
197
148
// close the hinter window once the preference is turned off
198
149
if ( ! autocompleteHinter ) hideHinter ( cmInstance . current ) ;
@@ -358,7 +309,6 @@ Editor.propTypes = {
358
309
autorefresh : PropTypes . bool . isRequired ,
359
310
isPlaying : PropTypes . bool . isRequired ,
360
311
theme : PropTypes . string . isRequired ,
361
- unsavedChanges : PropTypes . bool . isRequired ,
362
312
files : PropTypes . arrayOf (
363
313
PropTypes . shape ( {
364
314
id : PropTypes . string . isRequired ,
0 commit comments