|
| 1 | +import type { DevOverlayPlugin as DevOverlayPluginDefinition } from '../../../@types/astro.js'; |
| 2 | +import { type AstroDevOverlay, type DevOverlayPlugin } from './overlay.js'; |
| 3 | + |
| 4 | +let overlay: AstroDevOverlay; |
| 5 | + |
| 6 | +document.addEventListener('DOMContentLoaded', async () => { |
| 7 | + const [ |
| 8 | + { loadDevOverlayPlugins }, |
| 9 | + { default: astroDevToolPlugin }, |
| 10 | + { default: astroAuditPlugin }, |
| 11 | + { default: astroXrayPlugin }, |
| 12 | + { AstroDevOverlay, DevOverlayCanvas }, |
| 13 | + { DevOverlayCard }, |
| 14 | + { DevOverlayHighlight }, |
| 15 | + { DevOverlayTooltip }, |
| 16 | + { DevOverlayWindow }, |
| 17 | + ] = await Promise.all([ |
| 18 | + // @ts-expect-error |
| 19 | + import('astro:dev-overlay'), |
| 20 | + import('./plugins/astro.js'), |
| 21 | + import('./plugins/audit.js'), |
| 22 | + import('./plugins/xray.js'), |
| 23 | + import('./overlay.js'), |
| 24 | + import('./ui-library/card.js'), |
| 25 | + import('./ui-library/highlight.js'), |
| 26 | + import('./ui-library/tooltip.js'), |
| 27 | + import('./ui-library/window.js'), |
| 28 | + ]); |
| 29 | + |
| 30 | + // Register custom elements |
| 31 | + customElements.define('astro-dev-overlay', AstroDevOverlay); |
| 32 | + customElements.define('astro-dev-overlay-window', DevOverlayWindow); |
| 33 | + customElements.define('astro-dev-overlay-plugin-canvas', DevOverlayCanvas); |
| 34 | + customElements.define('astro-dev-overlay-tooltip', DevOverlayTooltip); |
| 35 | + customElements.define('astro-dev-overlay-highlight', DevOverlayHighlight); |
| 36 | + customElements.define('astro-dev-overlay-card', DevOverlayCard); |
| 37 | + |
| 38 | + overlay = document.createElement('astro-dev-overlay'); |
| 39 | + |
| 40 | + const preparePlugin = ( |
| 41 | + pluginDefinition: DevOverlayPluginDefinition, |
| 42 | + builtIn: boolean |
| 43 | + ): DevOverlayPlugin => { |
| 44 | + const eventTarget = new EventTarget(); |
| 45 | + const plugin = { |
| 46 | + ...pluginDefinition, |
| 47 | + builtIn: builtIn, |
| 48 | + active: false, |
| 49 | + status: 'loading' as const, |
| 50 | + eventTarget: eventTarget, |
| 51 | + }; |
| 52 | + |
| 53 | + // Events plugins can send to the overlay to update their status |
| 54 | + eventTarget.addEventListener('plugin-notification', (evt) => { |
| 55 | + const target = overlay.shadowRoot?.querySelector(`[data-plugin-id="${plugin.id}"]`); |
| 56 | + if (!target) return; |
| 57 | + |
| 58 | + let newState = true; |
| 59 | + if (evt instanceof CustomEvent) { |
| 60 | + newState = evt.detail.state ?? true; |
| 61 | + } |
| 62 | + |
| 63 | + target.querySelector('.notification')?.toggleAttribute('data-active', newState); |
| 64 | + }); |
| 65 | + |
| 66 | + return plugin; |
| 67 | + }; |
| 68 | + |
| 69 | + const customPluginsDefinitions = (await loadDevOverlayPlugins()) as DevOverlayPluginDefinition[]; |
| 70 | + const plugins: DevOverlayPlugin[] = [ |
| 71 | + ...[astroDevToolPlugin, astroXrayPlugin, astroAuditPlugin].map((pluginDef) => |
| 72 | + preparePlugin(pluginDef, true) |
| 73 | + ), |
| 74 | + ...customPluginsDefinitions.map((pluginDef) => preparePlugin(pluginDef, false)), |
| 75 | + ]; |
| 76 | + |
| 77 | + overlay.plugins = plugins; |
| 78 | + |
| 79 | + document.body.append(overlay); |
| 80 | + |
| 81 | + document.addEventListener('astro:after-swap', () => { |
| 82 | + document.body.append(overlay); |
| 83 | + }); |
| 84 | +}); |
0 commit comments