-import {BaseSelection, LexicalEditor} from "lexical";
-import {EditorUiElement, EditorUiStateUpdate} from "./base-elements";
+import {BaseSelection} from "lexical";
+import {EditorUiContext, EditorUiElement, EditorUiStateUpdate} from "./core";
import {el} from "../../helpers";
export interface EditorButtonDefinition {
label: string;
- action: (editor: LexicalEditor) => void;
+ 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();
const button = el('button', {
type: 'button',
class: 'editor-button',
- }, [this.definition.label]) as HTMLButtonElement;
+ }, [this.getLabel()]) as HTMLButtonElement;
button.addEventListener('click', this.onClick.bind(this));
}
protected onClick() {
- this.definition.action(this.getContext().editor);
+ this.definition.action(this.getContext());
}
updateActiveState(selection: BaseSelection|null) {
- const isActive = this.definition.isActive(selection);
- this.dom?.classList.toggle('editor-button-active', isActive);
+ 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);
+ }
}
export class FormatPreviewButton extends EditorButton {
const preview = el('span', {
class: 'editor-button-format-preview'
- }, [this.definition.label]);
+ }, [this.getLabel()]);
const stylesToApply = this.getStylesFromPreview();
console.log(stylesToApply);
protected getStylesFromPreview(): Record<string, string> {
const wrap = el('div', {style: 'display: none', hidden: 'true', class: 'page-content'});
const sampleClone = this.previewSampleElement.cloneNode() as HTMLElement;
- sampleClone.textContent = this.definition.label;
+ sampleClone.textContent = this.getLabel();
wrap.append(sampleClone);
document.body.append(wrap);