Files
esengine/packages/editor-core/src/Services/FileActionRegistry.ts

111 lines
3.2 KiB
TypeScript
Raw Normal View History

import { IService } from '@esengine/ecs-framework';
import { FileActionHandler, FileCreationTemplate } from '../Plugins/IEditorPlugin';
/**
*
*
*
*/
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);
}
}
/**
*
*/
registerCreationTemplate(template: FileCreationTemplate): void {
this.creationTemplates.push(template);
}
/**
*
*/
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 extension = this.getFileExtension(filePath);
console.log('[FileActionRegistry] handleDoubleClick:', filePath);
console.log('[FileActionRegistry] Extension:', extension);
console.log('[FileActionRegistry] Total handlers:', this.actionHandlers.size);
console.log('[FileActionRegistry] Registered extensions:', Array.from(this.actionHandlers.keys()));
const handlers = this.getHandlersForFile(filePath);
console.log('[FileActionRegistry] Found handlers:', handlers.length);
for (const handler of handlers) {
if (handler.onDoubleClick) {
console.log('[FileActionRegistry] Calling handler for extensions:', handler.extensions);
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();
}
}