Commit 67146f1f authored by yann300's avatar yann300

move createWorkspace to filePanel.js

parent 22ee1cf6
...@@ -589,6 +589,12 @@ class FileManager extends Plugin { ...@@ -589,6 +589,12 @@ class FileManager extends Plugin {
if (!this.exists(workspaceRootPath)) await this.mkdir(workspaceRootPath) if (!this.exists(workspaceRootPath)) await this.mkdir(workspaceRootPath)
if (!this.exists(workspacePath)) await this.mkdir(workspacePath) 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 module.exports = FileManager
...@@ -72,6 +72,7 @@ module.exports = class Filepanel extends ViewPlugin { ...@@ -72,6 +72,7 @@ module.exports = class Filepanel extends ViewPlugin {
renderComponent() { renderComponent() {
ReactDOM.render( ReactDOM.render(
<Workspace <Workspace
createWorkspace={this.createWorkspace.bind(this)}
setWorkspace={this.setWorkspace.bind(this)} setWorkspace={this.setWorkspace.bind(this)}
workspaceRenamed={this.workspaceRenamed.bind(this)} workspaceRenamed={this.workspaceRenamed.bind(this)}
workspaceDeleted={this.workspaceDeleted.bind(this)} workspaceDeleted={this.workspaceDeleted.bind(this)}
...@@ -153,13 +154,7 @@ module.exports = class Filepanel extends ViewPlugin { ...@@ -153,13 +154,7 @@ module.exports = class Filepanel extends ViewPlugin {
this._deps.fileProviders.browser.resolveDirectory('/', async (error, filesList) => { this._deps.fileProviders.browser.resolveDirectory('/', async (error, filesList) => {
if (error) console.error(error) if (error) console.error(error)
if (Object.keys(filesList).length === 0) { if (Object.keys(filesList).length === 0) {
for (const file in examples) { await this.createWorkspace('default_workspace')
try {
await this._deps.fileManager.writeFile('browser/' + workspacesPath + '/default_workspace/' + examples[file].name, examples[file].content)
} catch (error) {
console.error(error)
}
}
} }
this.getWorkspaces() this.getWorkspaces()
}) })
...@@ -173,9 +168,18 @@ module.exports = class Filepanel extends ViewPlugin { ...@@ -173,9 +168,18 @@ module.exports = class Filepanel extends ViewPlugin {
return await this.request.uploadFile() return await this.request.uploadFile()
} }
async createWorkspace () { async createWorkspace (workspaceName) {
return await this.request.createWorkspace() 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 */ /** these are called by the react component, action is already finished whent it's called */
async setWorkspace (workspace) { async setWorkspace (workspace) {
......
...@@ -9,6 +9,7 @@ type CodeExamples = { ...@@ -9,6 +9,7 @@ type CodeExamples = {
/* eslint-disable-next-line */ /* eslint-disable-next-line */
export interface WorkspaceProps { export interface WorkspaceProps {
setWorkspace: ({ name: string, isLocalhost: boolean }) => void, setWorkspace: ({ name: string, isLocalhost: boolean }) => void,
createWorkspace: (name: string) => void,
workspaceRenamed: ({ name: string }) => void, workspaceRenamed: ({ name: string }) => void,
workspaceCreated: ({ name: string }) => void, workspaceCreated: ({ name: string }) => void,
workspaceDeleted: ({ name: string }) => void, workspaceDeleted: ({ name: string }) => void,
...@@ -136,6 +137,16 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -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 workspaceRenameInput = useRef()
const workspaceCreateInput = useRef() const workspaceCreateInput = useRef()
...@@ -153,18 +164,14 @@ export const Workspace = (props: WorkspaceProps) => { ...@@ -153,18 +164,14 @@ export const Workspace = (props: WorkspaceProps) => {
if (workspaceCreateInput.current === undefined) return if (workspaceCreateInput.current === undefined) return
// @ts-ignore: Object is possibly 'null'. // @ts-ignore: Object is possibly 'null'.
const workspaceName = workspaceCreateInput.current.value const workspaceName = workspaceCreateInput.current.value
const workspacesPath = props.workspace.workspacesPath
props.browser.createDir(workspacesPath + '/' + workspaceName, async () => { try {
await setWorkspace(workspaceName) await props.createWorkspace(workspaceName)
for (const file in props.examples) { } catch (e) {
try { modalMessage('Workspace Creation', e.message)
await props.fileManager.writeFile(props.examples[file].name, props.examples[file].content) console.error(e)
} catch (error) { }
console.error(error) await setWorkspace(workspaceName)
}
}
})
} }
const onFinishDeleteWorkspace = async () => { const onFinishDeleteWorkspace = async () => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment