// ══════════════════════════════════════════════════════════════════════════════ // Slide Paginator - Auto-split slides based on visual line count // ══════════════════════════════════════════════════════════════════════════════ /** * Configuration for slide pagination */ export const PaginationConfig = { maxLinesPerSlide: 6, // Maximum visual lines per slide (reduced to prevent overflow) minLinesForSplit: 4, // Minimum lines to justify creating new slide balanceThreshold: 0.3, // Balance ratio for splitting (30%) // Visual line weights (how many lines each content type occupies) visualLines: { h1: 2.5, // H1 takes 2.5 lines (large font with spacing) h2: 2, // H2 takes 2 lines h3: 1.5, h4: 1.2, h5: 1, h6: 1, text: 1, // Normal text = 1 line 'list-item': 1.2, // Lists need more space 'numbered-item': 1.2, 'task-list': 1.2, 'code-line': 1, // Code lines are more readable at 1:1 ratio blockquote: 1.2, image: 4, // Images take significant space table: 2.5, // Base table size 'table-row': 0.8, // Each table row hr: 1, html: 3, link: 1 }, // Block types that should NOT be split atomicBlocks: ['image', 'html', 'hr'], // Block types that CAN be split into multiple parts splittableBlocks: ['code', 'blockquote', 'table'] }; /** * Calculate visual lines for a content item */ export function calculateVisualLines(item) { const config = PaginationConfig; switch (item.type) { case 'code': const codeLines = item.content.split('\n').length; return Math.ceil(codeLines * config.visualLines['code-line']) + 1; // +1 for wrapper case 'blockquote': // Count lines in blockquote const quoteLines = item.content.split('\n').length; return quoteLines * config.visualLines.blockquote; case 'table': const rows = item.rows ? item.rows.length : 0; return config.visualLines.table + (rows * config.visualLines['table-row']); default: return config.visualLines[item.type] || 1; } } /** * Calculate total visual lines for an array of content items */ export function calculateTotalLines(contentArray) { return contentArray.reduce((total, item) => { return total + calculateVisualLines(item); }, 0); } /** * Check if content exceeds max lines per slide */ export function needsPagination(contentArray) { return calculateTotalLines(contentArray) > PaginationConfig.maxLinesPerSlide; } /** * Split a code block into multiple parts */ function splitCodeBlock(codeItem, targetLines) { const lines = codeItem.content.split('\n'); const linesPerPart = Math.ceil(targetLines / PaginationConfig.visualLines['code-line']); const parts = []; for (let i = 0; i < lines.length; i += linesPerPart) { parts.push({ type: 'code', language: codeItem.language, content: lines.slice(i, i + linesPerPart).join('\n'), isContinuation: i > 0, hasMore: i + linesPerPart < lines.length }); } return parts; } /** * Split a blockquote into multiple parts */ function splitBlockquote(quoteItem, targetLines) { const lines = quoteItem.content.split('\n'); const parts = []; for (let i = 0; i < lines.length; i += targetLines) { parts.push({ type: 'blockquote', content: lines.slice(i, i + targetLines).join('\n'), level: quoteItem.level, isContinuation: i > 0, hasMore: i + targetLines < lines.length }); } return parts; } /** * Split a table into multiple parts */ function splitTable(tableItem, targetLines) { if (!tableItem.rows || tableItem.rows.length <= 2) { return [tableItem]; // Too small to split } const header = tableItem.rows[0]; const separator = tableItem.rows[1]; const dataRows = tableItem.rows.slice(2); const rowsPerPart = Math.max(2, Math.floor(targetLines / PaginationConfig.visualLines['table-row'])); const parts = []; for (let i = 0; i < dataRows.length; i += rowsPerPart) { parts.push({ type: 'table', rows: [ header, separator, ...dataRows.slice(i, i + rowsPerPart) ], isContinuation: i > 0, hasMore: i + rowsPerPart < dataRows.length }); } return parts; } /** * Split a large content block into multiple parts */ function splitLargeBlock(item, availableLines) { const targetLines = Math.max(3, availableLines); switch (item.type) { case 'code': return splitCodeBlock(item, targetLines); case 'blockquote': return splitBlockquote(item, targetLines); case 'table': return splitTable(item, targetLines); default: return [item]; // Can't split this type } } /** * Main pagination function - split content into multiple slides */ export function paginateContent(contentArray, slideInfo = {}) { const config = PaginationConfig; const slides = []; let currentSlide = []; let currentLines = 0; // Extract title and subtitle (they go on first slide only) const title = slideInfo.title || ''; const subtitle = slideInfo.subtitle || ''; const titleLines = title ? config.visualLines.h1 : 0; const subtitleLines = subtitle ? config.visualLines.h2 : 0; const headerLines = titleLines + subtitleLines; for (let i = 0; i < contentArray.length; i++) { const item = contentArray[i]; const itemLines = calculateVisualLines(item); // Check if this item is atomic (cannot be split) const isAtomic = config.atomicBlocks.includes(item.type); const isSplittable = config.splittableBlocks.includes(item.type); // Calculate available space in current slide const usedLines = currentSlide.length === 0 ? headerLines : 0; const availableLines = config.maxLinesPerSlide - currentLines - usedLines; // Case 1: Item fits in current slide if (currentLines + itemLines <= config.maxLinesPerSlide - usedLines) { currentSlide.push(item); currentLines += itemLines; } // Case 2: Item is too large and can be split else if (isSplittable && itemLines > config.maxLinesPerSlide / 2) { // Try to use remaining space in current slide if (availableLines >= config.minLinesForSplit) { const parts = splitLargeBlock(item, availableLines); // Add first part to current slide if (parts.length > 0) { currentSlide.push(parts[0]); currentLines += calculateVisualLines(parts[0]); } // Start new slide if current is full if (currentSlide.length > 0) { slides.push({ content: currentSlide, lines: currentLines, pageNumber: slides.length + 1 }); currentSlide = []; currentLines = 0; } // Add remaining parts for (let j = 1; j < parts.length; j++) { const part = parts[j]; const partLines = calculateVisualLines(part); if (currentLines + partLines <= config.maxLinesPerSlide) { currentSlide.push(part); currentLines += partLines; } else { // Flush current slide if (currentSlide.length > 0) { slides.push({ content: currentSlide, lines: currentLines, pageNumber: slides.length + 1 }); } // Start new slide with this part currentSlide = [part]; currentLines = partLines; } } } else { // Not enough space, start new slide if (currentSlide.length > 0) { slides.push({ content: currentSlide, lines: currentLines, pageNumber: slides.length + 1 }); } // Split item across multiple slides const parts = splitLargeBlock(item, config.maxLinesPerSlide); for (const part of parts) { slides.push({ content: [part], lines: calculateVisualLines(part), pageNumber: slides.length + 1 }); } currentSlide = []; currentLines = 0; } } // Case 3: Item doesn't fit and is atomic (must start new slide) else { // Flush current slide if not empty if (currentSlide.length > 0) { slides.push({ content: currentSlide, lines: currentLines, pageNumber: slides.length + 1 }); } // Start new slide with this item currentSlide = [item]; currentLines = itemLines; } } // Add last slide if (currentSlide.length > 0) { slides.push({ content: currentSlide, lines: currentLines, pageNumber: slides.length + 1 }); } return slides; } /** * Apply pagination to parsed slide and create multiple slides if needed */ export function applyPagination(slide) { const totalLines = calculateTotalLines(slide.content); // No pagination needed if (totalLines <= PaginationConfig.maxLinesPerSlide) { return [{ ...slide, visualLines: totalLines, totalPages: 1, pageNumber: 1 }]; } // Split into multiple pages const pages = paginateContent(slide.content, { title: slide.title, subtitle: slide.subtitle }); // Create slide objects for each page return pages.map((page, index) => ({ ...slide, content: page.content, visualLines: page.lines, totalPages: pages.length, pageNumber: index + 1, title: !slide.title ? '' : index === 0 ? slide.title : `${slide.title} (${index + 1}/${pages.length})`, subtitle: index === 0 ? slide.subtitle : '', // Adjust duration proportionally duration: Math.ceil((slide.duration / pages.length) * 10) / 10 })); } /** * Get pagination info for a content array */ export function getPaginationInfo(contentArray) { const totalLines = calculateTotalLines(contentArray); const needsSplit = totalLines > PaginationConfig.maxLinesPerSlide; const estimatedPages = Math.ceil(totalLines / PaginationConfig.maxLinesPerSlide); return { totalLines, maxLinesPerSlide: PaginationConfig.maxLinesPerSlide, needsPagination: needsSplit, estimatedPages, breakdown: contentArray.map(item => ({ type: item.type, lines: calculateVisualLines(item) })) }; }