1 import {BaseSelection, LexicalEditor} from "lexical";
2 import {EditorUiElement, EditorUiStateUpdate} from "./base-elements";
3 import {el} from "../../helpers";
5 export interface EditorButtonDefinition {
7 action: (editor: LexicalEditor) => void;
8 isActive: (selection: BaseSelection|null) => boolean;
11 export class EditorButton extends EditorUiElement {
12 protected definition: EditorButtonDefinition;
14 constructor(definition: EditorButtonDefinition) {
16 this.definition = definition;
19 protected buildDOM(): HTMLButtonElement {
20 const button = el('button', {
22 class: 'editor-toolbar-button',
23 }, [this.definition.label]) as HTMLButtonElement;
25 button.addEventListener('click', event => {
26 this.definition.action(this.getContext().editor);
32 updateActiveState(selection: BaseSelection|null) {
33 const isActive = this.definition.isActive(selection);
34 this.dom?.classList.toggle('editor-toolbar-button-active', isActive);
37 updateState(state: EditorUiStateUpdate): void {
38 this.updateActiveState(state.selection);