2024-06-01 23:49:47 +08:00
|
|
|
import {EditorFormModal, EditorFormModalDefinition} from "./modals";
|
|
|
|
import {EditorUiContext} from "./core";
|
2024-06-05 20:04:49 +08:00
|
|
|
import {EditorDecorator} from "./decorator";
|
2024-05-30 23:50:55 +08:00
|
|
|
|
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
export class EditorUIManager {
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
protected modalDefinitionsByKey: Record<string, EditorFormModalDefinition> = {};
|
2024-06-05 20:04:49 +08:00
|
|
|
protected decoratorConstructorsByType: Record<string, typeof EditorDecorator> = {};
|
|
|
|
protected decoratorInstancesByNodeKey: Record<string, EditorDecorator> = {};
|
2024-06-01 23:49:47 +08:00
|
|
|
protected context: EditorUiContext|null = null;
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
setContext(context: EditorUiContext) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
getContext(): EditorUiContext {
|
|
|
|
if (this.context === null) {
|
|
|
|
throw new Error(`Context attempted to be used without being set`);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.context;
|
|
|
|
}
|
|
|
|
|
|
|
|
registerModal(key: string, modalDefinition: EditorFormModalDefinition) {
|
|
|
|
this.modalDefinitionsByKey[key] = modalDefinition;
|
|
|
|
}
|
|
|
|
|
|
|
|
createModal(key: string): EditorFormModal {
|
|
|
|
const modalDefinition = this.modalDefinitionsByKey[key];
|
|
|
|
if (!modalDefinition) {
|
2024-06-05 20:04:49 +08:00
|
|
|
throw new Error(`Attempted to show modal of key [${key}] but no modal registered for that key`);
|
2024-06-01 23:49:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
const modal = new EditorFormModal(modalDefinition);
|
|
|
|
modal.setContext(this.getContext());
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-01 23:49:47 +08:00
|
|
|
return modal;
|
|
|
|
}
|
2024-05-30 23:50:55 +08:00
|
|
|
|
2024-06-05 20:04:49 +08:00
|
|
|
registerDecoratorType(type: string, decorator: typeof EditorDecorator) {
|
|
|
|
this.decoratorConstructorsByType[type] = decorator;
|
|
|
|
}
|
|
|
|
|
|
|
|
getDecorator(decoratorType: string, nodeKey: string): EditorDecorator {
|
|
|
|
if (this.decoratorInstancesByNodeKey[nodeKey]) {
|
|
|
|
return this.decoratorInstancesByNodeKey[nodeKey];
|
|
|
|
}
|
|
|
|
|
|
|
|
const decoratorClass = this.decoratorConstructorsByType[decoratorType];
|
|
|
|
if (!decoratorClass) {
|
|
|
|
throw new Error(`Attempted to use decorator of type [${decoratorType}] but not decorator registered for that type`);
|
|
|
|
}
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
const decorator = new decoratorClass(nodeKey);
|
|
|
|
this.decoratorInstancesByNodeKey[nodeKey] = decorator;
|
|
|
|
return decorator;
|
|
|
|
}
|
2024-05-30 23:50:55 +08:00
|
|
|
}
|