Skip to content

Commit 877f0aa

Browse files
authored
Merge pull request alibaba#16 from wangshihao111/feature-singleton
feat: 单例优化
2 parents 36426d8 + 262ab7b commit 877f0aa

File tree

5 files changed

+71
-17
lines changed

5 files changed

+71
-17
lines changed

packages/base-monaco-editor/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 1.1.0-beta.1
2+
3+
- 添加 controller 实例,可用作中间交换介质,解决与代码编辑相关的插件间的协作问题 [@wangshihao111](https://github.com/wangshihao111)
4+
- 优化 Monaco 单例,元信息保存在 controller 实例内部 [@wangshihao111](https://github.com/wangshihao111)
5+
16
## 1.1.0-beta.0
27

38
- 重构 type / path / value 联动,彻底修复 monaco 在多文件模式下覆盖多个 path 值的 bug

packages/base-monaco-editor/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,24 @@ function App() {
125125
ReactDOM.render(<App />, mountNode);
126126
```
127127

128+
### Using controller
129+
130+
```ts
131+
import { controller } from '@alilc/lowcode-plugin-base-monaco-editor';
132+
133+
// configure Monaco to be singleton
134+
controller.updateMeta({ singleton: true });
135+
136+
// Get all metadata
137+
controller.getMeta();
138+
139+
// register a custom method
140+
controller.registerMethod('methodName', (a, b, c) => { });
141+
142+
// call custom methods
143+
const ret = controller.call('methodName', a, b, c);
144+
```
145+
128146
## Citation
129147

130148
This is forked from [monaco-react](https://github.com/suren-atoyan/monaco-react). Thanks for [suren-atoyan](https://github.com/suren-atoyan)'s effort for making monaco editor appoachable.
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
export interface EditorMeta {
2+
singleton: boolean;
3+
[key: string]: any;
4+
}
5+
6+
export class Controller {
7+
private methodMap: Record<string, Function>;
8+
private meta: EditorMeta;
9+
10+
constructor() {
11+
this.methodMap = {};
12+
this.meta = { singleton: false };
13+
}
14+
15+
registerMethod(name: string, fn: Function) {
16+
this.methodMap[name] = fn;
17+
}
18+
19+
call(name: string, ...args: any[]) {
20+
return this.methodMap[name]?.(...args);
21+
}
22+
23+
updateMeta(obj: Partial<EditorMeta>) {
24+
Object.assign(this.meta, obj);
25+
}
26+
27+
getMeta() {
28+
return Object.freeze({ ...this.meta });
29+
}
30+
}
31+
32+
const CONFIGURE_KEY = '__base_monaco_editor_controller__';
33+
const fakeWindow: any = window;
34+
35+
if (!fakeWindow[CONFIGURE_KEY]) {
36+
fakeWindow[CONFIGURE_KEY] = new Controller();
37+
}
38+
39+
export const controller: Controller = fakeWindow[CONFIGURE_KEY];

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from './helper';
1212

1313
export * from './monaco';
14+
export * from './controller';
1415

1516
const SingleMonacoEditor = (props: ISingleMonacoEditorProps) => {
1617
const { onChange, enableOutline, width, height, language, supportFullScreen } = props;

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

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,7 @@
11
import loader, { Monaco } from '@monaco-editor/loader';
2+
//@ts-ignore
23
import isEqual from 'lodash/isEqual';
3-
4-
export interface Configure {
5-
singleton?: boolean;
6-
}
7-
8-
const CONFIGURE_KEY = '__base_monaco_editor_config__';
9-
const fakeWindow: any = window;
10-
11-
if (!fakeWindow[CONFIGURE_KEY]) {
12-
fakeWindow[CONFIGURE_KEY] = {};
13-
}
14-
15-
export const configuration: Configure = fakeWindow[CONFIGURE_KEY];
4+
import { controller, EditorMeta } from './controller';
165

176
export const getSingletonMonaco = (() => {
187
let monaco: Monaco;
@@ -29,7 +18,9 @@ export const getSingletonMonaco = (() => {
2918
},
3019
},
3120
);
21+
// eslint-disable-next-line require-atomic-updates
3222
monaco = await loader.init();
23+
// eslint-disable-next-line require-atomic-updates
3324
prevOptions = options;
3425
}
3526
return monaco;
@@ -49,14 +40,14 @@ export const getCommonMonaco = (config: any): Promise<Monaco> => {
4940
return loader.init();
5041
};
5142

52-
export function getMonaco(config: any) {
43+
export function getMonaco(config?: any) {
5344
const hasConfig = Object.keys(config || {}).length > 0;
5445
const monacoConfig = hasConfig ? config : undefined;
55-
return configuration.singleton
46+
return controller.getMeta().singleton
5647
? getSingletonMonaco(monacoConfig)
5748
: getCommonMonaco(monacoConfig);
5849
}
5950

60-
export function configure(conf: Configure) {
61-
Object.assign(configuration, conf || {});
51+
export function configure(config: EditorMeta) {
52+
controller.updateMeta(config);
6253
}

0 commit comments

Comments
 (0)