Skip to content

Commit d6b7dd9

Browse files
sq ace
1 parent a6ee2fc commit d6b7dd9

File tree

1 file changed

+36
-23
lines changed

1 file changed

+36
-23
lines changed

client/app/lib/components/core/fields/EditorField.tsx

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,34 +37,47 @@ const DEFAULT_FONT_FAMILY = [
3737
'monospace',
3838
].join(',');
3939

40-
const EditorField = forwardRef(
41-
(props: EditorProps, ref: ForwardedRef<AceEditor>): JSX.Element => {
42-
const { language, value, disabled, onChange, cursorStart, ...otherProps } =
43-
props;
40+
/**
41+
* Loads Ace's mode scripts on demand for `language` and returns `true` if the mode
42+
* has been loaded.
43+
*
44+
* Remember to update the regex in the `webpackInclude` comment below to bundle any
45+
* new languages we support in the future.
46+
*/
47+
const useLazyMode = (language: LanguageMode): boolean => {
48+
const [loading, setLoading] = useState(true);
4449

45-
const [loading, setLoading] = useState(true);
50+
useEffect(() => {
51+
setLoading(true);
4652

47-
useEffect(() => {
48-
setLoading(true);
53+
let ignore = false;
4954

50-
let ignore = false;
55+
(async (): Promise<void> => {
56+
await import(
57+
/* webpackInclude: /ace-builds\/src-noconflict\/mode-(c_cpp|python|r|java|javascript)\./ */
58+
/* webpackChunkName: "ace-[request]" */
59+
`ace-builds/src-noconflict/mode-${language}`
60+
);
5161

52-
(async (): Promise<void> => {
53-
await import(
54-
/* webpackInclude: /ace-builds\/src-noconflict\/mode-(c_cpp|python|r|java)./ */
55-
/* webpackChunkName: "ace-builds" */
56-
`ace-builds/src-noconflict/mode-${language}`
57-
);
62+
if (ignore) return;
5863

59-
if (ignore) return;
64+
setLoading(false);
65+
})();
6066

61-
setLoading(false);
62-
})();
67+
return () => {
68+
ignore = true;
69+
};
70+
}, [language]);
71+
72+
return loading;
73+
};
74+
75+
const EditorField = forwardRef(
76+
(props: EditorProps, ref: ForwardedRef<AceEditor>): JSX.Element => {
77+
const { language, value, disabled, onChange, cursorStart, ...otherProps } =
78+
props;
6379

64-
return () => {
65-
ignore = true;
66-
};
67-
}, [language]);
80+
const loading = useLazyMode(language);
6881

6982
return (
7083
<AceEditor
@@ -73,8 +86,8 @@ const EditorField = forwardRef(
7386
* This "mode" parameter should match one of the file names in this git directory:
7487
* https://github.com/thlorenz/brace/tree/master/mode
7588
*
76-
* Short-circuit this because during build time, `mode` can be `undefined` and
77-
* AceEditor will request for `/webpack/mode-mode.js`, which doesn't exist.
89+
* Python is always available. For other modes, we lazy-load them, and when it is
90+
* loaded, the editor will simply "snap" to the new mode's syntax highlighting.
7891
*
7992
* TODO: This parameter is called by many names in various places in the codebase,
8093
* such as "language", "editorMode", "languageMode", or "ace_mode".

0 commit comments

Comments
 (0)