/** * Deep links and the Share panel. * * Link format (hash, so it works on any page without server changes): * #pdf=&page= a specific embed (or its popup trigger) * #page= the first viewer on the page */ export interface DeepLink { pdfId: number; page: number; } export function parseDeepLink(hash = window.location.hash): DeepLink | null { const params = new URLSearchParams(hash.replace(/^#/, '')); const pdfId = Number(params.get('pdf')) || 0; const page = Number(params.get('page')) || 0; if (!pdfId && !page) return null; return { pdfId, page: Math.max(0, Math.floor(page)) }; } export function buildDeepLink(pdfId: number, page: number): string { const base = window.location.href.split('#')[0]; const parts: string[] = []; if (pdfId) parts.push(`pdf=${pdfId}`); if (page > 1 || !pdfId) parts.push(`page=${Math.max(1, page)}`); return `${base}#${parts.join('&')}`; } const ICONS: Record = { link: '', mail: '', x: '', linkedin: '', facebook: '', whatsapp: '', more: '', close: '', }; const svg = (name: string) => ``; async function copyText(text: string): Promise { try { await navigator.clipboard.writeText(text); return true; } catch { const area = document.createElement('textarea'); area.value = text; area.style.position = 'fixed'; area.style.opacity = '0'; document.body.appendChild(area); area.select(); const ok = document.execCommand('copy'); area.remove(); return ok; } } /** * Show the Share panel inside a viewer. Returns a function that closes it. */ export function openSharePanel(container: HTMLElement, opts: { pdfId: number; page: number; title: string }): () => void { container.querySelector('.pdfrack-share-panel')?.remove(); const root = document.createElement('div'); root.className = 'pdfrack-share-panel'; root.setAttribute('role', 'dialog'); root.setAttribute('aria-label', 'Share'); const startAt = opts.page > 1; root.innerHTML = `

Share

${opts.page > 1 ? `` : ''}
${svg('mail')}Email ${svg('x')}X ${svg('linkedin')}LinkedIn ${svg('facebook')}Facebook ${svg('whatsapp')}WhatsApp ${typeof navigator.share === 'function' ? `` : ''}
`; const input = root.querySelector('input[type="text"]') as HTMLInputElement; const pageBox = root.querySelector('.pdfrack-share-page input') as HTMLInputElement | null; const copyBtn = root.querySelector('.pdfrack-share-copy') as HTMLButtonElement; const title = opts.title || document.title; const link = () => buildDeepLink(opts.pdfId, pageBox && !pageBox.checked ? 1 : opts.page); const update = () => { const url = link(); const u = encodeURIComponent(url); const t = encodeURIComponent(title); input.value = url; const hrefs: Record = { email: `mailto:?subject=${t}&body=${encodeURIComponent(`${title}\n${url}`)}`, x: `https://twitter.com/intent/tweet?url=${u}&text=${t}`, linkedin: `https://www.linkedin.com/sharing/share-offsite/?url=${u}`, facebook: `https://www.facebook.com/sharer/sharer.php?u=${u}`, whatsapp: `https://wa.me/?text=${encodeURIComponent(`${title} ${url}`)}`, }; root.querySelectorAll('a[data-target]').forEach((a) => { a.href = hrefs[a.dataset.target as string]; }); }; update(); pageBox?.addEventListener('change', update); copyBtn.addEventListener('click', async () => { const ok = await copyText(input.value); const label = copyBtn.querySelector('span') as HTMLSpanElement; label.textContent = ok ? 'Copied' : 'Press Ctrl+C'; copyBtn.classList.toggle('is-done', ok); if (!ok) input.select(); window.setTimeout(() => { label.textContent = 'Copy'; copyBtn.classList.remove('is-done'); }, 1600); }); input.addEventListener('focus', () => input.select()); root.querySelector('[data-target="native"]')?.addEventListener('click', () => { navigator.share({ title, url: input.value }).catch(() => { /* dismissed */ }); }); const close = () => { root.remove(); document.removeEventListener('pointerdown', onOutside, true); document.removeEventListener('keydown', onKey, true); }; const onOutside = (e: Event) => { if (!root.contains(e.target as Node)) close(); }; const onKey = (e: KeyboardEvent) => { if (e.key === 'Escape') { e.stopPropagation(); close(); } }; root.querySelector('.pdfrack-share-close')?.addEventListener('click', close); // Keep the viewer's drag-to-pan from seeing presses inside the panel. ['pointerdown', 'mousedown', 'touchstart'].forEach((type) => root.addEventListener(type, (e) => e.stopPropagation())); // Keep viewer shortcuts out of the panel. root.addEventListener('keydown', (e) => e.stopPropagation()); container.appendChild(root); window.setTimeout(() => { document.addEventListener('pointerdown', onOutside, true); document.addEventListener('keydown', onKey, true); copyBtn.focus({ preventScroll: true }); }, 0); return close; }