// ObsiGate — Floating Action Button (FAB) for the AI Assistant sidebar. // Always visible in the bottom-right corner; hides while the assistant panel // is expanded. Clicking it opens the assistant in the context that matches // the current application state (open documents, or General when none). import booksLM from './bookslm.js'; import { t } from './i18n.js'; import { safeCreateIcons } from './utils.js'; let _fab = null; // The panel is visible when it has `.open` without `.hidden`. function isBooksLmVisible() { const panel = document.querySelector('.bookslm-panel'); return !!(panel && panel.classList.contains('open') && !panel.classList.contains('hidden')); } function syncVisibility() { if (!_fab) return; const visible = isBooksLmVisible(); _fab.hidden = visible; const label = visible ? t('fab.close') : t('fab.open'); _fab.title = label; _fab.setAttribute('aria-label', label); } async function onClick() { const panel = document.querySelector('.bookslm-panel'); // Collapsed panel: reveal it instead of reopening. if (panel && panel.classList.contains('open') && panel.classList.contains('hidden')) { booksLM._toggleSidebar(); return; } // Otherwise open (or re-focus) the assistant for the current context. await booksLM.openForCurrentContext(); } export function initAIFab() { if (_fab) return; const btn = document.createElement('button'); btn.type = 'button'; btn.id = 'ai-fab'; btn.className = 'ai-fab'; btn.innerHTML = ''; document.body.appendChild(btn); _fab = btn; btn.addEventListener('click', onClick); document.addEventListener('bookslm:opened', syncVisibility); document.addEventListener('bookslm:closed', syncVisibility); if (typeof safeCreateIcons === 'function') safeCreateIcons(); syncVisibility(); } export default initAIFab;