1 import {BaseSelection, LexicalEditor} from "lexical";
2 import {EditorUIManager} from "./manager";
3 import {el} from "../../helpers";
5 export type EditorUiStateUpdate = {
7 selection: BaseSelection|null,
10 export type EditorUiContext = {
11 editor: LexicalEditor,
12 editorDOM: HTMLElement,
13 containerDOM: HTMLElement,
14 translate: (text: string) => string,
15 manager: EditorUIManager,
16 lastSelection: BaseSelection|null,
19 export abstract class EditorUiElement {
20 protected dom: HTMLElement|null = null;
21 private context: EditorUiContext|null = null;
23 protected abstract buildDOM(): HTMLElement;
25 setContext(context: EditorUiContext): void {
26 this.context = context;
29 getContext(): EditorUiContext {
30 if (this.context === null) {
31 throw new Error('Attempted to use EditorUIContext before it has been set');
37 getDOMElement(): HTMLElement {
39 this.dom = this.buildDOM();
46 return this.getContext().translate(text);
49 updateState(state: EditorUiStateUpdate): void {
54 export class EditorContainerUiElement extends EditorUiElement {
55 protected children : EditorUiElement[] = [];
57 constructor(children: EditorUiElement[]) {
59 this.children.push(...children);
62 protected buildDOM(): HTMLElement {
63 return el('div', {}, this.getChildren().map(child => child.getDOMElement()));
66 getChildren(): EditorUiElement[] {
70 protected addChildren(...children: EditorUiElement[]): void {
71 this.children.push(...children);
74 protected removeChildren(...children: EditorUiElement[]): void {
75 for (const child of children) {
76 this.removeChild(child);
80 protected removeChild(child: EditorUiElement) {
81 const index = this.children.indexOf(child);
83 this.children.splice(index, 1);
87 updateState(state: EditorUiStateUpdate): void {
88 for (const child of this.children) {
89 child.updateState(state);
93 setContext(context: EditorUiContext) {
94 super.setContext(context);
95 for (const child of this.getChildren()) {
96 child.setContext(context);
101 export class EditorSimpleClassContainer extends EditorContainerUiElement {
104 constructor(className: string, children: EditorUiElement[]) {
106 this.className = className;
109 protected buildDOM(): HTMLElement {
111 class: this.className,
112 }, this.getChildren().map(child => child.getDOMElement()));