Skip to content

Commit 984a872

Browse files
authored
fix(form/Editor): only use the bundled monaco editor (#3169)
1 parent 1bdbfa6 commit 984a872

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

src/components/form/Editor.tsx

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,8 @@
1515
* limitations under the License.
1616
*/
1717
import { InputWrapper, type InputWrapperProps, Skeleton } from '@mantine/core';
18-
import { Editor, loader, type Monaco, useMonaco } from '@monaco-editor/react';
19-
import { clsx } from 'clsx';
20-
import { editor } from 'monaco-editor';
21-
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
22-
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
18+
import { Editor } from '@monaco-editor/react';
19+
import clsx from 'clsx';
2320
import { useEffect, useMemo, useRef, useState } from 'react';
2421
import {
2522
type FieldValues,
@@ -29,26 +26,13 @@ import {
2926
} from 'react-hook-form';
3027
import { useTranslation } from 'react-i18next';
3128

32-
import { genControllerProps } from './util';
29+
import { monaco, setupMonacoEditor } from '@/utils/monaco';
3330

34-
type SetupMonacoProps = {
35-
monaco: Monaco;
36-
};
31+
import { genControllerProps } from './util';
3732

38-
const setupMonaco = ({ monaco }: SetupMonacoProps) => {
39-
window.MonacoEnvironment = {
40-
getWorker(_, label) {
41-
if (label === 'json') {
42-
return new jsonWorker();
43-
}
44-
return new editorWorker();
45-
},
46-
};
47-
loader.config({ monaco });
48-
return loader.init();
49-
};
33+
setupMonacoEditor();
5034

51-
const options: editor.IStandaloneEditorConstructionOptions = {
35+
const options: monaco.editor.IStandaloneEditorConstructionOptions = {
5236
minimap: { enabled: false },
5337
contextmenu: false,
5438
lineNumbersMinChars: 3,
@@ -102,11 +86,9 @@ export const FormItemEditor = <T extends FieldValues>(
10286
fieldState,
10387
} = useController<T>(enhancedControllerProps);
10488

105-
const monaco = useMonaco();
10689
const [internalLoading, setLoading] = useState(false);
10790

10891
useEffect(() => {
109-
if (!monaco) return;
11092
setLoading(true);
11193

11294
const schemas = [];
@@ -125,7 +107,7 @@ export const FormItemEditor = <T extends FieldValues>(
125107
});
126108

127109
setLoading(false);
128-
}, [monaco, customSchema]);
110+
}, [customSchema]);
129111

130112
return (
131113
<InputWrapper
@@ -155,7 +137,6 @@ export const FormItemEditor = <T extends FieldValues>(
155137
restField.disabled && 'editor-wrapper--disabled'
156138
),
157139
}}
158-
beforeMount={(monaco) => setupMonaco({ monaco })}
159140
defaultValue={controllerProps.defaultValue}
160141
value={value}
161142
onChange={fOnChange}

src/utils/monaco.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
import 'monaco-editor/esm/vs/editor/editor.all.js';
18+
import 'monaco-editor/esm/vs/language/json/monaco.contribution';
19+
20+
import { loader } from '@monaco-editor/react';
21+
import * as monaco from 'monaco-editor/esm/vs/editor/editor.api';
22+
import editorWorker from 'monaco-editor/esm/vs/editor/editor.worker?worker';
23+
import jsonWorker from 'monaco-editor/esm/vs/language/json/json.worker?worker';
24+
25+
export { monaco };
26+
27+
export const setupMonacoEditor = () => {
28+
self.MonacoEnvironment = {
29+
getWorker(_, label) {
30+
if (label === 'json') return new jsonWorker();
31+
return new editorWorker();
32+
},
33+
};
34+
loader.config({ monaco });
35+
loader.init();
36+
};

vite.config.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ export default defineConfig({
6363
},
6464
}),
6565
},
66+
build: {
67+
rollupOptions: {
68+
output: {
69+
manualChunks: {
70+
'react-vendor': ['react', 'react-dom'],
71+
'monaco-editor-vendor': ['monaco-editor', '@monaco-editor/react'],
72+
'antd-vendor': [
73+
'antd',
74+
'@ant-design/pro-components',
75+
'@ant-design/v5-patch-for-react-19',
76+
],
77+
},
78+
},
79+
},
80+
},
6681
plugins: [
6782
tsconfigPaths(),
6883
UnpluginIcons({

0 commit comments

Comments
 (0)