mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-27 16:25:36 +00:00
Show a view's entries as a folder tree
Folders start closed, show how many entries they hold and nest by path, so trees/oak sits under trees. The chosen entry's folders open on their own, and a search opens every folder with a match.
This commit is contained in:
1 parent
1b08857bb8
commit
5e211fb1c4
1 file changed
+130
-31
@@ -1,5 +1,5 @@
|
||||
import { IdeIcon } from '@/components/IdeIcon'
|
||||
import { useMemo, useState } from 'react'
|
||||
import { useEffect, useMemo, useState } from 'react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger } from '@/components/ui/context-menu'
|
||||
import { Input } from '@/components/ui/input'
|
||||
@@ -9,6 +9,10 @@ import { folderPrefixOf } from '../editor/entryKeys'
|
||||
|
||||
const NUMBER = new Intl.NumberFormat('en-US')
|
||||
const DELETE_KEY = 'Delete'
|
||||
const FOLDER_SEPARATOR = '/'
|
||||
const ROW_PADDING_PX = 8
|
||||
const TREE_INDENT_PX = 14
|
||||
const CHEVRON_SLOT_PX = 18
|
||||
|
||||
/** An entry of a module view: its key, the name it is listed by, and the heading it is listed under (empty for none). */
|
||||
export interface BrowsableEntry {
|
||||
@@ -36,12 +40,17 @@ interface EntryBrowserProps {
|
||||
|
||||
type MenuTarget = { entry: string } | { folder: string } | null
|
||||
|
||||
/** A view's entries under their headings, with a search over keys, names and headings (v3e ObjectBrowser). */
|
||||
/** A view's entries in a tree of their folders, with a search over keys, names and folders that opens every folder holding a match (v3e ObjectBrowser). */
|
||||
export function EntryBrowser({ title, entries, current, onSelect, fileActions, className }: EntryBrowserProps): React.JSX.Element {
|
||||
const [search, setSearch] = useState('')
|
||||
const [menuTarget, setMenuTarget] = useState<MenuTarget>(null)
|
||||
const [openFolders, setOpenFolders] = useState<ReadonlySet<string>>(new Set())
|
||||
const wanted = search.trim().toLowerCase()
|
||||
const groups = useMemo(() => groupedEntries(entries, wanted), [entries, wanted])
|
||||
const tree = useMemo(() => folderTreeOf(entries, wanted), [entries, wanted])
|
||||
const currentGroup = useMemo(() => entries.find((entry) => entry.key === current)?.group ?? '', [entries, current])
|
||||
useEffect(() => setOpenFolders((open) => withFolderAndAncestors(open, currentGroup)), [currentGroup])
|
||||
const toggleFolder = (path: string): void => setOpenFolders((open) => toggled(open, path))
|
||||
const tier: TierContext = { current, isOpen: (path) => wanted !== '' || openFolders.has(path), onToggle: toggleFolder, onSelect, fileActions }
|
||||
const list = (
|
||||
<div
|
||||
className="px-2 py-1.5"
|
||||
@@ -53,28 +62,8 @@ export function EntryBrowser({ title, entries, current, onSelect, fileActions, c
|
||||
}
|
||||
}}
|
||||
>
|
||||
{groups.map(([group, members]) => (
|
||||
<section key={group}>
|
||||
{group !== '' && (
|
||||
<h3 data-entry-folder={group} className="truncate px-2 pt-2 pb-1 text-[11px] font-semibold text-muted-foreground">
|
||||
{group}
|
||||
</h3>
|
||||
)}
|
||||
{members.map((entry) => (
|
||||
<button
|
||||
key={entry.key}
|
||||
data-entry-key={entry.key}
|
||||
title={entry.key}
|
||||
onClick={() => onSelect(entry.key)}
|
||||
onKeyDown={(event) => fileActions !== undefined && event.key === DELETE_KEY && fileActions.onDelete(entry.key)}
|
||||
className={cn('flex h-[26px] w-full items-center rounded-md px-2 text-left', entry.key === current ? 'bg-selection' : 'hover:bg-accent')}
|
||||
>
|
||||
<span className="truncate">{entry.name}</span>
|
||||
</button>
|
||||
))}
|
||||
</section>
|
||||
))}
|
||||
{groups.length === 0 && <p className="px-2 py-1 text-[12px] text-muted-foreground">{entries.length === 0 ? 'Nothing to show' : 'Nothing matches'}</p>}
|
||||
<FolderTier folder={tree} depth={0} context={tier} />
|
||||
{tree.size === 0 && <p className="px-2 py-1 text-[12px] text-muted-foreground">{entries.length === 0 ? 'Nothing to show' : 'Nothing matches'}</p>}
|
||||
</div>
|
||||
)
|
||||
return (
|
||||
@@ -142,15 +131,125 @@ function menuTargetOf(eventTarget: EventTarget): MenuTarget {
|
||||
return folder === undefined ? null : { folder }
|
||||
}
|
||||
|
||||
function groupedEntries<Entry extends BrowsableEntry>(entries: Entry[], wanted: string): [string, Entry[]][] {
|
||||
const groups = new Map<string, Entry[]>()
|
||||
/** A folder of the tree: its path such as `trees/oak`, its subfolders and entries, and how many entries it holds in all. */
|
||||
interface FolderNode {
|
||||
path: string
|
||||
name: string
|
||||
folders: FolderNode[]
|
||||
entries: BrowsableEntry[]
|
||||
size: number
|
||||
}
|
||||
|
||||
interface TierContext {
|
||||
current: string | null
|
||||
isOpen(path: string): boolean
|
||||
onToggle(path: string): void
|
||||
onSelect(key: string): void
|
||||
fileActions?: EntryFileActions
|
||||
}
|
||||
|
||||
function FolderTier({ folder, depth, context }: { folder: FolderNode; depth: number; context: TierContext }): React.JSX.Element {
|
||||
return (
|
||||
<>
|
||||
{folder.folders.map((child) => (
|
||||
<FolderBranch key={child.path} folder={child} depth={depth} context={context} />
|
||||
))}
|
||||
{folder.entries.map((entry) => (
|
||||
<EntryRow key={entry.key} entry={entry} depth={depth} context={context} />
|
||||
))}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function FolderBranch({ folder, depth, context }: { folder: FolderNode; depth: number; context: TierContext }): React.JSX.Element {
|
||||
const open = context.isOpen(folder.path)
|
||||
return (
|
||||
<>
|
||||
<button
|
||||
data-entry-folder={folder.path}
|
||||
title={folder.path}
|
||||
onClick={() => context.onToggle(folder.path)}
|
||||
className="flex h-[26px] w-full items-center gap-1 rounded-md px-2 text-left hover:bg-accent"
|
||||
style={{ paddingLeft: indentOf(depth) }}
|
||||
>
|
||||
<IdeIcon name="chevronRight" className={cn('size-3.5 text-muted-foreground transition-transform', open && 'rotate-90')} />
|
||||
<span className="min-w-0 truncate">{folder.name}</span>
|
||||
<span className="ml-auto shrink-0 pl-2 text-[11px] text-secondary-text">{NUMBER.format(folder.size)}</span>
|
||||
</button>
|
||||
{open && <FolderTier folder={folder} depth={depth + 1} context={context} />}
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
function EntryRow({ entry, depth, context }: { entry: BrowsableEntry; depth: number; context: TierContext }): React.JSX.Element {
|
||||
const { fileActions } = context
|
||||
return (
|
||||
<button
|
||||
data-entry-key={entry.key}
|
||||
title={entry.key}
|
||||
onClick={() => context.onSelect(entry.key)}
|
||||
onKeyDown={(event) => fileActions !== undefined && event.key === DELETE_KEY && fileActions.onDelete(entry.key)}
|
||||
className={cn('flex h-[26px] w-full items-center rounded-md px-2 text-left', entry.key === context.current ? 'bg-selection' : 'hover:bg-accent')}
|
||||
style={{ paddingLeft: indentOf(depth) + CHEVRON_SLOT_PX }}
|
||||
>
|
||||
<span className="truncate">{entry.name}</span>
|
||||
</button>
|
||||
)
|
||||
}
|
||||
|
||||
function indentOf(depth: number): number {
|
||||
return ROW_PADDING_PX + depth * TREE_INDENT_PX
|
||||
}
|
||||
|
||||
/** The entries matching `wanted` as a tree of their folders, folders before entries at each tier. */
|
||||
function folderTreeOf(entries: BrowsableEntry[], wanted: string): FolderNode {
|
||||
const root = folderNamed('')
|
||||
const folders = new Map<string, FolderNode>([['', root]])
|
||||
const folderAt = (path: string): FolderNode => {
|
||||
const known = folders.get(path)
|
||||
if (known !== undefined) {
|
||||
return known
|
||||
}
|
||||
const folder = folderNamed(path)
|
||||
folders.set(path, folder)
|
||||
folderAt(parentOf(path)).folders.push(folder)
|
||||
return folder
|
||||
}
|
||||
for (const entry of entries) {
|
||||
if (wanted !== '' && ![entry.key, entry.name, entry.group].some((text) => text.toLowerCase().includes(wanted))) {
|
||||
continue
|
||||
}
|
||||
const members = groups.get(entry.group) ?? []
|
||||
members.push(entry)
|
||||
groups.set(entry.group, members)
|
||||
folderAt(entry.group).entries.push(entry)
|
||||
for (let path = entry.group; ; path = parentOf(path)) {
|
||||
folderAt(path).size++
|
||||
if (path === '') {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return [...groups.entries()]
|
||||
return root
|
||||
}
|
||||
|
||||
function folderNamed(path: string): FolderNode {
|
||||
return { path, name: path.slice(path.lastIndexOf(FOLDER_SEPARATOR) + 1), folders: [], entries: [], size: 0 }
|
||||
}
|
||||
|
||||
function parentOf(path: string): string {
|
||||
return path.slice(0, Math.max(path.lastIndexOf(FOLDER_SEPARATOR), 0))
|
||||
}
|
||||
|
||||
function withFolderAndAncestors(open: ReadonlySet<string>, path: string): ReadonlySet<string> {
|
||||
const opened = new Set(open)
|
||||
for (let folder = path; folder !== ''; folder = parentOf(folder)) {
|
||||
opened.add(folder)
|
||||
}
|
||||
return opened.size === open.size ? open : opened
|
||||
}
|
||||
|
||||
function toggled(open: ReadonlySet<string>, path: string): ReadonlySet<string> {
|
||||
const next = new Set(open)
|
||||
if (!next.delete(path)) {
|
||||
next.add(path)
|
||||
}
|
||||
return next
|
||||
}
|
||||
Reference in new issue
Block a user