Preserve QPowerPoint text alignment in PPTX
This commit is contained in:
@@ -662,6 +662,56 @@ describe("powerPointOffice helpers", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("импортирует и экспортирует выравнивание текста слайда PPTX", async () => {
|
||||
const zip = new JSZip();
|
||||
zip.file(
|
||||
"ppt/presentation.xml",
|
||||
`<p:presentation xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:sldIdLst><p:sldId id="256" r:id="rId1"/></p:sldIdLst></p:presentation>`
|
||||
);
|
||||
zip.file(
|
||||
"ppt/_rels/presentation.xml.rels",
|
||||
`<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships"><Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide" Target="slides/slide1.xml"/></Relationships>`
|
||||
);
|
||||
zip.file("ppt/slides/slide1.xml", slideXmlWithTextAlignment("Заголовок", "Подзаголовок"));
|
||||
|
||||
const importedBlob = await zip.generateAsync({
|
||||
type: "blob",
|
||||
mimeType: "application/vnd.openxmlformats-officedocument.presentationml.presentation"
|
||||
});
|
||||
const imported = await importPptxFile(new File([importedBlob], "Выравнивание.pptx", { type: importedBlob.type }));
|
||||
|
||||
expect(imported.slides[0]).toMatchObject({
|
||||
titleAlign: "center",
|
||||
subtitleAlign: "right"
|
||||
});
|
||||
|
||||
const exportedBlob = await exportPptxBlob({
|
||||
title: "Выравнивание.pptx",
|
||||
slides: [
|
||||
{
|
||||
id: "slide-1",
|
||||
title: "Заголовок",
|
||||
subtitle: "Подзаголовок",
|
||||
notes: "",
|
||||
theme: "classic",
|
||||
titleAlign: "center",
|
||||
subtitleAlign: "right"
|
||||
}
|
||||
]
|
||||
});
|
||||
const exportedZip = await JSZip.loadAsync(await exportedBlob.arrayBuffer());
|
||||
const exportedSlideXml = (await exportedZip.file("ppt/slides/slide1.xml")?.async("string")) ?? "";
|
||||
|
||||
expect(exportedSlideXml).toContain('algn="ctr"');
|
||||
expect(exportedSlideXml).toContain('algn="r"');
|
||||
|
||||
const reimported = await importPptxFile(new File([exportedBlob], "Выравнивание.pptx", { type: exportedBlob.type }));
|
||||
expect(reimported.slides[0]).toMatchObject({
|
||||
titleAlign: "center",
|
||||
subtitleAlign: "right"
|
||||
});
|
||||
});
|
||||
|
||||
it("импортирует внешние ссылки текста PPTX через hlinkClick relationships", async () => {
|
||||
const zip = new JSZip();
|
||||
zip.file(
|
||||
@@ -753,6 +803,10 @@ function slideXmlWithTextRunStyles(title: string, subtitle: string) {
|
||||
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:rPr b="1" i="1" u="sng" strike="sngStrike"/><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:txBody><a:p><a:r><a:rPr b="1" i="1" u="sng" strike="sngStrike"/><a:t>${subtitle}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
function slideXmlWithTextAlignment(title: string, subtitle: string) {
|
||||
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:pPr algn="ctr"/><a:r><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:txBody><a:p><a:pPr algn="r"/><a:r><a:t>${subtitle}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
function slideXmlWithTextHyperlinks(title: string, subtitle: string) {
|
||||
return `<p:sld xmlns:p="http://schemas.openxmlformats.org/presentationml/2006/main" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><p:cSld><p:spTree><p:sp><p:txBody><a:p><a:r><a:rPr><a:hlinkClick r:id="rIdTitleLink"/></a:rPr><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:sp><p:txBody><a:p><a:r><a:rPr><a:hlinkClick r:id="rIdSubtitleLink"/></a:rPr><a:t>${subtitle}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user