forked from processing/p5.js-web-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodemirror.js
More file actions
234 lines (212 loc) · 6.6 KB
/
codemirror.js
File metadata and controls
234 lines (212 loc) · 6.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import { useRef, useEffect } from 'react';
import { EditorView, lineNumbers as lineNumbersExt } from '@codemirror/view';
import { autocompletion, closeBrackets } from '@codemirror/autocomplete';
import { debounce } from 'lodash';
import { openSearchPanel } from '@codemirror/search';
import { saveLocalBackup } from '../../utils/localBackup';
import {
getFileMode,
createNewFileState,
updateFileStates,
createAutocompleteOptions
} from './stateUtils';
import { useEffectWithComparison } from '../../hooks/custom-hooks';
import { tidyCodeWithPrettier } from './tidier';
// ----- GENERAL TODOS (in order of priority) -----
// - revisit keymap differences, esp around sublime
// - emmet doesn't trigger if text is copy pasted in
// - need to re-implement emmet auto rename tag
// - color picker should be triggered by metakey cmd k
// - clike addon
/** This is a custom React hook that manages CodeMirror state. */
export default function useCodeMirror({
project,
lineNumbers,
linewrap,
autocloseBracketsQuotes,
setUnsavedChanges,
setCurrentLine,
updateFileContent,
file,
files,
autorefresh,
clearConsole,
startSketch,
autocompleteHinter,
fontSize,
onUpdateLinting,
referenceBaseUrl
}) {
// The codemirror instance.
const cmView = useRef();
// The current codemirror files.
const fileStates = useRef();
// We have to create a ref for the file ID, or else the debouncer
// will old onto an old version of the fileId and just overrwrite the initial file.
const fileId = useRef();
fileId.current = file.id;
// When the file changes, update the file content and save status.
function onChange() {
setUnsavedChanges(true);
updateFileContent(fileId.current, cmView.current.state.doc.toString());
// Save a local backup to localStorage for crash recovery (#3891).
// This ensures work is recoverable even if the tab crashes
// (e.g. from an infinite loop) before the server autosave fires.
const projectId = project?.id || 'unsaved';
saveLocalBackup(projectId, files);
if (autorefresh) {
clearConsole();
startSketch();
}
}
// Call onChange at most once every second.
const debouncedOnChange = debounce(onChange, 1000);
// This is called when the CM view updates.
function onViewUpdate(updateView) {
const { state } = updateView;
// TODO - check if need to subtract one
setCurrentLine(state.doc.lineAt(state.selection.main.head).number);
if (updateView.docChanged) {
debouncedOnChange();
}
}
// When the container component enters the DOM, we want this function
// to be called so we can setup the CodeMirror instance with the container.
function setupCodeMirrorOnContainerMounted(container) {
cmView.current = new EditorView({
parent: container
});
}
// When the component unmounts, we want to clean up the CodeMirror instance.
function teardownCodeMirror() {
if (cmView.current) {
cmView.current.destroy();
cmView.current = null;
}
}
// When settings change, we pass those changes into CodeMirror.
useEffect(() => {
const reconfigureEffect = (fileState) =>
fileState.fontSizeCpt.reconfigure(
EditorView.theme({ '&': { fontSize: `${fontSize}px` } })
);
updateFileStates({
fileStates: fileStates.current,
cmView: cmView.current,
file,
reconfigureEffect
});
}, [fontSize]);
useEffect(() => {
const reconfigureEffect = (fileState) =>
fileState.lineWrappingCpt.reconfigure(
linewrap ? EditorView.lineWrapping : []
);
updateFileStates({
fileStates: fileStates.current,
cmView: cmView.current,
file,
reconfigureEffect
});
}, [linewrap]);
useEffect(() => {
const reconfigureEffect = (fileState) =>
fileState.lineNumbersCpt.reconfigure(lineNumbers ? lineNumbersExt() : []);
updateFileStates({
fileStates: fileStates.current,
cmView: cmView.current,
file,
reconfigureEffect
});
}, [lineNumbers]);
useEffect(() => {
const reconfigureEffect = (fileState) =>
fileState.closeBracketsCpt.reconfigure(
autocloseBracketsQuotes ? closeBrackets() : []
);
updateFileStates({
fileStates: fileStates.current,
cmView: cmView.current,
file,
reconfigureEffect
});
}, [autocloseBracketsQuotes]);
useEffect(() => {
const reconfigureEffect = (fileState) =>
fileState.autocompleteCpt.reconfigure(
autocompleteHinter
? autocompletion(createAutocompleteOptions(referenceBaseUrl))
: []
);
updateFileStates({
fileStates: fileStates.current,
cmView: cmView.current,
file,
reconfigureEffect
});
}, [autocompleteHinter, referenceBaseUrl]);
// Initializes the files as CodeMirror states.
function initializeDocuments() {
if (!fileStates.current) {
fileStates.current = {};
}
files.forEach((currentFile) => {
if (
currentFile.name !== 'root' &&
!(currentFile.id in fileStates.current)
) {
fileStates.current[currentFile.id] = createNewFileState(
currentFile.name,
currentFile.content,
{
linewrap,
lineNumbers,
autocloseBracketsQuotes,
autocomplete: autocompleteHinter,
onUpdateLinting,
onViewUpdate,
referenceBaseUrl,
fontSize
}
);
}
});
}
// When the files change, reinitialize the documents.
useEffect(initializeDocuments, [files]);
// When the file changes, make the CodeMirror call to swap out the document.
useEffectWithComparison(
(_, prevProps) => {
// We need to save the previous CodeMirror state so we can restore it
// when we switch back to it.
const previousState = cmView.current.state;
if (Array.isArray(prevProps) && prevProps.length > 0 && previousState) {
const prevId = prevProps[0];
fileStates.current[prevId].cmState = previousState;
}
const { cmState } = fileStates.current[file.id];
cmView.current.setState(cmState);
},
[file.id]
);
const getContent = () => {
const content = cmView.current.state.doc.toString();
const updatedFile = Object.assign({}, file, { content });
return updatedFile;
};
const showSearch = () => {
openSearchPanel(cmView.current);
};
const tidyCode = () => {
const fileMode = getFileMode(file.name);
tidyCodeWithPrettier(cmView.current, fileMode);
};
return {
setupCodeMirrorOnContainerMounted,
teardownCodeMirror,
getContent,
tidyCode,
showSearch,
codemirrorView: cmView
};
}