From 67c38614207cd36b7bb041bd24ef89360dc086c6 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Mon, 19 Apr 2021 17:13:00 +0100 Subject: [PATCH] Resolve directory --- .../src/lib/actions/fileSystem.ts | 17 ++++++- .../file-explorer/src/lib/file-explorer.tsx | 48 ++---------------- .../src/lib/reducers/fileSystem.ts | 49 ++++++------------- package-lock.json | 6 +-- package.json | 1 + 5 files changed, 40 insertions(+), 81 deletions(-) diff --git a/libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts b/libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts index 3586342875..faba186ed9 100644 --- a/libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts +++ b/libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts @@ -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) => { 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) => { + if (name) { + dispatch(setCurrentWorkspace(name)) + } +} 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 e53648d28d..9ba8a1dc3f 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -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 => { - // 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 ? { - file.child.map((file, index) => { - return renderFiles(file, index) + Object.keys(file.child).map((key, index) => { + return renderFiles(file.child[key], index) }) } : diff --git a/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts b/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts index 2f4a88331a..0d0ecd7c85 100644 --- a/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts +++ b/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts @@ -1,4 +1,5 @@ -import { File } from '../types' +import * as _ from 'lodash' +import { extractNameFromKey } from '../utils' interface Action { type: string; payload: Record; @@ -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 } diff --git a/package-lock.json b/package-lock.json index 4f92e09fa8..d74204bd8f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -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", diff --git a/package.json b/package.json index 2587e3bfbf..a7ae4d3288 100644 --- a/package.json +++ b/package.json @@ -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",