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