Skip to content

Commit

Permalink
feat: add api to programmatically run menu plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
trusz committed Dec 13, 2024
1 parent 7ea53ed commit 3891a24
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions packages/openscd/src/addons/Layout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export class OscdLayout extends LitElement {
<div
@open-plugin-download=${() => this.pluginDownloadUI.show()}
@oscd-activate-editor=${this.handleActivateEditorByEvent}
@oscd-run-menu=${this.handleRunMenuByEvent}
>
<slot></slot>
${this.renderHeader()} ${this.renderAside()} ${this.renderContent()}
Expand Down Expand Up @@ -331,9 +332,6 @@ export class OscdLayout extends LitElement {
icon: plugin.icon || pluginIcons['menu'],
name: plugin.name,
action: ae => {
const targetMenuElement = (<List>ae.target).items[ae.detail.index];
const nextElement = targetMenuElement.nextElementSibling;

this.dispatchEvent(
newPendingStateEvent(
(<MenuPlugin>(
Expand All @@ -347,7 +345,7 @@ export class OscdLayout extends LitElement {
disabled: (): boolean => plugin.requireDoc! && this.doc === null,
content: () => {
if(plugin.content){ return plugin.content(); }
return nothing
return html``;
},
kind: kind,
}
Expand Down Expand Up @@ -390,6 +388,7 @@ export class OscdLayout extends LitElement {
class="${me.kind}"
iconid="${me.icon}"
graphic="icon"
data-name="${me.name}"
.disabled=${me.disabled?.() || !me.action}
><mwc-icon slot="graphic">${me.icon}</mwc-icon>
<span>${get(me.name)}</span>
Expand Down Expand Up @@ -534,6 +533,17 @@ export class OscdLayout extends LitElement {
this.activeTab = index;
}

private handleRunMenuByEvent(e: CustomEvent<{name: string}>): void {

// TODO: this is a workaround, fix it
this.menuUI.open = true;
const menuEntry = this.menuUI.querySelector(`[data-name="${e.detail.name}"]`) as HTMLElement
const menuElement = menuEntry.nextElementSibling
if(!menuElement){ return; } // TODO: log error

(menuElement as unknown as MenuPlugin).run()
}

/**
* Renders the landing buttons (open project and new project)
* it no document loaded we display the menu item that are in the position
Expand Down

0 comments on commit 3891a24

Please sign in to comment.