Skip to content

Commit fba5856

Browse files
authored
Merge pull request #4098 from ethereum/filesortingagain
File sorting
2 parents dea2043 + 5e8b8b7 commit fba5856

File tree

3 files changed

+53
-4
lines changed

3 files changed

+53
-4
lines changed

libs/remix-ui/workspace/src/lib/components/file-explorer.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,15 @@ import {useIntl} from 'react-intl'
33
import {TreeView, TreeViewItem} from '@remix-ui/tree-view' // eslint-disable-line
44
import {FileExplorerMenu} from './file-explorer-menu' // eslint-disable-line
55
import {FileExplorerContextMenu} from './file-explorer-context-menu' // eslint-disable-line
6-
import {FileExplorerProps, WorkSpaceState} from '../types'
6+
import {FileExplorerProps, FileType, WorkSpaceState} from '../types'
77

88
import '../css/file-explorer.css'
99
import {checkSpecialChars, extractNameFromKey, extractParentFromKey, joinPath} from '@remix-ui/helper'
1010
// eslint-disable-next-line @typescript-eslint/no-unused-vars
1111
import {FileRender} from './file-render'
1212
import {Drag, Draggable} from '@remix-ui/drag-n-drop'
1313
import {ROOT_PATH} from '../utils/constants'
14+
import { fileKeySort } from '../utils'
1415
import { moveFileIsAllowed, moveFolderIsAllowed } from '../actions'
1516

1617
export const FileExplorer = (props: FileExplorerProps) => {
@@ -33,6 +34,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
3334
} = props
3435
const [state, setState] = useState<WorkSpaceState>(workspaceState)
3536
const treeRef = useRef<HTMLDivElement>(null)
37+
const [childrenKeys, setChildrenKeys] = useState<string[]>([])
3638

3739
useEffect(() => {
3840
if (contextMenuItems) {
@@ -332,6 +334,20 @@ export const FileExplorer = (props: FileExplorerProps) => {
332334
}
333335
}
334336

337+
useEffect(() => {
338+
if (files[ROOT_PATH]){
339+
try {
340+
const children: FileType[] = files[ROOT_PATH] as any
341+
setChildrenKeys(fileKeySort(children))
342+
} catch (error) {
343+
setChildrenKeys(Object.keys(files[ROOT_PATH]))
344+
}
345+
} else{
346+
setChildrenKeys([])
347+
}
348+
}, [props])
349+
350+
335351
return (
336352
<Drag onFileMoved={handleFileMove} onFolderMoved={handleFolderMove}>
337353
<div ref={treeRef} tabIndex={0} style={{outline: 'none'}}>
@@ -360,7 +376,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
360376
<div>
361377
<TreeView id="treeViewMenu">
362378
{files[ROOT_PATH] &&
363-
Object.keys(files[ROOT_PATH]).map((key, index) => (
379+
childrenKeys.map((key, index) => (
364380
<FileRender
365381
file={files[ROOT_PATH][key]}
366382
fileDecorations={fileState}

libs/remix-ui/workspace/src/lib/components/file-render.tsx

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {getPathIcon} from '@remix-ui/helper'
88
import {FileLabel} from './file-label'
99
import {fileDecoration, FileDecorationIcons} from '@remix-ui/file-decorators'
1010
import {Draggable} from '@remix-ui/drag-n-drop'
11+
import { fileKeySort } from '../utils'
1112

1213
export interface RenderFileProps {
1314
file: FileType
@@ -30,6 +31,7 @@ export const FileRender = (props: RenderFileProps) => {
3031
const [file, setFile] = useState<FileType>({} as FileType)
3132
const [hover, setHover] = useState<boolean>(false)
3233
const [icon, setIcon] = useState<string>('')
34+
const [childrenKeys, setChildrenKeys] = useState<string[]>([])
3335

3436
useEffect(() => {
3537
if (props.file && props.file.path && props.file.type) {
@@ -38,6 +40,19 @@ export const FileRender = (props: RenderFileProps) => {
3840
}
3941
}, [props.file])
4042

43+
useEffect(() => {
44+
if (file.child) {
45+
try {
46+
const children: FileType[] = file.child as any
47+
setChildrenKeys(fileKeySort(children))
48+
} catch (e) {
49+
setChildrenKeys(Object.keys(file.child))
50+
}
51+
} else {
52+
setChildrenKeys([])
53+
}
54+
}, [file.child, props.expandPath, props.file])
55+
4156
const labelClass =
4257
props.focusEdit.element === file.path
4358
? 'bg-light'
@@ -108,7 +123,7 @@ export const FileRender = (props: RenderFileProps) => {
108123
>
109124
{file.child ? (
110125
<TreeView id={`treeView${file.path}`} key={`treeView${file.path}`} {...spreadProps}>
111-
{Object.keys(file.child).map((key, index) => (
126+
{childrenKeys.map((key, index) => (
112127
<FileRender
113128
file={file.child[key]}
114129
fileDecorations={props.fileDecorations}

libs/remix-ui/workspace/src/lib/utils/index.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { FileType } from '@remix-ui/file-decorators'
12
import { WorkspaceProps, MenuItems } from '../types'
23

34
export const contextMenuActions: MenuItems = [{
@@ -113,4 +114,21 @@ export const contextMenuActions: MenuItems = [{
113114
multiselect: false,
114115
label: '',
115116
group: 4
116-
}]
117+
}]
118+
119+
export const fileKeySort = (children: FileType[]): string[] => {
120+
const directories = Object.keys(children).filter((key: string) => children[key].isDirectory && children[key].name !== '')
121+
122+
// sort case insensitive
123+
directories.sort((a: string, b: string) => a.toLowerCase().localeCompare(b.toLowerCase()))
124+
125+
const fileKeys = Object.keys(children).filter((key: string) => !children[key].isDirectory && children[key].name !== '')
126+
// sort case insensitive
127+
fileKeys.sort((a: string, b: string) => a.toLowerCase().localeCompare(b.toLowerCase()))
128+
129+
// find the children with a blank name
130+
const blankChildren = Object.keys(children).filter((key: string) => children[key].name === '')
131+
132+
const keys = [...directories, ...fileKeys, ...blankChildren]
133+
return keys
134+
}

0 commit comments

Comments
 (0)