Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

feat(content): search type-tokens + type/name sort in the Content Browser

esengined5e0942a0bb0
diff --git a/desktop/src/panels/ContentBrowser.tsx b/desktop/src/panels/ContentBrowser.tsx
index 2fac1a054..9dfabb35d 100644
--- a/desktop/src/panels/ContentBrowser.tsx
+++ b/desktop/src/panels/ContentBrowser.tsx
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
import { Fragment, useCallback, useEffect, useMemo, useRef, useState, useSyncExternalStore } from 'react';
-import { ChevronRight, Search, LayoutGrid, List, Import, FolderOpen, FolderPlus, ArrowLeft, ArrowRight, ArrowUp } from 'lucide-react';
+import { ChevronRight, Search, LayoutGrid, List, Import, FolderOpen, FolderPlus, ArrowLeft, ArrowRight, ArrowUp, ArrowDownUp } from 'lucide-react';
import { AssetIcon, assetTint } from '@/components/icons';
import { ContextMenu, type MenuItem } from '@/components/Menu';
import { useTooltip } from '@/components/Tooltip';
@@ -11,6 +11,7 @@ import { useSelection } from '@/store/selectionStore';
import { IMAGE_RE, assetTypeOf as assetType, TYPE_CODE } from '@/project/assetMeta';
import { ASSET_OPEN } from '@/project/assetOpen';
import { referencingPaths } from '@/project/assetRefs';
+import { parseAssetQuery, filterAndSortAssets, type AssetSort } from '@/project/assetFilter';
import { createTilesetFromTexture } from '@/tileset/openTileset';
import { createTilemapFromTileset } from '@/tilemap/createTilemap';
import { createMaterial, createMaterialInstance } from '@/material/openMaterial';
@@ -238,6 +239,7 @@ export function ContentBrowser() {
const [ctx, setCtx] = useState<{ x: number; y: number; target: { path: string; entry: DirEntry } | null } | null>(null);
const [renaming, setRenaming] = useState<string | null>(null);
const [filters, setFilters] = useState<Set<AssetType>>(new Set());
+ const [sort, setSort] = useState<AssetSort>('name');
const tip = useTooltip<{ path: string; entry: DirEntry }>((p) => <AssetTipCard path={p.path} entry={p.entry} />);
// Asset selection lives in the shared store (unified inspector): selecting an
@@ -260,17 +262,14 @@ export function ContentBrowser() {
useEffect(() => selectAsset(null), [cwd, selectAsset]);
const entries = useDir(project ? cwd : null);
- const q = query.trim().toLowerCase();
- const items = useMemo(() => {
- const list = entries.filter((e) => {
- if (q && !e.name.toLowerCase().includes(q)) return false;
- // Folders always show (they're navigation); a type filter narrows files.
- if (filters.size > 0 && !e.isDir && !filters.has(assetType(e.name))) return false;
- return true;
- });
- // Folders first, then files; both alphabetical.
- return [...list].sort((a, b) => (a.isDir === b.isDir ? a.name.localeCompare(b.name) : a.isDir ? -1 : 1));
- }, [entries, q, filters]);
+ const q = query.trim();
+ // Search supports `type:`/`t:` tokens + free text; the type chips add to the
+ // token constraint; sort is folders-first, then by name or type.
+ const parsed = useMemo(() => parseAssetQuery(query), [query]);
+ const items = useMemo(
+ () => filterAndSortAssets(entries, parsed, filters as ReadonlySet<string>, sort, assetType),
+ [entries, parsed, filters, sort],
+ );
// Double-click: enter folders; otherwise dispatch through the per-type open
// table (scene/clip/tileset editors).
@@ -549,12 +548,20 @@ export function ContentBrowser() {
<div className="search cb-search">
<Search size={13} strokeWidth={1.9} />
<input
- placeholder="Search assets"
+ placeholder="Search (type:texture …)"
spellCheck={false}
value={query}
onChange={(e) => setQuery(e.target.value)}
/>
</div>
+ <button
+ type="button"
+ className="cb-ghost"
+ title={`Sort by ${sort === 'name' ? 'name' : 'type'} — click to sort by ${sort === 'name' ? 'type' : 'name'}`}
+ onClick={() => setSort((s) => (s === 'name' ? 'type' : 'name'))}
+ >
+ <ArrowDownUp size={14} strokeWidth={2} />
+ </button>
<div className="cb-seg">
<button type="button" className={view === 'grid' ? 'on' : ''} title="Grid view" onClick={() => setView('grid')}>
<LayoutGrid size={13} strokeWidth={1.9} />
diff --git a/desktop/src/project/assetFilter.ts b/desktop/src/project/assetFilter.ts
new file mode 100644
index 000000000..f4ef5e26b
--- /dev/null
+++ b/desktop/src/project/assetFilter.ts
@@ -0,0 +1,65 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file assetFilter.ts
+ * @brief Pure search/filter/sort for the Content Browser — query-token parsing
+ * (`type:`/`t:` type tokens + free text), the type-chip filter, and the sort
+ * order. Kept engine/DOM-free so it unit-tests; the panel just feeds it the
+ * current folder's entries and renders the result.
+ */
+import type { DirEntry } from './format';
+
+export type AssetSort = 'name' | 'type';
+
+export interface ParsedQuery {
+ /** Free-text terms joined by spaces (name substring match). */
+ text: string;
+ /** Type tokens from `type:x` / `t:x` (matched as a prefix of the asset type). */
+ types: string[];
+}
+
+/** Split a raw search string into free text + `type:`/`t:` type tokens. */
+export function parseAssetQuery(raw: string): ParsedQuery {
+ const types: string[] = [];
+ const text: string[] = [];
+ for (const tok of raw.trim().toLowerCase().split(/\s+/).filter(Boolean)) {
+ const m = /^(?:type|t):(.+)$/.exec(tok);
+ if (m) types.push(m[1]);
+ else text.push(tok);
+ }
+ return { text: text.join(' '), types };
+}
+
+const matchesType = (actual: string, constraint: string): boolean =>
+ actual === constraint || actual.startsWith(constraint);
+
+/**
+ * Filter `entries` by the parsed query + active type chips, then sort (folders
+ * first, then by name or type). A type constraint (token or chip) is about files,
+ * so it hides folders; free text still matches folder names (navigation).
+ */
+export function filterAndSortAssets(
+ entries: readonly DirEntry[],
+ parsed: ParsedQuery,
+ chipTypes: ReadonlySet<string>,
+ sort: AssetSort,
+ typeOf: (name: string) => string,
+): DirEntry[] {
+ const constraints = [...new Set([...chipTypes, ...parsed.types])];
+ const hasType = constraints.length > 0;
+
+ const list = entries.filter((e) => {
+ if (parsed.text && !e.name.toLowerCase().includes(parsed.text)) return false;
+ if (e.isDir) return !hasType;
+ return !hasType || constraints.some((c) => matchesType(typeOf(e.name), c));
+ });
+
+ return [...list].sort((a, b) => {
+ if (a.isDir !== b.isDir) return a.isDir ? -1 : 1; // folders first
+ if (sort === 'type' && !a.isDir && !b.isDir) {
+ const t = typeOf(a.name).localeCompare(typeOf(b.name));
+ if (t !== 0) return t;
+ }
+ return a.name.localeCompare(b.name);
+ });
+}
diff --git a/desktop/tests/asset-filter.test.ts b/desktop/tests/asset-filter.test.ts
new file mode 100644
index 000000000..5e807127d
--- /dev/null
+++ b/desktop/tests/asset-filter.test.ts
@@ -0,0 +1,64 @@
+// SPDX-License-Identifier: Apache-2.0
+// SPDX-FileCopyrightText: Copyright (c) 2024-present ESEngine Team
+/**
+ * @file Content Browser pure search/filter/sort — query-token parsing, the type
+ * filter (tokens + chips, prefix match), and sort order.
+ */
+import { describe, it, expect } from 'vitest';
+import { parseAssetQuery, filterAndSortAssets, type AssetSort } from '@/project/assetFilter';
+import type { DirEntry } from '@/project/format';
+
+const typeOf = (name: string): string => {
+ if (name.endsWith('.png')) return 'texture';
+ if (name.endsWith('.esmat')) return 'material';
+ if (name.endsWith('.esscene')) return 'scene';
+ return 'other';
+};
+const E = (name: string, isDir = false): DirEntry => ({ name, isDir });
+const entries: DirEntry[] = [
+ E('zfolder', true),
+ E('afolder', true),
+ E('villain.png'),
+ E('hero.png'),
+ E('ground.esmat'),
+ E('main.esscene'),
+];
+const run = (q: string, chips: string[] = [], sort: AssetSort = 'name') =>
+ filterAndSortAssets(entries, parseAssetQuery(q), new Set(chips), sort, typeOf).map((e) => e.name);
+
+describe('parseAssetQuery', () => {
+ it('splits free text from type tokens', () => {
+ expect(parseAssetQuery('hero type:texture')).toEqual({ text: 'hero', types: ['texture'] });
+ expect(parseAssetQuery('t:mat foo bar')).toEqual({ text: 'foo bar', types: ['mat'] });
+ expect(parseAssetQuery(' ')).toEqual({ text: '', types: [] });
+ });
+});
+
+describe('filterAndSortAssets', () => {
+ it('free text matches names (files and folders)', () => {
+ expect(run('hero')).toEqual(['hero.png']);
+ expect(run('folder')).toEqual(['afolder', 'zfolder']); // folders sorted, files excluded
+ });
+
+ it('a type token filters files by type and hides folders', () => {
+ expect(run('type:texture')).toEqual(['hero.png', 'villain.png']);
+ expect(run('t:tex')).toEqual(['hero.png', 'villain.png']); // prefix match
+ });
+
+ it('type chips filter the same way', () => {
+ expect(run('', ['material'])).toEqual(['ground.esmat']);
+ });
+
+ it('combines text and type', () => {
+ expect(run('villain type:texture')).toEqual(['villain.png']);
+ });
+
+ it('sort=name lists folders first, then files alphabetically', () => {
+ expect(run('')).toEqual(['afolder', 'zfolder', 'ground.esmat', 'hero.png', 'main.esscene', 'villain.png']);
+ });
+
+ it('sort=type groups files by type after the folders', () => {
+ // material, scene, texture, texture — folders first.
+ expect(run('', [], 'type')).toEqual(['afolder', 'zfolder', 'ground.esmat', 'main.esscene', 'hero.png', 'villain.png']);
+ });
+});