Skip to content

Commit

Permalink
fix: fix model's value not change and add onEditorInstanceMount event
Browse files Browse the repository at this point in the history
  • Loading branch information
mortalYoung committed Dec 26, 2023
1 parent bef563d commit ce46463
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 16 deletions.
10 changes: 10 additions & 0 deletions src/controller/__tests__/editor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ const editorService = container.resolve(EditorService);
const statusBarService = container.resolve(StatusBarService);
const builtinService = container.resolve(BuiltinService);

jest.mock('mo/monaco', () => {
const original = jest.requireActual('mo/monaco');
return {
...original,
editor: {
getModel: jest.fn(),
},
};
});

describe('The ediotr controller', () => {
test('The initEditorEvents method', () => {
const editorInstance = {} as MonacoEditor.IStandaloneCodeEditor;
Expand Down
8 changes: 8 additions & 0 deletions src/controller/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ export class EditorController extends Controller implements IEditorController {
tab?.data?.value!,
tab?.data?.language!
);

this.onEditorInstanceMount(editorInstance);

Check warning on line 203 in src/controller/editor.tsx

View check run for this annotation

Codecov / codecov/patch

src/controller/editor.tsx#L203

Added line #L203 was not covered by tests
};

public onClickActions = (action: IEditorActionsProps) => {
Expand Down Expand Up @@ -374,4 +376,10 @@ export class EditorController extends Controller implements IEditorController {
}
}
}

public onEditorInstanceMount(
editorInstance: MonacoEditor.IStandaloneCodeEditor
) {
this.emit(EditorEvent.onEditorInstanceMount, editorInstance);

Check warning on line 383 in src/controller/editor.tsx

View check run for this annotation

Codecov / codecov/patch

src/controller/editor.tsx#L382-L383

Added lines #L382 - L383 were not covered by tests
}
}
1 change: 1 addition & 0 deletions src/model/workbench/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export enum EditorEvent {
OnUpdateTab = 'editor.updateTab',
onActionsClick = 'editor.actionsClick',
OnSplitEditorRight = 'editor.splitEditorRight',
onEditorInstanceMount = 'editor.onEditorInstanceMount',
}

export interface BuiltInEditorTabDataType {
Expand Down
46 changes: 37 additions & 9 deletions src/services/workbench/__tests__/editorService.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@ import { editor as MonacoEditor } from 'mo/monaco';
import { cloneDeep } from 'lodash';
import { act } from 'react-dom/test-utils';

jest.mock('mo/monaco', () => {
const original = jest.requireActual('mo/monaco');
return {
...original,
editor: {
getModel: jest.fn(),
},
};
});

describe('Test EditorService', () => {
let mockTab: IEditorTab;

Expand Down Expand Up @@ -132,7 +142,7 @@ describe('Test EditorService', () => {
expect(editor.editorInstance).toBeUndefined();
});

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

const setValFn = jest.fn();
const getValFn = jest.fn(() => '');
groups![0].editorInstance = {
getModel: () => ({
getValue: getValFn,
setValue: setValFn,
}),
};

(MonacoEditor.getModel as jest.Mock).mockImplementation(() => ({
setValue: setValFn,
}));
act(() => {
editor.updateTab({
id: mockTab.id,
Expand All @@ -217,6 +222,20 @@ describe('Test EditorService', () => {

expect(setValFn).toBeCalled();
expect(setValFn.mock.calls[0][0]).toBe('test');

// Update editor text even not current tab
editor.setActive(1, 1);
act(() => {
editor.updateTab({
id: mockTab.id,
data: {
value: 'test2',
},
});
});

expect(setValFn).toBeCalledTimes(2);
expect(setValFn.mock.calls[1][0]).toBe('test2');
});

test('Should prevent update editor text if current tab with renderPane', () => {
Expand Down Expand Up @@ -614,4 +633,13 @@ describe('Test EditorService', () => {
expect(testFn.mock.calls[0]).toEqual(expected);
});
});

test('Listen to the editorInstance mount', () => {
const editor = new EditorService();
expectFnCalled((testFn) => {
editor.onEditorInstanceMount(testFn);
editor.emit(EditorEvent.onEditorInstanceMount, {});
expect(testFn.mock.calls[0][0]).toEqual({});
});
});
});
35 changes: 28 additions & 7 deletions src/services/workbench/editorService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'reflect-metadata';
import { singleton, container } from 'tsyringe';
import { cloneDeep, isString } from 'lodash';
import { cloneDeep } from 'lodash';
import { Component } from 'mo/react';
import {
EditorModel,
Expand Down Expand Up @@ -205,6 +205,12 @@ export interface IEditorService extends Component<IEditor> {
* @param tabId
*/
getGroupIdByTab(tabId: UniqueId): UniqueId | null;
/**
* Listen to the editor instance mount event
*/
onEditorInstanceMount(
callback: (editorInstance: MonacoEditor.IStandaloneCodeEditor) => void
): void;
}
@singleton()
export class EditorService
Expand Down Expand Up @@ -325,11 +331,15 @@ export class EditorService
updatedTab = Object.assign(tabData, tab);
}
if (group.activeTab === tab.id) {
isString(editorValue) &&
!tabData?.renderPane &&
this.setGroupEditorValue(group, editorValue);
updatedTab = Object.assign(group.tab, tab);
}
// Update model's value
const model = MonacoEditor.getModel(
Uri.parse(tab.id.toString())
);
if (model) {
model.setValue(editorValue || '');
}
this.updateGroup(groupId, group);

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

if (group.activeTab === tab.id) {
isString(editorValue) &&
!tabData?.renderPane &&
this.setGroupEditorValue(group, editorValue);
updatedTab = Object.assign(group.tab, tab);
}

// Update model's value
const model = MonacoEditor.getModel(
Uri.parse(tab.id.toString())
);
if (model) {
model.setValue(editorValue || '');
}
});

if (current?.activeTab === tab.id) {
Expand Down Expand Up @@ -777,4 +792,10 @@ export class EditorService
) {
this.subscribe(EditorEvent.onActionsClick, callback);
}

public onEditorInstanceMount(
callback: (editorInstance: MonacoEditor.IStandaloneCodeEditor) => void
) {
this.subscribe(EditorEvent.onEditorInstanceMount, callback);
}
}
4 changes: 4 additions & 0 deletions stories/extensions/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -229,5 +229,9 @@ export const ExtendsTestPane: IExtension = {

molecule.layout.setAuxiliaryBar(!tab);
});

molecule.editor.onEditorInstanceMount((editorInstance) => {
console.log('editorInstance:', editorInstance);
});
},
};
16 changes: 16 additions & 0 deletions stories/extensions/test/testPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,19 @@ export type GenericClassDecorator<T> = (target: T) => void;`,
}
};

const updateEditor = function () {
// get first inactive tab
const state = molecule.editor.getState();
const firstInactiveTab = state.current?.data?.find(
(tab) => tab.id !== state.current?.activeTab
);
if (firstInactiveTab) {
firstInactiveTab.data.value +=
'\nconst firstInactiveTab = "test";\n';
molecule.editor.updateTab(firstInactiveTab, state.current?.id);
}
};

const newPane = function () {
const key = shortRandomId();
const name = `pane-${key}.ts`;
Expand Down Expand Up @@ -481,6 +494,9 @@ PARTITIONED BY (DE STRING) LIFECYCLE 1000;
<div style={{ marginBottom: 50 }}>
<h2>Simple examples:</h2>
<Button onClick={newEditor}>New Editor</Button>
<Button onClick={updateEditor}>
Update Inactive Editor{`'`}s value
</Button>
<Button onClick={toggleEditorStatus}>
Toggle Editor status
</Button>
Expand Down

0 comments on commit ce46463

Please sign in to comment.