Skip to content

Commit 567f629

Browse files
authored
fix: initView and load extensions after workbench be ready (#853)
* fix: only alow to create monacoInstance once * fix: initView and load extensions after workbench be ready
1 parent 122eff9 commit 567f629

File tree

4 files changed

+30
-30
lines changed

4 files changed

+30
-30
lines changed

src/components/monaco/index.tsx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ export class MonacoEditor extends PureComponent<IMonacoEditorProps> {
4444

4545
componentDidMount() {
4646
const { options = {}, override, editorInstanceRef } = this.props;
47-
this.monacoInstance = this.monacoService?.create(
48-
this.monacoDom,
49-
options,
50-
override
51-
);
52-
editorInstanceRef?.(this.monacoInstance);
47+
if (!this.monacoInstance) {
48+
this.monacoInstance = this.monacoService?.create(
49+
this.monacoDom,
50+
options,
51+
override
52+
);
53+
editorInstanceRef?.(this.monacoInstance);
54+
}
5355
}
5456

5557
componentDidUpdate(prevProps) {

src/provider/__tests__/__snapshots__/molecule.test.tsx.snap

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,16 +1058,7 @@ dmlldzJfOV8xNjM4ODQ4MDI1NDI2MTE4Ml8zOF9bMF0TNSl1AAAAAElFTkSuQmCC"
10581058
<div
10591059
class="keybindings"
10601060
>
1061-
<ul>
1062-
<li
1063-
class="keys"
1064-
>
1065-
<span>
1066-
Command Palette
1067-
</span>
1068-
<span />
1069-
</li>
1070-
</ul>
1061+
<ul />
10711062
</div>
10721063
</div>
10731064
</div>

src/services/__tests__/instanceService.test.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import React from 'react';
22
import { defaultExtensions } from 'mo/extensions';
33
import InstanceService from '../instanceService';
44
import { render } from '@testing-library/react';
5+
import { LayoutEvents } from 'mo/model/workbench/layout';
6+
import molecule from 'mo';
57

68
describe('The InstanceService', () => {
79
test('Constuctor with default config', () => {
@@ -39,6 +41,7 @@ describe('The InstanceService', () => {
3941
const mockFn = jest.fn();
4042
instance.onBeforeInit(mockFn);
4143
instance.onBeforeLoad(mockFn);
44+
molecule.layout.emit(LayoutEvents.OnWorkbenchDidMount);
4245
instance.render(<div>123</div>);
4346

4447
expect(mockFn).toBeCalledTimes(2);

src/services/instanceService.tsx

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -112,22 +112,26 @@ export default class InstanceService
112112
StatusBarController,
113113
];
114114

115-
// resolve all controllers, and call `initView` to inject initial values into services
116-
Object.keys(controllers).forEach((key) => {
117-
const module = controllers[key];
118-
const controller = container.resolve<Controller>(module);
119-
controller.initView?.();
120-
});
121-
122-
this.emit(InstanceHookKind.beforeLoad);
123-
molecule.extension.load(others);
124-
125115
molecule.layout.onWorkbenchDidMount(() => {
126-
molecule.monacoService.initWorkspace(
127-
molecule.layout.container!
128-
);
116+
if (!this.rendered) {
117+
molecule.monacoService.initWorkspace(
118+
molecule.layout.container!
119+
);
120+
121+
// resolve all controllers, and call `initView` to inject initial values into services
122+
Object.keys(controllers).forEach((key) => {
123+
const module = controllers[key];
124+
const controller =
125+
container.resolve<Controller>(module);
126+
controller.initView?.();
127+
});
128+
129+
this.emit(InstanceHookKind.beforeLoad);
130+
molecule.extension.load(others);
131+
132+
this.rendered = true;
133+
}
129134
});
130-
this.rendered = true;
131135
}
132136

133137
return workbench;

0 commit comments

Comments
 (0)