From ce317597462f5c10571f132023de211e106f6362 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Fri, 27 Nov 2020 10:00:30 +0100 Subject: [PATCH] Async/await for files --- .../file-explorer/src/lib/file-explorer.tsx | 127 +++++++++++++----- .../src/lib/tree-view-item/tree-view-item.tsx | 4 +- libs/remix-ui/tree-view/src/types/index.ts | 5 +- 3 files changed, 95 insertions(+), 41 deletions(-) diff --git a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx index 571a133cb6..19384724d6 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -1,5 +1,6 @@ import React, { useEffect, useState } from 'react' // 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 { FileExplorerProps } from './types' @@ -75,7 +76,7 @@ export const FileExplorer = (props: FileExplorerProps) => { } const [state, setState] = useState({ - focusElement: null, + focusElement: [], focusPath: null, menuItems: [ { @@ -106,41 +107,42 @@ export const FileExplorer = (props: FileExplorerProps) => { publishToGist, createNewFile }, - fileManager: null + fileManager: null, + ctrlKey: false }) useEffect(() => { - const fileManager = registry.get('filemanager').api + (async () => { + const fileManager = registry.get('filemanager').api + const files = await resolveDirectory(name) - setState(prevState => { - return { ...prevState, fileManager } - }) - resolveDirectory(name) + setState(prevState => { + return { ...prevState, fileManager, files } + }) + })() }, []) - const resolveDirectory = (folderPath) => { - const folderIndex = state.files.findIndex(({ path }) => path === folderPath) + const resolveDirectory = async (folderPath): Promise<{ path: string, name: string, isDirectory: boolean }[]> => { + return new Promise((resolve) => { + const folderIndex = state.files.findIndex(({ path }) => path === folderPath) - if (folderIndex === -1) { - files.resolveDirectory(folderPath, (error, fileTree) => { - if (error) console.error(error) - const files = normalize(folderPath, fileTree) + if (folderIndex === -1) { + files.resolveDirectory(folderPath, (error, fileTree) => { + if (error) console.error(error) + const files = normalize(folderPath, fileTree) - setState(prevState => { - return { ...prevState, files } + resolve(files) }) - }) - } else { - files.resolveDirectory(folderPath, (error, fileTree) => { - if (error) console.error(error) - const files = state.files + } else { + files.resolveDirectory(folderPath, (error, fileTree) => { + if (error) console.error(error) + const files = state.files - files[folderIndex].child = normalize(folderPath, fileTree) - setState(prevState => { - return { ...prevState, files } + files[folderIndex].child = normalize(folderPath, fileTree) + resolve(files) }) - }) - } + } + }) } 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 files = [] 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 = () => { let items if (state.menuItems) { @@ -401,7 +420,16 @@ export const FileExplorer = (props: FileExplorerProps) => { const renderFiles = (file, index) => { if (file.isDirectory) { return ( - { resolveDirectory(file.path) }}> + // + { e.stopPropagation(); handleClickFolder(file.path) }} + labelClass={ state.focusElement.findIndex(item => item === file.path) !== -1 ? 'bg-secondary' : '' } + > { file.child ? { file.child.map((file, index) => { @@ -411,31 +439,56 @@ export const FileExplorer = (props: FileExplorerProps) => { : } + // ) } else { return ( + // { state.fileManager.open(file.path) }} + onClick={(event) => { + handleClickFile(event, file.path) + }} icon='fa fa-file' + labelClass={ state.focusElement.findIndex(item => item === file.path) !== -1 ? 'bg-secondary' : '' } /> + // ) } } return ( -
+
{ + if (e.ctrlKey) { + console.log('TRUE') + setState(prevState => { + return { ...prevState, ctrlKey: true } + }) + } + }} + onKeyUp={() => { + console.log('FALSE') + setState(prevState => { + return { ...prevState, ctrlKey: false } + }) + }} + > - - { - state.files.map((file, index) => { - return renderFiles(file, index) - }) - } - + {/* false}> */} +
+ + { + state.files.map((file, index) => { + return renderFiles(file, index) + }) + } + +
+ {/*
*/}
diff --git a/libs/remix-ui/tree-view/src/lib/tree-view-item/tree-view-item.tsx b/libs/remix-ui/tree-view/src/lib/tree-view-item/tree-view-item.tsx index e04e738212..25c0fed0d7 100644 --- a/libs/remix-ui/tree-view/src/lib/tree-view-item/tree-view-item.tsx +++ b/libs/remix-ui/tree-view/src/lib/tree-view-item/tree-view-item.tsx @@ -4,7 +4,7 @@ import { TreeViewItemProps } from '../../types' import './tree-view-item.css' 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) useEffect(() => { @@ -13,7 +13,7 @@ export const TreeViewItem = (props: TreeViewItemProps) => { return (
  • -
    setIsExpanded(!isExpanded)}> +
    setIsExpanded(!isExpanded)}> { children ?
    : icon ?
    : null } { label } diff --git a/libs/remix-ui/tree-view/src/types/index.ts b/libs/remix-ui/tree-view/src/types/index.ts index 5650b85191..f3e9346004 100644 --- a/libs/remix-ui/tree-view/src/types/index.ts +++ b/libs/remix-ui/tree-view/src/types/index.ts @@ -8,9 +8,10 @@ export interface TreeViewItemProps { id?: string, label: string | number | React.ReactNode, expand?: boolean, - onClick?: VoidFunction, + onClick?: (...args: any) => void, className?: string, iconX?: string, iconY?: string, - icon?: string + icon?: string, + labelClass?: string }