From ffa57087049831af9fb069e9d423674d35865a51 Mon Sep 17 00:00:00 2001 From: zhangtengjin <1257450210@qq.com> Date: Tue, 1 Dec 2020 20:14:17 +0800 Subject: [PATCH] feat: open file open file --- src/components/tree/index.tsx | 12 +++++- src/extensions/explore/tree.tsx | 15 +++++++- src/extensions/explore/treeMock.ts | 59 +++++++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 4 deletions(-) diff --git a/src/components/tree/index.tsx b/src/components/tree/index.tsx index 3635a4869..eb8b4dd7b 100644 --- a/src/components/tree/index.tsx +++ b/src/components/tree/index.tsx @@ -20,6 +20,7 @@ export function generateTreeId(id?: string): string { export interface ITreeNodeItem { key?: string | number; title?: React.ReactNode | string; + name?: string; type?: 'folder' | 'file'; contextMenu?: IMenuItem[]; children?: ITreeNodeItem[]; @@ -29,6 +30,7 @@ export interface ITreeNodeItem { } export interface ITreeProps extends TreeProps { data: ITreeNodeItem[]; + onSelectFile?: (IMenuItem) => void className?: string; } const TreeView: React.FunctionComponent = (props: ITreeProps) => { @@ -156,7 +158,7 @@ const TreeView: React.FunctionComponent = (props: ITreeProps) => { } > @@ -175,7 +177,13 @@ const TreeView: React.FunctionComponent = (props: ITreeProps) => { onRightClick={({ event, node }: any) => { setActiveData(node.data) }} - onSelect={(selectedKeys, e) => { console.log('select', selectedKeys, e) }} + onSelect={(selectedKeys, e: any) => { + console.log('select', selectedKeys, e) + const isFile = e.node.data.type === 'file'; + if (isFile && props.onSelectFile) { + props.onSelectFile(e.node.data) + } + }} > {renderTreeNodes(treeData)} diff --git a/src/extensions/explore/tree.tsx b/src/extensions/explore/tree.tsx index 8aaefe77d..d60eff17d 100644 --- a/src/extensions/explore/tree.tsx +++ b/src/extensions/explore/tree.tsx @@ -1,9 +1,22 @@ import * as React from 'react'; import { memo } from 'react'; +import { activityBarService, editorService } from 'mo'; import Tree from 'mo/components/tree'; // service +const onSelectFile = function (fileData) { + const tabData = { + ...fileData, + activeTab: fileData.id, + modified: false + }; + editorService.open(tabData, tabData.activeTab); +}; const TreeView: React.FunctionComponent = (props: any) => { - return + return } export default memo(TreeView); diff --git a/src/extensions/explore/treeMock.ts b/src/extensions/explore/treeMock.ts index e6301562a..2d3827e21 100644 --- a/src/extensions/explore/treeMock.ts +++ b/src/extensions/explore/treeMock.ts @@ -2,6 +2,7 @@ export const data = [ { id: '1', title: 'folder', + name: 'folder', key: 'folder', type: 'folder', contextMenu: [ @@ -15,41 +16,94 @@ export const data = [ { id: '2', title: 'abccccccccc', + name: 'abccccccccc', key: 'abccccccccc', type: 'folder', children: [ { id: '3', title: 'test.txt', + name: 'test.txt', key: 'test.txt', type: 'file', - icon: 'symbol-file' + icon: 'symbol-file', + value: `create table if not exists ods_order_header ( + order_header_id string comment '订单头id' + ,order_date bigint comment '订单日期' + ,shop_id string comment '店铺id' + ,customer_id string comment '客户id' + ,order_status bigint comment '订单状态' + ,pay_date bigint comment '支付日期' + +)comment '销售订单明细表'` }, { id: '4', title: 'test.js', + name: 'test.js', key: 'test.js', type: 'file', icon: 'file-binary', + value: `function mix(constructor) { + /// + /// Defines a class using the given constructor and the union of the set of instance members + /// specified by all the mixin objects. The mixin parameter list can be of variable length. + /// + /// + /// A constructor function that will be used to instantiate this class. + /// + /// + /// The newly defined class. + /// + constructor = constructor || function () { }; + var i, len; + for (i = 0, len = arguments.length; i < len; i++) { + initializeProperties(constructor.prototype, arguments[i]); + } + return constructor; + }` }, { id: '5', title: 'test.html', + name: 'test.html', key: 'test.html', type: 'file', icon: 'file-code', + value: ` + + + + + HTML Sample + + + + + + + + +` }, ], }, { id: '6', title: 'xyz', + name: 'xyz', key: 'xyz', type: 'folder', children: [ { id: '7', title: 'test.pdf', + name: 'test.pdf', key: 'test.pdf', type: 'file', icon: 'file-pdf', @@ -57,6 +111,7 @@ export const data = [ { id: '8', title: 'test.media', + name: 'test.media', key: 'test.media', type: 'file', icon: 'file-media', @@ -64,6 +119,7 @@ export const data = [ { id: '9', title: 'test.zip', + name: 'test.zip', key: 'test.zip', type: 'file', icon: 'file-zip', @@ -73,6 +129,7 @@ export const data = [ { id: '10', title: 'file.yaml', + name: 'file.yaml', key: 'file.yaml', type: 'file', },