|
|
import { LAYOUTS, getNextLayoutType } from "./config"; |
|
|
|
|
|
|
|
|
const segmentLayoutMap = new Map(); |
|
|
|
|
|
|
|
|
export function groupSegmentsIntoLayouts(segments) { |
|
|
if (!segments || segments.length === 0) return []; |
|
|
|
|
|
const layouts = []; |
|
|
|
|
|
segments.forEach((segment, index) => { |
|
|
|
|
|
if (!segment.images || segment.images.length === 0) { |
|
|
return; |
|
|
} |
|
|
|
|
|
const imageCount = segment.images.length; |
|
|
|
|
|
|
|
|
if (segment.layoutType) { |
|
|
layouts.push({ type: segment.layoutType, segments: [segment] }); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
if (segment.is_first_step || segment.is_last_step) { |
|
|
layouts.push({ type: "COVER", segments: [segment] }); |
|
|
return; |
|
|
} |
|
|
|
|
|
|
|
|
let layoutType = segmentLayoutMap.get(segment.text); |
|
|
if (!layoutType) { |
|
|
layoutType = getNextLayoutType(layouts.length, imageCount); |
|
|
segmentLayoutMap.set(segment.text, layoutType); |
|
|
} |
|
|
layouts.push({ type: layoutType, segments: [segment] }); |
|
|
}); |
|
|
|
|
|
return layouts; |
|
|
} |
|
|
|
|
|
|
|
|
export function getNextPanelDimensions(segments) { |
|
|
const nonChoiceSegments = segments.filter((segment) => !segment.isChoice); |
|
|
|
|
|
|
|
|
if ( |
|
|
nonChoiceSegments.length === 0 || |
|
|
(nonChoiceSegments.length === 1 && nonChoiceSegments[0].is_first_step) || |
|
|
(nonChoiceSegments.length > 0 && |
|
|
nonChoiceSegments[nonChoiceSegments.length - 1].is_last_step) |
|
|
) { |
|
|
return LAYOUTS.COVER.panels[0]; |
|
|
} |
|
|
|
|
|
|
|
|
const lastSegment = nonChoiceSegments[nonChoiceSegments.length - 1]; |
|
|
const imageCount = lastSegment.images?.length || 0; |
|
|
let layoutType = segmentLayoutMap.get(lastSegment.text); |
|
|
if (!layoutType) { |
|
|
layoutType = getNextLayoutType(nonChoiceSegments.length - 1, imageCount); |
|
|
segmentLayoutMap.set(lastSegment.text, layoutType); |
|
|
} |
|
|
|
|
|
return LAYOUTS[layoutType].panels[0]; |
|
|
} |
|
|
|
|
|
|
|
|
export function resetLayoutMap() { |
|
|
segmentLayoutMap.clear(); |
|
|
} |
|
|
|