1 import {Component} from './component';
3 export interface EditorToolboxChangeEventData {
8 export class EditorToolbox extends Component {
10 protected container!: HTMLElement;
11 protected buttons!: HTMLButtonElement[];
12 protected contentElements!: HTMLElement[];
13 protected toggleButton!: HTMLElement;
14 protected editorWrapEl!: HTMLElement;
16 protected open: boolean = false;
17 protected tab: string = '';
21 this.container = this.$el;
22 this.buttons = this.$manyRefs.tabButton as HTMLButtonElement[];
23 this.contentElements = this.$manyRefs.tabContent;
24 this.toggleButton = this.$refs.toggle;
25 this.editorWrapEl = this.container.closest('.page-editor') as HTMLElement;
27 this.setupListeners();
29 // Set the first tab as active on load
30 this.setActiveTab(this.contentElements[0].dataset.tabContent || '');
33 protected setupListeners(): void {
34 // Toolbox toggle button click
35 this.toggleButton.addEventListener('click', () => this.toggle());
37 this.container.addEventListener('click', (event: MouseEvent) => {
38 const button = (event.target as HTMLElement).closest('button');
39 if (button instanceof HTMLButtonElement && this.buttons.includes(button)) {
40 const name = button.dataset.tab || '';
41 this.setActiveTab(name, true);
46 protected toggle(): void {
47 this.container.classList.toggle('open');
48 const isOpen = this.container.classList.contains('open');
49 this.toggleButton.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
50 this.editorWrapEl.classList.toggle('toolbox-open', isOpen);
55 protected setActiveTab(tabName: string, openToolbox: boolean = false): void {
56 // Set button visibility
57 for (const button of this.buttons) {
58 button.classList.remove('active');
59 const bName = button.dataset.tab;
60 if (bName === tabName) button.classList.add('active');
63 // Set content visibility
64 for (const contentEl of this.contentElements) {
65 contentEl.style.display = 'none';
66 const cName = contentEl.dataset.tabContent;
67 if (cName === tabName) contentEl.style.display = 'block';
70 if (openToolbox && !this.container.classList.contains('open')) {
78 protected emitState(): void {
79 const data: EditorToolboxChangeEventData = {tab: this.tab, open: this.open};
80 this.$emit('change', data);