1 import {BaseSelection} from "lexical";
2 import {EditorUiContext, EditorUiElement, EditorUiStateUpdate} from "./core";
3 import {el} from "../../helpers";
5 export interface EditorBasicButtonDefinition {
7 icon?: string|undefined;
10 export interface EditorButtonDefinition extends EditorBasicButtonDefinition {
11 action: (context: EditorUiContext) => void;
12 isActive: (selection: BaseSelection|null) => boolean;
15 export class EditorButton extends EditorUiElement {
16 protected definition: EditorButtonDefinition;
17 protected active: boolean = false;
19 constructor(definition: EditorButtonDefinition) {
21 this.definition = definition;
24 protected buildDOM(): HTMLButtonElement {
26 const label = this.getLabel();
27 let child: string|HTMLElement = label;
28 if (this.definition.icon) {
29 child = el('span', {class: 'editor-button-icon'});
30 child.innerHTML = this.definition.icon;
33 const button = el('button', {
35 class: 'editor-button',
36 title: this.definition.icon ? label : null,
37 }, [child]) as HTMLButtonElement;
39 button.addEventListener('click', this.onClick.bind(this));
45 this.definition.action(this.getContext());
48 updateActiveState(selection: BaseSelection|null) {
49 this.active = this.definition.isActive(selection);
50 this.dom?.classList.toggle('editor-button-active', this.active);
53 updateState(state: EditorUiStateUpdate): void {
54 this.updateActiveState(state.selection);
62 return this.trans(this.definition.label);