import { createApp, type App } from 'vue'; import { emitFeatureEvent } from '@/features/runtime/mainWorldAdapters'; import FloatingPanel from './FloatingPanel.vue'; import styles from './styles.css?inline'; const HOST_ID = 'ecbi-float-host'; const LAUNCHER_ID = 'ecbi-float-launcher'; const FEATURE_ID = 'pdd-ad-fee'; let app: App | null = null; let visible = false; let minimized = false; function ensureHost(): HTMLElement { let host = document.getElementById(HOST_ID); if (!host) { host = document.createElement('div'); host.id = HOST_ID; document.documentElement.appendChild(host); } return host; } function ensureShadow(host: HTMLElement): ShadowRoot { return host.shadowRoot ?? host.attachShadow({ mode: 'open' }); } function setHostHidden(hidden: boolean) { const host = document.getElementById(HOST_ID); if (host) { host.style.display = hidden ? 'none' : ''; } } function removeLauncher() { document.getElementById(LAUNCHER_ID)?.remove(); } function showLauncher() { removeLauncher(); const btn = document.createElement('button'); btn.id = LAUNCHER_ID; btn.type = 'button'; btn.textContent = 'ECBI'; btn.title = '展开 ECBI 悬浮面板'; Object.assign(btn.style, { position: 'fixed', right: '16px', bottom: '16px', zIndex: '2147483646', border: '0', borderRadius: '999px', padding: '10px 16px', background: '#1677ff', color: '#fff', fontSize: '13px', fontWeight: '500', cursor: 'pointer', boxShadow: '0 6px 16px rgba(22, 119, 255, 0.35)', } as CSSStyleDeclaration); btn.addEventListener('click', () => { minimized = false; setFloatingVisible(true); emitFeatureEvent(FEATURE_ID, 'restored'); }); document.documentElement.appendChild(btn); } function unmountApp() { if (app) { app.unmount(); app = null; } const host = document.getElementById(HOST_ID); if (host?.shadowRoot) { host.shadowRoot.innerHTML = ''; } if (host) { host.style.display = ''; } } function mountApp() { const host = ensureHost(); const shadow = ensureShadow(host); shadow.innerHTML = ''; host.style.display = ''; const styleEl = document.createElement('style'); styleEl.textContent = styles; shadow.appendChild(styleEl); const mountEl = document.createElement('div'); shadow.appendChild(mountEl); app = createApp(FloatingPanel, { onClose: () => setFloatingVisible(false), onMinimize: () => { // 只隐藏,不销毁 Vue,保留已累计的多日数据 minimized = true; setHostHidden(true); showLauncher(); visible = true; emitFeatureEvent(FEATURE_ID, 'minimized'); }, }); app.mount(mountEl); } export function isFloatingVisible(): boolean { return visible; } export function setFloatingVisible(next: boolean): boolean { if (!next) { const wasVisible = visible; visible = false; minimized = false; unmountApp(); removeLauncher(); if (wasVisible) { emitFeatureEvent(FEATURE_ID, 'closed'); } return false; } visible = true; minimized = false; removeLauncher(); setHostHidden(false); if (!app) { mountApp(); } return true; } export function toggleFloatingPanel(): boolean { return setFloatingVisible(!visible || minimized); }