Skip to content

Commit ffa5708

Browse files
committed
feat: open file
open file
1 parent d3f334a commit ffa5708

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

src/components/tree/index.tsx

+10-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export function generateTreeId(id?: string): string {
2020
export interface ITreeNodeItem {
2121
key?: string | number;
2222
title?: React.ReactNode | string;
23+
name?: string;
2324
type?: 'folder' | 'file';
2425
contextMenu?: IMenuItem[];
2526
children?: ITreeNodeItem[];
@@ -29,6 +30,7 @@ export interface ITreeNodeItem {
2930
}
3031
export interface ITreeProps extends TreeProps {
3132
data: ITreeNodeItem[];
33+
onSelectFile?: (IMenuItem) => void
3234
className?: string;
3335
}
3436
const TreeView: React.FunctionComponent<ITreeProps> = (props: ITreeProps) => {
@@ -156,7 +158,7 @@ const TreeView: React.FunctionComponent<ITreeProps> = (props: ITreeProps) => {
156158
<TreeNode
157159
data-id={generateTreeId(item.id)}
158160
data={item}
159-
title={item.title}
161+
title={item.name}
160162
key={item.key}
161163
icon={<Icon type={item.icon} />}
162164
>
@@ -175,7 +177,13 @@ const TreeView: React.FunctionComponent<ITreeProps> = (props: ITreeProps) => {
175177
onRightClick={({ event, node }: any) => {
176178
setActiveData(node.data)
177179
}}
178-
onSelect={(selectedKeys, e) => { console.log('select', selectedKeys, e) }}
180+
onSelect={(selectedKeys, e: any) => {
181+
console.log('select', selectedKeys, e)
182+
const isFile = e.node.data.type === 'file';
183+
if (isFile && props.onSelectFile) {
184+
props.onSelectFile(e.node.data)
185+
}
186+
}}
179187
>
180188
{renderTreeNodes(treeData)}
181189
</Tree>

src/extensions/explore/tree.tsx

+14-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11

22
import * as React from 'react';
33
import { memo } from 'react';
4+
import { activityBarService, editorService } from 'mo';
45
import Tree from 'mo/components/tree';
56
// service
7+
const onSelectFile = function (fileData) {
8+
const tabData = {
9+
...fileData,
10+
activeTab: fileData.id,
11+
modified: false
12+
};
13+
editorService.open(tabData, tabData.activeTab);
14+
};
615
const TreeView: React.FunctionComponent<any> = (props: any) => {
7-
return <Tree prefixCls="rc-tree" data={props.data} />
16+
return <Tree
17+
prefixCls="rc-tree"
18+
data={props.data}
19+
onSelectFile={onSelectFile}
20+
/>
821
}
922
export default memo(TreeView);

src/extensions/explore/treeMock.ts

+58-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ export const data = [
22
{
33
id: '1',
44
title: 'folder',
5+
name: 'folder',
56
key: 'folder',
67
type: 'folder',
78
contextMenu: [
@@ -15,55 +16,110 @@ export const data = [
1516
{
1617
id: '2',
1718
title: 'abccccccccc',
19+
name: 'abccccccccc',
1820
key: 'abccccccccc',
1921
type: 'folder',
2022
children: [
2123
{
2224
id: '3',
2325
title: 'test.txt',
26+
name: 'test.txt',
2427
key: 'test.txt',
2528
type: 'file',
26-
icon: 'symbol-file'
29+
icon: 'symbol-file',
30+
value: `create table if not exists ods_order_header (
31+
order_header_id string comment '订单头id'
32+
,order_date bigint comment '订单日期'
33+
,shop_id string comment '店铺id'
34+
,customer_id string comment '客户id'
35+
,order_status bigint comment '订单状态'
36+
,pay_date bigint comment '支付日期'
37+
38+
)comment '销售订单明细表'`
2739
},
2840
{
2941
id: '4',
3042
title: 'test.js',
43+
name: 'test.js',
3144
key: 'test.js',
3245
type: 'file',
3346
icon: 'file-binary',
47+
value: `function mix(constructor) {
48+
/// <summary locid="15">
49+
/// Defines a class using the given constructor and the union of the set of instance members
50+
/// specified by all the mixin objects. The mixin parameter list can be of variable length.
51+
/// </summary>
52+
/// <param name="constructor" locid="9">
53+
/// A constructor function that will be used to instantiate this class.
54+
/// </param>
55+
/// <returns locid="12">
56+
/// The newly defined class.
57+
/// </returns>
58+
constructor = constructor || function () { };
59+
var i, len;
60+
for (i = 0, len = arguments.length; i < len; i++) {
61+
initializeProperties(constructor.prototype, arguments[i]);
62+
}
63+
return constructor;
64+
}`
3465
},
3566
{
3667
id: '5',
3768
title: 'test.html',
69+
name: 'test.html',
3870
key: 'test.html',
3971
type: 'file',
4072
icon: 'file-code',
73+
value: `<!DOCTYPE HTML>
74+
<!--Example of comments in HTML-->
75+
<html>
76+
<head>
77+
<!--This is the head section-->
78+
<title>HTML Sample</title>
79+
<meta charset="utf-8">
80+
81+
<!--This is the script tag-->
82+
<script type="text/javascript">
83+
function ButtonClick(){
84+
// Example of comments in JavaScript
85+
window.alert("I'm an alert sample!");
86+
}
87+
</script>
88+
</head>
89+
<body>
90+
</body>
91+
</html>
92+
`
4193
},
4294
],
4395
},
4496
{
4597
id: '6',
4698
title: 'xyz',
99+
name: 'xyz',
47100
key: 'xyz',
48101
type: 'folder',
49102
children: [
50103
{
51104
id: '7',
52105
title: 'test.pdf',
106+
name: 'test.pdf',
53107
key: 'test.pdf',
54108
type: 'file',
55109
icon: 'file-pdf',
56110
},
57111
{
58112
id: '8',
59113
title: 'test.media',
114+
name: 'test.media',
60115
key: 'test.media',
61116
type: 'file',
62117
icon: 'file-media',
63118
},
64119
{
65120
id: '9',
66121
title: 'test.zip',
122+
name: 'test.zip',
67123
key: 'test.zip',
68124
type: 'file',
69125
icon: 'file-zip',
@@ -73,6 +129,7 @@ export const data = [
73129
{
74130
id: '10',
75131
title: 'file.yaml',
132+
name: 'file.yaml',
76133
key: 'file.yaml',
77134
type: 'file',
78135
},

0 commit comments

Comments
 (0)