]> BookStack Code Mirror - bookstack/blob - resources/js/wysiwyg/ui/framework/core.ts
2fdadcb40c9835678e747fffa1b828de65dc1f5b
[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     lastSelection: BaseSelection|null,
14 };
15
16 export abstract class EditorUiElement {
17     protected dom: HTMLElement|null = null;
18     private context: EditorUiContext|null = null;
19
20     protected abstract buildDOM(): HTMLElement;
21
22     setContext(context: EditorUiContext): void {
23         this.context = context;
24     }
25
26     getContext(): EditorUiContext {
27         if (this.context === null) {
28             throw new Error('Attempted to use EditorUIContext before it has been set');
29         }
30
31         return this.context;
32     }
33
34     getDOMElement(): HTMLElement {
35         if (!this.dom) {
36             this.dom = this.buildDOM();
37         }
38
39         return this.dom;
40     }
41
42     trans(text: string) {
43         return this.getContext().translate(text);
44     }
45
46     updateState(state: EditorUiStateUpdate): void {
47         return;
48     }
49 }