-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathindex.jsx
808 lines (733 loc) · 22.1 KB
/
index.jsx
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
import React, {
useEffect,
useRef,
useState,
useCallback,
useMemo
} from 'react';
import PropTypes from 'prop-types';
import { EditorState, StateEffect, Prec } from '@codemirror/state';
import {
EditorView,
keymap,
highlightActiveLine,
lineNumbers,
Decoration
} from '@codemirror/view';
import Fuse from 'fuse.js';
import { javascript } from '@codemirror/lang-javascript';
import { css } from '@codemirror/lang-css';
import {
bracketMatching,
foldGutter,
syntaxHighlighting,
codeFolding
} from '@codemirror/language';
import { autocompletion, closeBrackets } from '@codemirror/autocomplete';
import { linter, lintGutter } from '@codemirror/lint';
import { standardKeymap } from '@codemirror/commands';
import MediaQuery from 'react-responsive';
import prettier from 'prettier/standalone';
import babelParser from 'prettier/parser-babel';
import htmlParser from 'prettier/parser-html';
import cssParser from 'prettier/parser-postcss';
import { debounce } from 'lodash';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import { withTranslation } from 'react-i18next';
import { JSHINT } from 'jshint';
import { CSSLint } from 'csslint';
import { HTMLHint } from 'htmlhint';
import {
expandAbbreviation,
abbreviationTracker,
insert as emmetInsert,
resetAbbreviation
} from '@emmetio/codemirror6-plugin';
import {
findNext,
findPrevious,
highlightSelectionMatches
} from '@codemirror/search';
import classNames from 'classnames';
import StackTrace from 'stacktrace-js';
import beepUrl from '../../../../sounds/audioAlert.mp3';
import RightArrowIcon from '../../../../images/right-arrow.svg';
import LeftArrowIcon from '../../../../images/left-arrow.svg';
import { metaKey } from '../../../../utils/metaKey';
import * as hinter from '../../../../utils/p5-hinter';
import AssetPreview from '../AssetPreview';
import Timer from '../Timer';
import EditorAccessibility from '../EditorAccessibility';
import UnsavedChangesIndicator from '../UnsavedChangesIndicator';
import { getHTMLFile } from '../../reducers/files';
import { selectActiveFile } from '../../selectors/files';
import { EditorContainer, EditorHolder } from './MobileEditor';
import { FolderIcon } from '../../../../common/icons';
import IconButton from '../../../../common/IconButton';
import {
lightHighlightStyle,
p5LightTheme
} from '../../utils/p5-light-cm-theme';
import { darkHighlightStyle, p5DarkTheme } from '../../utils/p5-dark-cm-theme';
import {
contrastHighlightStyle,
p5ContrastTheme
} from '../../utils/p5-contrast-cm-theme';
import p5ViewPlugin from '../../utils/p5ViewPlugin';
import * as FileActions from '../../actions/files';
import * as IDEActions from '../../actions/ide';
import * as ProjectActions from '../../actions/project';
import * as EditorAccessibilityActions from '../../actions/editorAccessibility';
import * as PreferencesActions from '../../actions/preferences';
import * as UserActions from '../../../User/actions';
import * as ConsoleActions from '../../actions/console';
const INDENTATION_AMOUNT = 2;
window.JSHINT = JSHINT;
window.CSSLint = CSSLint;
window.HTMLHint = HTMLHint;
const getFileMode = (fileName) => {
if (fileName.match(/.+\.js$/i)) return 'javascript';
if (fileName.match(/.+\.css$/i)) return 'css';
if (fileName.match(/.+\.(html|xml)$/i)) return 'htmlmixed';
return 'text/plain';
};
const prettierFormatWithCursor = (view, parser, plugins) => {
try {
const { formatted, cursorOffset } = prettier.formatWithCursor(
view.state.doc.toString(),
{
cursorOffset: view.state.selection.main.head,
parser,
plugins
}
);
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: formatted },
selection: { anchor: cursorOffset }
});
view.focus();
} catch (error) {
console.error(error);
}
};
const getThemeByName = (themeName) => {
switch (themeName) {
case 'dark':
return p5DarkTheme;
case 'contrast':
return p5ContrastTheme;
case 'light':
default:
return p5LightTheme;
}
};
const getHighlightStyleByName = (themeName) => {
switch (themeName) {
case 'dark':
return darkHighlightStyle;
case 'contrast':
return contrastHighlightStyle;
case 'light':
default:
return lightHighlightStyle;
}
};
const Editor = (props) => {
const {
file,
fontSize,
unsavedChanges,
setUnsavedChanges,
updateFileContent,
autorefresh,
isPlaying,
clearConsole,
startSketch,
provideController,
isExpanded,
t,
collapseSidebar,
expandSidebar,
lintMessages,
clearLintMessage,
updateLintMessage,
lintWarning,
theme,
hideRuntimeErrorWarning,
files,
setSelectedFile,
expandConsole,
runtimeErrorWarningVisible,
consoleEvents,
autocompleteHinter
} = props;
const beepRef = useRef(null);
useEffect(() => {
beepRef.current = new Audio(beepUrl);
return () => {
beepRef.current = null;
};
}, [beepUrl]);
const updateLintingMessageAccessibility = useCallback(
debounce((annotations) => {
clearLintMessage();
annotations.forEach((x) => {
if (x.from.line > -1) {
updateLintMessage(x.severity, x.from.line + 1, x.message);
}
});
if (lintMessages.length > 0 && lintWarning) {
beepRef.current.play();
}
}, 2000),
[lintMessages]
);
const editorRef = useRef(null);
const viewRef = useRef(null);
const pickrRef = useRef(null);
const fuseRef = useRef(null); // Store the Fuse instance in a ref
const docsRef = useRef({}); // Store the documents in a ref
const prevFileIdRef = useRef(null); // Store the previous file ID in a ref
useEffect(() => {
// Initialize the Fuse instance only when `hinter` changes
if (hinter && hinter.p5Hinter) {
fuseRef.current = new Fuse(hinter.p5Hinter, {
threshold: 0.05, // Set your threshold
keys: ['text'] // Keys to search against
});
}
}, [hinter]);
const [currentLine, setCurrentLine] = useState(1);
const replaceCommand = metaKey === 'Ctrl' ? `Mod-h` : `Mod-Alt-f`;
// TODO: test debounce
const handleEditorChange = useCallback(() => {
setUnsavedChanges(true);
hideRuntimeErrorWarning();
updateFileContent(file.id, viewRef.current.state.doc.toString());
if (autorefresh && isPlaying) {
clearConsole();
startSketch();
}
}, [
setUnsavedChanges,
updateFileContent,
file.id,
autorefresh,
isPlaying,
clearConsole,
startSketch
]);
const tidyCode = () => {
const mode = getFileMode(file.name);
if (mode === 'javascript') {
prettierFormatWithCursor(viewRef.current, 'babel', [babelParser]);
} else if (mode === 'css') {
prettierFormatWithCursor(viewRef.current, 'css', [cssParser]);
} else if (mode === 'htmlmixed') {
prettierFormatWithCursor(viewRef.current, 'html', [htmlParser]);
}
};
const showFind = () => {
viewRef.current.dispatch({ effect: EditorView.findPersistent.of() });
};
const showReplace = () => {
viewRef.current.dispatch({ effect: EditorView.replacePersistent.of() });
};
const getContent = () => viewRef.current.state.doc.toString();
const handleKeyUp = () => {
const lineNumber = viewRef.current.state.doc.lineAt(
viewRef.current.state.selection.main.head
).number;
setCurrentLine(lineNumber);
};
const customLinterFunction = (view) => {
const diagnostics = [];
const content = view.state.doc.toString();
JSHINT(content, {
asi: true,
eqeqeq: false,
'-W041': false,
esversion: 11
});
JSHINT.errors.forEach((error) => {
if (!error) return;
diagnostics.push({
from: view.state.doc.line(error.line).from + (error.character - 1),
to: view.state.doc.line(error.line).from + error.character,
severity: error.code.startsWith('W') ? 'warning' : 'error',
message: error.reason
});
});
updateLintingMessageAccessibility(diagnostics);
return diagnostics;
};
const triggerFindPersistent = () => {
const view = viewRef.current;
if (view) {
// TODO: fix this use persistent using codemirror search
view.dispatch({ effects: findNext });
}
};
const customKeymap = keymap.of([
{
key: 'Tab',
run: (view) => {
if (!view.dispatch(expandAbbreviation())) return;
const selection = view.state.selection.main.head;
if (selection.length > 0) {
view.dispatch({ changes: { indentMore: true } });
} else {
view.dispatch({
changes: { insert: ' '.repeat(INDENTATION_AMOUNT) }
});
}
}
},
// TODO: test emmet
// {
// key: 'Enter',
// run: emmetInsert, // Run Emmet insert on Enter key
// },
// {
// key: 'Esc',
// run: resetAbbreviation, // Reset Emmet abbreviation on Esc key
// },
{
key: `Mod-Enter`,
run: () => {} // No action, handle meta + Enter
},
{
key: `Shift-Mod-Enter`,
run: () => null // No action, handle shift + meta + Enter
},
{
// TODO: connect search widget
key: `Mod-f`, // Meta + F to trigger findPersistent
run: () => {
triggerFindPersistent(); // Trigger the findPersistent functionality
return true; // Prevent default behavior
}
},
{
key: `Mod-F`,
run: tidyCode
},
{
key: `Mod-g`,
run: findNext
},
{
key: `Mod-G`,
run: findPrevious
},
{
key: metaKey === 'Ctrl' ? `Mod-h` : `Mod-Alt-f`,
run: replaceCommand
},
{
key: `Mod-k`, // TODO: need to find a way to create custom color picker since codemirror 6 doesn't have one
run: (view) => view.state.colorpicker.popup_color_picker({ length: 0 })
}
]);
let focusedLinkElement = null;
const setFocusedLinkElement = (set) => {
if (set && !focusedLinkElement) {
const activeItemLink = document.querySelector(
'.cm-tooltip-autocomplete a'
);
if (activeItemLink) {
focusedLinkElement = activeItemLink;
focusedLinkElement.classList.add('focused-hint-link');
activeItemLink.parentElement?.parentElement?.classList.add('unfocused');
}
}
};
const removeFocusedLinkElement = () => {
if (focusedLinkElement) {
focusedLinkElement.classList.remove('focused-hint-link');
focusedLinkElement.parentElement?.parentElement?.classList.remove(
'unfocused'
);
focusedLinkElement = null;
return true;
}
return false;
};
const javascriptCompletion = (context) => {
const token = context.matchBefore(/\w*/);
if (!token) return null;
const hints = fuseRef.current
.search(token.text)
.filter((h) => h.item.text[0] === token.text[0]);
return {
from: token.from,
options: hints.map((h) => ({
label: h.item.text,
apply: h.item.text
}))
};
};
const hintExtension = autocompletion({
override: [javascriptCompletion], // Use the javascript completion we defined earlier
closeOnUnfocus: false,
extraKeys: {
'Shift-Right': () => {
const activeItemLink = document.querySelector(
'.cm-tooltip-autocomplete a'
);
if (activeItemLink) activeItemLink.click();
return true;
},
Right: () => {
setFocusedLinkElement(true);
return true;
},
Left: () => {
removeFocusedLinkElement();
return true;
},
Up: () => {
const onLink = removeFocusedLinkElement();
setFocusedLinkElement(onLink);
return true;
},
Down: () => {
const onLink = removeFocusedLinkElement();
setFocusedLinkElement(onLink);
return true;
},
Enter: () => {
if (focusedLinkElement) {
focusedLinkElement.click();
return true;
}
return false;
}
}
});
const getCommonExtensions = () => [
javascript(),
autocompletion(),
linter(customLinterFunction),
lintGutter(),
abbreviationTracker(),
lineNumbers(),
highlightActiveLine(),
foldGutter(),
bracketMatching(),
closeBrackets(),
highlightSelectionMatches(),
css(),
keymap.of(standardKeymap),
Prec.highest(customKeymap),
EditorView.updateListener.of((update) => {
if (update.docChanged) {
handleEditorChange();
}
}),
EditorView.updateListener.of((update) => {
if (update.domEvent && update.domEvent.type === 'keyup') {
handleKeyUp(update.domEvent);
}
}),
EditorView.lineWrapping,
hintExtension,
codeFolding(),
p5ViewPlugin
];
useEffect(() => {
if (!editorRef.current) return;
const startState = EditorState.create({
doc: file.content,
extensions: [...getCommonExtensions()]
});
const view = new EditorView({
state: startState,
parent: editorRef.current
});
viewRef.current = view;
const showHint = () => {
if (view) {
view.dispatch({
effects: autocompletion.startCompletion.of({
source: javascriptCompletion
})
});
}
};
const onKeyDown = (e) => {
const mode = view?.state.facet(EditorState.languageData)?.[0];
if (/^[a-z]$/i.test(e.key) && (mode === 'css' || mode === 'javascript')) {
showHint();
}
if (e.key === 'Escape') {
e.preventDefault();
e.target.blur();
}
};
view.dom.style.fontSize = `${fontSize}px`;
view.dom.addEventListener('keydown', onKeyDown);
provideController({
tidyCode,
showFind,
showReplace,
getContent
});
// eslint-disable-next-line consistent-return
return () => {
view.dom.removeEventListener('keydown', onKeyDown);
view.destroy();
};
}, [fuseRef, pickrRef]);
const prevConsoleEventsLengthRef = useRef(consoleEvents.length);
const errorDecoration = useRef([]);
// Effect to handle runtime error highlighting
useEffect(() => {
if (runtimeErrorWarningVisible) {
const prevConsoleEventsLength = prevConsoleEventsLengthRef.current;
if (consoleEvents.length !== prevConsoleEventsLength) {
consoleEvents.forEach((consoleEvent) => {
if (consoleEvent.method === 'error') {
const errorObj = { stack: consoleEvent.data[0].toString() };
StackTrace.fromError(errorObj).then((stackLines) => {
expandConsole();
const line = stackLines.find(
(l) => l.fileName && l.fileName.startsWith('/')
);
if (!line) return;
const fileNameArray = line.fileName.split('/');
const fileName = fileNameArray.slice(-1)[0];
const filePath = fileNameArray.slice(0, -1).join('/');
const fileWithError = files.find(
(f) => f.name === fileName && f.filePath === filePath
);
if (fileWithError) {
setSelectedFile(fileWithError.id);
const decoration = Decoration.line({
class: 'line-runtime-error'
}).range(
viewRef.current.state.doc.line(line.lineNumber - 1).from
);
viewRef.current.dispatch({
effects: StateEffect.appendConfig.of(
EditorView.decorations.of(Decoration.set([decoration]))
)
});
errorDecoration.current = Decoration.set([decoration]);
}
});
}
});
} else if (errorDecoration.current) {
viewRef.current.dispatch({
effects: StateEffect.appendConfig.of(
EditorView.decorations.of(Decoration.none)
)
});
errorDecoration.current = Decoration.none;
}
prevConsoleEventsLengthRef.current = consoleEvents.length;
}
}, [
runtimeErrorWarningVisible,
consoleEvents,
files,
expandConsole,
setSelectedFile,
viewRef
]);
// Handle file changes
useEffect(() => {
if (!viewRef.current) return;
const view = viewRef.current;
const prevFileId = prevFileIdRef.current;
docsRef.current[prevFileId] = view.state.doc;
const newDoc =
docsRef.current[file.id] || EditorState.create({ doc: file.content }).doc;
view.dispatch({
changes: { from: 0, to: view.state.doc.length, insert: newDoc.toString() }
});
view.focus();
prevFileIdRef.current = file.id;
}, [file.id]);
// Handle unsaved changes
useEffect(() => {
if (!viewRef.current) return;
if (!unsavedChanges) {
setTimeout(() => setUnsavedChanges(false), 400);
}
}, [unsavedChanges]);
// Handle theme change
useEffect(() => {
if (!viewRef.current) return;
const view = viewRef.current;
const newTheme = getThemeByName(theme);
const newHighLightStyle = syntaxHighlighting(
getHighlightStyleByName(theme)
);
console.log('theme', newTheme, newHighLightStyle);
view.dispatch({
effects: StateEffect.reconfigure.of([
...getCommonExtensions(),
newTheme,
newHighLightStyle
])
});
}, [props.file.content, theme]);
return (
<MediaQuery minWidth={770}>
{(matches) =>
matches ? (
<section
className={classNames('editor', {
'sidebar--contracted': !isExpanded
})}
>
<div className="editor__header">
<button
aria-label={t('Editor.OpenSketchARIA')}
className="sidebar__contract"
onClick={collapseSidebar}
>
<LeftArrowIcon focusable="false" aria-hidden="true" />
</button>
<button
aria-label={t('Editor.CloseSketchARIA')}
className="sidebar__expand"
onClick={expandSidebar}
>
<RightArrowIcon focusable="false" aria-hidden="true" />
</button>
<div className="editor__file-name">
<span>
{file.name}
<UnsavedChangesIndicator />
</span>
<Timer />
</div>
</div>
<article
ref={editorRef}
className={classNames('editor-holder', {
'editor-holder--hidden': file.fileType === 'folder' || file.url
})}
/>
{file.url ? <AssetPreview url={file.url} name={file.name} /> : null}
<EditorAccessibility
lintMessages={lintMessages}
currentLine={currentLine}
/>
</section>
) : (
<EditorContainer expanded={isExpanded}>
<div>
<IconButton onClick={expandSidebar} icon={FolderIcon} />
<span>
{file.name}
<UnsavedChangesIndicator />
</span>
</div>
<section>
<EditorHolder ref={editorRef} />
{file.url ? (
<AssetPreview url={file.url} name={file.name} />
) : null}
<EditorAccessibility
lintMessages={lintMessages}
currentLine={currentLine}
/>
</section>
</EditorContainer>
)
}
</MediaQuery>
);
};
Editor.propTypes = {
// autocloseBracketsQuotes: PropTypes.bool.isRequired,
autocompleteHinter: PropTypes.bool.isRequired,
lintWarning: PropTypes.bool.isRequired,
lintMessages: PropTypes.arrayOf(
PropTypes.shape({
severity: PropTypes.oneOf(['error', 'hint', 'info', 'warning'])
.isRequired,
line: PropTypes.number.isRequired,
message: PropTypes.string.isRequired,
id: PropTypes.number.isRequired
})
).isRequired,
consoleEvents: PropTypes.arrayOf(
PropTypes.shape({
method: PropTypes.string.isRequired,
args: PropTypes.arrayOf(PropTypes.string)
})
).isRequired,
updateLintMessage: PropTypes.func.isRequired,
clearLintMessage: PropTypes.func.isRequired,
updateFileContent: PropTypes.func.isRequired,
fontSize: PropTypes.number.isRequired,
file: PropTypes.shape({
name: PropTypes.string.isRequired,
content: PropTypes.string.isRequired,
id: PropTypes.string.isRequired,
fileType: PropTypes.string.isRequired,
url: PropTypes.string
}).isRequired,
setUnsavedChanges: PropTypes.func.isRequired,
startSketch: PropTypes.func.isRequired,
autorefresh: PropTypes.bool.isRequired,
isPlaying: PropTypes.bool.isRequired,
theme: PropTypes.string.isRequired,
unsavedChanges: PropTypes.bool.isRequired,
files: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.string.isRequired,
name: PropTypes.string.isRequired,
content: PropTypes.string.isRequired
})
).isRequired,
isExpanded: PropTypes.bool.isRequired,
collapseSidebar: PropTypes.func.isRequired,
// closeProjectOptions: PropTypes.func.isRequired,
expandSidebar: PropTypes.func.isRequired,
clearConsole: PropTypes.func.isRequired,
hideRuntimeErrorWarning: PropTypes.func.isRequired,
runtimeErrorWarningVisible: PropTypes.bool.isRequired,
provideController: PropTypes.func.isRequired,
t: PropTypes.func.isRequired,
setSelectedFile: PropTypes.func.isRequired,
expandConsole: PropTypes.func.isRequired
};
const mapStateToProps = (state) => ({
files: state.files,
file: selectActiveFile(state),
htmlFile: getHTMLFile(state.files),
ide: state.ide,
preferences: state.preferences,
editorAccessibility: state.editorAccessibility,
user: state.user,
project: state.project,
consoleEvents: state.console,
...state.preferences,
...state.ide,
...state.project,
...state.editorAccessibility,
isExpanded: state.ide.sidebarIsExpanded
});
const mapDispatchToProps = (dispatch) =>
bindActionCreators(
{
...EditorAccessibilityActions,
...FileActions,
...ProjectActions,
...IDEActions,
...PreferencesActions,
...UserActions,
...ConsoleActions
},
dispatch
);
export default withTranslation()(
connect(mapStateToProps, mapDispatchToProps)(Editor)
);