Skip to content

Commit eb93979

Browse files
mortalYoungmumiao
authored andcommitted
fix: fix model's value not change and add onEditorInstanceMount event
1 parent e15b8b6 commit eb93979

File tree

7 files changed

+104
-16
lines changed

7 files changed

+104
-16
lines changed

src/controller/__tests__/editor.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ const editorService = container.resolve(EditorService);
99
const statusBarService = container.resolve(StatusBarService);
1010
const builtinService = container.resolve(BuiltinService);
1111

12+
jest.mock('mo/monaco', () => {
13+
const original = jest.requireActual('mo/monaco');
14+
return {
15+
...original,
16+
editor: {
17+
getModel: jest.fn(),
18+
},
19+
};
20+
});
21+
1222
describe('The ediotr controller', () => {
1323
test('The initEditorEvents method', () => {
1424
const editorInstance = {} as MonacoEditor.IStandaloneCodeEditor;

src/controller/editor.tsx

+8
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ export class EditorController extends Controller implements IEditorController {
199199
tab?.data?.value!,
200200
tab?.data?.language!
201201
);
202+
203+
this.onEditorInstanceMount(editorInstance);
202204
};
203205

204206
public onClickActions = (action: IEditorActionsProps) => {
@@ -374,4 +376,10 @@ export class EditorController extends Controller implements IEditorController {
374376
}
375377
}
376378
}
379+
380+
public onEditorInstanceMount(
381+
editorInstance: MonacoEditor.IStandaloneCodeEditor
382+
) {
383+
this.emit(EditorEvent.onEditorInstanceMount, editorInstance);
384+
}
377385
}

src/model/workbench/editor.ts

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export enum EditorEvent {
1717
OnUpdateTab = 'editor.updateTab',
1818
onActionsClick = 'editor.actionsClick',
1919
OnSplitEditorRight = 'editor.splitEditorRight',
20+
onEditorInstanceMount = 'editor.onEditorInstanceMount',
2021
}
2122

2223
export interface BuiltInEditorTabDataType {

src/services/workbench/__tests__/editorService.test.tsx

+37-9
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ import { editor as MonacoEditor } from 'mo/monaco';
99
import { cloneDeep } from 'lodash';
1010
import { act } from 'react-dom/test-utils';
1111

12+
jest.mock('mo/monaco', () => {
13+
const original = jest.requireActual('mo/monaco');
14+
return {
15+
...original,
16+
editor: {
17+
getModel: jest.fn(),
18+
},
19+
};
20+
});
21+
1222
describe('Test EditorService', () => {
1323
let mockTab: IEditorTab;
1424

@@ -132,7 +142,7 @@ describe('Test EditorService', () => {
132142
expect(editor.editorInstance).toBeUndefined();
133143
});
134144

135-
test('Update the tab', () => {
145+
test('Update the tab without model', () => {
136146
const editor = new EditorService();
137147
editor.open(mockTab);
138148
expect(editor.getState().current?.activeTab).toBe(mockTab.id);
@@ -198,14 +208,9 @@ describe('Test EditorService', () => {
198208
expect(groups?.length).toBe(1);
199209

200210
const setValFn = jest.fn();
201-
const getValFn = jest.fn(() => '');
202-
groups![0].editorInstance = {
203-
getModel: () => ({
204-
getValue: getValFn,
205-
setValue: setValFn,
206-
}),
207-
};
208-
211+
(MonacoEditor.getModel as jest.Mock).mockImplementation(() => ({
212+
setValue: setValFn,
213+
}));
209214
act(() => {
210215
editor.updateTab({
211216
id: mockTab.id,
@@ -217,6 +222,20 @@ describe('Test EditorService', () => {
217222

218223
expect(setValFn).toBeCalled();
219224
expect(setValFn.mock.calls[0][0]).toBe('test');
225+
226+
// Update editor text even not current tab
227+
editor.setActive(1, 1);
228+
act(() => {
229+
editor.updateTab({
230+
id: mockTab.id,
231+
data: {
232+
value: 'test2',
233+
},
234+
});
235+
});
236+
237+
expect(setValFn).toBeCalledTimes(2);
238+
expect(setValFn.mock.calls[1][0]).toBe('test2');
220239
});
221240

222241
test('Should prevent update editor text if current tab with renderPane', () => {
@@ -614,4 +633,13 @@ describe('Test EditorService', () => {
614633
expect(testFn.mock.calls[0]).toEqual(expected);
615634
});
616635
});
636+
637+
test('Listen to the editorInstance mount', () => {
638+
const editor = new EditorService();
639+
expectFnCalled((testFn) => {
640+
editor.onEditorInstanceMount(testFn);
641+
editor.emit(EditorEvent.onEditorInstanceMount, {});
642+
expect(testFn.mock.calls[0][0]).toEqual({});
643+
});
644+
});
617645
});

src/services/workbench/editorService.ts

+28-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import 'reflect-metadata';
22
import { singleton, container } from 'tsyringe';
3-
import { cloneDeep, isString } from 'lodash';
3+
import { cloneDeep } from 'lodash';
44
import { Component } from 'mo/react';
55
import {
66
EditorModel,
@@ -205,6 +205,12 @@ export interface IEditorService extends Component<IEditor> {
205205
* @param tabId
206206
*/
207207
getGroupIdByTab(tabId: UniqueId): UniqueId | null;
208+
/**
209+
* Listen to the editor instance mount event
210+
*/
211+
onEditorInstanceMount(
212+
callback: (editorInstance: MonacoEditor.IStandaloneCodeEditor) => void
213+
): void;
208214
}
209215
@singleton()
210216
export class EditorService
@@ -325,11 +331,15 @@ export class EditorService
325331
updatedTab = Object.assign(tabData, tab);
326332
}
327333
if (group.activeTab === tab.id) {
328-
isString(editorValue) &&
329-
!tabData?.renderPane &&
330-
this.setGroupEditorValue(group, editorValue);
331334
updatedTab = Object.assign(group.tab, tab);
332335
}
336+
// Update model's value
337+
const model = MonacoEditor.getModel(
338+
Uri.parse(tab.id.toString())
339+
);
340+
if (model) {
341+
model.setValue(editorValue || '');
342+
}
333343
this.updateGroup(groupId, group);
334344

335345
if (groupId === this.state.current?.id) {
@@ -345,11 +355,16 @@ export class EditorService
345355
}
346356

347357
if (group.activeTab === tab.id) {
348-
isString(editorValue) &&
349-
!tabData?.renderPane &&
350-
this.setGroupEditorValue(group, editorValue);
351358
updatedTab = Object.assign(group.tab, tab);
352359
}
360+
361+
// Update model's value
362+
const model = MonacoEditor.getModel(
363+
Uri.parse(tab.id.toString())
364+
);
365+
if (model) {
366+
model.setValue(editorValue || '');
367+
}
353368
});
354369

355370
if (current?.activeTab === tab.id) {
@@ -777,4 +792,10 @@ export class EditorService
777792
) {
778793
this.subscribe(EditorEvent.onActionsClick, callback);
779794
}
795+
796+
public onEditorInstanceMount(
797+
callback: (editorInstance: MonacoEditor.IStandaloneCodeEditor) => void
798+
) {
799+
this.subscribe(EditorEvent.onEditorInstanceMount, callback);
800+
}
780801
}

stories/extensions/test/index.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -229,5 +229,9 @@ export const ExtendsTestPane: IExtension = {
229229

230230
molecule.layout.setAuxiliaryBar(!tab);
231231
});
232+
233+
molecule.editor.onEditorInstanceMount((editorInstance) => {
234+
console.log('editorInstance:', editorInstance);
235+
});
232236
},
233237
};

stories/extensions/test/testPane.tsx

+16
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,19 @@ export type GenericClassDecorator<T> = (target: T) => void;`,
238238
}
239239
};
240240

241+
const updateEditor = function () {
242+
// get first inactive tab
243+
const state = molecule.editor.getState();
244+
const firstInactiveTab = state.current?.data?.find(
245+
(tab) => tab.id !== state.current?.activeTab
246+
);
247+
if (firstInactiveTab) {
248+
firstInactiveTab.data.value +=
249+
'\nconst firstInactiveTab = "test";\n';
250+
molecule.editor.updateTab(firstInactiveTab, state.current?.id);
251+
}
252+
};
253+
241254
const newPane = function () {
242255
const key = shortRandomId();
243256
const name = `pane-${key}.ts`;
@@ -481,6 +494,9 @@ PARTITIONED BY (DE STRING) LIFECYCLE 1000;
481494
<div style={{ marginBottom: 50 }}>
482495
<h2>Simple examples:</h2>
483496
<Button onClick={newEditor}>New Editor</Button>
497+
<Button onClick={updateEditor}>
498+
Update Inactive Editor{`'`}s value
499+
</Button>
484500
<Button onClick={toggleEditorStatus}>
485501
Toggle Editor status
486502
</Button>

0 commit comments

Comments
 (0)