Fixed renamed position

pull/1140/head
ioedeveloper 4 years ago
parent 59afc28810
commit b71001ea8f
  1. 11
      libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts
  2. 33
      libs/remix-ui/file-explorer/src/lib/reducers/fileSystem.ts

@ -172,10 +172,10 @@ export const fileRemovedSuccess = (path: string, removePath: string) => {
} }
} }
export const fileRenamedSuccess = (parentPath: string, oldPath: string, newPath: string) => { export const fileRenamedSuccess = (path: string, removePath: string, files) => {
return { return {
type: 'FILE_RENAMED', type: 'FILE_RENAMED',
payload: { parentPath, oldPath, newPath } payload: { path, removePath, files }
} }
} }
@ -202,11 +202,10 @@ export const init = (provider, workspaceName: string, plugin) => (dispatch: Reac
dispatch(fileRemovedSuccess(path, removePath)) dispatch(fileRemovedSuccess(path, removePath))
}) })
provider.event.register('fileRenamed', async (oldPath, newPath) => { provider.event.register('fileRenamed', async (oldPath, newPath) => {
console.log('oldPath: ', oldPath) const path = extractParentFromKey(oldPath) || workspaceName
console.log('newPath: ', newPath) const data = await fetchDirectoryContent(provider, path)
const parentPath = extractParentFromKey(oldPath) || workspaceName
dispatch(fileRenamedSuccess(parentPath, oldPath, newPath)) dispatch(fileRenamedSuccess(path, oldPath, data))
}) })
dispatch(fetchProviderSuccess(provider)) dispatch(fetchProviderSuccess(provider))
dispatch(setCurrentWorkspace(workspaceName)) dispatch(setCurrentWorkspace(workspaceName))

@ -220,7 +220,7 @@ export const fileSystemReducer = (state = fileSystemInitialState, action: Action
...state, ...state,
files: { files: {
...state.files, ...state.files,
files: fileRenamed(state.files.workspaceName, action.payload.parentPath, action.payload.oldPath, action.payload.newPath, state.files.files), files: fileRenamed(state.files.workspaceName, action.payload.path, action.payload.removePath, state.files.files, action.payload.files),
isRequesting: false, isRequesting: false,
isSuccessful: true, isSuccessful: true,
error: null error: null
@ -305,18 +305,14 @@ const fileRemoved = (root, path: string, removedPath: string, files) => {
return removePath(root, path, extractNameFromKey(removedPath), files) return removePath(root, path, extractNameFromKey(removedPath), files)
} }
const fileRenamed = (root, parentPath: string, oldPath: string, newPath: string, files) => { const fileRenamed = (root, path: string, removePath: string, files, content) => {
if (parentPath === root) { if (path === root) {
const newPathName = extractNameFromKey(newPath) || newPath const allFiles = { [root]: { ...content[root], ...files[root] } }
files[root][newPathName] = {
...files[root][oldPath], delete allFiles[root][extractNameFromKey(removePath) || removePath]
path: newPath, return allFiles
name: newPathName
}
delete files[root][extractNameFromKey(oldPath) || oldPath]
return files
} }
const pathArr: string[] = parentPath.split('/').filter(value => value) const pathArr: string[] = path.split('/').filter(value => value)
if (pathArr[0] !== root) pathArr.unshift(root) if (pathArr[0] !== root) pathArr.unshift(root)
const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => { const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => {
@ -324,17 +320,12 @@ const fileRenamed = (root, parentPath: string, oldPath: string, newPath: string,
}, []) }, [])
const prevFiles = _.get(files, _path) const prevFiles = _.get(files, _path)
prevFiles.child[extractNameFromKey(newPath)] = { delete prevFiles.child[extractNameFromKey(removePath)]
...prevFiles.child[oldPath],
path: newPath,
name: extractNameFromKey(newPath)
}
delete prevFiles.child[extractNameFromKey(oldPath)]
files = _.set(files, _path, { files = _.set(files, _path, {
isDirectory: true, isDirectory: true,
path: parentPath, path,
name: extractNameFromKey(parentPath), name: extractNameFromKey(path),
child: prevFiles.child child: { ...content[pathArr[pathArr.length - 1]], ...prevFiles.child }
}) })
return files return files

Loading…
Cancel
Save