Skip to content

Commit aafa07d

Browse files
authored
fix: filename missing and file path in EditorTree is not updated (#659)
1 parent e36b8ad commit aafa07d

File tree

3 files changed

+106
-10
lines changed

3 files changed

+106
-10
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import React from 'react';
2+
import '@testing-library/jest-dom';
3+
import molecule, { MoleculeProvider, Workbench } from 'mo';
4+
import { cleanup, fireEvent, render } from '@testing-library/react';
5+
import type { ITreeNodeItemProps } from 'mo/components';
6+
import type { IEditorTab } from 'mo/model/workbench/editor';
7+
8+
const testFileId = 'testFileId';
9+
const testFileName = 'testFileName';
10+
const mockTreeData: ITreeNodeItemProps[] = [
11+
{
12+
id: 'root',
13+
name: 'root',
14+
isLeaf: false,
15+
children: [
16+
{
17+
id: testFileId,
18+
name: testFileName,
19+
isLeaf: true,
20+
isEditable: true,
21+
},
22+
],
23+
},
24+
];
25+
const mockTabData: IEditorTab = {
26+
id: testFileId,
27+
name: testFileName,
28+
data: {
29+
value: '',
30+
path: testFileName,
31+
},
32+
};
33+
34+
describe('folderTree extension', () => {
35+
afterEach(cleanup);
36+
37+
test('Execute the listener function of onUpdateFileName', () => {
38+
const { getByRole } = render(
39+
<MoleculeProvider>
40+
<Workbench />
41+
</MoleculeProvider>
42+
);
43+
44+
molecule.folderTree.setState({ folderTree: { data: mockTreeData } });
45+
expect(molecule.folderTree.getState().folderTree?.data).toEqual(
46+
mockTreeData
47+
);
48+
49+
molecule.editor.open(mockTabData);
50+
expect(molecule.editor.getState().current?.data?.length).toBe(1);
51+
52+
const input = getByRole('input');
53+
expect(input).toBeTruthy();
54+
55+
// Update filename to a valid name
56+
const mockEnterValue = 'test-enter';
57+
fireEvent.keyDown(input, {
58+
keyCode: 13,
59+
target: { value: mockEnterValue },
60+
});
61+
expect(molecule.editor.getState().current?.tab?.name).toBe(
62+
mockEnterValue
63+
);
64+
65+
molecule.folderTree.update({
66+
id: testFileId,
67+
isEditable: true,
68+
});
69+
const input2 = getByRole('input');
70+
expect(input2).toBeTruthy();
71+
72+
// Update filename to an invalid name
73+
const mockEmptyValue = '';
74+
fireEvent.keyDown(input2, {
75+
keyCode: 13,
76+
target: { value: mockEmptyValue },
77+
});
78+
expect(molecule.editor.getState().current?.tab?.name).toBe(
79+
mockEnterValue
80+
);
81+
});
82+
});

src/extensions/folderTree/index.tsx

+22-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import molecule from 'mo';
22
import { IExtension } from 'mo/model/extension';
3+
import {
4+
IEditorTab,
5+
BuiltInEditorTabDataType,
6+
} from 'mo/model/workbench/editor';
37

48
export const ExtendsFolderTree: IExtension = {
59
id: 'ExtendsFolderTree',
@@ -17,12 +21,29 @@ export const ExtendsFolderTree: IExtension = {
1721
if (name) {
1822
const newLoc = location?.split('/') || [];
1923
newLoc[newLoc.length - 1] = name;
24+
const newLocation = newLoc.join('/');
2025
molecule.folderTree.update({
2126
...file,
2227
id,
23-
location: newLoc.join('/'),
28+
location: newLocation,
2429
isEditable: false,
2530
});
31+
32+
const groupId = molecule.editor.getGroupIdByTab(id.toString());
33+
const isValidGroupId = !!groupId || groupId === 0;
34+
if (isValidGroupId) {
35+
const prevTab =
36+
molecule.editor.getTabById<BuiltInEditorTabDataType>(
37+
id.toString(),
38+
groupId
39+
);
40+
const newTab: IEditorTab = { id: id.toString(), name };
41+
const prevTabData = prevTab?.data;
42+
if (prevTabData && prevTabData.path) {
43+
newTab.data = { ...prevTabData, path: newLocation };
44+
}
45+
molecule.editor.updateTab(newTab);
46+
}
2647
} else {
2748
const node = molecule.folderTree.get(id);
2849
if (node?.name) {
@@ -34,14 +55,6 @@ export const ExtendsFolderTree: IExtension = {
3455
molecule.folderTree.remove(id);
3556
}
3657
}
37-
38-
const isOpened = molecule.editor.isOpened(id.toString());
39-
if (isOpened) {
40-
molecule.editor.updateTab({
41-
id: id.toString(),
42-
name,
43-
});
44-
}
4558
});
4659
},
4760
dispose() {},

src/model/workbench/editor.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ export enum EditorEvent {
1818
onActionsClick = 'editor.actionsClick',
1919
OnSplitEditorRight = 'editor.splitEditorRight',
2020
}
21-
interface BuiltInEditorTabDataType {
21+
22+
export interface BuiltInEditorTabDataType {
2223
language?: string | undefined;
2324
path?: string;
2425
value?: string;

0 commit comments

Comments
 (0)