mirror of
https://github.com/BookStackApp/BookStack.git
synced 2025-07-31 15:24:31 +03:00
Lexical: Added ui container type
Structured UI logical to be fairly standard and mostly covered via a base class that handles context and core dom work.
This commit is contained in:
40
resources/js/wysiwyg/ui/framework/buttons.ts
Normal file
40
resources/js/wysiwyg/ui/framework/buttons.ts
Normal file
@ -0,0 +1,40 @@
|
||||
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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user