Commit c5057116 authored by ioedeveloper's avatar ioedeveloper

Update plugin with workspaces and current selected workspace

parent 2adda600
...@@ -143,6 +143,7 @@ module.exports = class Filepanel extends ViewPlugin { ...@@ -143,6 +143,7 @@ module.exports = class Filepanel extends ViewPlugin {
} }
setWorkspace (workspace) { setWorkspace (workspace) {
console.log('workspace: ', workspace)
const workspaceProvider = this.fileProviders.workspace const workspaceProvider = this.fileProviders.workspace
this.currentWorkspaceMetadata = { name: workspace.name, isLocalhost: workspace.isLocalhost, absolutePath: `${workspaceProvider.workspacesPath}/${workspace.name}` } this.currentWorkspaceMetadata = { name: workspace.name, isLocalhost: workspace.isLocalhost, absolutePath: `${workspaceProvider.workspacesPath}/${workspace.name}` }
......
...@@ -4,7 +4,7 @@ import Gists from 'gists' ...@@ -4,7 +4,7 @@ import Gists from 'gists'
import { customAction } from '@remixproject/plugin-api/lib/file-system/file-panel/type' 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 { displayNotification, displayPopUp, fetchDirectoryError, fetchDirectoryRequest, fetchDirectorySuccess, focusElement, hidePopUp, removeInputFieldSuccess, setCurrentWorkspace, setDeleteWorkspace, setExpandPath, setMode, setWorkspaces } from './payload'
import { listenOnPluginEvents, listenOnProviderEvents } from './events' import { listenOnPluginEvents, listenOnProviderEvents } from './events'
import { createWorkspaceTemplate, loadWorkspacePreset, setPlugin } from './workspace' import { createWorkspaceTemplate, getWorkspaces, loadWorkspacePreset, setPlugin } from './workspace'
export * from './events' export * from './events'
export * from './workspace' export * from './workspace'
...@@ -28,20 +28,20 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React. ...@@ -28,20 +28,20 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
if (params.gist) { if (params.gist) {
await createWorkspaceTemplate('gist-sample', 'gist-template') await createWorkspaceTemplate('gist-sample', 'gist-template')
await loadWorkspacePreset('gist-template') await loadWorkspacePreset('gist-template')
dispatch(setCurrentWorkspace('gist-sample')) dispatch(setCurrentWorkspace('gist-sample', plugin))
} else if (params.code || params.url) { } else if (params.code || params.url) {
await createWorkspaceTemplate('code-sample', 'code-template') await createWorkspaceTemplate('code-sample', 'code-template')
await loadWorkspacePreset('code-template') await loadWorkspacePreset('code-template')
dispatch(setCurrentWorkspace('code-sample')) dispatch(setCurrentWorkspace('code-sample', plugin))
} else { } else {
if (workspaces.length === 0) { if (workspaces.length === 0) {
await createWorkspaceTemplate('default_workspace', 'default-template') await createWorkspaceTemplate('default_workspace', 'default-template')
await loadWorkspacePreset('default-template') await loadWorkspacePreset('default-template')
dispatch(setCurrentWorkspace('default_workspace')) dispatch(setCurrentWorkspace('default_workspace', plugin))
} else { } else {
if (workspaces.length > 0) { if (workspaces.length > 0) {
workspaceProvider.setWorkspace(workspaces[workspaces.length - 1]) 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. ...@@ -50,6 +50,7 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
listenOnProviderEvents(workspaceProvider)(dispatch) listenOnProviderEvents(workspaceProvider)(dispatch)
listenOnProviderEvents(localhostProvider)(dispatch) listenOnProviderEvents(localhostProvider)(dispatch)
dispatch(setMode('browser')) dispatch(setMode('browser'))
plugin.setWorkspaces(await getWorkspaces())
plugin.emit('workspaceInitializationCompleted') plugin.emit('workspaceInitializationCompleted')
} }
} }
...@@ -259,29 +260,7 @@ const deleteWorkspaceFromProvider = async (workspaceName: string) => { ...@@ -259,29 +260,7 @@ const deleteWorkspaceFromProvider = async (workspaceName: string) => {
await plugin.fileManager.closeAllFiles() await plugin.fileManager.closeAllFiles()
plugin.fileProviders.browser.remove(workspacesPath + '/' + workspaceName) plugin.fileProviders.browser.remove(workspacesPath + '/' + workspaceName)
plugin.emit('deleteWorkspace', { name: workspaceName }) plugin.emit('deleteWorkspace', { name: workspaceName })
} plugin.setWorkspaces(await getWorkspaces())
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)
}
} }
const packageGistFiles = (directory) => { const packageGistFiles = (directory) => {
......
import { action } from '../types' import { action } from '../types'
export const setCurrentWorkspace = (workspace: string) => { export const setCurrentWorkspace = (workspace: string, plugin?) => {
plugin && plugin.setWorkspace(workspace)
return { return {
type: 'SET_CURRENT_WORKSPACE', type: 'SET_CURRENT_WORKSPACE',
payload: workspace payload: workspace
...@@ -125,7 +126,8 @@ export const createWorkspaceRequest = (promise: Promise<any>) => { ...@@ -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 { return {
type: 'CREATE_WORKSPACE_SUCCESS', type: 'CREATE_WORKSPACE_SUCCESS',
payload: workspaceName payload: workspaceName
...@@ -153,7 +155,8 @@ export const fetchWorkspaceDirectorySuccess = (path: string, fileTree) => { ...@@ -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 { return {
type: 'RENAME_WORKSPACE', type: 'RENAME_WORKSPACE',
payload: { oldName, workspaceName } payload: { oldName, workspaceName }
......
...@@ -45,9 +45,10 @@ export const createWorkspace = async (workspaceName: string, isEmpty = false, cb ...@@ -45,9 +45,10 @@ export const createWorkspace = async (workspaceName: string, isEmpty = false, cb
dispatch(createWorkspaceRequest(promise)) dispatch(createWorkspaceRequest(promise))
promise.then(async () => { promise.then(async () => {
dispatch(createWorkspaceSuccess(workspaceName)) dispatch(createWorkspaceSuccess(workspaceName, plugin))
if (!isEmpty) await loadWorkspacePreset('default-template') if (!isEmpty) await loadWorkspacePreset('default-template')
plugin.emit('setWorkspace', { name: workspaceName, isLocalhost: false }) plugin.emit('setWorkspace', { name: workspaceName, isLocalhost: false })
plugin.setWorkspaces(await getWorkspaces())
cb && cb(null, workspaceName) cb && cb(null, workspaceName)
}).catch((error) => { }).catch((error) => {
dispatch(createWorkspaceError({ error })) dispatch(createWorkspaceError({ error }))
...@@ -172,7 +173,7 @@ export const fetchWorkspaceDirectory = async (path: string) => { ...@@ -172,7 +173,7 @@ export const fetchWorkspaceDirectory = async (path: string) => {
export const renameWorkspace = async (oldName: string, workspaceName: string) => { export const renameWorkspace = async (oldName: string, workspaceName: string) => {
await renameWorkspaceFromProvider(oldName, workspaceName) await renameWorkspaceFromProvider(oldName, workspaceName)
await dispatch(setRenameWorkspace(oldName, workspaceName)) await dispatch(setRenameWorkspace(oldName, workspaceName, plugin))
} }
export const renameWorkspaceFromProvider = async (oldName: string, workspaceName: string) => { export const renameWorkspaceFromProvider = async (oldName: string, workspaceName: string) => {
...@@ -185,6 +186,7 @@ export const renameWorkspaceFromProvider = async (oldName: string, workspaceName ...@@ -185,6 +186,7 @@ export const renameWorkspaceFromProvider = async (oldName: string, workspaceName
browserProvider.rename('browser/' + workspacesPath + '/' + oldName, 'browser/' + workspacesPath + '/' + workspaceName, true) browserProvider.rename('browser/' + workspacesPath + '/' + oldName, 'browser/' + workspacesPath + '/' + workspaceName, true)
workspaceProvider.setWorkspace(workspaceName) workspaceProvider.setWorkspace(workspaceName)
plugin.emit('renameWorkspace', { name: workspaceName }) plugin.emit('renameWorkspace', { name: workspaceName })
plugin.setWorkspaces(await getWorkspaces())
} }
export const switchToWorkspace = async (name: string) => { export const switchToWorkspace = async (name: string) => {
...@@ -197,14 +199,14 @@ export const switchToWorkspace = async (name: string) => { ...@@ -197,14 +199,14 @@ export const switchToWorkspace = async (name: string) => {
plugin.emit('setWorkspace', { name: LOCALHOST, isLocalhost: true }) plugin.emit('setWorkspace', { name: LOCALHOST, isLocalhost: true })
} else if (name === NO_WORKSPACE) { } else if (name === NO_WORKSPACE) {
plugin.fileProviders.workspace.clearWorkspace() plugin.fileProviders.workspace.clearWorkspace()
dispatch(setCurrentWorkspace(null)) dispatch(setCurrentWorkspace(null, plugin))
} else { } else {
const isActive = await plugin.call('manager', 'isActive', 'remixd') const isActive = await plugin.call('manager', 'isActive', 'remixd')
if (isActive) plugin.call('manager', 'deactivatePlugin', 'remixd') if (isActive) plugin.call('manager', 'deactivatePlugin', 'remixd')
await plugin.fileProviders.workspace.setWorkspace(name) await plugin.fileProviders.workspace.setWorkspace(name)
dispatch(setMode('browser')) dispatch(setMode('browser'))
dispatch(setCurrentWorkspace(name)) dispatch(setCurrentWorkspace(name, plugin))
dispatch(setReadOnlyMode(false)) dispatch(setReadOnlyMode(false))
plugin.emit('setWorkspace', { name, isLocalhost: false }) plugin.emit('setWorkspace', { name, isLocalhost: false })
} }
...@@ -255,3 +257,26 @@ export const uploadFile = async (target, targetFolder: string, cb?: (err: Error, ...@@ -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)
}
}
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