]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/ui/framework/base-elements.ts
66501178238016ac34fe0f836531e97d95220e04
[bookstack] / resources / js / wysiwyg / ui / framework / base-elements.ts
1 import {BaseSelection, LexicalEditor} from "lexical";
2
3 export type EditorUiStateUpdate = {
4     editor: LexicalEditor,
5     selection: BaseSelection|null,
6 };
7
8 export type EditorUiContext = {
9     editor: LexicalEditor,
10 };
11
12 export abstract class EditorUiElement {
13     protected dom: HTMLElement|null = null;
14     private context: EditorUiContext|null = null;
15
16     protected abstract buildDOM(): HTMLElement;
17
18     setContext(context: EditorUiContext): void {
19         this.context = context;
20     }
21
22     getContext(): EditorUiContext {
23         if (this.context === null) {
24             throw new Error('Attempted to use EditorUIContext before it has been set');
25         }
26
27         return this.context;
28     }
29
30     getDOMElement(): HTMLElement {
31         if (!this.dom) {
32             this.dom = this.buildDOM();
33         }
34
35         return this.dom;
36     }
37
38     abstract updateState(state: EditorUiStateUpdate): void;
39 }