mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-03-02 12:58:40 +08:00

Structured UI logical to be fairly standard and mostly covered via a base class that handles context and core dom work.
40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import {BaseSelection, LexicalEditor} from "lexical";
|
|
import {EditorUiElement, EditorUiStateUpdate} from "./base-elements";
|
|
import {el} from "../../helpers";
|
|
|
|
export interface EditorButtonDefinition {
|
|
label: string;
|
|
action: (editor: LexicalEditor) => void;
|
|
isActive: (selection: BaseSelection|null) => boolean;
|
|
}
|
|
|
|
export class EditorButton extends EditorUiElement {
|
|
protected definition: EditorButtonDefinition;
|
|
|
|
constructor(definition: EditorButtonDefinition) {
|
|
super();
|
|
this.definition = definition;
|
|
}
|
|
|
|
protected buildDOM(): HTMLButtonElement {
|
|
const button = el('button', {
|
|
type: 'button',
|
|
class: 'editor-toolbar-button',
|
|
}, [this.definition.label]) as HTMLButtonElement;
|
|
|
|
button.addEventListener('click', event => {
|
|
this.definition.action(this.getContext().editor);
|
|
});
|
|
|
|
return button;
|
|
}
|
|
|
|
updateActiveState(selection: BaseSelection|null) {
|
|
const isActive = this.definition.isActive(selection);
|
|
this.dom?.classList.toggle('editor-toolbar-button-active', isActive);
|
|
}
|
|
|
|
updateState(state: EditorUiStateUpdate): void {
|
|
this.updateActiveState(state.selection);
|
|
}
|
|
} |