Skip to content

Commit ff7a035

Browse files
committed
feat: 添加 controller
1 parent 58231ea commit ff7a035

File tree

4 files changed

+61
-19
lines changed

4 files changed

+61
-19
lines changed

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
@@ -10,6 +10,7 @@ import {
1010
} from './helper';
1111

1212
export * from './monaco';
13+
export * from './controller';
1314

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

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
import loader, { Monaco } from '@monaco-editor/loader';
22
//@ts-ignore
33
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];
4+
import { controller } from './controller';
175

186
export const getSingletonMonaco = (() => {
197
let monaco: Monaco;
@@ -52,14 +40,10 @@ export const getCommonMonaco = (config: any): Promise<Monaco> => {
5240
return loader.init();
5341
};
5442

55-
export function getMonaco(config: any) {
43+
export function getMonaco(config?: any) {
5644
const hasConfig = Object.keys(config || {}).length > 0;
5745
const monacoConfig = hasConfig ? config : undefined;
58-
return configuration.singleton
46+
return controller.getMeta().singleton
5947
? getSingletonMonaco(monacoConfig)
6048
: getCommonMonaco(monacoConfig);
6149
}
62-
63-
export function configure(conf: Configure) {
64-
Object.assign(configuration, conf || {});
65-
}

0 commit comments

Comments
 (0)