Commit cf05a97d authored by ioedeveloper's avatar ioedeveloper

Sort files and folders

parent dd27f66d
...@@ -51,7 +51,7 @@ class WorkspaceFileProvider extends FileProvider { ...@@ -51,7 +51,7 @@ class WorkspaceFileProvider extends FileProvider {
} }
resolveDirectory (path, callback) { resolveDirectory (path, callback) {
if (!this.workspace) this.createWorkspace() // if (!this.workspace) this.createWorkspace()
super.resolveDirectory(path, (error, files) => { super.resolveDirectory(path, (error, files) => {
if (error) return callback(error) if (error) return callback(error)
const unscoped = {} const unscoped = {}
......
...@@ -86,6 +86,13 @@ const fileRemovedSuccess = (removePath: string) => { ...@@ -86,6 +86,13 @@ const fileRemovedSuccess = (removePath: string) => {
} }
} }
const fileRenamedSuccess = (oldPath: string, newPath: string) => {
return {
type: 'FILE_RENAMED_SUCCESS',
payload: { oldPath, newPath }
}
}
const rootFolderChangedSuccess = (path: string) => { const rootFolderChangedSuccess = (path: string) => {
return { return {
type: 'ROOT_FOLDER_CHANGED', type: 'ROOT_FOLDER_CHANGED',
...@@ -232,19 +239,23 @@ const getWorkspaces = async (): Promise<string[]> | undefined => { ...@@ -232,19 +239,23 @@ const getWorkspaces = async (): Promise<string[]> | undefined => {
} }
const listenOnEvents = (provider) => { const listenOnEvents = (provider) => {
provider.event.on('fileAdded', async (filePath) => { provider.event.on('fileAdded', async (filePath: string) => {
await executeEvent('fileAdded', filePath) await executeEvent('fileAdded', filePath)
}) })
provider.event.on('folderAdded', async (folderPath) => { provider.event.on('folderAdded', async (folderPath: string) => {
await executeEvent('folderAdded', folderPath) await executeEvent('folderAdded', folderPath)
}) })
provider.event.on('fileRemoved', async (removePath) => { provider.event.on('fileRemoved', async (removePath: string) => {
await executeEvent('fileRemoved', removePath) await executeEvent('fileRemoved', removePath)
}) })
plugin.on('remixd', 'rootFolderChanged', async (path) => { provider.event.on('fileRenamed', async (oldPath: string, newPath: string) => {
await executeEvent('fileRenamed', oldPath, newPath)
})
plugin.on('remixd', 'rootFolderChanged', async (path: string) => {
await executeEvent('rootFolderChanged', path) await executeEvent('rootFolderChanged', path)
}) })
...@@ -331,10 +342,6 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React. ...@@ -331,10 +342,6 @@ export const initWorkspace = (filePanelPlugin) => async (reducerDispatch: React.
listenOnEvents(workspaceProvider) listenOnEvents(workspaceProvider)
listenOnEvents(localhostProvider) listenOnEvents(localhostProvider)
// provider.event.on('fileRenamed', async (oldPath) => {
// await executeEvent('fileRenamed', oldPath)
// })
// provider.event.on('createWorkspace', (name) => { // provider.event.on('createWorkspace', (name) => {
// createNewWorkspace(name) // createNewWorkspace(name)
// }) // })
...@@ -413,26 +420,23 @@ const fileRemoved = async (removePath: string) => { ...@@ -413,26 +420,23 @@ const fileRemoved = async (removePath: string) => {
await dispatch(fileRemovedSuccess(removePath)) await dispatch(fileRemovedSuccess(removePath))
} }
const fileRenamed = async (oldPath: string) => { const fileRenamed = async (oldPath: string, newPath: string) => {
const path = extractParentFromKey(oldPath) || provider.workspace || provider.type || '' await dispatch(fileRenamedSuccess(oldPath, newPath))
const data = await fetchDirectoryContent(provider, path)
await dispatch(fileRenamedSuccess(path, oldPath, data))
} }
const rootFolderChanged = async (path) => { const rootFolderChanged = async (path) => {
await dispatch(rootFolderChangedSuccess(path)) await dispatch(rootFolderChangedSuccess(path))
} }
const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemoved' | 'fileRenamed' | 'rootFolderChanged', path?: string) => { const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemoved' | 'fileRenamed' | 'rootFolderChanged', ...args) => {
if (Object.keys(pendingEvents).length) { if (Object.keys(pendingEvents).length) {
return queuedEvents.push({ eventName, path }) return queuedEvents.push({ eventName, path: args[0] })
} }
pendingEvents[eventName + path] = { eventName, path } pendingEvents[eventName + args[0]] = { eventName, path: args[0] }
switch (eventName) { switch (eventName) {
case 'fileAdded': case 'fileAdded':
await fileAdded(path) await fileAdded(args[0])
delete pendingEvents[eventName + path] delete pendingEvents[eventName + args[0]]
if (queuedEvents.length) { if (queuedEvents.length) {
const next = queuedEvents.pop() const next = queuedEvents.pop()
...@@ -441,8 +445,8 @@ const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemove ...@@ -441,8 +445,8 @@ const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemove
break break
case 'folderAdded': case 'folderAdded':
await folderAdded(path) await folderAdded(args[0])
delete pendingEvents[eventName + path] delete pendingEvents[eventName + args[0]]
if (queuedEvents.length) { if (queuedEvents.length) {
const next = queuedEvents.pop() const next = queuedEvents.pop()
...@@ -451,8 +455,8 @@ const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemove ...@@ -451,8 +455,8 @@ const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemove
break break
case 'fileRemoved': case 'fileRemoved':
await fileRemoved(path) await fileRemoved(args[0])
delete pendingEvents[eventName + path] delete pendingEvents[eventName + args[0]]
if (queuedEvents.length) { if (queuedEvents.length) {
const next = queuedEvents.pop() const next = queuedEvents.pop()
...@@ -461,8 +465,8 @@ const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemove ...@@ -461,8 +465,8 @@ const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemove
break break
case 'fileRenamed': case 'fileRenamed':
await fileRenamed(path) await fileRenamed(args[0], args[1])
delete pendingEvents[eventName + path] delete pendingEvents[eventName + args[0]]
if (queuedEvents.length) { if (queuedEvents.length) {
const next = queuedEvents.pop() const next = queuedEvents.pop()
...@@ -471,8 +475,8 @@ const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemove ...@@ -471,8 +475,8 @@ const executeEvent = async (eventName: 'fileAdded' | 'folderAdded' | 'fileRemove
break break
case 'rootFolderChanged': case 'rootFolderChanged':
await rootFolderChanged(path) await rootFolderChanged(args[0])
delete pendingEvents[eventName + path] delete pendingEvents[eventName + args[0]]
if (queuedEvents.length) { if (queuedEvents.length) {
const next = queuedEvents.pop() const next = queuedEvents.pop()
......
import { extractNameFromKey, File } from '@remix-ui/file-explorer' import { extractNameFromKey, File } from '@remix-ui/file-explorer'
import { extractParentFromKey } from '@remix-ui/helper'
import * as _ from 'lodash' import * as _ from 'lodash'
interface Action { interface Action {
type: string type: string
...@@ -288,6 +289,22 @@ export const browserReducer = (state = browserInitialState, action: Action) => { ...@@ -288,6 +289,22 @@ export const browserReducer = (state = browserInitialState, action: Action) => {
} }
} }
case 'FILE_RENAMED_SUCCESS': {
const payload = action.payload as { oldPath: string, newPath: string }
return {
...state,
browser: {
...state.browser,
files: state.mode === 'browser' ? fileRenamed(state, payload) : state.browser.files
},
localhost: {
...state.localhost,
files: state.mode === 'localhost' ? fileRenamed(state, payload) : state.localhost.files
}
}
}
default: default:
throw new Error() throw new Error()
} }
...@@ -309,6 +326,14 @@ const fileAdded = (state: BrowserState, path: string): { [x: string]: Record<str ...@@ -309,6 +326,14 @@ const fileAdded = (state: BrowserState, path: string): { [x: string]: Record<str
const folderAdded = (state: BrowserState, path: string): { [x: string]: Record<string, File> } => { const folderAdded = (state: BrowserState, path: string): { [x: string]: Record<string, File> } => {
let files = state.mode === 'browser' ? state.browser.files : state.localhost.files let files = state.mode === 'browser' ? state.browser.files : state.localhost.files
const _path = splitPath(state, path) const _path = splitPath(state, path)
const _dir = splitPath(state, extractParentFromKey(path))
const prevFiles = _.get(files, _dir)
if (prevFiles.child) {
} else {
}
files = _.set(files, _path, { files = _.set(files, _path, {
path: path, path: path,
...@@ -327,6 +352,28 @@ const fileRemoved = (state: BrowserState, path: string): { [x: string]: Record<s ...@@ -327,6 +352,28 @@ const fileRemoved = (state: BrowserState, path: string): { [x: string]: Record<s
return files return files
} }
const fileRenamed = (state: BrowserState, payload: { oldPath: string, newPath: string }): { [x: string]: Record<string, File> } => {
let files = state.mode === 'browser' ? state.browser.files : state.localhost.files
const _oldPath = splitPath(state, payload.oldPath)
const _newPath = splitPath(state, payload.newPath)
const prevFiles = _.get(files, _oldPath)
const nextFiles = {
...prevFiles,
path: payload.newPath,
name: extractNameFromKey(payload.newPath)
}
if (nextFiles.child) {
nextFiles.child = sortFolderAndFiles(nextFiles.child)
files = _.set(files, _newPath, nextFiles)
} else {
files = _.set(files, _newPath, nextFiles)
files = state.mode === 'browser' ? { [state.browser.currentWorkspace]: sortFolderAndFiles(files[state.browser.currentWorkspace]) } : { [state.mode]: sortFolderAndFiles(files[state.mode]) }
}
_.unset(files, _oldPath)
return files
}
// IDEA: Modify function to remove blank input field without fetching content // IDEA: Modify function to remove blank input field without fetching content
const fetchDirectoryContent = (state: BrowserState, payload: { fileTree, path: string, type?: 'file' | 'folder' }, deletePath?: string) => { const fetchDirectoryContent = (state: BrowserState, payload: { fileTree, path: string, type?: 'file' | 'folder' }, deletePath?: string) => {
if (state.mode === 'browser') { if (state.mode === 'browser') {
...@@ -417,7 +464,7 @@ const normalize = (filesList, directory?: string, newInputType?: 'folder' | 'fil ...@@ -417,7 +464,7 @@ const normalize = (filesList, directory?: string, newInputType?: 'folder' | 'fil
const splitPath = (state: BrowserState, path: string): string[] | string => { const splitPath = (state: BrowserState, path: string): string[] | string => {
const root = state.mode === 'browser' ? state.browser.currentWorkspace : 'localhost' const root = state.mode === 'browser' ? state.browser.currentWorkspace : 'localhost'
const pathArr: string[] = path.split('/').filter(value => value) const pathArr: string[] = (path || '').split('/').filter(value => value)
if (pathArr[0] !== root) pathArr.unshift(root) if (pathArr[0] !== root) pathArr.unshift(root)
const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => { const _path = pathArr.map((key, index) => index > 1 ? ['child', key] : key).reduce((acc: string[], cur) => {
...@@ -426,3 +473,18 @@ const splitPath = (state: BrowserState, path: string): string[] | string => { ...@@ -426,3 +473,18 @@ const splitPath = (state: BrowserState, path: string): string[] | string => {
return _path return _path
} }
const sortFolderAndFiles = (filesList: Record<string, File>): Record<string, File> => {
const folders = {}
const files = {}
Object.keys(filesList || {}).forEach(key => {
if (filesList[key].isDirectory) {
folders[key] = filesList[key]
} else {
files[key] = filesList[key]
}
})
return Object.assign({}, folders, files)
}
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