mirror of
https://github.com/BookStackApp/BookStack.git
synced 2024-12-04 06:43:38 +08:00
13d970c7ce
With a bunch of default icons
65 lines
1.8 KiB
TypeScript
65 lines
1.8 KiB
TypeScript
import {BaseSelection} from "lexical";
|
|
import {EditorUiContext, EditorUiElement, EditorUiStateUpdate} from "./core";
|
|
import {el} from "../../helpers";
|
|
|
|
export interface EditorBasicButtonDefinition {
|
|
label: string;
|
|
icon?: string|undefined;
|
|
}
|
|
|
|
export interface EditorButtonDefinition extends EditorBasicButtonDefinition {
|
|
action: (context: EditorUiContext) => void;
|
|
isActive: (selection: BaseSelection|null) => boolean;
|
|
}
|
|
|
|
export class EditorButton extends EditorUiElement {
|
|
protected definition: EditorButtonDefinition;
|
|
protected active: boolean = false;
|
|
|
|
constructor(definition: EditorButtonDefinition) {
|
|
super();
|
|
this.definition = definition;
|
|
}
|
|
|
|
protected buildDOM(): HTMLButtonElement {
|
|
|
|
const label = this.getLabel();
|
|
let child: string|HTMLElement = label;
|
|
if (this.definition.icon) {
|
|
child = el('span', {class: 'editor-button-icon'});
|
|
child.innerHTML = this.definition.icon;
|
|
}
|
|
|
|
const button = el('button', {
|
|
type: 'button',
|
|
class: 'editor-button',
|
|
title: this.definition.icon ? label : null,
|
|
}, [child]) as HTMLButtonElement;
|
|
|
|
button.addEventListener('click', this.onClick.bind(this));
|
|
|
|
return button;
|
|
}
|
|
|
|
protected onClick() {
|
|
this.definition.action(this.getContext());
|
|
}
|
|
|
|
updateActiveState(selection: BaseSelection|null) {
|
|
this.active = this.definition.isActive(selection);
|
|
this.dom?.classList.toggle('editor-button-active', this.active);
|
|
}
|
|
|
|
updateState(state: EditorUiStateUpdate): void {
|
|
this.updateActiveState(state.selection);
|
|
}
|
|
|
|
isActive(): boolean {
|
|
return this.active;
|
|
}
|
|
|
|
getLabel(): string {
|
|
return this.trans(this.definition.label);
|
|
}
|
|
}
|