Skip to content

Commit 0b34d24

Browse files
filip mertensbunsenstraat
authored andcommitted
alphabetic file folder sorting
1 parent 800d2fe commit 0b34d24

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

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

Lines changed: 14 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} from '@remix-ui/drag-n-drop'
1313
import {ROOT_PATH} from '../utils/constants'
14+
import { fileKeySort } from '../utils'
1415

1516
export const FileExplorer = (props: FileExplorerProps) => {
1617
const intl = useIntl()
@@ -32,6 +33,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
3233
} = props
3334
const [state, setState] = useState<WorkSpaceState>(workspaceState)
3435
const treeRef = useRef<HTMLDivElement>(null)
36+
const [childrenKeys, setChildrenKeys] = useState<string[]>([])
3537

3638
useEffect(() => {
3739
if (contextMenuItems) {
@@ -315,6 +317,16 @@ export const FileExplorer = (props: FileExplorerProps) => {
315317
}
316318
}
317319

320+
useEffect(() => {
321+
if(files[ROOT_PATH]){
322+
const children: FileType[] = files[ROOT_PATH] as any
323+
setChildrenKeys(fileKeySort(children))
324+
} else{
325+
setChildrenKeys([])
326+
}
327+
}, [props])
328+
329+
318330
return (
319331
<Drag onFileMoved={handleFileMove} onFolderMoved={handleFolderMove}>
320332
<div ref={treeRef} tabIndex={0} style={{outline: 'none'}}>
@@ -343,7 +355,7 @@ export const FileExplorer = (props: FileExplorerProps) => {
343355
<div className="pb-4 mb-4">
344356
<TreeView id="treeViewMenu">
345357
{files[ROOT_PATH] &&
346-
Object.keys(files[ROOT_PATH]).map((key, index) => (
358+
childrenKeys.map((key, index) => (
347359
<FileRender
348360
file={files[ROOT_PATH][key]}
349361
fileDecorations={fileState}

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

Lines changed: 16 additions & 3 deletions
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,17 @@ export const FileRender = (props: RenderFileProps) => {
3840
}
3941
}, [props.file])
4042

43+
useEffect(() => {
44+
45+
if(file.child){
46+
const children: FileType[] = file.child as any
47+
setChildrenKeys(fileKeySort(children))
48+
} else {
49+
setChildrenKeys([])
50+
}
51+
52+
}, [file.child, props.expandPath, props.file])
53+
4154
const labelClass =
4255
props.focusEdit.element === file.path
4356
? 'bg-light'
@@ -85,8 +98,8 @@ export const FileRender = (props: RenderFileProps) => {
8598
return (
8699
<TreeViewItem
87100
id={`treeViewItem${file.path}`}
88-
iconX="mr-2 fa fa-folder"
89-
iconY={props.expandPath.includes(file.path) ? 'fa fa-folder-open' : 'fa fa-folder'}
101+
iconX="pr-3 fa fa-folder"
102+
iconY={props.expandPath.includes(file.path) ? 'pr-0 fa fa-folder-open' : 'pr-3 fa fa-folder'}
90103
key={`${file.path + props.index}`}
91104
label={
92105
<>
@@ -108,7 +121,7 @@ export const FileRender = (props: RenderFileProps) => {
108121
>
109122
{file.child ? (
110123
<TreeView id={`treeView${file.path}`} key={`treeView${file.path}`} {...spreadProps}>
111-
{Object.keys(file.child).map((key, index) => (
124+
{childrenKeys.map((key, index) => (
112125
<FileRender
113126
file={file.child[key]}
114127
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)