Files
esengine/packages/editor-core/src/Services/FileActionRegistry.ts
YHH 107439d70c Feature/runtime cdn and plugin loader (#240)
* feat(ui): 完善 UI 布局系统和编辑器可视化工具

* refactor: 移除 ModuleRegistry,统一使用 PluginManager 插件系统

* fix: 修复 CodeQL 警告并提升测试覆盖率

* refactor: 分离运行时入口点,解决 runtime bundle 包含 React 的问题

* fix(ci): 添加 editor-core 和 editor-runtime 到 CI 依赖构建步骤

* docs: 完善 ServiceContainer 文档,新增 Symbol.for 模式和 @InjectProperty 说明

* fix(ci): 修复 type-check 失败问题

* fix(ci): 修复类型检查失败问题

* fix(ci): 修复类型检查失败问题

* fix(ci): behavior-tree 构建添加 @tauri-apps 外部依赖

* fix(ci): behavior-tree 添加 @tauri-apps/plugin-fs 类型依赖

* fix(ci): platform-web 添加缺失的 behavior-tree 依赖

* fix(lint): 移除正则表达式中不必要的转义字符
2025-11-27 20:42:46 +08:00

134 lines
3.6 KiB
TypeScript

import { IService } from '@esengine/ecs-framework';
import type { FileActionHandler, FileCreationTemplate } from '../Plugin/IPluginLoader';
// Re-export for backwards compatibility
export type { FileCreationTemplate } from '../Plugin/IPluginLoader';
/**
* 文件操作注册表服务
*
* 管理插件注册的文件操作处理器和文件创建模板
*/
export class FileActionRegistry implements IService {
private actionHandlers: Map<string, FileActionHandler[]> = new Map();
private creationTemplates: FileCreationTemplate[] = [];
/**
* 注册文件操作处理器
*/
registerActionHandler(handler: FileActionHandler): void {
for (const ext of handler.extensions) {
const handlers = this.actionHandlers.get(ext) || [];
handlers.push(handler);
this.actionHandlers.set(ext, handlers);
}
}
/**
* 注销文件操作处理器
*/
unregisterActionHandler(handler: FileActionHandler): void {
for (const ext of handler.extensions) {
const handlers = this.actionHandlers.get(ext);
if (handlers) {
const index = handlers.indexOf(handler);
if (index !== -1) {
handlers.splice(index, 1);
}
if (handlers.length === 0) {
this.actionHandlers.delete(ext);
}
}
}
}
/**
* 注册文件创建模板
*/
registerCreationTemplate(template: FileCreationTemplate): void {
this.creationTemplates.push(template);
}
/**
* 注销文件创建模板
*/
unregisterCreationTemplate(template: FileCreationTemplate): void {
const index = this.creationTemplates.indexOf(template);
if (index !== -1) {
this.creationTemplates.splice(index, 1);
}
}
/**
* 获取文件扩展名的处理器
*/
getHandlersForExtension(extension: string): FileActionHandler[] {
return this.actionHandlers.get(extension) || [];
}
/**
* 获取文件的处理器
*/
getHandlersForFile(filePath: string): FileActionHandler[] {
const extension = this.getFileExtension(filePath);
return extension ? this.getHandlersForExtension(extension) : [];
}
/**
* 获取所有文件创建模板
*/
getCreationTemplates(): FileCreationTemplate[] {
return this.creationTemplates;
}
/**
* 处理文件双击
*/
async handleDoubleClick(filePath: string): Promise<boolean> {
const handlers = this.getHandlersForFile(filePath);
for (const handler of handlers) {
if (handler.onDoubleClick) {
await handler.onDoubleClick(filePath);
return true;
}
}
return false;
}
/**
* 处理文件打开
*/
async handleOpen(filePath: string): Promise<boolean> {
const handlers = this.getHandlersForFile(filePath);
for (const handler of handlers) {
if (handler.onOpen) {
await handler.onOpen(filePath);
return true;
}
}
return false;
}
/**
* 清空所有注册
*/
clear(): void {
this.actionHandlers.clear();
this.creationTemplates = [];
}
/**
* 释放资源
*/
dispose(): void {
this.clear();
}
private getFileExtension(filePath: string): string | null {
const lastDot = filePath.lastIndexOf('.');
if (lastDot === -1) return null;
return filePath.substring(lastDot + 1).toLowerCase();
}
}