2024-05-30 23:50:55 +08:00
|
|
|
import {BaseSelection} from "lexical";
|
|
|
|
import {EditorUiContext, EditorUiElement, EditorUiStateUpdate} from "./core";
|
2024-05-30 03:38:31 +08:00
|
|
|
import {el} from "../../helpers";
|
|
|
|
|
|
|
|
export interface EditorButtonDefinition {
|
|
|
|
label: string;
|
2024-05-30 23:50:55 +08:00
|
|
|
action: (context: EditorUiContext) => void;
|
2024-05-30 03:38:31 +08:00
|
|
|
isActive: (selection: BaseSelection|null) => boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
export class EditorButton extends EditorUiElement {
|
|
|
|
protected definition: EditorButtonDefinition;
|
2024-05-30 23:50:55 +08:00
|
|
|
protected active: boolean = false;
|
2024-05-30 03:38:31 +08:00
|
|
|
|
|
|
|
constructor(definition: EditorButtonDefinition) {
|
|
|
|
super();
|
|
|
|
this.definition = definition;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected buildDOM(): HTMLButtonElement {
|
|
|
|
const button = el('button', {
|
|
|
|
type: 'button',
|
2024-05-30 19:25:25 +08:00
|
|
|
class: 'editor-button',
|
2024-05-30 23:50:55 +08:00
|
|
|
}, [this.getLabel()]) as HTMLButtonElement;
|
2024-05-30 03:38:31 +08:00
|
|
|
|
2024-05-30 19:25:25 +08:00
|
|
|
button.addEventListener('click', this.onClick.bind(this));
|
2024-05-30 03:38:31 +08:00
|
|
|
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
2024-05-30 19:25:25 +08:00
|
|
|
protected onClick() {
|
2024-05-30 23:50:55 +08:00
|
|
|
this.definition.action(this.getContext());
|
2024-05-30 19:25:25 +08:00
|
|
|
}
|
|
|
|
|
2024-05-30 03:38:31 +08:00
|
|
|
updateActiveState(selection: BaseSelection|null) {
|
2024-05-30 23:50:55 +08:00
|
|
|
this.active = this.definition.isActive(selection);
|
|
|
|
this.dom?.classList.toggle('editor-button-active', this.active);
|
2024-05-30 03:38:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
updateState(state: EditorUiStateUpdate): void {
|
|
|
|
this.updateActiveState(state.selection);
|
|
|
|
}
|
2024-05-30 23:50:55 +08:00
|
|
|
|
|
|
|
isActive(): boolean {
|
|
|
|
return this.active;
|
|
|
|
}
|
|
|
|
|
|
|
|
getLabel(): string {
|
|
|
|
return this.trans(this.definition.label);
|
|
|
|
}
|
2024-05-30 19:25:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
export class FormatPreviewButton extends EditorButton {
|
|
|
|
protected previewSampleElement: HTMLElement;
|
|
|
|
|
|
|
|
constructor(previewSampleElement: HTMLElement,definition: EditorButtonDefinition) {
|
|
|
|
super(definition);
|
|
|
|
this.previewSampleElement = previewSampleElement;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected buildDOM(): HTMLButtonElement {
|
|
|
|
const button = super.buildDOM();
|
|
|
|
button.innerHTML = '';
|
|
|
|
|
|
|
|
const preview = el('span', {
|
|
|
|
class: 'editor-button-format-preview'
|
2024-05-30 23:50:55 +08:00
|
|
|
}, [this.getLabel()]);
|
2024-05-30 19:25:25 +08:00
|
|
|
|
|
|
|
const stylesToApply = this.getStylesFromPreview();
|
|
|
|
for (const style of Object.keys(stylesToApply)) {
|
|
|
|
preview.style.setProperty(style, stylesToApply[style]);
|
|
|
|
}
|
|
|
|
|
|
|
|
button.append(preview);
|
|
|
|
return button;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected getStylesFromPreview(): Record<string, string> {
|
|
|
|
const wrap = el('div', {style: 'display: none', hidden: 'true', class: 'page-content'});
|
|
|
|
const sampleClone = this.previewSampleElement.cloneNode() as HTMLElement;
|
2024-05-30 23:50:55 +08:00
|
|
|
sampleClone.textContent = this.getLabel();
|
2024-05-30 19:25:25 +08:00
|
|
|
wrap.append(sampleClone);
|
|
|
|
document.body.append(wrap);
|
|
|
|
|
|
|
|
const propertiesToFetch = ['color', 'font-size', 'background-color', 'border-inline-start'];
|
|
|
|
const propertiesToReturn: Record<string, string> = {};
|
|
|
|
|
|
|
|
const computed = window.getComputedStyle(sampleClone);
|
|
|
|
for (const property of propertiesToFetch) {
|
|
|
|
propertiesToReturn[property] = computed.getPropertyValue(property);
|
|
|
|
}
|
|
|
|
wrap.remove();
|
|
|
|
|
|
|
|
return propertiesToReturn;
|
|
|
|
}
|
2024-05-30 03:38:31 +08:00
|
|
|
}
|