|
|
|
|
|
|
|
|
| |
| |
|
|
| export const PaginationConfig = {
|
| maxLinesPerSlide: 6,
|
| minLinesForSplit: 4,
|
| balanceThreshold: 0.3,
|
|
|
|
|
| visualLines: {
|
| h1: 2.5,
|
| h2: 2,
|
| h3: 1.5,
|
| h4: 1.2,
|
| h5: 1,
|
| h6: 1,
|
| text: 1,
|
| 'list-item': 1.2,
|
| 'numbered-item': 1.2,
|
| 'task-list': 1.2,
|
| 'code-line': 1,
|
| blockquote: 1.2,
|
| image: 4,
|
| table: 2.5,
|
| 'table-row': 0.8,
|
| hr: 1,
|
| html: 3,
|
| link: 1
|
| },
|
|
|
|
|
| atomicBlocks: ['image', 'html', 'hr'],
|
|
|
|
|
| splittableBlocks: ['code', 'blockquote', 'table']
|
| };
|
|
|
| |
| |
|
|
| 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;
|
|
|
| case '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;
|
| }
|
| }
|
|
|
| |
| |
|
|
| export function calculateTotalLines(contentArray) {
|
| return contentArray.reduce((total, item) => {
|
| return total + calculateVisualLines(item);
|
| }, 0);
|
| }
|
|
|
| |
| |
|
|
| export function needsPagination(contentArray) {
|
| return calculateTotalLines(contentArray) > PaginationConfig.maxLinesPerSlide;
|
| }
|
|
|
| |
| |
|
|
| 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;
|
| }
|
|
|
| |
| |
|
|
| 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;
|
| }
|
|
|
| |
| |
|
|
| function splitTable(tableItem, targetLines) {
|
| if (!tableItem.rows || tableItem.rows.length <= 2) {
|
| return [tableItem];
|
| }
|
|
|
| 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;
|
| }
|
|
|
| |
| |
|
|
| 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];
|
| }
|
| }
|
|
|
| |
| |
|
|
| export function paginateContent(contentArray, slideInfo = {}) {
|
| const config = PaginationConfig;
|
| const slides = [];
|
| let currentSlide = [];
|
| let currentLines = 0;
|
|
|
|
|
| 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);
|
|
|
|
|
| const isAtomic = config.atomicBlocks.includes(item.type);
|
| const isSplittable = config.splittableBlocks.includes(item.type);
|
|
|
|
|
| const usedLines = currentSlide.length === 0 ? headerLines : 0;
|
| const availableLines = config.maxLinesPerSlide - currentLines - usedLines;
|
|
|
|
|
| if (currentLines + itemLines <= config.maxLinesPerSlide - usedLines) {
|
| currentSlide.push(item);
|
| currentLines += itemLines;
|
| }
|
|
|
| else if (isSplittable && itemLines > config.maxLinesPerSlide / 2) {
|
|
|
| if (availableLines >= config.minLinesForSplit) {
|
| const parts = splitLargeBlock(item, availableLines);
|
|
|
|
|
| if (parts.length > 0) {
|
| currentSlide.push(parts[0]);
|
| currentLines += calculateVisualLines(parts[0]);
|
| }
|
|
|
|
|
| if (currentSlide.length > 0) {
|
| slides.push({
|
| content: currentSlide,
|
| lines: currentLines,
|
| pageNumber: slides.length + 1
|
| });
|
| currentSlide = [];
|
| currentLines = 0;
|
| }
|
|
|
|
|
| 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 {
|
|
|
| if (currentSlide.length > 0) {
|
| slides.push({
|
| content: currentSlide,
|
| lines: currentLines,
|
| pageNumber: slides.length + 1
|
| });
|
| }
|
|
|
| currentSlide = [part];
|
| currentLines = partLines;
|
| }
|
| }
|
| } else {
|
|
|
| if (currentSlide.length > 0) {
|
| slides.push({
|
| content: currentSlide,
|
| lines: currentLines,
|
| pageNumber: slides.length + 1
|
| });
|
| }
|
|
|
|
|
| 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;
|
| }
|
| }
|
|
|
| else {
|
|
|
| if (currentSlide.length > 0) {
|
| slides.push({
|
| content: currentSlide,
|
| lines: currentLines,
|
| pageNumber: slides.length + 1
|
| });
|
| }
|
|
|
|
|
| currentSlide = [item];
|
| currentLines = itemLines;
|
| }
|
| }
|
|
|
|
|
| if (currentSlide.length > 0) {
|
| slides.push({
|
| content: currentSlide,
|
| lines: currentLines,
|
| pageNumber: slides.length + 1
|
| });
|
| }
|
|
|
| return slides;
|
| }
|
|
|
| |
| |
|
|
| export function applyPagination(slide) {
|
| const totalLines = calculateTotalLines(slide.content);
|
|
|
|
|
| if (totalLines <= PaginationConfig.maxLinesPerSlide) {
|
| return [{
|
| ...slide,
|
| visualLines: totalLines,
|
| totalPages: 1,
|
| pageNumber: 1
|
| }];
|
| }
|
|
|
|
|
| const pages = paginateContent(slide.content, {
|
| title: slide.title,
|
| subtitle: slide.subtitle
|
| });
|
|
|
|
|
| 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 : '',
|
|
|
| duration: Math.ceil((slide.duration / pages.length) * 10) / 10
|
| }));
|
| }
|
|
|
| |
| |
|
|
| 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)
|
| }))
|
| };
|
| }
|
|
|