1 import {BaseSelection} from "lexical";
2 import {EditorUiContext, EditorUiElement, EditorUiStateUpdate} from "./core";
3 import {el} from "../../helpers";
5 export interface EditorBasicButtonDefinition {
9 export interface EditorButtonDefinition extends EditorBasicButtonDefinition {
10 action: (context: EditorUiContext) => void;
11 isActive: (selection: BaseSelection|null) => boolean;
14 export class EditorButton extends EditorUiElement {
15 protected definition: EditorButtonDefinition;
16 protected active: boolean = false;
18 constructor(definition: EditorButtonDefinition) {
20 this.definition = definition;
23 protected buildDOM(): HTMLButtonElement {
24 const button = el('button', {
26 class: 'editor-button',
27 }, [this.getLabel()]) as HTMLButtonElement;
29 button.addEventListener('click', this.onClick.bind(this));
35 this.definition.action(this.getContext());
38 updateActiveState(selection: BaseSelection|null) {
39 this.active = this.definition.isActive(selection);
40 this.dom?.classList.toggle('editor-button-active', this.active);
43 updateState(state: EditorUiStateUpdate): void {
44 this.updateActiveState(state.selection);
52 return this.trans(this.definition.label);