Preserve QPowerPoint slide backgrounds
This commit is contained in:
@@ -201,6 +201,45 @@ describe("powerPointOffice helpers", () => {
|
||||
expect(mediaFiles.length).toBeGreaterThan(0);
|
||||
});
|
||||
|
||||
it("импортирует и экспортирует пользовательский цвет фона слайда", 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", slideXmlWithBackground("Фон слайда", "FCE4D6"));
|
||||
|
||||
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].backgroundColor).toBe("FCE4D6");
|
||||
|
||||
const exportedBlob = await exportPptxBlob({
|
||||
title: "Фон.pptx",
|
||||
slides: [
|
||||
{
|
||||
id: "slide-1",
|
||||
title: "Фон слайда",
|
||||
subtitle: "",
|
||||
notes: "",
|
||||
theme: "classic",
|
||||
backgroundColor: "FCE4D6"
|
||||
}
|
||||
]
|
||||
});
|
||||
const exportedZip = await JSZip.loadAsync(await exportedBlob.arrayBuffer());
|
||||
const exportedSlideXml = await exportedZip.file("ppt/slides/slide1.xml")?.async("string");
|
||||
|
||||
expect(exportedSlideXml).toContain("FCE4D6");
|
||||
});
|
||||
|
||||
it("нормализует пробелы в импортированном тексте", () => {
|
||||
expect(normalizeWhitespace(" Строка\tс \n пробелами ")).toBe("Строка с пробелами");
|
||||
});
|
||||
@@ -212,6 +251,10 @@ function slideXml(...texts: string[]) {
|
||||
.join("")}</p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
function slideXmlWithBackground(title: string, color: 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:bg><p:bgPr><a:solidFill><a:srgbClr val="${color}"/></a:solidFill></p:bgPr></p:bg><p:spTree><p:sp><p:txBody><a:p><a:r><a:t>${title}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
function slideXmlWithPicture(title: 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:t>${title}</a:t></a:r></a:p></p:txBody></p:sp><p:pic><p:nvPicPr><p:cNvPr id="4" name="Picture 1" descr="Логотип"/></p:nvPicPr><p:blipFill><a:blip r:embed="rIdImage"/></p:blipFill><p:spPr><a:xfrm><a:off x="914400" y="1828800"/><a:ext cx="2743200" cy="1371600"/></a:xfrm></p:spPr></p:pic></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
@@ -54,6 +54,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
const notesPath = await slideNotesPath(zip, parser, path);
|
||||
const notesXml = notesPath ? await readOptionalZipText(zip, notesPath) : await readOptionalZipText(zip, `ppt/notesSlides/notesSlide${index}.xml`);
|
||||
const notes = notesXml ? extractTextBlocks(parser.parse(notesXml)).join("\n") : "";
|
||||
const backgroundColor = slideBackgroundColor(parsedSlide);
|
||||
|
||||
return {
|
||||
id: `pptx-slide-${index}`,
|
||||
@@ -61,6 +62,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
subtitle: slideTextBlocks.slice(1).join("\n"),
|
||||
notes,
|
||||
theme: "classic" as const,
|
||||
...(backgroundColor ? { backgroundColor } : {}),
|
||||
images
|
||||
};
|
||||
})
|
||||
@@ -97,7 +99,7 @@ export async function exportPptxBlob(deck: ExportablePowerPointDeck): Promise<Bl
|
||||
for (const sourceSlide of deck.slides) {
|
||||
const colors = themeColors[sourceSlide.theme] ?? themeColors.classic;
|
||||
const slide = pptx.addSlide();
|
||||
slide.background = { color: colors.background };
|
||||
slide.background = { color: normalizeHexColor(sourceSlide.backgroundColor) || colors.background };
|
||||
sourceSlide.images?.forEach((image, index) => {
|
||||
const data = normalizedImageData(image.src);
|
||||
if (!data || !slide.addImage) {
|
||||
@@ -335,6 +337,19 @@ function normalizedImageData(src: string) {
|
||||
return /^data:image\/(?:png|jpe?g|gif|bmp|svg\+xml);base64,/i.test(value) ? value : "";
|
||||
}
|
||||
|
||||
function slideBackgroundColor(slideXml: unknown) {
|
||||
const background = firstRecordByKey(slideXml, "bg");
|
||||
const backgroundProperties = childRecord(background, "bgPr");
|
||||
const solidFill = childRecord(backgroundProperties, "solidFill");
|
||||
const srgbColor = childRecord(solidFill, "srgbClr");
|
||||
return normalizeHexColor(stringValue(srgbColor.val ?? srgbColor["@_val"]));
|
||||
}
|
||||
|
||||
function normalizeHexColor(value?: string) {
|
||||
const raw = value?.trim().replace(/^#/, "").toUpperCase() ?? "";
|
||||
return /^[0-9A-F]{6}$/.test(raw) ? raw : "";
|
||||
}
|
||||
|
||||
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