Commit a23514ee authored by ioedeveloper's avatar ioedeveloper

Rename and delete workspaces

parent 736aab3e
...@@ -201,10 +201,6 @@ module.exports = class Filepanel extends ViewPlugin { ...@@ -201,10 +201,6 @@ module.exports = class Filepanel extends ViewPlugin {
} }
} }
workspaceRenamed (workspace) {
this.emit('renameWorkspace', workspace)
}
workspaceDeleted (workspace) { workspaceDeleted (workspace) {
this.emit('deleteWorkspace', workspace) this.emit('deleteWorkspace', workspace)
} }
......
...@@ -165,6 +165,20 @@ const fetchWorkspaceDirectorySuccess = (path: string, fileTree) => { ...@@ -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') => { 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 (!workspaceName) throw new Error('workspace name cannot be empty')
if (checkSpecialChars(workspaceName) || checkSlash(workspaceName)) throw new Error('special characters are not allowed') if (checkSpecialChars(workspaceName) || checkSlash(workspaceName)) throw new Error('special characters are not allowed')
...@@ -259,6 +273,27 @@ const workspaceExists = async (name: string) => { ...@@ -259,6 +273,27 @@ const workspaceExists = async (name: string) => {
return browserProvider.exists(workspacePath) 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 => { const getWorkspaces = async (): Promise<string[]> | undefined => {
try { try {
const workspaces: string[] = await new Promise((resolve, reject) => { const workspaces: string[] = await new Promise((resolve, reject) => {
...@@ -297,6 +332,7 @@ const listenOnEvents = (provider) => { ...@@ -297,6 +332,7 @@ const listenOnEvents = (provider) => {
}) })
provider.event.on('fileRenamed', async (oldPath: string, newPath: string) => { provider.event.on('fileRenamed', async (oldPath: string, newPath: string) => {
console.log('oldPath: ', oldPath, 'newPath: ', newPath)
await executeEvent('fileRenamed', oldPath, newPath) await executeEvent('fileRenamed', oldPath, newPath)
}) })
...@@ -500,6 +536,16 @@ export const switchToWorkspace = (name: string) => async (dispatch: React.Dispat ...@@ -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) => { const fileAdded = async (filePath: string) => {
await dispatch(fileAddedSuccess(filePath)) await dispatch(fileAddedSuccess(filePath))
if (filePath.includes('_test.sol')) { if (filePath.includes('_test.sol')) {
......
...@@ -11,5 +11,7 @@ export const FileSystemContext = createContext<{ ...@@ -11,5 +11,7 @@ export const FileSystemContext = createContext<{
dispatchCreateWorkspace: (workspaceName: string) => Promise<void>, dispatchCreateWorkspace: (workspaceName: string) => Promise<void>,
toast: (toasterMsg: string) => void, toast: (toasterMsg: string) => void,
dispatchFetchWorkspaceDirectory: (path: 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) }>(null)
...@@ -5,7 +5,7 @@ import { Toaster } from '@remix-ui/toaster' // eslint-disable-line ...@@ -5,7 +5,7 @@ import { Toaster } from '@remix-ui/toaster' // eslint-disable-line
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
import { FileSystemContext } from '../contexts' import { FileSystemContext } from '../contexts'
import { browserReducer, browserInitialState } from '../reducers/workspace' 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' import { Modal, WorkspaceProps } from '../types'
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Workspace } from '../remix-ui-workspace' import { Workspace } from '../remix-ui-workspace'
...@@ -54,6 +54,14 @@ export const FileSystemProvider = (props: WorkspaceProps) => { ...@@ -54,6 +54,14 @@ export const FileSystemProvider = (props: WorkspaceProps) => {
await switchToWorkspace(name)(fsDispatch) 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(() => { useEffect(() => {
if (modals.length > 0) { if (modals.length > 0) {
setFocusModal(() => { setFocusModal(() => {
...@@ -127,7 +135,9 @@ export const FileSystemProvider = (props: WorkspaceProps) => { ...@@ -127,7 +135,9 @@ export const FileSystemProvider = (props: WorkspaceProps) => {
dispatchRemoveInputField, dispatchRemoveInputField,
dispatchCreateWorkspace, dispatchCreateWorkspace,
dispatchFetchWorkspaceDirectory, dispatchFetchWorkspaceDirectory,
dispatchSwitchToWorkspace dispatchSwitchToWorkspace,
dispatchRenameWorkspace,
dispatchDeleteWorkspace
} }
return ( return (
<FileSystemContext.Provider value={value}> <FileSystemContext.Provider value={value}>
......
...@@ -402,6 +402,35 @@ export const browserReducer = (state = browserInitialState, action: Action) => { ...@@ -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: default:
throw new Error() throw new Error()
} }
......
...@@ -97,9 +97,7 @@ export function Workspace (props: WorkspaceProps) { ...@@ -97,9 +97,7 @@ export function Workspace (props: WorkspaceProps) {
const workspaceName = workspaceRenameInput.current.value const workspaceName = workspaceRenameInput.current.value
try { try {
await props.plugin.renameWorkspace(currentWorkspace, workspaceName) await global.dispatchRenameWorkspace(currentWorkspace, workspaceName)
setWorkspace(workspaceName)
props.plugin.workspaceRenamed({ name: workspaceName })
} catch (e) { } catch (e) {
global.modal('Rename Workspace', e.message, 'OK', () => {}, '') global.modal('Rename Workspace', e.message, 'OK', () => {}, '')
console.error(e) console.error(e)
...@@ -120,12 +118,12 @@ export function Workspace (props: WorkspaceProps) { ...@@ -120,12 +118,12 @@ export function Workspace (props: WorkspaceProps) {
} }
const onFinishDeleteWorkspace = async () => { const onFinishDeleteWorkspace = async () => {
await props.plugin.fileManager.closeAllFiles() try {
const workspacesPath = props.plugin.workspace.workspacesPath await global.dispatchDeleteWorkspace(global.fs.browser.currentWorkspace)
props.plugin.browser.remove(workspacesPath + '/' + currentWorkspace) } catch (e) {
const name = currentWorkspace global.modal('Delete Workspace', e.message, 'OK', () => {}, '')
setWorkspace(NO_WORKSPACE) console.error(e)
props.plugin.workspaceDeleted({ name }) }
} }
/** ** ****/ /** ** ****/
......
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