Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix model's value not change and add onEditorInstanceMount event #882

Merged
merged 4 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/components/scrollBar/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

&--hidden {
opacity: 0;
pointer-events: none;
transition: opacity 0.8s linear;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/components/tree/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export interface ITreeProps {
expandKeys?: UniqueId[];
loadedKeys?: string[];
activeKey?: UniqueId;
onExpand?: (expandedKeys: React.Key[], node: ITreeNodeItemProps) => void;
onExpand?: (expandedKeys: UniqueId[], node: ITreeNodeItemProps) => void;
onSelect?: (node: ITreeNodeItemProps, isUpdate?) => void;
onTreeClick?: () => void;
renderTitle?: (
Expand Down
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 @@
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
mumiao marked this conversation as resolved.
Show resolved Hide resolved
};

public onClickActions = (action: IEditorActionsProps) => {
Expand Down Expand Up @@ -374,4 +376,10 @@
}
}
}

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
Loading