Skip to content

Commit 58231ea

Browse files
committed
feat: monaco单例化
1 parent 1a0525d commit 58231ea

File tree

3 files changed

+78
-16
lines changed

3 files changed

+78
-16
lines changed

packages/base-monaco-editor/src/helper.ts

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
/* eslint-disable no-empty */
22
import { useEffect, useState, useRef, CSSProperties } from 'react';
33
import loader from '@monaco-editor/loader';
4-
5-
loader.config({
6-
paths: {
7-
vs: 'https://g.alicdn.com/code/lib/monaco-editor/0.31.1/min/vs',
8-
},
9-
});
4+
import { getMonaco } from './monaco';
105

116
type IAmbigousFn = (...args: any[]) => any;
127

@@ -52,7 +47,7 @@ export interface IMonacoInstance {
5247
[otherKeys: string]: any;
5348
}
5449

55-
type ICodeEditorViewState = {
50+
interface ICodeEditorViewState {
5651
contributionsState: any;
5752
cursorState: any;
5853
viewState: any;
@@ -154,7 +149,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
154149
const editorWillMountRef = useRef<ISingleMonacoEditorProps['editorWillMount']>();
155150

156151
const decomposeRef = useRef(false);
157-
const viewStatusRef = useRef<Map<any, ICodeEditorViewState>>(new Map())
152+
const viewStatusRef = useRef<Map<any, ICodeEditorViewState>>(new Map());
158153

159154
useEffect(() => {
160155
editorDidMountRef.current = editorDidMount;
@@ -185,7 +180,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
185180
loader.config(requireConfigRef.current);
186181
}
187182

188-
loader.init()
183+
getMonaco(requireConfigRef.current)
189184
.then((monaco: any) => {
190185
// 兼容旧版本 monaco-editor 写死 MonacoEnvironment 的问题
191186
(window as any).MonacoEnvironment = undefined;
@@ -209,7 +204,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
209204
monaco,
210205
valueRef.current ?? defaultValueRef.current ?? '',
211206
languageRef.current,
212-
pathRef.current
207+
pathRef.current,
213208
);
214209
editor = monaco.editor.create(containerRef.current, {
215210
automaticLayout: true,
@@ -268,7 +263,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
268263
? editorRef.current.getModifiedEditor()
269264
: editorRef.current;
270265

271-
const nextValue = value ?? defaultValueRef.current ?? ''
266+
const nextValue = value ?? defaultValueRef.current ?? '';
272267
if (editor?.getOption?.(monacoRef.current?.editor.EditorOption.readOnly)) {
273268
editor?.setValue(nextValue);
274269
} else if (value !== editor?.getValue()) {
@@ -330,7 +325,7 @@ export const useEditor = (type: 'single' | 'diff', props: IGeneralManacoEditorPr
330325
if (valueRef.current !== null && valueRef.current !== undefined && model.getValue() !== valueRef.current) {
331326
model.setValue(valueRef.current);
332327
}
333-
328+
334329
if (model !== editorRef.current.getModel()) {
335330
saveViewState && viewStatusRef.current.set(previousPath, editorRef.current.saveViewState());
336331
editorRef.current.setModel(model);
@@ -365,9 +360,9 @@ function getOrCreateModel(monaco: IMonacoInstance, value?: string, language?: st
365360
}
366361

367362
function usePrevious<T>(value: T) {
368-
const ref = useRef<T>()
363+
const ref = useRef<T>();
369364
useEffect(() => {
370-
ref.current = value
371-
}, [value])
372-
return ref.current
365+
ref.current = value;
366+
}, [value]);
367+
return ref.current;
373368
}

packages/base-monaco-editor/src/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
INITIAL_OPTIONS,
1010
} from './helper';
1111

12+
export * from './monaco';
13+
1214
const SingleMonacoEditor = (props: ISingleMonacoEditorProps) => {
1315
const { onChange, enableOutline, width, height, language, supportFullScreen } = props;
1416
const onChangeRef = useRef(onChange);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import loader, { Monaco } from '@monaco-editor/loader';
2+
//@ts-ignore
3+
import isEqual from 'lodash/isEqual';
4+
5+
export interface Configure {
6+
singleton?: boolean;
7+
}
8+
9+
const CONFIGURE_KEY = '__base_monaco_editor_config__';
10+
const fakeWindow: any = window;
11+
12+
if (!fakeWindow[CONFIGURE_KEY]) {
13+
fakeWindow[CONFIGURE_KEY] = {};
14+
}
15+
16+
export const configuration: Configure = fakeWindow[CONFIGURE_KEY];
17+
18+
export const getSingletonMonaco = (() => {
19+
let monaco: Monaco;
20+
let prevOptions: any;
21+
return async (options?: any) => {
22+
if (!monaco || !isEqual(prevOptions, options)) {
23+
const hasConfig = Object.keys(options || {}).length > 0;
24+
loader.config(
25+
hasConfig
26+
? options
27+
: {
28+
paths: {
29+
vs: 'https://g.alicdn.com/code/lib/monaco-editor/0.33.0/min/vs',
30+
},
31+
},
32+
);
33+
// eslint-disable-next-line require-atomic-updates
34+
monaco = await loader.init();
35+
// eslint-disable-next-line require-atomic-updates
36+
prevOptions = options;
37+
}
38+
return monaco;
39+
};
40+
})();
41+
42+
export const getCommonMonaco = (config: any): Promise<Monaco> => {
43+
if (config) {
44+
loader.config(config);
45+
} else {
46+
loader.config({
47+
paths: {
48+
vs: 'https://g.alicdn.com/code/lib/monaco-editor/0.31.1/min/vs',
49+
},
50+
});
51+
}
52+
return loader.init();
53+
};
54+
55+
export function getMonaco(config: any) {
56+
const hasConfig = Object.keys(config || {}).length > 0;
57+
const monacoConfig = hasConfig ? config : undefined;
58+
return configuration.singleton
59+
? getSingletonMonaco(monacoConfig)
60+
: getCommonMonaco(monacoConfig);
61+
}
62+
63+
export function configure(conf: Configure) {
64+
Object.assign(configuration, conf || {});
65+
}

0 commit comments

Comments
 (0)