UNPKG

pptx-automizer

Version:

A template based pptx generator

93 lines (92 loc) 4.82 kB
import { ElementInfo, ElementPosition, LayoutInfo, PlaceholderInfo, PlaceholderMappingResult, PlaceholderType, XmlElement } from '../types/xml-types'; import { ShapeModificationCallback } from '../types/types'; import { ISlide } from '../interfaces/islide'; export type EnrichedElementInfo = ElementInfo & { fromTopRank: number; fromLeftRank: number; sizeRank: number; }; export type GroupedByType = Record<ElementInfo['visualType'], EnrichedElementInfo[]>; type MapCandidates = Partial<Record<PlaceholderType, ElementInfo['visualType'][]>>; export default class XmlPlaceholderHelper { slide: ISlide; slideElements: ElementInfo[]; sourceLayoutInfo: LayoutInfo; targetPlaceholders: PlaceholderInfo[]; mappingResult: PlaceholderMappingResult; mapCandidates: MapCandidates; constructor(slide: ISlide, slideElements: ElementInfo[], sourceLayoutInfo: LayoutInfo, targetPlaceholders: PlaceholderInfo[]); run(): void; /** * Performs the initial placeholder matching between source elements and target placeholders. * Elements with exact placeholder type matches are processed first. */ performInitialPlaceholderMatching(): void; /** * Removes an element from the unmatched elements array. * * @param element - Element to remove * @param unmatchedElements - Array to remove from * @private */ private removeElementFromUnmatched; /** * Handles force assignment of placeholder types by finding the best matching * unmatched slide elements for unassigned target placeholders. * * @param forceAssignPhTypes - Array of placeholder types that should be force assigned */ handleForceAssignmentPlaceholders(forceAssignPhTypes: string[]): void; private findBestCandidateElementForPlaceholder; private findClosestCandidate; private findLargestCandidate; private findFromTopCandidate; /** * Cleans up elements that still don't have placeholder matches by removing * their placeholder properties and applying fallback positioning. */ cleanupUnmatchedPlaceholders(): void; clearUnmatchedPlaceholder(element: ElementInfo, sourcePlaceholders: PlaceholderInfo[]): void; applyPlaceholderToElement(layoutPlaceholders: PlaceholderInfo[], element: ElementInfo, placeholder: PlaceholderInfo): PlaceholderInfo; applyPlaceholder(element: ElementInfo, bestMatch: PlaceholderInfo): void; applyForceAssignedPlaceholderToElement(bestCandidate: ElementInfo, bestMatch: PlaceholderInfo, unmatchedElements: ElementInfo[]): void; postApplyModification(element: ElementInfo, callback: ShapeModificationCallback): void; /** * Extracts placeholder information from an XML element. * * This method parses a PowerPoint shape element to extract its placeholder properties, * including placeholder type, size, index, position, and element type. It also attempts to merge * position information from layout placeholders when available. * * @param element - The XML element representing a PowerPoint shape * @param layoutPlaceholders - Optional array of placeholder info from the slide layout * @returns PlaceholderInfo object containing all extracted placeholder data, or undefined if no placeholder found */ static getPlaceholderInfo(element: XmlElement, layoutPlaceholders?: PlaceholderInfo[]): PlaceholderInfo | undefined; static setPlaceholderDefaults(element: XmlElement, layoutPlaceholder: PlaceholderInfo): void; static updatePlaceholderParams(tag: string, ph: XmlElement, layoutPlaceholder: PlaceholderInfo): void; static removePlaceholder(element: XmlElement, fallbackPosition: ElementPosition): void; /** * Finds the best matching target placeholder for a source placeholder based on multiple criteria. * * @returns The best matching target placeholder or null if no suitable match found * @param sourceElement * @param typeMatches */ static findBestMatchingPlaceholder(sourceElement: ElementInfo, typeMatches: PlaceholderInfo[]): PlaceholderInfo | null; static calculatePlaceholderSimilarityScore(score: number, availablePlaceholder: PlaceholderInfo, element: ElementInfo): number; static calculateDistanceScore(pos1: ElementPosition, pos2: ElementPosition): number; /** * @param unmatchedElements - Elements that couldn't be matched initially * @returns groupedByType * @private */ static groupElements(unmatchedElements: ElementInfo[]): GroupedByType; /** * Adds or updates coordinates in a shape element * @param element The XML element of the shape * @param coords The coordinates to set */ static assertShapeCoordinates(element: XmlElement, coords: ElementPosition): void; } export {};