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: improve content manager UX with rename, validation, and error feedback

esenginee7b48d26c0ec
diff --git a/desktop/src/TauriPlatformAdapter.ts b/desktop/src/TauriPlatformAdapter.ts
index 04db6c765..e46626b52 100644
--- a/desktop/src/TauriPlatformAdapter.ts
+++ b/desktop/src/TauriPlatformAdapter.ts
@@ -5,7 +5,7 @@
import { convertFileSrc } from '@tauri-apps/api/core';
import { open, save } from '@tauri-apps/plugin-dialog';
-import { readTextFile, writeTextFile, exists, mkdir, remove } from '@tauri-apps/plugin-fs';
+import { readTextFile, writeTextFile, exists, mkdir, remove, rename as fsRename } from '@tauri-apps/plugin-fs';
import { join } from '@tauri-apps/api/path';
import type { PlatformAdapter, FileDialogOptions, SaveDialogOptions } from '@esengine/editor';
@@ -58,6 +58,10 @@ export class TauriPlatformAdapter implements PlatformAdapter {
await remove(path, { recursive: true });
}
+ async rename(oldPath: string, newPath: string): Promise<void> {
+ await fsRename(oldPath, newPath);
+ }
+
joinPath(...segments: string[]): string {
return segments.join('/').replace(/\\/g, '/').replace(/\/+/g, '/');
}
diff --git a/editor/src/Editor.ts b/editor/src/Editor.ts
index 5abe3705d..33bea99bb 100644
--- a/editor/src/Editor.ts
+++ b/editor/src/Editor.ts
@@ -58,6 +58,7 @@ import { showToast, showSuccessToast, showErrorToast } from './ui/Toast';
import { showContextMenu } from './ui/ContextMenu';
import { showConfirmDialog, showInputDialog } from './ui/dialog';
import { getEditorStore } from './store';
+import { generateUniqueName } from './utils/naming';
import {
registerBuiltinSettings,
lockBuiltinSettings,
@@ -304,8 +305,15 @@ export class Editor {
const entityData = this.store_.getEntityData(entity as number);
if (!entityData) return;
+ const scene = this.store_.scene;
+ const siblings = scene.entities
+ .filter(e => e.parent === entityData.parent)
+ .map(e => e.name);
+ const siblingNames = new Set(siblings);
+ const newName = generateUniqueName(entityData.name, siblingNames);
+
const newEntity = this.store_.createEntity(
- `${entityData.name}_copy`,
+ newName,
entityData.parent as Entity | null
);
diff --git a/editor/src/asset/AssetLibrary.ts b/editor/src/asset/AssetLibrary.ts
index 52ed24712..024b0d18b 100644
--- a/editor/src/asset/AssetLibrary.ts
+++ b/editor/src/asset/AssetLibrary.ts
@@ -231,6 +231,17 @@ export class AssetLibrary {
this.pathToUuid_.delete(relativePath);
}
+ updatePath(oldRelativePath: string, newRelativePath: string): void {
+ const uuid = this.pathToUuid_.get(oldRelativePath);
+ if (uuid) {
+ this.uuidToPath_.set(uuid, newRelativePath);
+ }
+ this.pathToUuid_.delete(oldRelativePath);
+ if (uuid) {
+ this.pathToUuid_.set(newRelativePath, uuid);
+ }
+ }
+
private register(uuid: string, relativePath: string): void {
this.uuidToPath_.set(uuid, relativePath);
this.pathToUuid_.set(relativePath, uuid);
diff --git a/editor/src/panels/ContentBrowserPanel.ts b/editor/src/panels/ContentBrowserPanel.ts
index 235db532f..29d2efcd7 100644
--- a/editor/src/panels/ContentBrowserPanel.ts
+++ b/editor/src/panels/ContentBrowserPanel.ts
@@ -12,6 +12,7 @@ import { getEditorContext } from '../context/EditorContext';
import { joinPath, getParentDir } from '../utils/path';
import type { NativeFS, DirectoryEntry } from '../types/NativeFS';
import { getAssetLibrary } from '../asset/AssetLibrary';
+import { showErrorToast } from '../ui/Toast';
// =============================================================================
// Types
@@ -501,6 +502,11 @@ export class ContentBrowserPanel {
navigator.clipboard.writeText(path);
},
},
+ {
+ label: 'Rename',
+ icon: icons.pencil(14),
+ onClick: () => this.renameAsset(path, type),
+ },
{ separator: true, label: '' },
{
label: 'Delete',
@@ -539,6 +545,69 @@ export class ContentBrowserPanel {
this.refresh();
} catch (err) {
console.error('Failed to delete asset:', err);
+ showErrorToast('Failed to delete asset', String(err));
+ }
+ }
+
+ private async renameAsset(path: string, type: AssetItem['type']): Promise<void> {
+ const platform = getPlatformAdapter();
+ const parentPath = getParentDir(path);
+ const fileName = path.split('/').pop() ?? path;
+ const dotIndex = fileName.lastIndexOf('.');
+ const baseName = type === 'folder' || dotIndex <= 0 ? fileName : fileName.substring(0, dotIndex);
+ const extension = type === 'folder' || dotIndex <= 0 ? '' : fileName.substring(dotIndex);
+
+ const newName = await showInputDialog({
+ title: 'Rename',
+ placeholder: 'Name',
+ defaultValue: baseName,
+ confirmText: 'Rename',
+ validator: async (value) => {
+ if (!value.trim()) return 'Name is required';
+ if (/[<>:"/\\|?*\x00-\x1f]/.test(value.trim())) {
+ return 'Name contains invalid characters';
+ }
+ const newFileName = extension ? value.trim() + extension : value.trim();
+ if (newFileName === fileName) return null;
+ const newPath = `${parentPath}/${newFileName}`;
+ if (await platform.exists(newPath)) {
+ return 'A file with this name already exists';
+ }
+ return null;
+ },
+ });
+
+ if (!newName) return;
+
+ const trimmed = newName.trim();
+ const newFileName = extension ? trimmed + extension : trimmed;
+ if (newFileName === fileName) return;
+
+ const newPath = `${parentPath}/${newFileName}`;
+
+ try {
+ await platform.rename(path, newPath);
+
+ const metaPath = `${path}.meta`;
+ if (await platform.exists(metaPath)) {
+ await platform.rename(metaPath, `${newPath}.meta`);
+ }
+
+ if (this.rootFolder_) {
+ const projectDir = this.rootFolder_.path;
+ const prefix = projectDir.endsWith('/') ? projectDir : projectDir + '/';
+ if (path.startsWith(prefix)) {
+ getAssetLibrary().updatePath(
+ path.substring(prefix.length),
+ newPath.substring(prefix.length)
+ );
+ }
+ }
+
+ this.refresh();
+ } catch (err) {
+ console.error('Failed to rename asset:', err);
+ showErrorToast('Failed to rename asset', String(err));
}
}
@@ -614,7 +683,7 @@ export class ContentBrowserPanel {
}
private async createNewFolder(parentPath: string): Promise<void> {
- const name = await this.promptFileName('New Folder');
+ const name = await this.promptFileName('New Folder', '', parentPath);
if (!name) return;
const platform = getPlatformAdapter();
@@ -625,11 +694,12 @@ export class ContentBrowserPanel {
this.refresh();
} catch (err) {
console.error('Failed to create folder:', err);
+ showErrorToast('Failed to create folder', String(err));
}
}
private async createNewScript(parentPath: string): Promise<void> {
- const name = await this.promptFileName('NewScript', '.ts');
+ const name = await this.promptFileName('NewScript', '.ts', parentPath);
if (!name) return;
const platform = getPlatformAdapter();
@@ -648,11 +718,12 @@ export const ${className} = defineComponent('${className}', {
this.refresh();
} catch (err) {
console.error('Failed to create script:', err);
+ showErrorToast('Failed to create script', String(err));
}
}
private async createNewMaterial(parentPath: string): Promise<void> {
- const name = await this.promptFileName('NewMaterial', '.esmaterial');
+ const name = await this.promptFileName('NewMaterial', '.esmaterial', parentPath);
if (!name) return;
const platform = getPlatformAdapter();
@@ -672,11 +743,12 @@ export const ${className} = defineComponent('${className}', {
this.refresh();
} catch (err) {
console.error('Failed to create material:', err);
+ showErrorToast('Failed to create material', String(err));
}
}
private async createNewScene(parentPath: string): Promise<void> {
- const name = await this.promptFileName('NewScene', '.esscene');
+ const name = await this.promptFileName('NewScene', '.esscene', parentPath);
if (!name) return;
const platform = getPlatformAdapter();
@@ -693,11 +765,12 @@ export const ${className} = defineComponent('${className}', {
this.refresh();
} catch (err) {
console.error('Failed to create scene:', err);
+ showErrorToast('Failed to create scene', String(err));
}
}
private async createNewShader(parentPath: string): Promise<void> {
- const name = await this.promptFileName('NewShader', '.esshader');
+ const name = await this.promptFileName('NewShader', '.esshader', parentPath);
if (!name) return;
const platform = getPlatformAdapter();
@@ -737,11 +810,12 @@ void main() {
this.refresh();
} catch (err) {
console.error('Failed to create shader:', err);
+ showErrorToast('Failed to create shader', String(err));
}
}
private async createNewBitmapFont(parentPath: string): Promise<void> {
- const name = await this.promptFileName('NewFont', '.bmfont');
+ const name = await this.promptFileName('NewFont', '.bmfont', parentPath);
if (!name) return;
const platform = getPlatformAdapter();
@@ -758,17 +832,32 @@ void main() {
this.refresh();
} catch (err) {
console.error('Failed to create bitmap font:', err);
+ showErrorToast('Failed to create bitmap font', String(err));
}
}
- private async promptFileName(defaultName: string, extension: string = ''): Promise<string | null> {
+ private async promptFileName(defaultName: string, extension: string = '', parentPath?: string): Promise<string | null> {
+ const platform = getPlatformAdapter();
+
const name = await showInputDialog({
title: 'Enter Name',
placeholder: 'Name',
defaultValue: defaultName,
confirmText: 'Create',
- validator: (value) => {
+ validator: async (value) => {
if (!value.trim()) return 'Name is required';
+ if (/[<>:"/\\|?*\x00-\x1f]/.test(value.trim())) {
+ return 'Name contains invalid characters';
+ }
+ if (parentPath) {
+ const fileName = extension && !value.trim().endsWith(extension)
+ ? value.trim() + extension
+ : value.trim();
+ const fullPath = `${parentPath}/${fileName}`;
+ if (await platform.exists(fullPath)) {
+ return 'A file with this name already exists';
+ }
+ }
return null;
},
});
diff --git a/editor/src/panels/HierarchyPanel.ts b/editor/src/panels/HierarchyPanel.ts
index a4081b352..b1a1f82b1 100644
--- a/editor/src/panels/HierarchyPanel.ts
+++ b/editor/src/panels/HierarchyPanel.ts
@@ -12,6 +12,7 @@ import { getDefaultComponentData } from '../schemas/ComponentSchemas';
import { showContextMenu, type ContextMenuItem } from '../ui/ContextMenu';
import { getEditorContext } from '../context/EditorContext';
import { getPlatformAdapter } from '../platform/PlatformAdapter';
+import { generateUniqueName } from '../utils/naming';
type DropPosition = 'before' | 'after' | 'inside';
@@ -605,8 +606,15 @@ export class HierarchyPanel {
const entityData = this.store_.getEntityData(entity as number);
if (!entityData) return;
+ const scene = this.store_.scene;
+ const siblings = scene.entities
+ .filter(e => e.parent === entityData.parent)
+ .map(e => e.name);
+ const siblingNames = new Set(siblings);
+ const newName = generateUniqueName(entityData.name, siblingNames);
+
const newEntity = this.store_.createEntity(
- `${entityData.name}_copy`,
+ newName,
entityData.parent as Entity | null
);
diff --git a/editor/src/platform/PlatformAdapter.ts b/editor/src/platform/PlatformAdapter.ts
index e85c0b6fe..79608d962 100644
--- a/editor/src/platform/PlatformAdapter.ts
+++ b/editor/src/platform/PlatformAdapter.ts
@@ -49,6 +49,9 @@ export interface PlatformAdapter {
/** Remove a file or directory */
remove(path: string): Promise<void>;
+ /** Rename a file or directory */
+ rename(oldPath: string, newPath: string): Promise<void>;
+
/** Join path segments */
joinPath(...segments: string[]): string;
}
@@ -92,6 +95,10 @@ export class WebPlatformAdapter implements PlatformAdapter {
throw new Error('File removal not available in web platform');
}
+ async rename(_oldPath: string, _newPath: string): Promise<void> {
+ throw new Error('File renaming not available in web platform');
+ }
+
joinPath(...segments: string[]): string {
return segments.join('/').replace(/\/+/g, '/');
}
diff --git a/editor/src/ui/dialog/Dialog.ts b/editor/src/ui/dialog/Dialog.ts
index e953023ee..ece2122f0 100644
--- a/editor/src/ui/dialog/Dialog.ts
+++ b/editor/src/ui/dialog/Dialog.ts
@@ -88,7 +88,8 @@ export class Dialog {
btn.addEventListener('click', async () => {
if (btnConfig.onClick) {
- await btnConfig.onClick();
+ const result = await btnConfig.onClick();
+ if (result === false) return;
}
this.close({ action: btnConfig.role ?? 'custom' });
});
diff --git a/editor/src/ui/dialog/presets.ts b/editor/src/ui/dialog/presets.ts
index 04ebe4fc2..6e391b2f3 100644
--- a/editor/src/ui/dialog/presets.ts
+++ b/editor/src/ui/dialog/presets.ts
@@ -43,9 +43,9 @@ export function showInputDialog(options: InputDialogOptions): Promise<string | n
let dialog: Dialog;
let inputValue: string | null = null;
- const validate = (): boolean => {
+ const validate = async (): Promise<boolean> => {
if (options.validator) {
- const error = options.validator(input.value.trim());
+ const error = await options.validator(input.value.trim());
if (error) {
errorEl.textContent = error;
errorEl.style.display = 'block';
@@ -56,10 +56,11 @@ export function showInputDialog(options: InputDialogOptions): Promise<string | n
return true;
};
- const submit = () => {
- if (!validate()) return;
+ const submit = async (): Promise<boolean> => {
+ if (!await validate()) return false;
inputValue = input.value.trim() || null;
dialog.close({ action: 'confirm', data: inputValue });
+ return true;
};
dialog = new Dialog({
@@ -67,7 +68,7 @@ export function showInputDialog(options: InputDialogOptions): Promise<string | n
content,
buttons: [
{ label: options.cancelText ?? 'Cancel', role: 'cancel' },
- { label: options.confirmText ?? 'Confirm', role: 'confirm', primary: true, onClick: submit },
+ { label: options.confirmText ?? 'Confirm', role: 'confirm', primary: true, onClick: () => submit() },
],
closeOnOverlay: true,
closeOnEscape: true,
diff --git a/editor/src/ui/dialog/types.ts b/editor/src/ui/dialog/types.ts
index 92c6670fa..8f72b2450 100644
--- a/editor/src/ui/dialog/types.ts
+++ b/editor/src/ui/dialog/types.ts
@@ -10,7 +10,7 @@ export interface DialogButton {
role?: DialogRole;
primary?: boolean;
disabled?: boolean;
- onClick?: () => void | Promise<void>;
+ onClick?: () => void | boolean | Promise<void | boolean>;
}
export interface DialogOptions {
@@ -35,7 +35,7 @@ export interface InputDialogOptions {
defaultValue?: string;
confirmText?: string;
cancelText?: string;
- validator?: (value: string) => string | null;
+ validator?: (value: string) => string | null | Promise<string | null>;
}
export interface ConfirmDialogOptions {
diff --git a/editor/src/utils/icons.ts b/editor/src/utils/icons.ts
index e48f5083b..bae62f40c 100644
--- a/editor/src/utils/icons.ts
+++ b/editor/src/utils/icons.ts
@@ -50,6 +50,7 @@ import {
LayoutTemplate,
Bone,
EyeOff,
+ Pencil,
} from 'lucide';
// =============================================================================
@@ -156,6 +157,7 @@ export const icons = {
download: (size?: number) => renderIcon(Download as IconNode, size),
template: (size?: number) => renderIcon(LayoutTemplate as IconNode, size),
bone: (size?: number) => renderIcon(Bone as IconNode, size),
+ pencil: (size?: number) => renderIcon(Pencil as IconNode, size),
};
export type IconName = keyof typeof icons;
diff --git a/editor/src/utils/naming.ts b/editor/src/utils/naming.ts
new file mode 100644
index 000000000..c7f4b9fe1
--- /dev/null
+++ b/editor/src/utils/naming.ts
@@ -0,0 +1,13 @@
+export function generateUniqueName(baseName: string, existingNames: Set<string>): string {
+ const match = baseName.match(/^(.+)\s*\((\d+)\)$/);
+ const coreName = match ? match[1].trimEnd() : baseName;
+ let startIndex = match ? parseInt(match[2], 10) + 1 : 1;
+
+ let candidate = `${coreName} (${startIndex})`;
+ while (existingNames.has(candidate)) {
+ startIndex++;
+ candidate = `${coreName} (${startIndex})`;
+ }
+
+ return candidate;
+}