1 import {EditorFormModal, EditorFormModalDefinition} from "./modals";
2 import {EditorUiContext, EditorUiElement} from "./core";
3 import {EditorDecorator} from "./decorator";
6 export class EditorUIManager {
8 protected modalDefinitionsByKey: Record<string, EditorFormModalDefinition> = {};
9 protected decoratorConstructorsByType: Record<string, typeof EditorDecorator> = {};
10 protected decoratorInstancesByNodeKey: Record<string, EditorDecorator> = {};
11 protected context: EditorUiContext|null = null;
13 setContext(context: EditorUiContext) {
14 this.context = context;
17 getContext(): EditorUiContext {
18 if (this.context === null) {
19 throw new Error(`Context attempted to be used without being set`);
25 triggerStateUpdate(element: EditorUiElement) {
28 editor: this.getContext().editor
32 registerModal(key: string, modalDefinition: EditorFormModalDefinition) {
33 this.modalDefinitionsByKey[key] = modalDefinition;
36 createModal(key: string): EditorFormModal {
37 const modalDefinition = this.modalDefinitionsByKey[key];
38 if (!modalDefinition) {
39 throw new Error(`Attempted to show modal of key [${key}] but no modal registered for that key`);
42 const modal = new EditorFormModal(modalDefinition);
43 modal.setContext(this.getContext());
48 registerDecoratorType(type: string, decorator: typeof EditorDecorator) {
49 this.decoratorConstructorsByType[type] = decorator;
52 getDecorator(decoratorType: string, nodeKey: string): EditorDecorator {
53 if (this.decoratorInstancesByNodeKey[nodeKey]) {
54 return this.decoratorInstancesByNodeKey[nodeKey];
57 const decoratorClass = this.decoratorConstructorsByType[decoratorType];
58 if (!decoratorClass) {
59 throw new Error(`Attempted to use decorator of type [${decoratorType}] but not decorator registered for that type`);
63 const decorator = new decoratorClass(nodeKey);
64 this.decoratorInstancesByNodeKey[nodeKey] = decorator;