]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/ui/framework/manager.ts
c3fe9ecd8779c12978e51c271ee9d3481653a5a3
[bookstack] / resources / js / wysiwyg / ui / framework / manager.ts
1 import {EditorFormModal, EditorFormModalDefinition} from "./modals";
2 import {EditorUiContext} from "./core";
3
4
5 export class EditorUIManager {
6
7     protected modalDefinitionsByKey: Record<string, EditorFormModalDefinition> = {};
8     protected context: EditorUiContext|null = null;
9
10     setContext(context: EditorUiContext) {
11         this.context = context;
12     }
13
14     getContext(): EditorUiContext {
15         if (this.context === null) {
16             throw new Error(`Context attempted to be used without being set`);
17         }
18
19         return this.context;
20     }
21
22     registerModal(key: string, modalDefinition: EditorFormModalDefinition) {
23         this.modalDefinitionsByKey[key] = modalDefinition;
24     }
25
26     createModal(key: string): EditorFormModal {
27         const modalDefinition = this.modalDefinitionsByKey[key];
28         if (!modalDefinition) {
29             console.error(`Attempted to show modal of key [${key}] but no modal registered for that key`);
30         }
31
32         const modal = new EditorFormModal(modalDefinition);
33         modal.setContext(this.getContext());
34
35         return modal;
36     }
37
38 }