Skip to content

Commit

Permalink
feat: new event to toggle a plugin from itself (#8968)
Browse files Browse the repository at this point in the history
  • Loading branch information
Princesseuh authored Nov 1, 2023
1 parent 49bb7be commit d0dc18c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
11 changes: 10 additions & 1 deletion packages/astro/src/runtime/client/dev-overlay/entrypoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ document.addEventListener('DOMContentLoaded', async () => {
};

// Events plugins can send to the overlay to update their status
eventTarget.addEventListener('plugin-notification', (evt) => {
eventTarget.addEventListener('toggle-notification', (evt) => {
const target = overlay.shadowRoot?.querySelector(`[data-plugin-id="${plugin.id}"]`);
if (!target) return;

Expand All @@ -63,6 +63,15 @@ document.addEventListener('DOMContentLoaded', async () => {
target.querySelector('.notification')?.toggleAttribute('data-active', newState);
});

eventTarget.addEventListener('toggle-plugin', (evt) => {
let newState = undefined;
if (evt instanceof CustomEvent) {
newState = evt.detail.state ?? true;
}

overlay.togglePluginStatus(plugin, newState);
});

return plugin;
};

Expand Down
6 changes: 3 additions & 3 deletions packages/astro/src/runtime/client/dev-overlay/overlay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export class AstroDevOverlay extends HTMLElement {
plugin.status = 'ready';

if (import.meta.hot) {
import.meta.hot.send(`${WS_EVENT_NAME}:${plugin.id}:init`);
import.meta.hot.send(`${WS_EVENT_NAME}:${plugin.id}:initialized`);
}
} catch (e) {
console.error(`Failed to init plugin ${plugin.id}, error: ${e}`);
Expand Down Expand Up @@ -402,7 +402,7 @@ export class AstroDevOverlay extends HTMLElement {
this.getPluginCanvasById(plugin.id)?.toggleAttribute('data-active', plugin.active);

plugin.eventTarget.dispatchEvent(
new CustomEvent('plugin-toggle', {
new CustomEvent('plugin-toggled', {
detail: {
state: plugin.active,
plugin,
Expand All @@ -411,7 +411,7 @@ export class AstroDevOverlay extends HTMLElement {
);

if (import.meta.hot) {
import.meta.hot.send(`${WS_EVENT_NAME}:${plugin.id}:toggle`, { state: plugin.active });
import.meta.hot.send(`${WS_EVENT_NAME}:${plugin.id}:toggled`, { state: plugin.active });
}
}

Expand Down

0 comments on commit d0dc18c

Please sign in to comment.