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 translate: (text: string) => string,
14 manager: EditorUIManager,
15 lastSelection: BaseSelection|null,
18 export abstract class EditorUiElement {
19 protected dom: HTMLElement|null = null;
20 private context: EditorUiContext|null = null;
22 protected abstract buildDOM(): HTMLElement;
24 setContext(context: EditorUiContext): void {
25 this.context = context;
28 getContext(): EditorUiContext {
29 if (this.context === null) {
30 throw new Error('Attempted to use EditorUIContext before it has been set');
36 getDOMElement(): HTMLElement {
38 this.dom = this.buildDOM();
45 return this.getContext().translate(text);
48 updateState(state: EditorUiStateUpdate): void {
53 export class EditorContainerUiElement extends EditorUiElement {
54 protected children : EditorUiElement[] = [];
56 constructor(children: EditorUiElement[]) {
58 this.children.push(...children);
61 protected buildDOM(): HTMLElement {
62 return el('div', {}, this.getChildren().map(child => child.getDOMElement()));
65 getChildren(): EditorUiElement[] {
69 protected addChildren(...children: EditorUiElement[]): void {
70 this.children.push(...children);
73 protected removeChildren(...children: EditorUiElement[]): void {
74 for (const child of children) {
75 this.removeChild(child);
79 protected removeChild(child: EditorUiElement) {
80 const index = this.children.indexOf(child);
82 this.children.splice(index, 1);
86 updateState(state: EditorUiStateUpdate): void {
87 for (const child of this.children) {
88 child.updateState(state);
92 setContext(context: EditorUiContext) {
93 super.setContext(context);
94 for (const child of this.getChildren()) {
95 child.setContext(context);
100 export class EditorSimpleClassContainer extends EditorContainerUiElement {
103 constructor(className: string, children: EditorUiElement[]) {
105 this.className = className;
108 protected buildDOM(): HTMLElement {
110 class: this.className,
111 }, this.getChildren().map(child => child.getDOMElement()));