Preserve QPowerPoint slide transitions in PPTX
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { useRef, useState } from "react";
|
||||
import type { CSSProperties } from "react";
|
||||
import { Copy, Download, EyeOff, FileText, FileUp, Image as ImageIcon, Palette, Plus, Save, Trash2 } from "lucide-react";
|
||||
import type { Slide, SlideDeck, SlideImage } from "../../types";
|
||||
import { Copy, Download, EyeOff, FileText, FileUp, Image as ImageIcon, Palette, Play, Plus, Save, Trash2 } from "lucide-react";
|
||||
import type { Slide, SlideDeck, SlideImage, SlideTransition } from "../../types";
|
||||
import { downloadBlobFile, downloadTextFile } from "../../utils/download";
|
||||
import { exportPptxBlob, importPptxFile } from "../../io/powerPointOffice";
|
||||
import { replaceFileExtension } from "../../io/fileHelpers";
|
||||
@@ -40,6 +40,12 @@ const textColorSwatches = [
|
||||
const slideFontFamilyOptions = ["Aptos", "Calibri", "Courier New", "Times New Roman"];
|
||||
const titleFontSizeOptions = [24, 28, 30, 34, 40, 44, 48];
|
||||
const subtitleFontSizeOptions = [14, 16, 18, 20, 22, 24, 28];
|
||||
const slideTransitionOptions: Array<{ value: SlideTransition | ""; label: string }> = [
|
||||
{ value: "", label: "Без перехода" },
|
||||
{ value: "fade", label: "Растворение" },
|
||||
{ value: "push", label: "Сдвиг" },
|
||||
{ value: "wipe", label: "Шторка" }
|
||||
];
|
||||
|
||||
const slideWidthInches = 13.333;
|
||||
const slideHeightInches = 7.5;
|
||||
@@ -448,6 +454,25 @@ export function QPowerPoint({ deck, onChange }: QPowerPointProps) {
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<label>
|
||||
<span>
|
||||
<Play size={16} />
|
||||
Переход
|
||||
</span>
|
||||
<select
|
||||
value={activeSlide.transition ?? ""}
|
||||
onChange={(event) => {
|
||||
const transition = event.target.value as SlideTransition | "";
|
||||
updateSlide({ ...activeSlide, transition: transition || undefined });
|
||||
}}
|
||||
>
|
||||
{slideTransitionOptions.map((option) => (
|
||||
<option key={option.value || "none"} value={option.value}>
|
||||
{option.label}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
</label>
|
||||
<div className="slide-background-swatches" aria-label="Фон слайда">
|
||||
{backgroundColorSwatches.map((swatch) => (
|
||||
<button
|
||||
|
||||
@@ -348,6 +348,47 @@ describe("powerPointOffice helpers", () => {
|
||||
expect(reimported.slides[0].hidden).toBe(true);
|
||||
});
|
||||
|
||||
it("импортирует и экспортирует переходы слайдов PPTX через p:transition", 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", slideXmlWithTransition("Слайд с переходом", "fade"));
|
||||
|
||||
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({ title: "Слайд с переходом", transition: "fade" });
|
||||
|
||||
const exportedBlob = await exportPptxBlob({
|
||||
title: "Переход.pptx",
|
||||
slides: [
|
||||
{
|
||||
id: "slide-1",
|
||||
title: "Слайд с переходом",
|
||||
subtitle: "",
|
||||
notes: "",
|
||||
theme: "classic",
|
||||
transition: "wipe"
|
||||
}
|
||||
]
|
||||
});
|
||||
const exportedZip = await JSZip.loadAsync(await exportedBlob.arrayBuffer());
|
||||
const exportedSlideXml = (await exportedZip.file("ppt/slides/slide1.xml")?.async("string")) ?? "";
|
||||
const reimportedTransition = await importPptxFile(new File([exportedBlob], "Переход.pptx", { type: exportedBlob.type }));
|
||||
|
||||
expect(exportedSlideXml).toContain("<p:transition><p:wipe/></p:transition>");
|
||||
expect(reimportedTransition.slides[0].transition).toBe("wipe");
|
||||
});
|
||||
|
||||
it("импортирует и экспортирует цвет и размер текста слайда", async () => {
|
||||
const zip = new JSZip();
|
||||
zip.file(
|
||||
@@ -425,6 +466,10 @@ function slideXmlWithShow(title: string, show: string) {
|
||||
return `<p:sld show="${show}" 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:t>${title}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld></p:sld>`;
|
||||
}
|
||||
|
||||
function slideXmlWithTransition(title: string, transition: "fade" | "push" | "wipe") {
|
||||
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:t>${title}</a:t></a:r></a:p></p:txBody></p:sp></p:spTree></p:cSld><p:transition><p:${transition}/></p:transition></p:sld>`;
|
||||
}
|
||||
|
||||
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>`;
|
||||
}
|
||||
|
||||
@@ -38,6 +38,7 @@ const slideRelationshipType = "http://schemas.openxmlformats.org/officeDocument/
|
||||
const notesRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide";
|
||||
const imageRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/image";
|
||||
const emuPerInch = 914400;
|
||||
const slideTransitions = ["fade", "push", "wipe"] as const;
|
||||
const themeColors: Record<Slide["theme"], { background: string; title: string; subtitle: string }> = {
|
||||
classic: { background: "FFFFFF", title: "202124", subtitle: "3C4043" },
|
||||
ocean: { background: "EAF6FF", title: "075985", subtitle: "0F766E" },
|
||||
@@ -75,6 +76,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
: "";
|
||||
const backgroundColor = slideBackgroundColor(parsedSlide);
|
||||
const hidden = slideHidden(parsedSlide);
|
||||
const transition = slideTransition(parsedSlide);
|
||||
|
||||
return {
|
||||
id: `pptx-slide-${index}`,
|
||||
@@ -83,6 +85,7 @@ export async function importPptxFile(file: File): Promise<ImportedPowerPointDeck
|
||||
notes,
|
||||
theme: "classic" as const,
|
||||
...(hidden ? { hidden } : {}),
|
||||
...(transition ? { transition } : {}),
|
||||
...(backgroundColor ? { backgroundColor } : {}),
|
||||
...(slideTextBlocks[0]?.color ? { titleColor: slideTextBlocks[0].color } : {}),
|
||||
...(slideTextBlocks[0]?.fontSize ? { titleFontSize: slideTextBlocks[0].fontSize } : {}),
|
||||
@@ -180,7 +183,7 @@ 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) ? applyHiddenSlidesToPptxBlob(blob, deck.slides) : blob;
|
||||
return deck.slides.some((slide) => slide.hidden || slide.transition) ? applySlideXmlOverridesToPptxBlob(blob, deck.slides) : blob;
|
||||
}
|
||||
|
||||
export function extractTextBlocks(xmlNode: unknown): string[] {
|
||||
@@ -464,19 +467,29 @@ function slideHidden(slideXml: unknown) {
|
||||
return isFalseXmlBoolean(slide.show ?? slide["@_show"]);
|
||||
}
|
||||
|
||||
async function applyHiddenSlidesToPptxBlob(blob: Blob, slides: Slide[]) {
|
||||
function slideTransition(slideXml: unknown): Slide["transition"] | undefined {
|
||||
const transition = childRecord(childRecord(slideXml, "sld"), "transition");
|
||||
const type = Object.keys(transition).find((key) => isSlideTransition(key));
|
||||
return isSlideTransition(type) ? type : undefined;
|
||||
}
|
||||
|
||||
function isSlideTransition(value: unknown): value is NonNullable<Slide["transition"]> {
|
||||
return typeof value === "string" && (slideTransitions as readonly string[]).includes(value);
|
||||
}
|
||||
|
||||
async function applySlideXmlOverridesToPptxBlob(blob: Blob, slides: Slide[]) {
|
||||
const JSZip = await loadJsZip();
|
||||
const zip = await JSZip.loadAsync(await blob.arrayBuffer());
|
||||
await Promise.all(
|
||||
slides.map(async (slide, index) => {
|
||||
if (!slide.hidden) {
|
||||
if (!slide.hidden && !slide.transition) {
|
||||
return;
|
||||
}
|
||||
|
||||
const path = `ppt/slides/slide${index + 1}.xml`;
|
||||
const xml = await zip.file(path)?.async("string");
|
||||
if (xml) {
|
||||
zip.file(path, pptxSlideXmlWithHiddenState(xml));
|
||||
zip.file(path, pptxSlideXmlWithOverrides(xml, slide));
|
||||
}
|
||||
})
|
||||
);
|
||||
@@ -484,6 +497,11 @@ async function applyHiddenSlidesToPptxBlob(blob: Blob, slides: Slide[]) {
|
||||
return zip.generateAsync({ type: "blob", mimeType: PPTX_MIME_TYPE });
|
||||
}
|
||||
|
||||
function pptxSlideXmlWithOverrides(xml: string, slide: Slide) {
|
||||
const withHiddenState = slide.hidden ? pptxSlideXmlWithHiddenState(xml) : xml;
|
||||
return slide.transition ? pptxSlideXmlWithTransition(withHiddenState, slide.transition) : withHiddenState;
|
||||
}
|
||||
|
||||
function pptxSlideXmlWithHiddenState(xml: string) {
|
||||
return xml.replace(/<p:sld\b([^>]*)>/, (_match, attributes: string) => {
|
||||
const withoutShow = attributes.replace(/\sshow="[^"]*"/, "");
|
||||
@@ -491,6 +509,21 @@ function pptxSlideXmlWithHiddenState(xml: string) {
|
||||
});
|
||||
}
|
||||
|
||||
function pptxSlideXmlWithTransition(xml: string, transition: NonNullable<Slide["transition"]>) {
|
||||
const transitionXml = `<p:transition><p:${transition}/></p:transition>`;
|
||||
if (/<p:transition\b[\s\S]*?<\/p:transition>/.test(xml)) {
|
||||
return xml.replace(/<p:transition\b[\s\S]*?<\/p:transition>/, transitionXml);
|
||||
}
|
||||
if (/<p:transition\b[^>]*\/>/.test(xml)) {
|
||||
return xml.replace(/<p:transition\b[^>]*\/>/, transitionXml);
|
||||
}
|
||||
if (/<\/p:cSld>/.test(xml)) {
|
||||
return xml.replace("</p:cSld>", `</p:cSld>${transitionXml}`);
|
||||
}
|
||||
|
||||
return xml.replace(/<p:sld\b[^>]*>/, (match) => `${match}${transitionXml}`);
|
||||
}
|
||||
|
||||
function normalizeHexColor(value?: string) {
|
||||
const raw = value?.trim().replace(/^#/, "").toUpperCase() ?? "";
|
||||
return /^[0-9A-F]{6}$/.test(raw) ? raw : "";
|
||||
|
||||
@@ -88,6 +88,7 @@ export interface Slide {
|
||||
notes: string;
|
||||
theme: "classic" | "ocean" | "graphite";
|
||||
hidden?: boolean;
|
||||
transition?: SlideTransition;
|
||||
backgroundColor?: string;
|
||||
titleColor?: string;
|
||||
titleFontSize?: number;
|
||||
@@ -98,6 +99,8 @@ export interface Slide {
|
||||
images?: SlideImage[];
|
||||
}
|
||||
|
||||
export type SlideTransition = "fade" | "push" | "wipe";
|
||||
|
||||
export interface SlideImage {
|
||||
id: string;
|
||||
src: string;
|
||||
|
||||
Reference in New Issue
Block a user