1
0
mirror of https://github.com/BookStackApp/BookStack.git synced 2025-07-30 04:23:11 +03:00

Lexical: Updated lexical, added undo state tracking, format styles

This commit is contained in:
Dan Brown
2024-06-23 11:36:48 +01:00
parent ac01c62e6e
commit a07092b7e6
6 changed files with 251 additions and 165 deletions

View File

@ -1,6 +1,7 @@
import {BaseSelection} from "lexical";
import {EditorUiContext, EditorUiElement, EditorUiStateUpdate} from "./core";
import {el} from "../../helpers";
import {context} from "esbuild";
export interface EditorBasicButtonDefinition {
label: string;
@ -10,17 +11,29 @@ export interface EditorBasicButtonDefinition {
export interface EditorButtonDefinition extends EditorBasicButtonDefinition {
action: (context: EditorUiContext) => void;
isActive: (selection: BaseSelection|null) => boolean;
setup?: (context: EditorUiContext, button: EditorButton) => void;
}
export class EditorButton extends EditorUiElement {
protected definition: EditorButtonDefinition;
protected active: boolean = false;
protected completedSetup: boolean = false;
protected disabled: boolean = false;
constructor(definition: EditorButtonDefinition) {
super();
this.definition = definition;
}
setContext(context: EditorUiContext) {
super.setContext(context);
if (this.definition.setup && !this.completedSetup) {
this.definition.setup(context, this);
this.completedSetup = true;
}
}
protected buildDOM(): HTMLButtonElement {
const label = this.getLabel();
@ -34,6 +47,7 @@ export class EditorButton extends EditorUiElement {
type: 'button',
class: 'editor-button',
title: this.definition.icon ? label : null,
disabled: this.disabled ? 'true' : null,
}, [child]) as HTMLButtonElement;
button.addEventListener('click', this.onClick.bind(this));
@ -61,4 +75,13 @@ export class EditorButton extends EditorUiElement {
getLabel(): string {
return this.trans(this.definition.label);
}
toggleDisabled(disabled: boolean) {
this.disabled = disabled;
if (disabled) {
this.dom?.setAttribute('disabled', 'true');
} else {
this.dom?.removeAttribute('disabled');
}
}
}