2024-05-30 16:50:55 +01:00
|
|
|
import {EditorUiContext, EditorUiElement, EditorUiStateUpdate} from "./core";
|
2024-05-29 20:38:31 +01:00
|
|
|
import {el} from "../../helpers";
|
2024-05-30 16:50:55 +01:00
|
|
|
import {EditorButton} from "./buttons";
|
2024-05-29 20:38:31 +01:00
|
|
|
|
|
|
|
export class EditorContainerUiElement extends EditorUiElement {
|
|
|
|
protected children : EditorUiElement[];
|
|
|
|
|
|
|
|
constructor(children: EditorUiElement[]) {
|
|
|
|
super();
|
|
|
|
this.children = children;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected buildDOM(): HTMLElement {
|
|
|
|
return el('div', {}, this.getChildren().map(child => child.getDOMElement()));
|
|
|
|
}
|
|
|
|
|
|
|
|
getChildren(): EditorUiElement[] {
|
|
|
|
return this.children;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateState(state: EditorUiStateUpdate): void {
|
|
|
|
for (const child of this.children) {
|
|
|
|
child.updateState(state);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setContext(context: EditorUiContext) {
|
2024-05-30 16:50:55 +01:00
|
|
|
super.setContext(context);
|
2024-05-29 20:38:31 +01:00
|
|
|
for (const child of this.getChildren()) {
|
|
|
|
child.setContext(context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-30 12:25:25 +01:00
|
|
|
export class EditorSimpleClassContainer extends EditorContainerUiElement {
|
|
|
|
protected className;
|
|
|
|
|
|
|
|
constructor(className: string, children: EditorUiElement[]) {
|
|
|
|
super(children);
|
|
|
|
this.className = className;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected buildDOM(): HTMLElement {
|
2024-05-29 20:38:31 +01:00
|
|
|
return el('div', {
|
2024-05-30 12:25:25 +01:00
|
|
|
class: this.className,
|
2024-05-29 20:38:31 +01:00
|
|
|
}, this.getChildren().map(child => child.getDOMElement()));
|
|
|
|
}
|
2024-05-30 12:25:25 +01:00
|
|
|
}
|
2024-05-29 20:38:31 +01:00
|
|
|
|
2024-05-30 12:25:25 +01:00
|
|
|
export class EditorFormatMenu extends EditorContainerUiElement {
|
|
|
|
buildDOM(): HTMLElement {
|
|
|
|
const childElements: HTMLElement[] = this.getChildren().map(child => child.getDOMElement());
|
|
|
|
const menu = el('div', {
|
|
|
|
class: 'editor-format-menu-dropdown editor-dropdown-menu editor-menu-list',
|
|
|
|
hidden: 'true',
|
|
|
|
}, childElements);
|
|
|
|
|
|
|
|
const toggle = el('button', {
|
2024-05-30 16:50:55 +01:00
|
|
|
class: 'editor-format-menu-toggle editor-button',
|
2024-05-30 12:25:25 +01:00
|
|
|
type: 'button',
|
2024-05-30 16:50:55 +01:00
|
|
|
}, [this.trans('Formats')]);
|
2024-05-30 12:25:25 +01:00
|
|
|
|
|
|
|
const wrapper = el('div', {
|
|
|
|
class: 'editor-format-menu editor-dropdown-menu-container',
|
|
|
|
}, [toggle, menu]);
|
|
|
|
|
|
|
|
let clickListener: Function|null = null;
|
|
|
|
|
|
|
|
const hide = () => {
|
|
|
|
menu.hidden = true;
|
|
|
|
if (clickListener) {
|
|
|
|
window.removeEventListener('click', clickListener as EventListener);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const show = () => {
|
|
|
|
menu.hidden = false
|
|
|
|
clickListener = (event: MouseEvent) => {
|
|
|
|
if (!wrapper.contains(event.target as HTMLElement)) {
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
window.addEventListener('click', clickListener as EventListener);
|
|
|
|
};
|
|
|
|
|
|
|
|
toggle.addEventListener('click', event => {
|
|
|
|
menu.hasAttribute('hidden') ? show() : hide();
|
|
|
|
});
|
|
|
|
menu.addEventListener('mouseleave', hide);
|
|
|
|
|
|
|
|
return wrapper;
|
|
|
|
}
|
2024-05-30 16:50:55 +01:00
|
|
|
|
|
|
|
updateState(state: EditorUiStateUpdate) {
|
|
|
|
super.updateState(state);
|
|
|
|
|
|
|
|
for (const child of this.children) {
|
|
|
|
if (child instanceof EditorButton && child.isActive()) {
|
|
|
|
this.updateToggleLabel(child.getLabel());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.updateToggleLabel(this.trans('Formats'));
|
|
|
|
}
|
|
|
|
|
|
|
|
protected updateToggleLabel(text: string): void {
|
|
|
|
const button = this.getDOMElement().querySelector('button');
|
|
|
|
if (button) {
|
|
|
|
button.innerText = text;
|
|
|
|
}
|
|
|
|
}
|
2024-05-29 20:38:31 +01:00
|
|
|
}
|