Rename and delete workspaces

pull/5370/head
ioedeveloper 3 years ago
parent 8bc4a2a034
commit f522059ec8
  1. 4
      apps/remix-ide/src/app/panels/file-panel.js
  2. 46
      libs/remix-ui/workspace/src/lib/actions/workspace.ts
  3. 4
      libs/remix-ui/workspace/src/lib/contexts/index.ts
  4. 14
      libs/remix-ui/workspace/src/lib/providers/FileSystemProvider.tsx
  5. 29
      libs/remix-ui/workspace/src/lib/reducers/workspace.ts
  6. 16
      libs/remix-ui/workspace/src/lib/remix-ui-workspace.tsx

@ -201,10 +201,6 @@ module.exports = class Filepanel extends ViewPlugin {
}
}
workspaceRenamed (workspace) {
this.emit('renameWorkspace', workspace)
}
workspaceDeleted (workspace) {
this.emit('deleteWorkspace', workspace)
}

@ -165,6 +165,20 @@ const fetchWorkspaceDirectorySuccess = (path: string, fileTree) => {
}
}
const setRenameWorkspace = (oldName: string, workspaceName: string) => {
return {
type: 'RENAME_WORKSPACE',
payload: { oldName, workspaceName }
}
}
const setDeleteWorkspace = (workspaceName: string) => {
return {
type: 'DELETE_WORKSPACE',
payload: workspaceName
}
}
const createWorkspaceTemplate = async (workspaceName: string, setDefaults = true, template: 'gist-template' | 'code-template' | 'default-template' = 'default-template') => {
if (!workspaceName) throw new Error('workspace name cannot be empty')
if (checkSpecialChars(workspaceName) || checkSlash(workspaceName)) throw new Error('special characters are not allowed')
@ -259,6 +273,27 @@ const workspaceExists = async (name: string) => {
return browserProvider.exists(workspacePath)
}
const renameWorkspaceFromProvider = async (oldName: string, workspaceName: string) => {
if (!workspaceName) throw new Error('name cannot be empty')
if (checkSpecialChars(workspaceName) || checkSlash(workspaceName)) throw new Error('special characters are not allowed')
if (await workspaceExists(workspaceName)) throw new Error('workspace already exists')
const browserProvider = plugin.fileProviders.browser
const workspaceProvider = plugin.fileProviders.workspace
const workspacesPath = workspaceProvider.workspacesPath
browserProvider.rename('browser/' + workspacesPath + '/' + oldName, 'browser/' + workspacesPath + '/' + workspaceName, true)
workspaceProvider.setWorkspace(workspaceName)
plugin.emit('renameWorkspace', { name: workspaceName })
}
const deleteWorkspaceFromProvider = async (workspaceName: string) => {
const workspacesPath = plugin.fileProviders.workspace.workspacesPath
await plugin.fileManager.closeAllFiles()
plugin.fileProviders.browser.remove(workspacesPath + '/' + workspaceName)
// switchToWorkspace(NO_WORKSPACE)
plugin.emit('deleteWorkspace', { name: workspaceName })
}
const getWorkspaces = async (): Promise<string[]> | undefined => {
try {
const workspaces: string[] = await new Promise((resolve, reject) => {
@ -297,6 +332,7 @@ const listenOnEvents = (provider) => {
})
provider.event.on('fileRenamed', async (oldPath: string, newPath: string) => {
console.log('oldPath: ', oldPath, 'newPath: ', newPath)
await executeEvent('fileRenamed', oldPath, newPath)
})
@ -500,6 +536,16 @@ export const switchToWorkspace = (name: string) => async (dispatch: React.Dispat
}
}
export const renameWorkspace = (oldName: string, workspaceName: string) => async (dispatch: React.Dispatch<any>) => {
await renameWorkspaceFromProvider(oldName, workspaceName)
await dispatch(setRenameWorkspace(oldName, workspaceName))
}
export const deleteWorkspace = (workspaceName: string) => async (dispatch: React.Dispatch<any>) => {
await deleteWorkspaceFromProvider(workspaceName)
await dispatch(setDeleteWorkspace(workspaceName))
}
const fileAdded = async (filePath: string) => {
await dispatch(fileAddedSuccess(filePath))
if (filePath.includes('_test.sol')) {

@ -11,5 +11,7 @@ export const FileSystemContext = createContext<{
dispatchCreateWorkspace: (workspaceName: string) => Promise<void>,
toast: (toasterMsg: string) => void,
dispatchFetchWorkspaceDirectory: (path: string) => void,
dispatchSwitchToWorkspace: (name: string) => void
dispatchSwitchToWorkspace: (name: string) => void,
dispatchRenameWorkspace: (oldName: string, workspaceName: string) => void,
dispatchDeleteWorkspace: (workspaceName: string) => void
}>(null)

@ -5,7 +5,7 @@ import { Toaster } from '@remix-ui/toaster' // eslint-disable-line
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { FileSystemContext } from '../contexts'
import { browserReducer, browserInitialState } from '../reducers/workspace'
import { initWorkspace, fetchDirectory, addInputField, removeInputField, createWorkspace, fetchWorkspaceDirectory, switchToWorkspace } from '../actions/workspace'
import { initWorkspace, fetchDirectory, addInputField, removeInputField, createWorkspace, fetchWorkspaceDirectory, switchToWorkspace, renameWorkspace, deleteWorkspace } from '../actions/workspace'
import { Modal, WorkspaceProps } from '../types'
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Workspace } from '../remix-ui-workspace'
@ -54,6 +54,14 @@ export const FileSystemProvider = (props: WorkspaceProps) => {
await switchToWorkspace(name)(fsDispatch)
}
const dispatchRenameWorkspace = async (oldName: string, workspaceName: string) => {
await renameWorkspace(oldName, workspaceName)(fsDispatch)
}
const dispatchDeleteWorkspace = async (workspaceName: string) => {
await deleteWorkspace(workspaceName)(fsDispatch)
}
useEffect(() => {
if (modals.length > 0) {
setFocusModal(() => {
@ -127,7 +135,9 @@ export const FileSystemProvider = (props: WorkspaceProps) => {
dispatchRemoveInputField,
dispatchCreateWorkspace,
dispatchFetchWorkspaceDirectory,
dispatchSwitchToWorkspace
dispatchSwitchToWorkspace,
dispatchRenameWorkspace,
dispatchDeleteWorkspace
}
return (
<FileSystemContext.Provider value={value}>

@ -402,6 +402,35 @@ export const browserReducer = (state = browserInitialState, action: Action) => {
}
}
case 'RENAME_WORKSPACE': {
const payload = action.payload as { oldName: string, workspaceName: string }
const workspaces = state.browser.workspaces.filter(name => name !== payload.oldName)
return {
...state,
browser: {
...state.browser,
currentWorkspace: payload.workspaceName,
workspaces: [...workspaces, payload.workspaceName]
}
}
}
case 'DELETE_WORKSPACE': {
const payload = action.payload as string
const workspaces = state.browser.workspaces.filter(name => name !== payload)
const currentWorkspace = state.browser.currentWorkspace === payload ? workspaces.length > 0 ? workspaces[0] : '' : state.browser.currentWorkspace
return {
...state,
browser: {
...state.browser,
currentWorkspace: currentWorkspace,
workspaces: workspaces
}
}
}
default:
throw new Error()
}

@ -97,9 +97,7 @@ export function Workspace (props: WorkspaceProps) {
const workspaceName = workspaceRenameInput.current.value
try {
await props.plugin.renameWorkspace(currentWorkspace, workspaceName)
setWorkspace(workspaceName)
props.plugin.workspaceRenamed({ name: workspaceName })
await global.dispatchRenameWorkspace(currentWorkspace, workspaceName)
} catch (e) {
global.modal('Rename Workspace', e.message, 'OK', () => {}, '')
console.error(e)
@ -120,12 +118,12 @@ export function Workspace (props: WorkspaceProps) {
}
const onFinishDeleteWorkspace = async () => {
await props.plugin.fileManager.closeAllFiles()
const workspacesPath = props.plugin.workspace.workspacesPath
props.plugin.browser.remove(workspacesPath + '/' + currentWorkspace)
const name = currentWorkspace
setWorkspace(NO_WORKSPACE)
props.plugin.workspaceDeleted({ name })
try {
await global.dispatchDeleteWorkspace(global.fs.browser.currentWorkspace)
} catch (e) {
global.modal('Delete Workspace', e.message, 'OK', () => {}, '')
console.error(e)
}
}
/** ** ****/

Loading…
Cancel
Save