1 import {EditorUiContext, EditorUiElement, EditorUiStateUpdate} from "./core";
2 import {el} from "../../helpers";
3 import {EditorButton} from "./buttons";
5 export class EditorContainerUiElement extends EditorUiElement {
6 protected children : EditorUiElement[];
8 constructor(children: EditorUiElement[]) {
10 this.children = children;
13 protected buildDOM(): HTMLElement {
14 return el('div', {}, this.getChildren().map(child => child.getDOMElement()));
17 getChildren(): EditorUiElement[] {
21 updateState(state: EditorUiStateUpdate): void {
22 for (const child of this.children) {
23 child.updateState(state);
27 setContext(context: EditorUiContext) {
28 super.setContext(context);
29 for (const child of this.getChildren()) {
30 child.setContext(context);
35 export class EditorSimpleClassContainer extends EditorContainerUiElement {
38 constructor(className: string, children: EditorUiElement[]) {
40 this.className = className;
43 protected buildDOM(): HTMLElement {
45 class: this.className,
46 }, this.getChildren().map(child => child.getDOMElement()));
50 export class EditorFormatMenu extends EditorContainerUiElement {
51 buildDOM(): HTMLElement {
52 const childElements: HTMLElement[] = this.getChildren().map(child => child.getDOMElement());
53 const menu = el('div', {
54 class: 'editor-format-menu-dropdown editor-dropdown-menu editor-menu-list',
58 const toggle = el('button', {
59 class: 'editor-format-menu-toggle editor-button',
61 }, [this.trans('Formats')]);
63 const wrapper = el('div', {
64 class: 'editor-format-menu editor-dropdown-menu-container',
67 let clickListener: Function|null = null;
72 window.removeEventListener('click', clickListener as EventListener);
78 clickListener = (event: MouseEvent) => {
79 if (!wrapper.contains(event.target as HTMLElement)) {
83 window.addEventListener('click', clickListener as EventListener);
86 toggle.addEventListener('click', event => {
87 menu.hasAttribute('hidden') ? show() : hide();
89 menu.addEventListener('mouseleave', hide);
94 updateState(state: EditorUiStateUpdate) {
95 super.updateState(state);
97 for (const child of this.children) {
98 if (child instanceof EditorButton && child.isActive()) {
99 this.updateToggleLabel(child.getLabel());
104 this.updateToggleLabel(this.trans('Formats'));
107 protected updateToggleLabel(text: string): void {
108 const button = this.getDOMElement().querySelector('button');
110 button.innerText = text;