From 59afc28810b105d78cc8d41aab1533a2b5b62ba9 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Sat, 24 Apr 2021 19:11:27 +0100 Subject: [PATCH] Renames but appends to bottom --- .../src/lib/actions/fileSystem.ts | 16 ++++- .../file-explorer/src/lib/file-explorer.tsx | 58 ++++++++----------- .../src/lib/reducers/fileSystem.ts | 49 +++++++++++++++- 3 files changed, 86 insertions(+), 37 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 e3dc559a4b..f1db07e81c 100644 --- a/libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts +++ b/libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts @@ -172,6 +172,13 @@ export const fileRemovedSuccess = (path: string, removePath: string) => { } } +export const fileRenamedSuccess = (parentPath: string, oldPath: string, newPath: string) => { + return { + type: 'FILE_RENAMED', + payload: { parentPath, oldPath, newPath } + } +} + export const init = (provider, workspaceName: string, plugin) => (dispatch: React.Dispatch) => { if (provider) { provider.event.register('fileAdded', async (filePath) => { @@ -179,6 +186,9 @@ export const init = (provider, workspaceName: string, plugin) => (dispatch: Reac const data = await fetchDirectoryContent(provider, path) dispatch(fileAddedSuccess(path, data)) + if (filePath.includes('_test.sol')) { + plugin.event.trigger('newTestFileCreated', [filePath]) + } }) provider.event.register('folderAdded', async (folderPath) => { const path = extractParentFromKey(folderPath) || workspaceName @@ -191,8 +201,12 @@ export const init = (provider, workspaceName: string, plugin) => (dispatch: Reac dispatch(fileRemovedSuccess(path, removePath)) }) - provider.event.register('fileRenamed', async () => { + provider.event.register('fileRenamed', async (oldPath, newPath) => { + console.log('oldPath: ', oldPath) + console.log('newPath: ', newPath) + const parentPath = extractParentFromKey(oldPath) || workspaceName + dispatch(fileRenamedSuccess(parentPath, oldPath, newPath)) }) dispatch(fetchProviderSuccess(provider)) dispatch(setCurrentWorkspace(workspaceName)) 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 2430b8e0f6..820699b95d 100644 --- a/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx +++ b/libs/remix-ui/file-explorer/src/lib/file-explorer.tsx @@ -148,18 +148,6 @@ export const FileExplorer = (props: FileExplorerProps) => { // } // }, [state.fileManager]) - // useEffect(() => { - // // unregister event to update state in callback - // if (filesProvider.event.registered.fileAdded) filesProvider.event.unregister('fileAdded', fileAdded) - // if (filesProvider.event.registered.folderAdded) filesProvider.event.unregister('folderAdded', folderAdded) - // if (filesProvider.event.registered.fileRemoved) filesProvider.event.unregister('fileRemoved', fileRemoved) - // if (filesProvider.event.registered.fileRenamed) filesProvider.event.unregister('fileRenamed', fileRenamed) - // filesProvider.event.register('fileAdded', fileAdded) - // filesProvider.event.register('folderAdded', folderAdded) - // filesProvider.event.register('fileRemoved', fileRemoved) - // filesProvider.event.register('fileRenamed', fileRenamed) - // }, [state.files]) - useEffect(() => { if (focusRoot) { setState(prevState => { @@ -303,26 +291,26 @@ export const FileExplorer = (props: FileExplorerProps) => { }) } - // const renamePath = async (oldPath: string, newPath: string) => { - // try { - // const fileManager = state.fileManager - // const exists = await fileManager.exists(newPath) - - // if (exists) { - // modal('Rename File Failed', `A file or folder ${extractNameFromKey(newPath)} already exists at this location. Please choose a different name.`, { - // label: 'Close', - // fn: () => {} - // }, null) - // } else { - // await fileManager.rename(oldPath, newPath) - // } - // } catch (error) { - // modal('Rename File Failed', 'Unexpected error while renaming: ' + typeof error === 'string' ? error : error.message, { - // label: 'Close', - // fn: async () => {} - // }, null) - // } - // } + const renamePath = async (oldPath: string, newPath: string) => { + try { + const fileManager = state.fileManager + const exists = await fileManager.exists(newPath) + + if (exists) { + modal('Rename File Failed', `A file or folder ${extractNameFromKey(newPath)} already exists at this location. Please choose a different name.`, { + label: 'Close', + fn: () => {} + }, null) + } else { + await fileManager.rename(oldPath, newPath) + } + } catch (error) { + modal('Rename File Failed', 'Unexpected error while renaming: ' + typeof error === 'string' ? error : error.message, { + label: 'Close', + fn: async () => {} + }, null) + } + } const fileExternallyChanged = (path: string, file: { content: string }) => { const config = registry.get('config').api @@ -686,11 +674,11 @@ export const FileExplorer = (props: FileExplorerProps) => { removeInputField(parentFolder)(dispatch) } else { const oldPath: string = state.focusEdit.element - // const oldName = extractNameFromKey(oldPath) - // const newPath = oldPath.replace(oldName, content) + const oldName = extractNameFromKey(oldPath) + const newPath = oldPath.replace(oldName, content) editRef.current.textContent = extractNameFromKey(oldPath) - // renamePath(oldPath, newPath) + renamePath(oldPath, newPath) } setState(prevState => { return { ...prevState, focusEdit: { element: null, isNew: false, type: '', lastEdit: '' } } 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 4f25db4eb7..c06dc87485 100644 --- a/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts +++ b/libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts @@ -215,6 +215,18 @@ export const fileSystemReducer = (state = fileSystemInitialState, action: Action } } } + case 'FILE_RENAMED': { + return { + ...state, + files: { + ...state.files, + files: fileRenamed(state.files.workspaceName, action.payload.parentPath, action.payload.oldPath, action.payload.newPath, state.files.files), + isRequesting: false, + isSuccessful: true, + error: null + } + } + } default: throw new Error() } @@ -250,7 +262,7 @@ const removePath = (root, path: string, pathName, files) => { }, []) const prevFiles = _.get(files, _path) - pathName && delete prevFiles.child[pathName] + prevFiles.child[pathName] && delete prevFiles.child[pathName] files = _.set(files, _path, { isDirectory: true, path, @@ -292,3 +304,38 @@ const fileRemoved = (root, path: string, removedPath: string, files) => { } return removePath(root, path, extractNameFromKey(removedPath), files) } + +const fileRenamed = (root, parentPath: string, oldPath: string, newPath: string, files) => { + if (parentPath === root) { + const newPathName = extractNameFromKey(newPath) || newPath + files[root][newPathName] = { + ...files[root][oldPath], + path: newPath, + name: newPathName + } + delete files[root][extractNameFromKey(oldPath) || oldPath] + return files + } + const pathArr: string[] = parentPath.split('/').filter(value => value) + + if (pathArr[0] !== root) pathArr.unshift(root) + const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => { + return Array.isArray(cur) ? [...acc, ...cur] : [...acc, cur] + }, []) + const prevFiles = _.get(files, _path) + + prevFiles.child[extractNameFromKey(newPath)] = { + ...prevFiles.child[oldPath], + path: newPath, + name: extractNameFromKey(newPath) + } + delete prevFiles.child[extractNameFromKey(oldPath)] + files = _.set(files, _path, { + isDirectory: true, + path: parentPath, + name: extractNameFromKey(parentPath), + child: prevFiles.child + }) + + return files +}