Async/await for files

pull/5370/head
ioedeveloper 4 years ago
parent e08cba6225
commit aed96d8475
  1. 85
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx
  2. 4
      libs/remix-ui/tree-view/src/lib/tree-view-item/tree-view-item.tsx
  3. 5
      libs/remix-ui/tree-view/src/types/index.ts

@ -1,5 +1,6 @@
import React, { useEffect, useState } from 'react' // eslint-disable-line import React, { useEffect, useState } from 'react' // eslint-disable-line
import { TreeView, TreeViewItem } from '@remix-ui/tree-view' // eslint-disable-line import { TreeView, TreeViewItem } from '@remix-ui/tree-view' // eslint-disable-line
import Draggable from 'react-draggable' // eslint-disable-line
import * as helper from '../../../../../apps/remix-ide/src/lib/helper' import * as helper from '../../../../../apps/remix-ide/src/lib/helper'
import { FileExplorerProps } from './types' import { FileExplorerProps } from './types'
@ -75,7 +76,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
const [state, setState] = useState({ const [state, setState] = useState({
focusElement: null, focusElement: [],
focusPath: null, focusPath: null,
menuItems: [ menuItems: [
{ {
@ -106,19 +107,23 @@ export const FileExplorer = (props: FileExplorerProps) => {
publishToGist, publishToGist,
createNewFile createNewFile
}, },
fileManager: null fileManager: null,
ctrlKey: false
}) })
useEffect(() => { useEffect(() => {
(async () => {
const fileManager = registry.get('filemanager').api const fileManager = registry.get('filemanager').api
const files = await resolveDirectory(name)
setState(prevState => { setState(prevState => {
return { ...prevState, fileManager } return { ...prevState, fileManager, files }
}) })
resolveDirectory(name) })()
}, []) }, [])
const resolveDirectory = (folderPath) => { const resolveDirectory = async (folderPath): Promise<{ path: string, name: string, isDirectory: boolean }[]> => {
return new Promise((resolve) => {
const folderIndex = state.files.findIndex(({ path }) => path === folderPath) const folderIndex = state.files.findIndex(({ path }) => path === folderPath)
if (folderIndex === -1) { if (folderIndex === -1) {
@ -126,9 +131,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
if (error) console.error(error) if (error) console.error(error)
const files = normalize(folderPath, fileTree) const files = normalize(folderPath, fileTree)
setState(prevState => { resolve(files)
return { ...prevState, files }
})
}) })
} else { } else {
files.resolveDirectory(folderPath, (error, fileTree) => { files.resolveDirectory(folderPath, (error, fileTree) => {
@ -136,11 +139,10 @@ export const FileExplorer = (props: FileExplorerProps) => {
const files = state.files const files = state.files
files[folderIndex].child = normalize(folderPath, fileTree) files[folderIndex].child = normalize(folderPath, fileTree)
setState(prevState => { resolve(files)
return { ...prevState, files }
})
}) })
} }
})
} }
const label = (data) => { const label = (data) => {
@ -159,7 +161,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
) )
} }
const normalize = (path, filesList) => { const normalize = (path, filesList): { path: string, name: string, isDirectory: boolean }[] => {
const folders = [] const folders = []
const files = [] const files = []
const prefix = path.split('/')[0] const prefix = path.split('/')[0]
@ -352,6 +354,23 @@ export const FileExplorer = (props: FileExplorerProps) => {
// } // }
// } // }
const handleClickFile = async (event, path) => {
event.stopPropagation()
await state.fileManager.open(path)
setState(prevState => {
return { ...prevState, focusElement: [path] }
})
}
const handleClickFolder = async (path) => {
console.log('path: ', path)
const files = await resolveDirectory(path)
setState(prevState => {
return { ...prevState, focusElement: [path], files }
})
}
const renderMenuItems = () => { const renderMenuItems = () => {
let items let items
if (state.menuItems) { if (state.menuItems) {
@ -401,7 +420,16 @@ export const FileExplorer = (props: FileExplorerProps) => {
const renderFiles = (file, index) => { const renderFiles = (file, index) => {
if (file.isDirectory) { if (file.isDirectory) {
return ( return (
<TreeViewItem id={`treeViewItem${file.path}`} iconX='pr-3 far fa-folder' iconY='pr-3 far fa-folder-open' key={index} label={label(file)} onClick={() => { resolveDirectory(file.path) }}> // <Draggable key={index} axis="y" bounds="parent">
<TreeViewItem
id={`treeViewItem${file.path}`}
iconX='pr-3 far fa-folder'
iconY='pr-3 far fa-folder-open'
key={`${file.path + index}`}
label={label(file)}
onClick={(e) => { e.stopPropagation(); handleClickFolder(file.path) }}
labelClass={ state.focusElement.findIndex(item => item === file.path) !== -1 ? 'bg-secondary' : '' }
>
{ {
file.child ? <TreeView id={`treeView${file.path}`} key={index}>{ file.child ? <TreeView id={`treeView${file.path}`} key={index}>{
file.child.map((file, index) => { file.child.map((file, index) => {
@ -411,24 +439,47 @@ export const FileExplorer = (props: FileExplorerProps) => {
</TreeView> : <TreeView id={`treeView${file.path}`} key={index} /> </TreeView> : <TreeView id={`treeView${file.path}`} key={index} />
} }
</TreeViewItem> </TreeViewItem>
// </Draggable>
) )
} else { } else {
return ( return (
// <Draggable key={index} axis="y" bounds="parent">
<TreeViewItem <TreeViewItem
id={`treeView${file.path}`} id={`treeViewItem${file.path}`}
key={index} key={index}
label={label(file)} label={label(file)}
onClick={() => { state.fileManager.open(file.path) }} onClick={(event) => {
handleClickFile(event, file.path)
}}
icon='fa fa-file' icon='fa fa-file'
labelClass={ state.focusElement.findIndex(item => item === file.path) !== -1 ? 'bg-secondary' : '' }
/> />
// </Draggable>
) )
} }
} }
return ( return (
<div> <div tabIndex={-1}
onKeyDown={(e) => {
if (e.ctrlKey) {
console.log('TRUE')
setState(prevState => {
return { ...prevState, ctrlKey: true }
})
}
}}
onKeyUp={() => {
console.log('FALSE')
setState(prevState => {
return { ...prevState, ctrlKey: false }
})
}}
>
<TreeView id='treeView'> <TreeView id='treeView'>
<TreeViewItem id="treeViewItem" label={renderMenuItems()} expand={true}> <TreeViewItem id="treeViewItem" label={renderMenuItems()} expand={true}>
{/* <Draggable onStart={() => false}> */}
<div>
<TreeView id='treeViewMenu'> <TreeView id='treeViewMenu'>
{ {
state.files.map((file, index) => { state.files.map((file, index) => {
@ -436,6 +487,8 @@ export const FileExplorer = (props: FileExplorerProps) => {
}) })
} }
</TreeView> </TreeView>
</div>
{/* </Draggable> */}
</TreeViewItem> </TreeViewItem>
</TreeView> </TreeView>
</div> </div>

@ -4,7 +4,7 @@ import { TreeViewItemProps } from '../../types'
import './tree-view-item.css' import './tree-view-item.css'
export const TreeViewItem = (props: TreeViewItemProps) => { export const TreeViewItem = (props: TreeViewItemProps) => {
const { id, children, label, expand, iconX = 'fas fa-caret-right', iconY = 'fas fa-caret-down', icon, ...otherProps } = props const { id, children, label, labelClass, expand, iconX = 'fas fa-caret-right', iconY = 'fas fa-caret-down', icon, ...otherProps } = props
const [isExpanded, setIsExpanded] = useState(false) const [isExpanded, setIsExpanded] = useState(false)
useEffect(() => { useEffect(() => {
@ -13,7 +13,7 @@ export const TreeViewItem = (props: TreeViewItemProps) => {
return ( return (
<li key={`treeViewLi${id}`} data-id={`treeViewLi${id}`} className='li_tv' {...otherProps}> <li key={`treeViewLi${id}`} data-id={`treeViewLi${id}`} className='li_tv' {...otherProps}>
<div key={`treeViewDiv${id}`} data-id={`treeViewDiv${id}`} className='d-flex flex-row align-items-center' onClick={() => setIsExpanded(!isExpanded)}> <div key={`treeViewDiv${id}`} data-id={`treeViewDiv${id}`} className={`d-flex flex-row align-items-center ${labelClass}`} onClick={() => setIsExpanded(!isExpanded)}>
{ children ? <div className={isExpanded ? `px-1 ${iconY} caret caret_tv` : `px-1 ${iconX} caret caret_tv`} style={{ visibility: children ? 'visible' : 'hidden' }}></div> : icon ? <div className={`pr-3 pl-1 ${icon} caret caret_tv`}></div> : null } { children ? <div className={isExpanded ? `px-1 ${iconY} caret caret_tv` : `px-1 ${iconX} caret caret_tv`} style={{ visibility: children ? 'visible' : 'hidden' }}></div> : icon ? <div className={`pr-3 pl-1 ${icon} caret caret_tv`}></div> : null }
<span className='w-100 pl-1'> <span className='w-100 pl-1'>
{ label } { label }

@ -8,9 +8,10 @@ export interface TreeViewItemProps {
id?: string, id?: string,
label: string | number | React.ReactNode, label: string | number | React.ReactNode,
expand?: boolean, expand?: boolean,
onClick?: VoidFunction, onClick?: (...args: any) => void,
className?: string, className?: string,
iconX?: string, iconX?: string,
iconY?: string, iconY?: string,
icon?: string icon?: string,
labelClass?: string
} }

Loading…
Cancel
Save