From c34ce4b4b58287199da51dfafcb88e0078009300 Mon Sep 17 00:00:00 2001 From: lianahus Date: Tue, 13 Apr 2021 11:49:03 +0200 Subject: [PATCH] createing default workspace --- apps/remix-ide/src/app/files/fileManager.js | 1 - .../src/app/files/workspaceFileProvider.js | 13 ++++++++--- .../workspace/src/lib/remix-ui-workspace.tsx | 22 ++++++++++++++++++- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/apps/remix-ide/src/app/files/fileManager.js b/apps/remix-ide/src/app/files/fileManager.js index 932368c20b..40e7aa2fff 100644 --- a/apps/remix-ide/src/app/files/fileManager.js +++ b/apps/remix-ide/src/app/files/fileManager.js @@ -564,7 +564,6 @@ class FileManager extends Plugin { return this._deps.filesProviders.browser } const provider = this._deps.filesProviders.workspace - if (!provider.isReady()) throw createError({ code: 'ECONNRESET', message: 'No workspace has been opened.' }) return this._deps.filesProviders.workspace } diff --git a/apps/remix-ide/src/app/files/workspaceFileProvider.js b/apps/remix-ide/src/app/files/workspaceFileProvider.js index 70854929d9..3a779f77bc 100644 --- a/apps/remix-ide/src/app/files/workspaceFileProvider.js +++ b/apps/remix-ide/src/app/files/workspaceFileProvider.js @@ -1,5 +1,6 @@ 'use strict' +const EventManager = require('../../lib/events') const FileProvider = require('./fileProvider') const pathModule = require('path') @@ -8,6 +9,7 @@ class WorkspaceFileProvider extends FileProvider { super('') this.workspacesPath = '.workspaces' this.workspace = null + this.event = new EventManager() } setWorkspace (workspace) { @@ -28,7 +30,7 @@ class WorkspaceFileProvider extends FileProvider { } removePrefix (path) { - if (!this.workspace) throw new Error('No workspace has been opened.') + if (!this.workspace) this.createDefaultWorkspace() path = path.replace(/^\/|\/$/g, '') // remove first and last slash if (path.startsWith(this.workspacesPath + '/' + this.workspace)) return path if (path.startsWith(this.workspace)) return this.workspacesPath + '/' + this.workspace @@ -49,7 +51,7 @@ class WorkspaceFileProvider extends FileProvider { } resolveDirectory (path, callback) { - if (!this.workspace) throw new Error('No workspace has been opened.') + if (!this.workspace) this.createDefaultWorkspace() super.resolveDirectory(path, (error, files) => { if (error) return callback(error) const unscoped = {} @@ -74,9 +76,14 @@ class WorkspaceFileProvider extends FileProvider { } _normalizePath (path) { - if (!this.workspace) throw new Error('No workspace has been opened.') + if (!this.workspace) this.createDefaultWorkspace() return path.replace(this.workspacesPath + '/' + this.workspace + '/', '') } + + createDefaultWorkspace() { + this.workspace = 'workspace_default' + this.event.trigger('create_workspace_default', [this.workspace]) + } } module.exports = WorkspaceFileProvider diff --git a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx index 6a41a942bc..c53d2336e2 100644 --- a/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx +++ b/libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx @@ -2,6 +2,7 @@ import React, { useState, useEffect, useRef } from 'react' // eslint-disable-lin import { FileExplorer } from '@remix-ui/file-explorer' // eslint-disable-line import './remix-ui-workspace.css' import { ModalDialog } from '@remix-ui/modal-dialog' // eslint-disable-line +import { Toaster } from '@remix-ui/toaster' /* eslint-disable-next-line */ export interface WorkspaceProps { @@ -101,6 +102,17 @@ export const Workspace = (props: WorkspaceProps) => { remixdExplorer.loading() }) + props.workspace.event.register('create_workspace_default', async (workspaceName) => { + try { + await props.createWorkspace(workspaceName) + await setWorkspace(workspaceName) + toast("New default workspace has been created.") + } catch (e) { + modalMessage('Create Default Workspace', e.message) + console.error(e) + } + }) + if (props.initialWorkspace) { props.workspace.setWorkspace(props.initialWorkspace) setState(prevState => { @@ -131,9 +143,16 @@ export const Workspace = (props: WorkspaceProps) => { }, handleHide: null }, - loadingLocalhost: false + loadingLocalhost: false, + toasterMsg: '', }) + const toast = (message: string) => { + setState(prevState => { + return { ...prevState, toasterMsg: message } + }) + } + /* workspace creation, renaming and deletion */ const renameCurrentWorkspace = () => { @@ -312,6 +331,7 @@ export const Workspace = (props: WorkspaceProps) => { handleHide={ handleHideModal }> { (typeof state.modal.message !== 'string') && state.modal.message } +
resetFocus(true)}>