File size: 11,178 Bytes
f462b1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// ══════════════════════════════════════════════════════════════════════════════
// 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)
    }))
  };
}