// Taking one outline out of the app: print or save as PDF, share it, or download it. import { buildOutlineHtml } from './bibleUtils.jsx'; import { outlineToText } from './outline.js'; const slug = (s) => String(s).replace(/[^\w]+/g, '-').replace(/^-|-$/g, '') || 'outline'; function downloadBlob(blob, filename) { const url = URL.createObjectURL(blob); const link = document.createElement('a'); link.href = url; link.download = filename; document.body.appendChild(link); link.click(); link.remove(); URL.revokeObjectURL(url); } // What gets shared as text: a heading, then the outline. export const outlineShareText = ({ title, subtitle = '', tree }) => `${title}${subtitle ? `\n${subtitle}` : ''}\n\n${outlineToText(tree)}`; // Opens a print-ready page of just this outline. Returns false when the browser blocked the window. export function printOutline({ title, subtitle, tree }) { const win = window.open('', '_blank'); if (!win) return false; win.document.write(buildOutlineHtml({ title, subtitle, tree })); win.document.close(); win.onload = () => { win.focus(); win.print(); }; return true; } // Uses the phone/tablet/computer share sheet when there is one (Messages, Mail, AirDrop…), otherwise // copies the text. -> 'shared' | 'copied' | 'cancelled' | 'failed' export async function shareOutline(outline) { const text = outlineShareText(outline); if (navigator.share) { try { await navigator.share({ title: `${outline.title} — Outline`, text }); return 'shared'; } catch (err) { if (err?.name === 'AbortError') return 'cancelled'; // fall through to copying } } try { await navigator.clipboard.writeText(text); return 'copied'; } catch { return 'failed'; } } export function downloadOutlineMarkdown({ title, subtitle, tree }) { const text = `# ${title}\n\n${subtitle ? `*${subtitle}*\n\n` : ''}${tree.length ? outlineToText(tree) : ''}\n`; downloadBlob(new Blob([text], { type: 'text/markdown;charset=utf-8' }), `${slug(title)}-outline.md`); } export async function downloadOutlineDocx(outline) { const { buildOutlineDocxBlob } = await import('./docxExport.js'); // the Word library only loads when used downloadBlob(await buildOutlineDocxBlob(outline), `${slug(outline.title)}-outline.docx`); }