move createWorkspace to filePanel.js

pull/5370/head
yann300 4 years ago
parent b4b6d61bd5
commit d722a23ef9
  1. 6
      apps/remix-ide/src/app/files/fileManager.js
  2. 24
      apps/remix-ide/src/app/panels/file-panel.js
  3. 29
      libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx

@ -589,6 +589,12 @@ class FileManager extends Plugin {
if (!this.exists(workspaceRootPath)) await this.mkdir(workspaceRootPath)
if (!this.exists(workspacePath)) await this.mkdir(workspacePath)
}
async workspaceExists (name) {
const workspaceProvider = this._deps.filesProviders.workspace
const workspacePath = 'browser/' + workspaceProvider.workspacesPath + '/' + name
return this.exists(workspacePath)
}
}
module.exports = FileManager

@ -72,6 +72,7 @@ module.exports = class Filepanel extends ViewPlugin {
renderComponent() {
ReactDOM.render(
<Workspace
createWorkspace={this.createWorkspace.bind(this)}
setWorkspace={this.setWorkspace.bind(this)}
workspaceRenamed={this.workspaceRenamed.bind(this)}
workspaceDeleted={this.workspaceDeleted.bind(this)}
@ -153,13 +154,7 @@ module.exports = class Filepanel extends ViewPlugin {
this._deps.fileProviders.browser.resolveDirectory('/', async (error, filesList) => {
if (error) console.error(error)
if (Object.keys(filesList).length === 0) {
for (const file in examples) {
try {
await this._deps.fileManager.writeFile('browser/' + workspacesPath + '/default_workspace/' + examples[file].name, examples[file].content)
} catch (error) {
console.error(error)
}
}
await this.createWorkspace('default_workspace')
}
this.getWorkspaces()
})
@ -173,9 +168,18 @@ module.exports = class Filepanel extends ViewPlugin {
return await this.request.uploadFile()
}
async createWorkspace () {
return await this.request.createWorkspace()
}
async createWorkspace (workspaceName) {
if (await this._deps.fileManager.workspaceExists(workspaceName)) throw new Error('workspace already exists')
const workspacesPath = this._deps.fileProviders.workspace.workspacesPath
await this._deps.fileManager.createWorkspace(workspaceName)
for (const file in examples) {
try {
await this._deps.fileManager.writeFile('browser/' + workspacesPath + '/' + workspaceName + '/' + examples[file].name, examples[file].content)
} catch (error) {
console.error(error)
}
}
}
/** these are called by the react component, action is already finished whent it's called */
async setWorkspace (workspace) {

@ -9,6 +9,7 @@ type CodeExamples = {
/* eslint-disable-next-line */
export interface WorkspaceProps {
setWorkspace: ({ name: string, isLocalhost: boolean }) => void,
createWorkspace: (name: string) => void,
workspaceRenamed: ({ name: string }) => void,
workspaceCreated: ({ name: string }) => void,
workspaceDeleted: ({ name: string }) => void,
@ -136,6 +137,16 @@ export const Workspace = (props: WorkspaceProps) => {
})
}
const modalMessage = (title: string, body: string) => {
modal(title, body, {
label: 'OK',
fn: () => {}
}, {
label: null,
fn: null
})
}
const workspaceRenameInput = useRef()
const workspaceCreateInput = useRef()
@ -153,18 +164,14 @@ export const Workspace = (props: WorkspaceProps) => {
if (workspaceCreateInput.current === undefined) return
// @ts-ignore: Object is possibly 'null'.
const workspaceName = workspaceCreateInput.current.value
const workspacesPath = props.workspace.workspacesPath
props.browser.createDir(workspacesPath + '/' + workspaceName, async () => {
await setWorkspace(workspaceName)
for (const file in props.examples) {
try {
await props.fileManager.writeFile(props.examples[file].name, props.examples[file].content)
} catch (error) {
console.error(error)
}
}
})
try {
await props.createWorkspace(workspaceName)
} catch (e) {
modalMessage('Workspace Creation', e.message)
console.error(e)
}
await setWorkspace(workspaceName)
}
const onFinishDeleteWorkspace = async () => {

Loading…
Cancel
Save