Preserve QPowerPoint subtitle lists in PPTX
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import type { Slide, SlideImage } from "../types";
|
||||
import type { Slide, SlideImage, SlideListStyle } from "../types";
|
||||
import { officeTitleFromFileName, PPTX_MIME_TYPE, readOfficeFileAsArrayBuffer } from "./fileHelpers";
|
||||
|
||||
export interface ImportedPowerPointDeck {
|
||||
@@ -13,6 +13,7 @@ export interface ExportablePowerPointDeck {
|
||||
|
||||
interface SlideTextBlock {
|
||||
text: string;
|
||||
listStyle?: SlideListStyle;
|
||||
color?: string;
|
||||
fontSize?: number;
|
||||
fontFamily?: string;
|
||||
@@ -77,6 +78,10 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
const backgroundColor = slideBackgroundColor(parsedSlide);
|
||||
const hidden = slideHidden(parsedSlide);
|
||||
const transition = slideTransition(parsedSlide);
|
||||
const subtitleListStyle = slideTextBlocks
|
||||
.slice(1)
|
||||
.map((block) => block.listStyle)
|
||||
.find((style): style is SlideListStyle => Boolean(style));
|
||||
|
||||
return {
|
||||
id: `pptx-slide-${index}`,
|
||||
@@ -93,6 +98,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
...(slideTextBlocks[1]?.color ? { subtitleColor: slideTextBlocks[1].color } : {}),
|
||||
...(slideTextBlocks[1]?.fontSize ? { subtitleFontSize: slideTextBlocks[1].fontSize } : {}),
|
||||
...(slideTextBlocks[1]?.fontFamily ? { subtitleFontFamily: slideTextBlocks[1].fontFamily } : {}),
|
||||
...(subtitleListStyle ? { subtitleListStyle } : {}),
|
||||
images
|
||||
};
|
||||
})
|
||||
@@ -183,7 +189,9 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
|
||||
const result = await pptx.write({ outputType: "blob" });
|
||||
const blob = result instanceof Blob ? result : new Blob([result], { type: PPTX_MIME_TYPE });
|
||||
return deck.slides.some((slide) => slide.hidden || slide.transition) ? applySlideXmlOverridesToPptxBlob(blob, deck.slides) : blob;
|
||||
return deck.slides.some((slide) => slide.hidden || slide.transition || slide.subtitleListStyle)
|
||||
? applySlideXmlOverridesToPptxBlob(blob, deck.slides)
|
||||
: blob;
|
||||
}
|
||||
|
||||
export function extractTextBlocks(xmlNode: unknown): string[] {
|
||||
@@ -202,11 +210,13 @@ function extractSlideTextBlocks(slideXml: unknown): SlideTextBlock[] {
|
||||
}
|
||||
|
||||
const runProperties = firstRecordByKey(textBody, "rPr");
|
||||
const listStyle = textBodyListStyle(textBody);
|
||||
const color = runTextColor(runProperties);
|
||||
const fontSize = runTextFontSize(runProperties);
|
||||
const fontFamily = runTextFontFamily(runProperties);
|
||||
return {
|
||||
text,
|
||||
...(listStyle ? { listStyle } : {}),
|
||||
...(color ? { color } : {}),
|
||||
...(fontSize ? { fontSize } : {}),
|
||||
...(fontFamily ? { fontFamily } : {})
|
||||
@@ -228,6 +238,26 @@ function mergeOrderedSlideTextBlocks(styledBlocks: SlideTextBlock[], orderedText
|
||||
});
|
||||
}
|
||||
|
||||
function textBodyListStyle(textBody: Record<string, unknown>): SlideListStyle | undefined {
|
||||
const paragraphStyles = toArray(textBody.p)
|
||||
.map(paragraphListStyle)
|
||||
.filter((style): style is SlideListStyle => Boolean(style));
|
||||
|
||||
return paragraphStyles.includes("number") ? "number" : paragraphStyles[0];
|
||||
}
|
||||
|
||||
function paragraphListStyle(paragraph: unknown): SlideListStyle | undefined {
|
||||
const paragraphProperties = childRecord(asRecord(paragraph), "pPr");
|
||||
if (Object.keys(childRecord(paragraphProperties, "buAutoNum")).length > 0) {
|
||||
return "number";
|
||||
}
|
||||
if (Object.keys(childRecord(paragraphProperties, "buChar")).length > 0) {
|
||||
return "bullet";
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function extractOrderedTextBodyTexts(xmlNode: unknown) {
|
||||
return orderedElementsByName(xmlNode, "txBody").map(orderedTextBodyText).filter(Boolean);
|
||||
}
|
||||
@@ -482,7 +512,7 @@ async function applySlideXmlOverridesToPptxBlob(blob: Blob, slides: Slide[]) {
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
await Promise.all(
|
||||
slides.map(async (slide, index) => {
|
||||
if (!slide.hidden && !slide.transition) {
|
||||
if (!slide.hidden && !slide.transition && !slide.subtitleListStyle) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -499,7 +529,8 @@ async function applySlideXmlOverridesToPptxBlob(blob: Blob, slides: Slide[]) {
|
||||
|
||||
function pptxSlideXmlWithOverrides(xml: string, slide: Slide) {
|
||||
const withHiddenState = slide.hidden ? pptxSlideXmlWithHiddenState(xml) : xml;
|
||||
return slide.transition ? pptxSlideXmlWithTransition(withHiddenState, slide.transition) : withHiddenState;
|
||||
const withTransition = slide.transition ? pptxSlideXmlWithTransition(withHiddenState, slide.transition) : withHiddenState;
|
||||
return slide.subtitleListStyle ? pptxSlideXmlWithSubtitleList(withTransition, slide.subtitleListStyle) : withTransition;
|
||||
}
|
||||
|
||||
function pptxSlideXmlWithHiddenState(xml: string) {
|
||||
@@ -524,6 +555,37 @@ function pptxSlideXmlWithTransition(xml: string, transition: NonNullable<Slide["
|
||||
return xml.replace(/<p:sld\b[^>]*>/, (match) => `${match}${transitionXml}`);
|
||||
}
|
||||
|
||||
function pptxSlideXmlWithSubtitleList(xml: string, listStyle: SlideListStyle) {
|
||||
let textBodyIndex = 0;
|
||||
return xml.replace(/<p:txBody\b[\s\S]*?<\/p:txBody>/g, (textBodyXml) => {
|
||||
textBodyIndex += 1;
|
||||
return textBodyIndex === 2 ? pptxTextBodyXmlWithList(textBodyXml, listStyle) : textBodyXml;
|
||||
});
|
||||
}
|
||||
|
||||
function pptxTextBodyXmlWithList(textBodyXml: string, listStyle: SlideListStyle) {
|
||||
return textBodyXml.replace(/<a:p>[\s\S]*?<\/a:p>/g, (paragraphXml) => pptxParagraphXmlWithList(paragraphXml, listStyle));
|
||||
}
|
||||
|
||||
function pptxParagraphXmlWithList(paragraphXml: string, listStyle: SlideListStyle) {
|
||||
const listXml = pptxParagraphListXml(listStyle);
|
||||
if (/<a:pPr\b[^>]*\/>/.test(paragraphXml)) {
|
||||
return paragraphXml.replace(/<a:pPr\b([^>]*)\/>/, `<a:pPr$1>${listXml}</a:pPr>`);
|
||||
}
|
||||
if (/<a:pPr\b/.test(paragraphXml)) {
|
||||
return paragraphXml.replace(/<a:pPr\b([^>]*)>([\s\S]*?)<\/a:pPr>/, (_match, attributes: string, content: string) => {
|
||||
const contentWithoutBullet = content.replace(/<a:bu[A-Za-z]+\b[^>]*\/>/g, "");
|
||||
return `<a:pPr${attributes}>${listXml}${contentWithoutBullet}</a:pPr>`;
|
||||
});
|
||||
}
|
||||
|
||||
return paragraphXml.replace("<a:p>", `<a:p><a:pPr>${listXml}</a:pPr>`);
|
||||
}
|
||||
|
||||
function pptxParagraphListXml(listStyle: SlideListStyle) {
|
||||
return listStyle === "number" ? '<a:buAutoNum type="arabicPeriod"/>' : '<a:buChar char="•"/>';
|
||||
}
|
||||
|
||||
function normalizeHexColor(value?: string) {
|
||||
const raw = value?.trim().replace(/^#/, "").toUpperCase() ?? "";
|
||||
return /^[0-9A-F]{6}$/.test(raw) ? raw : "";
|
||||
|
||||
Reference in New Issue
Block a user