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; // Lexical editor instance
12 editorDOM: HTMLElement; // DOM element the editor is bound to
13 containerDOM: HTMLElement; // DOM element which contains all editor elements
14 scrollDOM: HTMLElement; // DOM element which is the main content scroll container
15 translate: (text: string) => string; // Translate function
16 manager: EditorUIManager; // UI Manager instance for this editor
17 lastSelection: BaseSelection|null; // The last tracked selection made by the user
18 options: Record<string, any>; // General user options which may be used by sub elements
21 export abstract class EditorUiElement {
22 protected dom: HTMLElement|null = null;
23 private context: EditorUiContext|null = null;
25 protected abstract buildDOM(): HTMLElement;
27 setContext(context: EditorUiContext): void {
28 this.context = context;
31 getContext(): EditorUiContext {
32 if (this.context === null) {
33 throw new Error('Attempted to use EditorUIContext before it has been set');
39 getDOMElement(): HTMLElement {
41 this.dom = this.buildDOM();
48 return this.getContext().translate(text);
51 updateState(state: EditorUiStateUpdate): void {
56 export class EditorContainerUiElement extends EditorUiElement {
57 protected children : EditorUiElement[] = [];
59 constructor(children: EditorUiElement[]) {
61 this.children.push(...children);
64 protected buildDOM(): HTMLElement {
65 return el('div', {}, this.getChildren().map(child => child.getDOMElement()));
68 getChildren(): EditorUiElement[] {
72 protected addChildren(...children: EditorUiElement[]): void {
73 this.children.push(...children);
76 protected removeChildren(...children: EditorUiElement[]): void {
77 for (const child of children) {
78 this.removeChild(child);
82 protected removeChild(child: EditorUiElement) {
83 const index = this.children.indexOf(child);
85 this.children.splice(index, 1);
89 updateState(state: EditorUiStateUpdate): void {
90 for (const child of this.children) {
91 child.updateState(state);
95 setContext(context: EditorUiContext) {
96 super.setContext(context);
97 for (const child of this.getChildren()) {
98 child.setContext(context);
103 export class EditorSimpleClassContainer extends EditorContainerUiElement {
106 constructor(className: string, children: EditorUiElement[]) {
108 this.className = className;
111 protected buildDOM(): HTMLElement {
113 class: this.className,
114 }, this.getChildren().map(child => child.getDOMElement()));