Preserve QPowerPoint text run styles in PPTX
This commit is contained in:
@@ -592,6 +592,69 @@ describe("powerPointOffice helpers", () => {
|
||||
expect(exportedSlideXml).toContain('typeface="Times New Roman"');
|
||||
});
|
||||
|
||||
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", slideXmlWithTextRunStyles("Заголовок", "Подзаголовок"));
|
||||
|
||||
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({
|
||||
titleBold: true,
|
||||
titleItalics: true,
|
||||
titleUnderline: true,
|
||||
subtitleBold: true,
|
||||
subtitleItalics: true,
|
||||
subtitleUnderline: true
|
||||
});
|
||||
|
||||
const exportedBlob = await exportPptxBlob({
|
||||
title: "Начертание.pptx",
|
||||
slides: [
|
||||
{
|
||||
id: "slide-1",
|
||||
title: "Заголовок",
|
||||
subtitle: "Подзаголовок",
|
||||
notes: "",
|
||||
theme: "classic",
|
||||
titleBold: false,
|
||||
titleItalics: true,
|
||||
titleUnderline: true,
|
||||
subtitleBold: true,
|
||||
subtitleItalics: true,
|
||||
subtitleUnderline: true
|
||||
}
|
||||
]
|
||||
});
|
||||
const exportedZip = await JSZip.loadAsync(await exportedBlob.arrayBuffer());
|
||||
const exportedSlideXml = (await exportedZip.file("ppt/slides/slide1.xml")?.async("string")) ?? "";
|
||||
|
||||
expect(exportedSlideXml).toContain('i="1"');
|
||||
expect(exportedSlideXml).toContain('u="sng"');
|
||||
expect(exportedSlideXml).toContain('b="1"');
|
||||
|
||||
const reimported = await importPptxFile(new File([exportedBlob], "Начертание.pptx", { type: exportedBlob.type }));
|
||||
expect(reimported.slides[0]).toMatchObject({
|
||||
titleBold: false,
|
||||
titleItalics: true,
|
||||
titleUnderline: true,
|
||||
subtitleBold: true,
|
||||
subtitleItalics: true,
|
||||
subtitleUnderline: true
|
||||
});
|
||||
});
|
||||
|
||||
it("импортирует внешние ссылки текста PPTX через hlinkClick relationships", async () => {
|
||||
const zip = new JSZip();
|
||||
zip.file(
|
||||
@@ -679,6 +742,10 @@ function slideXmlWithTextStyles(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 sz="3400"><a:solidFill><a:srgbClr val="C2410C"/></a:solidFill><a:latin typeface="Courier New"/></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 sz="2200"><a:solidFill><a:srgbClr val="0F766E"/></a:solidFill><a:latin typeface="Times New Roman"/></a:rPr><a:t>${subtitle}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
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"/><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"/><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>`;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,9 @@ interface SlideTextBlock {
|
||||
text: string;
|
||||
listStyle?: SlideListStyle;
|
||||
hyperlink?: string;
|
||||
bold?: boolean;
|
||||
italics?: boolean;
|
||||
underline?: boolean;
|
||||
color?: string;
|
||||
fontSize?: number;
|
||||
fontFamily?: string;
|
||||
@@ -99,9 +102,15 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
...(hidden ? { hidden } : {}),
|
||||
...(transition ? { transition } : {}),
|
||||
...(backgroundColor ? { backgroundColor } : {}),
|
||||
titleBold: slideTextBlocks[0]?.bold ?? false,
|
||||
...(slideTextBlocks[0]?.italics ? { titleItalics: true } : {}),
|
||||
...(slideTextBlocks[0]?.underline ? { titleUnderline: true } : {}),
|
||||
...(slideTextBlocks[0]?.color ? { titleColor: slideTextBlocks[0].color } : {}),
|
||||
...(slideTextBlocks[0]?.fontSize ? { titleFontSize: slideTextBlocks[0].fontSize } : {}),
|
||||
...(slideTextBlocks[0]?.fontFamily ? { titleFontFamily: slideTextBlocks[0].fontFamily } : {}),
|
||||
...(slideTextBlocks[1]?.bold ? { subtitleBold: true } : {}),
|
||||
...(slideTextBlocks[1]?.italics ? { subtitleItalics: true } : {}),
|
||||
...(slideTextBlocks[1]?.underline ? { subtitleUnderline: true } : {}),
|
||||
...(slideTextBlocks[1]?.color ? { subtitleColor: slideTextBlocks[1].color } : {}),
|
||||
...(slideTextBlocks[1]?.fontSize ? { subtitleFontSize: slideTextBlocks[1].fontSize } : {}),
|
||||
...(slideTextBlocks[1]?.fontFamily ? { subtitleFontFamily: slideTextBlocks[1].fontFamily } : {}),
|
||||
@@ -147,6 +156,12 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
const subtitleFontSize = normalizeFontSize(sourceSlide.subtitleFontSize) || 18;
|
||||
const titleFontFamily = normalizeFontFamily(sourceSlide.titleFontFamily) || "Arial";
|
||||
const subtitleFontFamily = normalizeFontFamily(sourceSlide.subtitleFontFamily) || "Arial";
|
||||
const titleBold = sourceSlide.titleBold ?? true;
|
||||
const titleItalics = Boolean(sourceSlide.titleItalics);
|
||||
const titleUnderline = Boolean(sourceSlide.titleUnderline);
|
||||
const subtitleBold = Boolean(sourceSlide.subtitleBold);
|
||||
const subtitleItalics = Boolean(sourceSlide.subtitleItalics);
|
||||
const subtitleUnderline = Boolean(sourceSlide.subtitleUnderline);
|
||||
const titleHyperlink = normalizedExternalHyperlinkTarget(sourceSlide.titleHyperlink);
|
||||
const subtitleHyperlink = normalizedExternalHyperlinkTarget(sourceSlide.subtitleHyperlink);
|
||||
const slide = pptx.addSlide();
|
||||
@@ -175,7 +190,9 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
h: 0.75,
|
||||
fontFace: titleFontFamily,
|
||||
fontSize: titleFontSize,
|
||||
bold: true,
|
||||
bold: titleBold,
|
||||
italic: titleItalics,
|
||||
...(titleUnderline ? { underline: { style: "sng" } } : {}),
|
||||
color: titleColor,
|
||||
margin: 0.04,
|
||||
breakLine: false,
|
||||
@@ -188,6 +205,9 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
h: 4.35,
|
||||
fontFace: subtitleFontFamily,
|
||||
fontSize: subtitleFontSize,
|
||||
bold: subtitleBold,
|
||||
italic: subtitleItalics,
|
||||
...(subtitleUnderline ? { underline: { style: "sng" } } : {}),
|
||||
color: subtitleColor,
|
||||
valign: "top",
|
||||
fit: "shrink",
|
||||
@@ -216,7 +236,7 @@ export function extractTextBlocks(xmlNode: unknown): string[] {
|
||||
function extractSlideTextBlocks(slideXml: unknown, hyperlinkTargets: Record<string, string> = {}): SlideTextBlock[] {
|
||||
const textBodies = slideTextBodyRecords(slideXml);
|
||||
const blocks = textBodies
|
||||
.map(({ textBody, hyperlinkSource }) => {
|
||||
.map(({ textBody, hyperlinkSource }): SlideTextBlock | null => {
|
||||
const text = extractTextBodyText(textBody);
|
||||
if (!text) {
|
||||
return null;
|
||||
@@ -225,6 +245,9 @@ function extractSlideTextBlocks(slideXml: unknown, hyperlinkTargets: Record<stri
|
||||
const runProperties = firstRecordByKey(textBody, "rPr");
|
||||
const hyperlink = textBodyHyperlink(hyperlinkSource, hyperlinkTargets);
|
||||
const listStyle = textBodyListStyle(textBody);
|
||||
const bold = runTextBold(runProperties);
|
||||
const italics = runTextItalics(runProperties);
|
||||
const underline = runTextUnderline(runProperties);
|
||||
const color = runTextColor(runProperties);
|
||||
const fontSize = runTextFontSize(runProperties);
|
||||
const fontFamily = runTextFontFamily(runProperties);
|
||||
@@ -232,6 +255,9 @@ function extractSlideTextBlocks(slideXml: unknown, hyperlinkTargets: Record<stri
|
||||
text,
|
||||
...(listStyle ? { listStyle } : {}),
|
||||
...(hyperlink ? { hyperlink } : {}),
|
||||
bold,
|
||||
italics,
|
||||
underline,
|
||||
...(color ? { color } : {}),
|
||||
...(fontSize ? { fontSize } : {}),
|
||||
...(fontFamily ? { fontFamily } : {})
|
||||
@@ -667,6 +693,19 @@ function runTextColor(runProperties: Record<string, unknown>) {
|
||||
return normalizeHexColor(stringValue(srgbColor.val ?? srgbColor["@_val"]));
|
||||
}
|
||||
|
||||
function runTextBold(runProperties: Record<string, unknown>) {
|
||||
return trueXmlBooleanAttribute(runProperties, "b");
|
||||
}
|
||||
|
||||
function runTextItalics(runProperties: Record<string, unknown>) {
|
||||
return trueXmlBooleanAttribute(runProperties, "i");
|
||||
}
|
||||
|
||||
function runTextUnderline(runProperties: Record<string, unknown>) {
|
||||
const value = stringValue(runProperties.u ?? runProperties["@_u"]).trim().toLowerCase();
|
||||
return Boolean(value && value !== "none");
|
||||
}
|
||||
|
||||
function runTextFontSize(runProperties: Record<string, unknown>) {
|
||||
const rawSize = numberAttribute(runProperties, "sz");
|
||||
return rawSize ? normalizeFontSize(rawSize / 100) : undefined;
|
||||
@@ -703,6 +742,12 @@ function isFalseXmlBoolean(value: unknown) {
|
||||
return normalized === "0" || normalized === "false";
|
||||
}
|
||||
|
||||
function trueXmlBooleanAttribute(record: Record<string, unknown>, name: string) {
|
||||
const value = record[name] ?? record[`@_${name}`];
|
||||
const normalized = String(value ?? "").trim();
|
||||
return Boolean(normalized && !isFalseXmlBoolean(value));
|
||||
}
|
||||
|
||||
function emuToInches(value: number) {
|
||||
return Math.max(0, Math.round((value / emuPerInch) * 1000) / 1000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user