Implement copyFile

pull/1209/head
ioedeveloper 4 years ago
parent ea02409324
commit a7b87f917a
  1. 10
      apps/remix-ide/src/app/files/fileManager.js
  2. 2
      libs/remix-ui/file-explorer/src/lib/file-explorer-context-menu.tsx
  3. 24
      libs/remix-ui/file-explorer/src/lib/file-explorer.tsx
  4. 2
      libs/remix-ui/file-explorer/src/lib/types/index.ts

@ -217,12 +217,14 @@ class FileManager extends Plugin {
try { try {
src = this.limitPluginScope(src) src = this.limitPluginScope(src)
dest = this.limitPluginScope(dest) dest = this.limitPluginScope(dest)
await this._handleExists(src, `Cannot copy from ${src}`) await this._handleExists(src, `Cannot copy from ${src}. Path does not exist.`)
await this._handleIsFile(src, `Cannot copy from ${src}`) await this._handleIsFile(src, `Cannot copy from ${src}. Path is not a file.`)
await this._handleIsFile(dest, `Cannot paste content into ${dest}`) await this._handleExists(dest, `Cannot paste content into ${dest}. Path does not exist.`)
await this._handleIsDir(dest, `Cannot paste content into ${dest}. Path is not directory.`)
const content = await this.readFile(src) const content = await this.readFile(src)
const copiedFileName = `/Copy_${src.split('/')[src.split('/').length - 1]}`
await this.writeFile(dest, content) await this.writeFile(dest + copiedFileName, content)
} catch (e) { } catch (e) {
throw new Error(e) throw new Error(e)
} }

@ -65,7 +65,7 @@ export const FileExplorerContextMenu = (props: FileExplorerContextMenuProps) =>
copy(path, type) copy(path, type)
break break
case 'Paste': case 'Paste':
paste(path) paste(path, type)
break break
default: default:
emit && emit(item.id, path) emit && emit(item.id, path)

@ -121,7 +121,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
reservedKeywords: [name, 'gist-'], reservedKeywords: [name, 'gist-'],
copyElement: [] copyElement: []
}) })
const [canPaste, setcanPaste] = useState(false) const [canPaste, setCanPaste] = useState(false)
const [fileSystem, dispatch] = useReducer(fileSystemReducer, fileSystemInitialState) const [fileSystem, dispatch] = useReducer(fileSystemReducer, fileSystemInitialState)
const editRef = useRef(null) const editRef = useRef(null)
@ -413,6 +413,16 @@ export const FileExplorer = (props: FileExplorerProps) => {
}) })
} }
const copyFile = (src: string, dest: string) => {
const fileManager = state.fileManager
try {
fileManager.copyFile(src, dest)
} catch (error) {
console.log('Oops! An error ocurred while performing copyFile operation.' + error)
}
}
const publishToGist = (path?: string, type?: string) => { const publishToGist = (path?: string, type?: string) => {
modal('Create a public gist', `Are you sure you want to anonymously publish all your files in the ${name} workspace as a public gist on github.com?`, 'OK', () => toGist(path, type), 'Cancel', () => {}) modal('Create a public gist', `Are you sure you want to anonymously publish all your files in the ${name} workspace as a public gist on github.com?`, 'OK', () => toGist(path, type), 'Cancel', () => {})
} }
@ -734,15 +744,19 @@ export const FileExplorer = (props: FileExplorerProps) => {
setState(prevState => { setState(prevState => {
return { ...prevState, copyElement: [...prevState.copyElement, { key: path, type }] } return { ...prevState, copyElement: [...prevState.copyElement, { key: path, type }] }
}) })
setcanPaste(true) setCanPaste(true)
toast('Copied to clipboard')
} }
const handlePasteClick = (dest: string) => { const handlePasteClick = (dest: string, destType: string) => {
console.log('destination: ', dest) dest = destType === 'file' ? extractParentFromKey(dest) || props.name : dest
state.copyElement.map(({ key, type }) => {
type === 'file' ? copyFile(key, dest) : copyFile(key, dest)
})
setState(prevState => { setState(prevState => {
return { ...prevState, copyElement: [] } return { ...prevState, copyElement: [] }
}) })
setcanPaste(false) setCanPaste(false)
} }
const label = (file: File) => { const label = (file: File) => {

@ -48,5 +48,5 @@ export interface FileExplorerContextMenuProps {
type: string, type: string,
onMouseOver?: (...args) => void, onMouseOver?: (...args) => void,
copy?: (path: string, type: string) => void copy?: (path: string, type: string) => void
paste?: (destination: string) => void paste?: (destination: string, type: string) => void
} }

Loading…
Cancel
Save