Skip to content

Commit f97e00c

Browse files
authored
fix: update tab set value (#750)
* feat: when the editor text changes, restrict updates through Uri comparison * fix: update the editor value with the updateTab method (#714) * test: add setGroupEditorValue test case
1 parent 0f97d83 commit f97e00c

File tree

3 files changed

+61
-2
lines changed

3 files changed

+61
-2
lines changed

src/controller/editor.tsx

+4
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,10 @@ export class EditorController extends Controller implements IEditorController {
255255
const tab = current?.tab;
256256
if (!tab) return;
257257

258+
const currentEditorUri = current.editorInstance?.getModel()?.uri;
259+
const updateEditorUri = editorInstance?.getModel()?.uri;
260+
if (currentEditorUri?.path !== updateEditorUri?.path) return;
261+
258262
const newValue = editorInstance.getModel()?.getValue();
259263
const updatedTab = {
260264
...tab,

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

+34
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { container } from 'tsyringe';
55
import { EditorEvent, IEditorTab } from 'mo/model';
66
import { expectFnCalled } from '@test/utils';
77
import { modules } from 'mo/services/builtinService/const';
8+
import { editor as MonacoEditor } from 'mo/monaco';
89
import { cloneDeep } from 'lodash';
910

1011
describe('Test EditorService', () => {
@@ -155,6 +156,39 @@ describe('Test EditorService', () => {
155156
).toBe('updated1');
156157
});
157158

159+
test('Set the editor text for a specific group', () => {
160+
const editor = new EditorService();
161+
const tabData = {
162+
...mockTab,
163+
data: {
164+
value: 'tabData',
165+
},
166+
};
167+
editor.open(tabData);
168+
const { groups } = editor.getState();
169+
if (groups) {
170+
const modifyText = 'newValue';
171+
const editorData = {
172+
value: '',
173+
};
174+
const editorInstance = {} as MonacoEditor.IStandaloneCodeEditor;
175+
editorInstance.getModel = jest.fn(() => {
176+
return {
177+
getValue: () => editorData.value,
178+
setValue: (newValue: string) => {
179+
editorData.value = newValue;
180+
},
181+
} as MonacoEditor.ITextModel;
182+
});
183+
184+
groups[0].editorInstance = editorInstance;
185+
editor.setGroupEditorValue(groups[0], modifyText);
186+
expect(groups[0].editorInstance?.getModel()?.getValue()).toBe(
187+
modifyText
188+
);
189+
}
190+
});
191+
158192
test('Close a tab', () => {
159193
const editor: any = new EditorService();
160194
editor.disposeModel = jest.fn();

src/services/workbench/editorService.ts

+23-2
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 from 'lodash/cloneDeep';
3+
import { cloneDeep, isString } from 'lodash';
44
import { Component } from 'mo/react';
55
import {
66
EditorModel,
@@ -41,6 +41,12 @@ export interface IEditorService extends Component<IEditor> {
4141
* @param groupId
4242
*/
4343
updateTab(tab: IEditorTab, groupId?: UniqueId): IEditorTab;
44+
/**
45+
* Updates the editor content for a specific group
46+
* @param group The editorInstance is required
47+
* @param value
48+
*/
49+
setGroupEditorValue(group: IEditorGroup, value: string): void;
4450
/**
4551
* Specify the Entry page of Workbench
4652
*/
@@ -308,6 +314,7 @@ export class EditorService
308314

309315
public updateTab(tab: IEditorTab, groupId?: UniqueId): IEditorTab {
310316
let updatedTab;
317+
const editorValue = tab?.data?.value;
311318
if (groupId) {
312319
const group = this.getGroupById(groupId);
313320

@@ -317,8 +324,9 @@ export class EditorService
317324
if (tabData) {
318325
updatedTab = Object.assign(tabData, tab);
319326
}
320-
321327
if (group.activeTab === tab.id) {
328+
isString(editorValue) &&
329+
this.setGroupEditorValue(group, editorValue);
322330
updatedTab = Object.assign(group.tab, tab);
323331
}
324332
this.updateGroup(groupId, group);
@@ -336,6 +344,8 @@ export class EditorService
336344
}
337345

338346
if (group.activeTab === tab.id) {
347+
isString(editorValue) &&
348+
this.setGroupEditorValue(group, editorValue);
339349
updatedTab = Object.assign(group.tab, tab);
340350
}
341351
});
@@ -349,9 +359,20 @@ export class EditorService
349359
groups,
350360
});
351361
}
362+
352363
return updatedTab;
353364
}
354365

366+
public setGroupEditorValue(group: IEditorGroup, value: string) {
367+
const model = group.editorInstance?.getModel();
368+
if (!model) return;
369+
370+
const currentValue = model?.getValue();
371+
if (currentValue !== value) {
372+
model?.setValue(value);
373+
}
374+
}
375+
355376
public closeTab(tabId: UniqueId, groupId: UniqueId) {
356377
const groupIndex = this.getGroupIndexById(groupId);
357378
if (groupIndex === -1) return;

0 commit comments

Comments
 (0)