]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/ui/framework/manager.ts
Lexical: Added basic list button/support
[bookstack] / resources / js / wysiwyg / ui / framework / manager.ts
1 import {EditorFormModal, EditorFormModalDefinition} from "./modals";
2 import {EditorUiContext, EditorUiElement} from "./core";
3 import {EditorDecorator} from "./decorator";
4
5
6 export class EditorUIManager {
7
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;
12
13     setContext(context: EditorUiContext) {
14         this.context = context;
15     }
16
17     getContext(): EditorUiContext {
18         if (this.context === null) {
19             throw new Error(`Context attempted to be used without being set`);
20         }
21
22         return this.context;
23     }
24
25     triggerStateUpdate(element: EditorUiElement) {
26         element.updateState({
27             selection: null,
28             editor: this.getContext().editor
29         });
30     }
31
32     registerModal(key: string, modalDefinition: EditorFormModalDefinition) {
33         this.modalDefinitionsByKey[key] = modalDefinition;
34     }
35
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`);
40         }
41
42         const modal = new EditorFormModal(modalDefinition);
43         modal.setContext(this.getContext());
44
45         return modal;
46     }
47
48     registerDecoratorType(type: string, decorator: typeof EditorDecorator) {
49         this.decoratorConstructorsByType[type] = decorator;
50     }
51
52     getDecorator(decoratorType: string, nodeKey: string): EditorDecorator {
53         if (this.decoratorInstancesByNodeKey[nodeKey]) {
54             return this.decoratorInstancesByNodeKey[nodeKey];
55         }
56
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`);
60         }
61
62         // @ts-ignore
63         const decorator = new decoratorClass(nodeKey);
64         this.decoratorInstancesByNodeKey[nodeKey] = decorator;
65         return decorator;
66     }
67 }