Update plugin with workspaces and current selected workspace

pull/1575/head
ioedeveloper 3 years ago
parent 2adda600a7
commit c5057116a9
  1. 1
      apps/remix-ide/src/app/panels/file-panel.js
  2. 35
      libs/remix-ui/workspace/src/lib/actions/index.ts
  3. 9
      libs/remix-ui/workspace/src/lib/actions/payload.ts
  4. 33
      libs/remix-ui/workspace/src/lib/actions/workspace.ts

@ -143,6 +143,7 @@ module.exports = class Filepanel extends ViewPlugin {
}
setWorkspace (workspace) {
console.log('workspace: ', workspace)
const workspaceProvider = this.fileProviders.workspace
this.currentWorkspaceMetadata = { name: workspace.name, isLocalhost: workspace.isLocalhost, absolutePath: `${workspaceProvider.workspacesPath}/${workspace.name}` }

@ -4,7 +4,7 @@ import Gists from 'gists'
import { customAction } from '@remixproject/plugin-api/lib/file-system/file-panel/type'
import { displayNotification, displayPopUp, fetchDirectoryError, fetchDirectoryRequest, fetchDirectorySuccess, focusElement, hidePopUp, removeInputFieldSuccess, setCurrentWorkspace, setDeleteWorkspace, setExpandPath, setMode, setWorkspaces } from './payload'
import { listenOnPluginEvents, listenOnProviderEvents } from './events'
import { createWorkspaceTemplate, loadWorkspacePreset, setPlugin } from './workspace'
import { createWorkspaceTemplate, getWorkspaces, loadWorkspacePreset, setPlugin } from './workspace'
export * from './events'
export * from './workspace'
@ -28,20 +28,20 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
if (params.gist) {
await createWorkspaceTemplate('gist-sample', 'gist-template')
await loadWorkspacePreset('gist-template')
dispatch(setCurrentWorkspace('gist-sample'))
dispatch(setCurrentWorkspace('gist-sample', plugin))
} else if (params.code || params.url) {
await createWorkspaceTemplate('code-sample', 'code-template')
await loadWorkspacePreset('code-template')
dispatch(setCurrentWorkspace('code-sample'))
dispatch(setCurrentWorkspace('code-sample', plugin))
} else {
if (workspaces.length === 0) {
await createWorkspaceTemplate('default_workspace', 'default-template')
await loadWorkspacePreset('default-template')
dispatch(setCurrentWorkspace('default_workspace'))
dispatch(setCurrentWorkspace('default_workspace', plugin))
} else {
if (workspaces.length > 0) {
workspaceProvider.setWorkspace(workspaces[workspaces.length - 1])
dispatch(setCurrentWorkspace(workspaces[workspaces.length - 1]))
dispatch(setCurrentWorkspace(workspaces[workspaces.length - 1], plugin))
}
}
}
@ -50,6 +50,7 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
listenOnProviderEvents(workspaceProvider)(dispatch)
listenOnProviderEvents(localhostProvider)(dispatch)
dispatch(setMode('browser'))
plugin.setWorkspaces(await getWorkspaces())
plugin.emit('workspaceInitializationCompleted')
}
}
@ -259,29 +260,7 @@ const deleteWorkspaceFromProvider = async (workspaceName: string) => {
await plugin.fileManager.closeAllFiles()
plugin.fileProviders.browser.remove(workspacesPath + '/' + workspaceName)
plugin.emit('deleteWorkspace', { name: workspaceName })
}
const getWorkspaces = async (): Promise<string[]> | undefined => {
try {
const workspaces: string[] = await new Promise((resolve, reject) => {
const workspacesPath = plugin.fileProviders.workspace.workspacesPath
plugin.fileProviders.browser.resolveDirectory('/' + workspacesPath, (error, items) => {
if (error) {
console.error(error)
return reject(error)
}
resolve(Object.keys(items)
.filter((item) => items[item].isDirectory)
.map((folder) => folder.replace(workspacesPath + '/', '')))
})
})
plugin.setWorkspaces(workspaces)
return workspaces
} catch (e) {
console.log(e)
}
plugin.setWorkspaces(await getWorkspaces())
}
const packageGistFiles = (directory) => {

@ -1,6 +1,7 @@
import { action } from '../types'
export const setCurrentWorkspace = (workspace: string) => {
export const setCurrentWorkspace = (workspace: string, plugin?) => {
plugin && plugin.setWorkspace(workspace)
return {
type: 'SET_CURRENT_WORKSPACE',
payload: workspace
@ -125,7 +126,8 @@ export const createWorkspaceRequest = (promise: Promise<any>) => {
}
}
export const createWorkspaceSuccess = (workspaceName: string) => {
export const createWorkspaceSuccess = (workspaceName: string, plugin?) => {
plugin && plugin.setWorkspace(workspaceName)
return {
type: 'CREATE_WORKSPACE_SUCCESS',
payload: workspaceName
@ -153,7 +155,8 @@ export const fetchWorkspaceDirectorySuccess = (path: string, fileTree) => {
}
}
export const setRenameWorkspace = (oldName: string, workspaceName: string) => {
export const setRenameWorkspace = (oldName: string, workspaceName: string, plugin?) => {
plugin && plugin.setWorkspace(workspaceName)
return {
type: 'RENAME_WORKSPACE',
payload: { oldName, workspaceName }

@ -45,9 +45,10 @@ export const createWorkspace = async (workspaceName: string, isEmpty = false, cb
dispatch(createWorkspaceRequest(promise))
promise.then(async () => {
dispatch(createWorkspaceSuccess(workspaceName))
dispatch(createWorkspaceSuccess(workspaceName, plugin))
if (!isEmpty) await loadWorkspacePreset('default-template')
plugin.emit('setWorkspace', { name: workspaceName, isLocalhost: false })
plugin.setWorkspaces(await getWorkspaces())
cb && cb(null, workspaceName)
}).catch((error) => {
dispatch(createWorkspaceError({ error }))
@ -172,7 +173,7 @@ export const fetchWorkspaceDirectory = async (path: string) => {
export const renameWorkspace = async (oldName: string, workspaceName: string) => {
await renameWorkspaceFromProvider(oldName, workspaceName)
await dispatch(setRenameWorkspace(oldName, workspaceName))
await dispatch(setRenameWorkspace(oldName, workspaceName, plugin))
}
export const renameWorkspaceFromProvider = async (oldName: string, workspaceName: string) => {
@ -185,6 +186,7 @@ export const renameWorkspaceFromProvider = async (oldName: string, workspaceName
browserProvider.rename('browser/' + workspacesPath + '/' + oldName, 'browser/' + workspacesPath + '/' + workspaceName, true)
workspaceProvider.setWorkspace(workspaceName)
plugin.emit('renameWorkspace', { name: workspaceName })
plugin.setWorkspaces(await getWorkspaces())
}
export const switchToWorkspace = async (name: string) => {
@ -197,14 +199,14 @@ export const switchToWorkspace = async (name: string) => {
plugin.emit('setWorkspace', { name: LOCALHOST, isLocalhost: true })
} else if (name === NO_WORKSPACE) {
plugin.fileProviders.workspace.clearWorkspace()
dispatch(setCurrentWorkspace(null))
dispatch(setCurrentWorkspace(null, plugin))
} else {
const isActive = await plugin.call('manager', 'isActive', 'remixd')
if (isActive) plugin.call('manager', 'deactivatePlugin', 'remixd')
await plugin.fileProviders.workspace.setWorkspace(name)
dispatch(setMode('browser'))
dispatch(setCurrentWorkspace(name))
dispatch(setCurrentWorkspace(name, plugin))
dispatch(setReadOnlyMode(false))
plugin.emit('setWorkspace', { name, isLocalhost: false })
}
@ -255,3 +257,26 @@ export const uploadFile = async (target, targetFolder: string, cb?: (err: Error,
})
})
}
export const getWorkspaces = async (): Promise<string[]> | undefined => {
try {
const workspaces: string[] = await new Promise((resolve, reject) => {
const workspacesPath = plugin.fileProviders.workspace.workspacesPath
plugin.fileProviders.browser.resolveDirectory('/' + workspacesPath, (error, items) => {
if (error) {
console.error(error)
return reject(error)
}
resolve(Object.keys(items)
.filter((item) => items[item].isDirectory)
.map((folder) => folder.replace(workspacesPath + '/', '')))
})
})
plugin.setWorkspaces(workspaces)
return workspaces
} catch (e) {
console.log(e)
}
}

Loading…
Cancel
Save