Renames but appends to bottom

pull/1140/head
ioedeveloper 4 years ago
parent ed03204f25
commit 59afc28810
  1. 16
      libs/remix-ui/file-explorer/src/lib/actions/fileSystem.ts
  2. 58
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx
  3. 49
      libs/remix-ui/file-explorer/src/lib/reducers/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<any>) => {
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))

@ -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: '' } }

@ -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
}

Loading…
Cancel
Save