Skip to content

Commit c505abe

Browse files
authored
fix: update foldertree new folder (#247)
1 parent 111a4cd commit c505abe

File tree

6 files changed

+106
-15
lines changed

6 files changed

+106
-15
lines changed

src/components/tree/index.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,31 @@ const TreeView = ({
168168
onRightClick?.(info);
169169
};
170170

171+
React.useLayoutEffect(() => {
172+
const cache: { path: string; data: ITreeNodeItemProps }[] = [];
173+
data.forEach((item) => {
174+
cache.push({ path: `${item.id}`, data: item });
175+
});
176+
177+
while (cache.length) {
178+
const { path, data } = cache.pop()!;
179+
const editableChild = data.children?.find(
180+
(child) => child.isEditable
181+
);
182+
if (editableChild) {
183+
treeRef.current?.setExpandedKeys(path.split('-'));
184+
break;
185+
} else {
186+
const children =
187+
data.children?.map((child) => ({
188+
path: `${path}-${child.id}`,
189+
data: child,
190+
})) || [];
191+
cache.push(...children);
192+
}
193+
}
194+
}, [data]);
195+
171196
return (
172197
<div className={classNames(prefixClaName('tree'), className)}>
173198
<div className={prefixClaName('tree', 'sidebar')}>

src/extensions/folderTree/index.tsx

+19-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export const ExtendsFolderTree: IExtension = {
4848
id: `${file.id}`?.split('_')?.[0],
4949
modified: false,
5050
data: {
51+
...(file.data || {}),
5152
value: file.content,
5253
path: file.location,
5354
language: extName,
@@ -92,12 +93,29 @@ export const ExtendsFolderTree: IExtension = {
9293
isEditable: false,
9394
});
9495
} else {
95-
tree.remove(id);
96+
// TODO: improve tree helper types
97+
const node = (tree.get(id) as unknown) as ITreeNodeItemProps;
98+
if (node.name) {
99+
tree.update(id, {
100+
isEditable: false,
101+
});
102+
} else {
103+
tree.remove(id);
104+
}
96105
}
106+
97107
if (index > -1) cloneData[index] = tree.obj;
98108
molecule.folderTree.setState({
99109
folderTree: { ...folderTree, data: cloneData },
100110
});
111+
112+
const isOpened = molecule.editor.isOpened(id.toString());
113+
if (isOpened) {
114+
molecule.editor.updateTab({
115+
id: id.toString(),
116+
name,
117+
});
118+
}
101119
if (file?.fileType === FileTypes.File && file.name) {
102120
// emit onSelectFile
103121
}

src/model/workbench/explorer/folderTree.tsx

+7
Original file line numberDiff line numberDiff line change
@@ -102,12 +102,15 @@ export class TreeNodeModel implements ITreeNodeItemProps {
102102
id?: number;
103103
name?: string;
104104
location?: string;
105+
isLeaf?: boolean;
105106
fileType?: FileType;
106107
children?: ITreeNodeItemProps[];
107108
icon?: string | React.ReactNode;
108109
isEditable?: boolean;
109110
content?: string;
110111

112+
data?: any;
113+
111114
constructor(props: ITreeNodeItemProps = {}) {
112115
const {
113116
id,
@@ -118,6 +121,8 @@ export class TreeNodeModel implements ITreeNodeItemProps {
118121
icon = '',
119122
isEditable = false,
120123
content = '',
124+
isLeaf = true,
125+
data,
121126
} = props;
122127
this.fileType = fileType;
123128
this.isEditable = isEditable;
@@ -127,6 +132,8 @@ export class TreeNodeModel implements ITreeNodeItemProps {
127132
this.children = children;
128133
this.icon = icon;
129134
this.content = content;
135+
this.data = data;
136+
this.isLeaf = isLeaf;
130137
}
131138
}
132139

src/services/workbench/editorService.ts

+49-13
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,19 @@ export interface IEditorService extends Component<IEditor> {
2626
tabId: string,
2727
group: IEditorGroup
2828
): IEditorTab<T> | undefined;
29-
updateTab(tab: IEditorTab, groupId: number): IEditorTab;
29+
/**
30+
*
31+
* @param groupId If not provided, molecule will update in all group
32+
*/
33+
updateTab(tab: IEditorTab, groupId?: number): IEditorTab;
3034
/**
3135
* Specify a entry page for editor
3236
*/
3337
setEntry(component: React.ReactNode): void;
38+
/**
39+
* Returns whether a specific tab exists
40+
*/
41+
isOpened(tabId: string): boolean;
3442
closeTab(tabId: string, groupId: number): void;
3543
closeOthers(tab: IEditorTab, groupId: number): void;
3644
closeToRight(tab: IEditorTab, groupId: number): void;
@@ -91,6 +99,11 @@ export class EditorService
9199
});
92100
}
93101

102+
public isOpened(tabId: string): boolean {
103+
const { groups = [] } = this.state;
104+
return groups.some((group) => this.getTabById(tabId, group));
105+
}
106+
94107
public updateGroupActions(actions: IMenuItemProps[]): void {
95108
this.groupActions = actions;
96109
}
@@ -112,24 +125,47 @@ export class EditorService
112125
return this.state.current?.editorInstance;
113126
}
114127

115-
public updateTab(tab: IEditorTab, groupId: number): IEditorTab {
116-
const { groups = [] } = this.state;
117-
const index = this.getGroupIndexById(groupId);
128+
public updateTab(tab: IEditorTab, groupId?: number): IEditorTab {
129+
if (groupId) {
130+
const group = this.getGroupById(groupId);
118131

119-
if (index > -1) {
120-
const group = groups[index];
132+
if (group?.data?.length) {
133+
const tabData = group.data.find(searchById(tab.id));
121134

122-
if (group.data && group.data.length > 0) {
123-
const tabIndex = group.data!.findIndex(searchById(tab.id));
135+
if (tabData) {
136+
Object.assign(tabData, tab);
137+
}
124138

125-
if (tabIndex > -1) {
126-
const tabData = group.data![tabIndex];
127-
const newTab = Object.assign({}, tabData, tab);
128-
group.data![tabIndex] = newTab;
139+
if (group.activeTab === tab.id) {
140+
Object.assign(group.tab, tab);
141+
}
142+
this.updateGroup(groupId, group);
129143

130-
this.render();
144+
if (groupId === this.state.current?.id) {
145+
this.updateCurrentGroup(group);
131146
}
132147
}
148+
} else {
149+
const { groups = [], current } = this.state;
150+
groups.forEach((group) => {
151+
const tabData = this.getTabById(tab.id!, group);
152+
if (tabData) {
153+
Object.assign(tabData, tab);
154+
}
155+
156+
if (group.activeTab === tab.id) {
157+
Object.assign(group.tab, tab);
158+
}
159+
});
160+
161+
if (current?.activeTab === tab.id) {
162+
Object.assign(current!.tab, tab);
163+
}
164+
165+
this.setState({
166+
current,
167+
groups,
168+
});
133169
}
134170
return tab;
135171
}

src/services/workbench/explorer/folderTreeService.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'reflect-metadata';
22
import { singleton, container } from 'tsyringe';
3+
import cloneDeep from 'lodash/cloneDeep';
34
import { Component } from 'mo/react/component';
45
import {
56
FileTypes,
@@ -91,7 +92,10 @@ export class FolderTreeService
9192
}
9293
cloneData[index] = tree.obj;
9394
this.setState({
94-
folderTree: { ...this.state.folderTree, data: cloneData },
95+
folderTree: {
96+
...this.state.folderTree,
97+
data: cloneDeep(cloneData),
98+
},
9599
});
96100
} else {
97101
console.warn('Please check id again, there is not folder tree');

stories/extensions/test/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ export const ExtendTestPane: IExtension = {
106106
id: randomId(),
107107
name: '',
108108
fileType: FileTypes.Folder,
109+
isLeaf: false,
109110
isEditable: true,
110111
})
111112
);

0 commit comments

Comments
 (0)