Preserve QPowerPoint text alignment in PPTX

This commit is contained in:
Курнат Андрей
2026-06-02 07:54:06 +03:00
parent b117e11431
commit f5ab0448c7
6 changed files with 182 additions and 8 deletions
+39 -1
View File
@@ -1,4 +1,4 @@
import type { Slide, SlideImage, SlideListStyle } from "../types";
import type { Slide, SlideImage, SlideListStyle, SlideTextAlign } from "../types";
import { officeTitleFromFileName, PPTX_MIME_TYPE, readOfficeFileAsArrayBuffer } from "./fileHelpers";
export interface ImportedPowerPointDeck {
@@ -19,6 +19,7 @@ interface SlideTextBlock {
italics?: boolean;
underline?: boolean;
strike?: boolean;
align?: SlideTextAlign;
color?: string;
fontSize?: number;
fontFamily?: string;
@@ -107,6 +108,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
...(slideTextBlocks[0]?.italics ? { titleItalics: true } : {}),
...(slideTextBlocks[0]?.underline ? { titleUnderline: true } : {}),
...(slideTextBlocks[0]?.strike ? { titleStrike: true } : {}),
...(slideTextBlocks[0]?.align ? { titleAlign: slideTextBlocks[0].align } : {}),
...(slideTextBlocks[0]?.color ? { titleColor: slideTextBlocks[0].color } : {}),
...(slideTextBlocks[0]?.fontSize ? { titleFontSize: slideTextBlocks[0].fontSize } : {}),
...(slideTextBlocks[0]?.fontFamily ? { titleFontFamily: slideTextBlocks[0].fontFamily } : {}),
@@ -114,6 +116,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
...(slideTextBlocks[1]?.italics ? { subtitleItalics: true } : {}),
...(slideTextBlocks[1]?.underline ? { subtitleUnderline: true } : {}),
...(slideTextBlocks[1]?.strike ? { subtitleStrike: true } : {}),
...(slideTextBlocks[1]?.align ? { subtitleAlign: slideTextBlocks[1].align } : {}),
...(slideTextBlocks[1]?.color ? { subtitleColor: slideTextBlocks[1].color } : {}),
...(slideTextBlocks[1]?.fontSize ? { subtitleFontSize: slideTextBlocks[1].fontSize } : {}),
...(slideTextBlocks[1]?.fontFamily ? { subtitleFontFamily: slideTextBlocks[1].fontFamily } : {}),
@@ -163,10 +166,12 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
const titleItalics = Boolean(sourceSlide.titleItalics);
const titleUnderline = Boolean(sourceSlide.titleUnderline);
const titleStrike = Boolean(sourceSlide.titleStrike);
const titleAlign = normalizedSlideTextAlign(sourceSlide.titleAlign);
const subtitleBold = Boolean(sourceSlide.subtitleBold);
const subtitleItalics = Boolean(sourceSlide.subtitleItalics);
const subtitleUnderline = Boolean(sourceSlide.subtitleUnderline);
const subtitleStrike = Boolean(sourceSlide.subtitleStrike);
const subtitleAlign = normalizedSlideTextAlign(sourceSlide.subtitleAlign);
const titleHyperlink = normalizedExternalHyperlinkTarget(sourceSlide.titleHyperlink);
const subtitleHyperlink = normalizedExternalHyperlinkTarget(sourceSlide.subtitleHyperlink);
const slide = pptx.addSlide();
@@ -195,6 +200,7 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
h: 0.75,
fontFace: titleFontFamily,
fontSize: titleFontSize,
...(titleAlign ? { align: titleAlign } : {}),
bold: titleBold,
italic: titleItalics,
...(titleUnderline ? { underline: { style: "sng" } } : {}),
@@ -211,6 +217,7 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
h: 4.35,
fontFace: subtitleFontFamily,
fontSize: subtitleFontSize,
...(subtitleAlign ? { align: subtitleAlign } : {}),
bold: subtitleBold,
italic: subtitleItalics,
...(subtitleUnderline ? { underline: { style: "sng" } } : {}),
@@ -256,6 +263,7 @@ function extractSlideTextBlocks(slideXml: unknown, hyperlinkTargets: Record<stri
const italics = runTextItalics(runProperties);
const underline = runTextUnderline(runProperties);
const strike = runTextStrike(runProperties);
const align = textBodyAlign(textBody);
const color = runTextColor(runProperties);
const fontSize = runTextFontSize(runProperties);
const fontFamily = runTextFontFamily(runProperties);
@@ -267,6 +275,7 @@ function extractSlideTextBlocks(slideXml: unknown, hyperlinkTargets: Record<stri
italics,
underline,
strike,
...(align ? { align } : {}),
...(color ? { color } : {}),
...(fontSize ? { fontSize } : {}),
...(fontFamily ? { fontFamily } : {})
@@ -318,6 +327,15 @@ function textBodyListStyle(textBody: Record<string, unknown>): SlideListStyle |
return paragraphStyles.includes("number") ? "number" : paragraphStyles[0];
}
function textBodyAlign(textBody: Record<string, unknown>): SlideTextAlign | undefined {
return toArray(textBody.p).map(paragraphAlign).find((align): align is SlideTextAlign => Boolean(align));
}
function paragraphAlign(paragraph: unknown): SlideTextAlign | undefined {
const paragraphProperties = childRecord(asRecord(paragraph), "pPr");
return slideTextAlignFromXml(stringValue(paragraphProperties.algn ?? paragraphProperties["@_algn"]));
}
function paragraphListStyle(paragraph: unknown): SlideListStyle | undefined {
const paragraphProperties = childRecord(asRecord(paragraph), "pPr");
if (Object.keys(childRecord(paragraphProperties, "buAutoNum")).length > 0) {
@@ -746,6 +764,26 @@ function normalizeFontFamily(value?: string) {
return normalized && normalized.toLowerCase() !== "arial" ? normalized : "";
}
function normalizedSlideTextAlign(value?: string) {
return value === "left" || value === "center" || value === "right" ? value : undefined;
}
function slideTextAlignFromXml(value: string): SlideTextAlign | undefined {
switch (value.trim().toLowerCase()) {
case "l":
case "left":
return "left";
case "ctr":
case "center":
return "center";
case "r":
case "right":
return "right";
default:
return undefined;
}
}
function numberAttribute(record: Record<string, unknown>, name: string) {
const value = Number.parseFloat(stringValue(record[name] ?? record[`@_${name}`]));
return Number.isFinite(value) ? value : undefined;