Resolve directory

pull/1140/head
ioedeveloper 4 years ago
parent ee357b65da
commit 99395f7ead
  1. 17
      libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts
  2. 48
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx
  3. 49
      libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts
  4. 6
      package-lock.json
  5. 1
      package.json

@ -39,13 +39,13 @@ const normalize = (filesList): any => {
path = path.replace(/^\/|\/$/g, '') // remove first and last slash
if (filesList[key].isDirectory) {
folders[key] = {
folders[extractNameFromKey(key)] = {
path,
name: extractNameFromKey(path),
isDirectory: filesList[key].isDirectory
}
} else {
files[key] = {
files[extractNameFromKey(key)] = {
path,
name: extractNameFromKey(path),
isDirectory: filesList[key].isDirectory
@ -140,3 +140,16 @@ export const setProvider = (provider) => (dispatch: React.Dispatch<any>) => {
dispatch(fetchProviderError('No provider available'))
}
}
export const setCurrentWorkspace = (name: string) => {
return {
type: 'SET_CURRENT_WORKSPACE',
payload: name
}
}
export const setWorkspace = (name: string) => (dispatch: React.Dispatch<any>) => {
if (name) {
dispatch(setCurrentWorkspace(name))
}
}

@ -8,7 +8,7 @@ import { FileExplorerMenu } from './file-explorer-menu' // eslint-disable-line
import { FileExplorerContextMenu } from './file-explorer-context-menu' // eslint-disable-line
import { FileExplorerProps, File } from './types'
import { fileSystemReducer, fileSystemInitialState } from './reducers/fileSystem'
import { fetchDirectory, setProvider, resolveDirectory } from './actions/fileSystem'
import { fetchDirectory, setProvider, resolveDirectory, setWorkspace } from './actions/fileSystem'
import * as helper from '../../../../../apps/remix-ide/src/lib/helper'
import QueryParams from '../../../../../apps/remix-ide/src/lib/query-params'
@ -109,6 +109,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
useEffect(() => {
if (props.filesProvider) {
setProvider(props.filesProvider)(dispatch)
setWorkspace(props.name)(dispatch)
}
}, [props.filesProvider])
@ -216,45 +217,6 @@ export const FileExplorer = (props: FileExplorerProps) => {
}
}, [state.modals])
// const resolveDirectory = async (folderPath, dir: File[], isChild = false): Promise<File[]> => {
// if (!isChild && (state.focusEdit.element === '/blank') && state.focusEdit.isNew && (dir.findIndex(({ path }) => path === '/blank') === -1)) {
// dir = state.focusEdit.type === 'file' ? [...dir, {
// path: state.focusEdit.element,
// name: '',
// isDirectory: false
// }] : [{
// path: state.focusEdit.element,
// name: '',
// isDirectory: true
// }, ...dir]
// }
// dir = await Promise.all(dir.map(async (file) => {
// if (file.path === folderPath) {
// if ((extractParentFromKey(state.focusEdit.element) === folderPath) && state.focusEdit.isNew) {
// file.child = state.focusEdit.type === 'file' ? [...await fetchDirectoryContent(folderPath), {
// path: state.focusEdit.element,
// name: '',
// isDirectory: false
// }] : [{
// path: state.focusEdit.element,
// name: '',
// isDirectory: true
// }, ...await fetchDirectoryContent(folderPath)]
// } else {
// file.child = await fetchDirectoryContent(folderPath)
// }
// return file
// } else if (file.child) {
// file.child = await resolveDirectory(folderPath, file.child, true)
// return file
// } else {
// return file
// }
// }))
// return dir
// }
const extractNameFromKey = (key: string):string => {
const keyPath = key.split('/')
@ -726,7 +688,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
setState(prevState => {
return { ...prevState, focusElement: [{ key: path, type: 'folder' }], expandPath }
})
resolveDirectory(path)(dispatch)
resolveDirectory(fileSystem.provider.provider, path)(dispatch)
}
}
@ -914,8 +876,8 @@ export const FileExplorer = (props: FileExplorerProps) => {
>
{
file.child ? <TreeView id={`treeView${file.path}`} key={index}>{
file.child.map((file, index) => {
return renderFiles(file, index)
Object.keys(file.child).map((key, index) => {
return renderFiles(file.child[key], index)
})
}
</TreeView> : <TreeView id={`treeView${file.path}`} key={index} />

@ -1,4 +1,5 @@
import { File } from '../types'
import * as _ from 'lodash'
import { extractNameFromKey } from '../utils'
interface Action {
type: string;
payload: Record<string, any>;
@ -7,8 +8,7 @@ interface Action {
export const fileSystemInitialState = {
files: {
files: [],
activeDirectory: {},
expandPath: [],
workspaceName: null,
isRequesting: false,
isSuccessful: false,
error: null
@ -40,7 +40,6 @@ export const fileSystemReducer = (state = fileSystemInitialState, action: Action
files: {
...state.files,
files: action.payload.files,
expandPath: [...state.files.expandPath, action.payload.path],
isRequesting: false,
isSuccessful: true,
error: null
@ -74,8 +73,7 @@ export const fileSystemReducer = (state = fileSystemInitialState, action: Action
...state,
files: {
...state.files,
files: action.payload.files,
expandPath: [...state.files.expandPath, action.payload.path],
files: resolveDirectory(state.files.workspaceName, action.payload.path, state.files.files, action.payload.files),
isRequesting: false,
isSuccessful: true,
error: null
@ -127,12 +125,12 @@ export const fileSystemReducer = (state = fileSystemInitialState, action: Action
}
}
}
case 'ADD_EMPTY_FILE': {
case 'SET_CURRENT_WORKSPACE': {
return {
...state,
files: {
...state.files,
files: []
workspaceName: action.payload
}
}
}
@ -141,31 +139,16 @@ export const fileSystemReducer = (state = fileSystemInitialState, action: Action
}
}
const addEmptyFile = (path: string, files: File[]): File[] => {
if (path === name) {
files.push({
path: 'browser/blank',
name: '',
isDirectory: false
})
return files
}
return files.map(file => {
if (file.child) {
if (file.path === path) {
file.child = [...file.child, {
path: file.path + '/blank',
name: '',
isDirectory: false
}]
return file
} else {
file.child = addEmptyFile(path, file.child)
const resolveDirectory = (root, path: string, files, content) => {
const pathArr = path.split('/')
if (pathArr[0] !== root) pathArr.unshift(root)
return file
}
} else {
return file
}
files = _.set(files, pathArr, {
isDirectory: true,
path,
name: extractNameFromKey(path),
child: { ...content[pathArr[pathArr.length - 1]] }
})
return files
}

6
package-lock.json generated

@ -25139,9 +25139,9 @@
}
},
"lodash": {
"version": "4.17.19",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.19.tgz",
"integrity": "sha512-JNvd8XER9GQX0v2qJgsaN/mzFCNA5BRe/j8JN9d+tWyGLSodKQHKFicdwNYzWwI3wjRnaKPsGj1XkBjx/F96DQ=="
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
},
"lodash-es": {
"version": "4.17.15",

@ -160,6 +160,7 @@
"jquery": "^3.3.1",
"jszip": "^3.6.0",
"latest-version": "^5.1.0",
"lodash": "^4.17.21",
"merge": "^1.2.0",
"npm-install-version": "^6.0.2",
"react": "16.13.1",

Loading…
Cancel
Save