Add folder from input in file-explorer

pull/668/head
ioedeveloper 4 years ago
parent a25a12ea3d
commit e07b38f156
  1. 75
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx

@ -144,16 +144,16 @@ export const FileExplorer = (props: FileExplorerProps) => {
if (!createFile) { if (!createFile) {
// tooltip('Failed to create file ' + newName) // tooltip('Failed to create file ' + newName)
} else { } else {
addFile(parentFolder, newFilePath) addFile(parentFolder, newName)
await fileManager.open(newName) await fileManager.open(newName)
} }
}) })
// }, null, true) // }, null, true)
} }
const createNewFolder = async (parentFolder?: string) => { const createNewFolder = async (parentFolder: string, newFolderPath: string) => {
if (!parentFolder) parentFolder = state.focusElement[0] ? state.focusElement[0].type === 'folder' ? state.focusElement[0].key : extractParentFromKey(state.focusElement[0].key) : name // if (!parentFolder) parentFolder = state.focusElement[0] ? state.focusElement[0].type === 'folder' ? state.focusElement[0].key : extractParentFromKey(state.focusElement[0].key) : name
else if (parentFolder.indexOf('.sol') !== -1) parentFolder = extractParentFromKey(parentFolder) // else if (parentFolder.indexOf('.sol') !== -1) parentFolder = extractParentFromKey(parentFolder)
// const self = this // const self = this
// modalDialogCustom.prompt('Create new folder', '', 'New folder', (input) => { // modalDialogCustom.prompt('Create new folder', '', 'New folder', (input) => {
// if (!input) { // if (!input) {
@ -161,15 +161,14 @@ export const FileExplorer = (props: FileExplorerProps) => {
// } // }
const fileManager = state.fileManager const fileManager = state.fileManager
const newFolderName = parentFolder + '/' + 'unnamed' + Math.floor(Math.random() * 101) // get filename from state (state.newFileName) const dirName = newFolderPath + '/'
const dirName = newFolderName + '/'
// if (error) return tooltip('Unexpected error while creating folder: ' + error) // if (error) return tooltip('Unexpected error while creating folder: ' + error)
const exists = fileManager.exists(dirName) const exists = fileManager.exists(dirName)
if (exists) return if (exists) return
try { try {
await fileManager.mkdir(dirName) await fileManager.mkdir(dirName)
addFolder(parentFolder, newFolderName) addFolder(parentFolder, newFolderPath)
} catch (e) { } catch (e) {
// tooltip('Failed to create file ' + newName) // tooltip('Failed to create file ' + newName)
} }
@ -296,6 +295,35 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
} }
const addEmptyFolder = (parentFolder: string, files: File[]): File[] => {
if (parentFolder === name) {
files.unshift({
path: 'browser/blank',
name: '',
isDirectory: true
})
return files
}
return files.map(file => {
if (file.child) {
if (file.path === parentFolder) {
file.child = [{
path: file.path + '/blank',
name: '',
isDirectory: true
}, ...file.child]
return file
} else {
file.child = addEmptyFolder(parentFolder, file.child)
return file
}
} else {
return file
}
})
}
const removePath = (path: string, files: File[]): File[] => { const removePath = (path: string, files: File[]): File[] => {
return files.map(file => { return files.map(file => {
if (file.path === path) { if (file.path === path) {
@ -432,7 +460,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
const editModeOff = async (content: string) => { const editModeOff = async (content: string) => {
const parentFolder = state.focusEdit.type === 'folder' ? state.focusEdit.element : extractParentFromKey(state.focusEdit.element) const parentFolder = extractParentFromKey(state.focusEdit.element)
if (!content || (content.trim() === '')) { if (!content || (content.trim() === '')) {
if (state.focusEdit.isNew) { if (state.focusEdit.isNew) {
@ -457,7 +485,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
return return
} }
if (state.focusEdit.isNew) { if (state.focusEdit.isNew) {
createNewFile(parentFolder, parentFolder + '/' + content) state.focusEdit.type === 'file' ? createNewFile(parentFolder, parentFolder + '/' + content) : createNewFolder(parentFolder, parentFolder + '/' + content)
const files = removePath(state.focusEdit.element, state.files) const files = removePath(state.focusEdit.element, state.files)
const updatedFiles = files.filter(file => file) const updatedFiles = files.filter(file => file)
@ -480,14 +508,25 @@ export const FileExplorer = (props: FileExplorerProps) => {
const handleNewFileInput = (parentFolder?: string) => { const handleNewFileInput = (parentFolder?: string) => {
if (!parentFolder) parentFolder = state.focusElement[0] ? state.focusElement[0].type === 'folder' ? state.focusElement[0].key : extractParentFromKey(state.focusElement[0].key) : name if (!parentFolder) parentFolder = state.focusElement[0] ? state.focusElement[0].type === 'folder' ? state.focusElement[0].key : extractParentFromKey(state.focusElement[0].key) : name
const files = addEmptyFile(parentFolder, state.files) const files = addEmptyFile(parentFolder, state.files)
setState(prevState => { setState(prevState => {
return { ...prevState, files } return { ...prevState, files }
}) })
editModeOn(parentFolder + '/blank', 'file', true) editModeOn(parentFolder + '/blank', 'file', true)
} }
const handleNewFolderInput = (parentFolder?: string) => {
if (!parentFolder) parentFolder = state.focusElement[0] ? state.focusElement[0].type === 'folder' ? state.focusElement[0].key : extractParentFromKey(state.focusElement[0].key) : name
else if (parentFolder.indexOf('.sol') !== -1) parentFolder = extractParentFromKey(parentFolder)
const files = addEmptyFolder(parentFolder, state.files)
setState(prevState => {
return { ...prevState, files }
})
editModeOn(parentFolder + '/blank', 'folder', true)
}
// warn if file changed outside of Remix // warn if file changed outside of Remix
const remixdDialog = () => { const remixdDialog = () => {
return <div>This file has been changed outside of Remix IDE.</div> return <div>This file has been changed outside of Remix IDE.</div>
@ -539,12 +578,13 @@ export const FileExplorer = (props: FileExplorerProps) => {
} }
</TreeView> : <TreeView id={`treeView${file.path}`} key={index} /> </TreeView> : <TreeView id={`treeView${file.path}`} key={index} />
} }
{ ((state.focusContext.element === file.path) && (state.focusEdit.element !== file.path)) && </TreeViewItem>
{ ((state.focusContext.element === file.path) && (state.focusEdit.element !== file.path)) &&
<FileExplorerContextMenu <FileExplorerContextMenu
actions={state.actions} actions={state.actions}
hideContextMenu={hideContextMenu} hideContextMenu={hideContextMenu}
createNewFile={handleNewFileInput} createNewFile={handleNewFileInput}
createNewFolder={createNewFolder} createNewFolder={handleNewFolderInput}
deletePath={deletePath} deletePath={deletePath}
renamePath={editModeOn} renamePath={editModeOn}
pageX={state.focusContext.x} pageX={state.focusContext.x}
@ -552,8 +592,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
path={file.path} path={file.path}
type='folder' type='folder'
/> />
} }
</TreeViewItem>
</div> </div>
) )
} else { } else {
@ -582,7 +621,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
actions={state.actions} actions={state.actions}
hideContextMenu={hideContextMenu} hideContextMenu={hideContextMenu}
createNewFile={handleNewFileInput} createNewFile={handleNewFileInput}
createNewFolder={createNewFolder} createNewFolder={handleNewFolderInput}
deletePath={deletePath} deletePath={deletePath}
renamePath={editModeOn} renamePath={editModeOn}
pageX={state.focusContext.x} pageX={state.focusContext.x}
@ -601,7 +640,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
ref={containerRef} ref={containerRef}
tabIndex={-1} tabIndex={-1}
onKeyDown={(e) => { onKeyDown={(e) => {
if (e.shiftKey) { if (e.ctrlKey) {
setState(prevState => { setState(prevState => {
return { ...prevState, ctrlKey: true } return { ...prevState, ctrlKey: true }
}) })
@ -621,14 +660,14 @@ export const FileExplorer = (props: FileExplorerProps) => {
menuItems={props.menuItems} menuItems={props.menuItems}
addFile={addFile} addFile={addFile}
createNewFile={handleNewFileInput} createNewFile={handleNewFileInput}
createNewFolder={createNewFolder} createNewFolder={handleNewFolderInput}
files={filesProvider} files={filesProvider}
fileManager={state.fileManager} fileManager={state.fileManager}
accessToken={state.accessToken} accessToken={state.accessToken}
/> />
} }
expand={true}> expand={true}>
<div> <div className='pb-2'>
<TreeView id='treeViewMenu'> <TreeView id='treeViewMenu'>
{ {
state.files.map((file, index) => { state.files.map((file, index) => {

Loading…
Cancel
Save